terraform-provider-greenhost/digitalocean/datasource_digitalocean_rec...

124 lines
3.0 KiB
Go
Raw Normal View History

package digitalocean
import (
"context"
"fmt"
"strconv"
"github.com/digitalocean/godo"
upgrade provider to use terraform-plugin-sdk v2 (#492) * upgrade terraform-plugin-sdk and `go mod vendor` * Update digitalocean/datasource_digitalocean_image_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_kubernetes_cluster_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_vpc_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_vpc_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * go fmt * fix droplet_id to be of the right type * fix digitalocean_project resource * fix creation order in digitalocean_certificate test * fix digitalocean_container_registry data source tes * Port new changes to v2. * Port all tests to resource.ParallelTest * Fix KubernetesProviderInteroperability test. * Fix TestAccDigitalOceanKubernetesCluster_UpgradeVersion * Fix firewall panic s/create_at/created_at/ * Fix TestAccDigitalOceanDroplet_Basic: Droplets now have private networking by default. * Fix TestAccDataSourceDigitalOceanDomain_Basic * Fix TestAccDataSourceDigitalOceanDropletSnapshot tests. * Fix TestAccDataSourceDigitalOceanSSHKey_Basic * Fix TestAccDataSourceDigitalOceanVolumeSnapshot tests. * Fix TestAccDataSourceDigitalOceanVolume tests. * Fix TestAccDataSourceDigitalOceanRecord_Basic * Fix TestAccDataSourceDigitalOceanProject_NonDefaultProject * Fix TestAccDigitalOceanImage_PublicSlug * Fix TestAccDataSourceDigitalOceanImages_Basic via bug in imageSchema() * go mod tidy * Fix TestAccDataSourceDigitalOceanDroplet tests. * Fix TestAccDataSourceDigitalOceanVPC_ByName * Fix TestAccDataSourceDigitalOceanTag_Basic * Fix TestAccDataSourceDigitalOceanTags_Basic * Ensure versions are set in DBaaS tests. * Fix TestAccDataSourceDigitalOceanApp_Basic * Fix non-set related issues with TestAccDataSourceDigitalOceanLoadBalancer tests. * Fix TestAccDataSourceDigitalOceanKubernetesCluster_Basic * Remove testAccDigitalOceanKubernetesConfigWithEmptyNodePool: Empty node pools are no longer supported. * Fix TestAccDigitalOceanProject_WithManyResources. * Fix TestAccDigitalOceanProject_UpdateFromDropletToSpacesResource * vendor set helpers from AWS provider * Fix TestAccDigitalOceanFloatingIP_Droplet. * Fix CDN panic. * fix TestAccDigitalOceanSpacesBucket_LifecycleBasic using setutil helpers * vendor set helpers from AWS provider * fix TestAccDigitalOceanSpacesBucket_LifecycleBasic using setutil helpers * Fix load balancer tests using setutil helpers. * Fix K8s tests using setutil helpers. * Fix TestAccDigitalOceanApp_Envs using setutil helpers. * Fix TestAccDigitalOceanSpacesBucket_LifecycleExpireMarkerOnly using setutil helpers. * Fix TestAccDigitalOceanFloatingIPAssignment_createBeforeDestroy * fix remaining TypeSet tests using setutil * Registry test can not run in parallel. One per account. * Fix TestAccDigitalOceanProject_UpdateWithDropletResource * Fix replica tests. * go mod tidy Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> Co-authored-by: Andrew Starr-Bochicchio <a.starr.b@gmail.com>
2020-10-16 19:50:20 +00:00
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceDigitalOceanRecord() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanRecordRead,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
Description: "domain of the name record",
ValidateFunc: validation.NoZeroValues,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "name of the record",
ValidateFunc: validation.NoZeroValues,
},
// computed attributes
"type": {
Type: schema.TypeString,
Computed: true,
Description: "type of the name record",
},
"data": {
Type: schema.TypeString,
Computed: true,
Description: "name record data",
},
"priority": {
Type: schema.TypeInt,
Computed: true,
Description: "priority of the name record",
},
"port": {
Type: schema.TypeInt,
Computed: true,
Description: "port of the name record",
},
"ttl": {
Type: schema.TypeInt,
Computed: true,
Description: "ttl of the name record",
},
"weight": {
Type: schema.TypeInt,
Computed: true,
Description: "weight of the name record",
},
"flags": {
Type: schema.TypeInt,
Computed: true,
Description: "flags of the name record",
},
"tag": {
Type: schema.TypeString,
Computed: true,
Description: "tag of the name record",
},
},
}
}
func dataSourceDigitalOceanRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
domain := d.Get("domain").(string)
name := d.Get("name").(string)
opts := &godo.ListOptions{}
records, resp, err := client.Domains.Records(context.Background(), domain, opts)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return fmt.Errorf("domain not found: %s", err)
}
return fmt.Errorf("Error retrieving domain: %s", err)
}
record, err := findRecordByName(records, name)
if err != nil {
return err
}
d.SetId(strconv.Itoa(record.ID))
d.Set("type", record.Type)
d.Set("name", record.Name)
d.Set("data", record.Data)
d.Set("priority", record.Priority)
d.Set("port", record.Port)
d.Set("ttl", record.TTL)
d.Set("weight", record.Weight)
d.Set("tag", record.Tag)
d.Set("flags", record.Flags)
return nil
}
func findRecordByName(records []godo.DomainRecord, name string) (*godo.DomainRecord, error) {
results := make([]godo.DomainRecord, 0)
for _, v := range records {
if v.Name == name {
results = append(results, v)
}
}
if len(results) == 1 {
return &results[0], nil
}
if len(results) == 0 {
return nil, fmt.Errorf("no records found with name %s", name)
}
return nil, fmt.Errorf("too many records found (found %d, expected 1)", len(results))
}