Skip to content

Commit

Permalink
add digitalocean_records data source (#502)
Browse files Browse the repository at this point in the history
* add digitalocean_records data source

* add docs

* id attribute

* fmt

* switch to single set of functions
  • Loading branch information
tdyas authored Oct 20, 2020
1 parent 410de8b commit 4ace704
Show file tree
Hide file tree
Showing 19 changed files with 349 additions and 40 deletions.
6 changes: 3 additions & 3 deletions digitalocean/datasource_digitalocean_droplet.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func dataSourceDigitalOceanDropletRead(ctx context.Context, d *schema.ResourceDa

foundDroplet = *droplet
} else if v, ok := d.GetOk("tag"); ok {
dropletList, err := getDigitalOceanDroplets(meta)
dropletList, err := getDigitalOceanDroplets(meta, nil)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -62,7 +62,7 @@ func dataSourceDigitalOceanDropletRead(ctx context.Context, d *schema.ResourceDa

foundDroplet = *droplet
} else if v, ok := d.GetOk("name"); ok {
dropletList, err := getDigitalOceanDroplets(meta)
dropletList, err := getDigitalOceanDroplets(meta, nil)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -78,7 +78,7 @@ func dataSourceDigitalOceanDropletRead(ctx context.Context, d *schema.ResourceDa
return diag.Errorf("Error: specify either a name, tag, or id to use to look up the droplet")
}

flattenedDroplet, err := flattenDigitalOceanDroplet(foundDroplet, meta)
flattenedDroplet, err := flattenDigitalOceanDroplet(foundDroplet, meta, nil)
if err != nil {
return diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion digitalocean/datasource_digitalocean_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func dataSourceDigitalOceanImageRead(ctx context.Context, d *schema.ResourceData
return diag.Errorf("Illegal state: one of id, name, or slug must be set")
}

flattenedImage, err := flattenDigitalOceanImage(*foundImage, meta)
flattenedImage, err := flattenDigitalOceanImage(*foundImage, meta, nil)
if err != nil {
return diag.FromErr(err)
}
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/datasource_digitalocean_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func dataSourceDigitalOceanProjectRead(ctx context.Context, d *schema.ResourceDa
}
foundProject = thisProject
} else if name, ok := d.GetOk("name"); ok {
projects, err := getDigitalOceanProjects(meta)
projects, err := getDigitalOceanProjects(meta, nil)
if err != nil {
return diag.Errorf("Unable to load projects: %s", err)
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func dataSourceDigitalOceanProjectRead(ctx context.Context, d *schema.ResourceDa
return diag.Errorf("No project found.")
}

flattenedProject, err := flattenDigitalOceanProject(*foundProject, meta)
flattenedProject, err := flattenDigitalOceanProject(*foundProject, meta, nil)
if err != nil {
return diag.FromErr(err)
}
Expand Down
23 changes: 23 additions & 0 deletions digitalocean/datasource_digitalocean_records.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package digitalocean

import (
"github.com/digitalocean/terraform-provider-digitalocean/internal/datalist"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceDigitalOceanRecords() *schema.Resource {
dataListConfig := &datalist.ResourceConfig{
RecordSchema: recordsSchema(),
ResultAttributeName: "records",
ExtraQuerySchema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
},
},
FlattenRecord: flattenDigitalOceanRecord,
GetRecords: getDigitalOceanRecords,
}

return datalist.NewResource(dataListConfig)
}
70 changes: 70 additions & 0 deletions digitalocean/datasource_digitalocean_records_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package digitalocean

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
)

func TestAccDataSourceDigitalOceanRecords_Basic(t *testing.T) {
name1 := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))

resourcesConfig := fmt.Sprintf(`
resource "digitalocean_domain" "foo" {
name = "%s"
}
resource "digitalocean_record" "mail" {
name = "mail"
domain = digitalocean_domain.foo.name
type = "MX"
priority = 10
value = "mail.example.com."
}
resource "digitalocean_record" "www" {
name = "www"
domain = digitalocean_domain.foo.name
type = "A"
value = "192.168.1.1"
}
`, name1)

datasourceConfig := fmt.Sprintf(`
data "digitalocean_records" "result" {
domain = "%s"
filter {
key = "type"
values = ["A"]
}
}
`, name1)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: resourcesConfig,
},
{
Config: resourcesConfig + datasourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.digitalocean_records.result", "records.#", "1"),
resource.TestCheckResourceAttr("data.digitalocean_records.result", "records.0.domain", name1),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.id", "digitalocean_record.www", "id"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.name", "digitalocean_record.www", "name"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.type", "digitalocean_record.www", "type"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.value", "digitalocean_record.www", "value"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.priority", "digitalocean_record.www", "priority"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.port", "digitalocean_record.www", "port"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.ttl", "digitalocean_record.www", "ttl"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.weight", "digitalocean_record.www", "weight"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.flags", "digitalocean_record.www", "flags"),
resource.TestCheckResourceAttrPair("data.digitalocean_records.result", "records.0.tag", "digitalocean_record.www", "tag"),
),
},
},
})
}
4 changes: 2 additions & 2 deletions digitalocean/datasource_digitalocean_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func dataSourceDigitalOceanRegion() *schema.Resource {
}

func dataSourceDigitalOceanRegionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
regions, err := getDigitalOceanRegions(meta)
regions, err := getDigitalOceanRegions(meta, nil)
if err != nil {
return diag.Errorf("Unable to load regions: %s", err)
}
Expand All @@ -61,7 +61,7 @@ func dataSourceDigitalOceanRegionRead(ctx context.Context, d *schema.ResourceDat
return diag.Errorf("Region does not exist: %s", slug)
}

flattenedRegion, err := flattenRegion(*regionForSlug, meta)
flattenedRegion, err := flattenRegion(*regionForSlug, meta, nil)
if err != nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/datasource_digitalocean_sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func dataSourceDigitalOceanSizes() *schema.Resource {
return datalist.NewResource(dataListConfig)
}

func getDigitalOceanSizes(meta interface{}) ([]interface{}, error) {
func getDigitalOceanSizes(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()

sizes := []interface{}{}
Expand Down Expand Up @@ -93,7 +93,7 @@ func getDigitalOceanSizes(meta interface{}) ([]interface{}, error) {
return sizes, nil
}

func flattenDigitalOceanSize(size, meta interface{}) (map[string]interface{}, error) {
func flattenDigitalOceanSize(size, meta interface{}, extra map[string]interface{}) (map[string]interface{}, error) {
s := size.(godo.Size)

flattenedSize := map[string]interface{}{}
Expand Down
2 changes: 1 addition & 1 deletion digitalocean/datasource_digitalocean_spaces_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func dataSourceDigitalOceanSpacesBucketRead(ctx context.Context, d *schema.Resou
region: region,
}

flattenedBucket, err := flattenSpacesBucket(&metadata, meta)
flattenedBucket, err := flattenSpacesBucket(&metadata, meta, nil)
if err != nil {
return diag.FromErr(err)
}
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/datasource_digitalocean_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func dataSourceDigitalOceanTags() *schema.Resource {
return datalist.NewResource(dataListConfig)
}

func getDigitalOceanTags(meta interface{}) ([]interface{}, error) {
func getDigitalOceanTags(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()

tagsList := []interface{}{}
Expand Down Expand Up @@ -77,7 +77,7 @@ func getDigitalOceanTags(meta interface{}) ([]interface{}, error) {
return tagsList, nil
}

func flattenDigitalOceanTag(tag, meta interface{}) (map[string]interface{}, error) {
func flattenDigitalOceanTag(tag, meta interface{}, extra map[string]interface{}) (map[string]interface{}, error) {
t := tag.(godo.Tag)

flattenedTag := map[string]interface{}{}
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func domainSchema() map[string]*schema.Schema {
}
}

func getDigitalOceanDomains(meta interface{}) ([]interface{}, error) {
func getDigitalOceanDomains(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()

opts := &godo.ListOptions{
Expand Down Expand Up @@ -61,7 +61,7 @@ func getDigitalOceanDomains(meta interface{}) ([]interface{}, error) {
return allDomains, nil
}

func flattenDigitalOceanDomain(rawDomain, meta interface{}) (map[string]interface{}, error) {
func flattenDigitalOceanDomain(rawDomain, meta interface{}, extra map[string]interface{}) (map[string]interface{}, error) {
domain := rawDomain.(godo.Domain)

flattenedDomain := map[string]interface{}{
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func dropletSchema() map[string]*schema.Schema {
}
}

func getDigitalOceanDroplets(meta interface{}) ([]interface{}, error) {
func getDigitalOceanDroplets(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()

opts := &godo.ListOptions{
Expand Down Expand Up @@ -149,7 +149,7 @@ func getDigitalOceanDroplets(meta interface{}) ([]interface{}, error) {
return dropletList, nil
}

func flattenDigitalOceanDroplet(rawDroplet, meta interface{}) (map[string]interface{}, error) {
func flattenDigitalOceanDroplet(rawDroplet, meta interface{}, extra map[string]interface{}) (map[string]interface{}, error) {
droplet := rawDroplet.(godo.Droplet)

flattenedDroplet := map[string]interface{}{
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func imageSchema() map[string]*schema.Schema {
}
}

func getDigitalOceanImages(meta interface{}) ([]interface{}, error) {
func getDigitalOceanImages(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()
return listDigitalOceanImages(client.Images.List)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func listDigitalOceanImages(listImages imageListFunc) ([]interface{}, error) {
return allImages, nil
}

func flattenDigitalOceanImage(rawImage interface{}, meta interface{}) (map[string]interface{}, error) {
func flattenDigitalOceanImage(rawImage interface{}, meta interface{}, extra map[string]interface{}) (map[string]interface{}, error) {
image, ok := rawImage.(godo.Image)
if !ok {
return nil, fmt.Errorf("Unable to convert to godo.Image")
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func projectSchema() map[string]*schema.Schema {
}
}

func getDigitalOceanProjects(meta interface{}) ([]interface{}, error) {
func getDigitalOceanProjects(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()

var allProjects []interface{}
Expand Down Expand Up @@ -86,7 +86,7 @@ func getDigitalOceanProjects(meta interface{}) ([]interface{}, error) {
return allProjects, nil
}

func flattenDigitalOceanProject(rawProject interface{}, meta interface{}) (map[string]interface{}, error) {
func flattenDigitalOceanProject(rawProject interface{}, meta interface{}, extra map[string]interface{}) (map[string]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()

project, ok := rawProject.(godo.Project)
Expand Down
1 change: 1 addition & 0 deletions digitalocean/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func Provider() *schema.Provider {
"digitalocean_project": dataSourceDigitalOceanProject(),
"digitalocean_projects": dataSourceDigitalOceanProjects(),
"digitalocean_record": dataSourceDigitalOceanRecord(),
"digitalocean_records": dataSourceDigitalOceanRecords(),
"digitalocean_region": dataSourceDigitalOceanRegion(),
"digitalocean_regions": dataSourceDigitalOceanRegions(),
"digitalocean_sizes": dataSourceDigitalOceanSizes(),
Expand Down
Loading

0 comments on commit 4ace704

Please sign in to comment.