diff --git a/mmv1/third_party/terraform/data_sources/data_source_dns_record_set.go b/mmv1/third_party/terraform/data_sources/data_source_dns_record_set.go new file mode 100644 index 000000000000..d9e4f83bdef2 --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_dns_record_set.go @@ -0,0 +1,86 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceDnsRecordSet() *schema.Resource { + return &schema.Resource{ + Read: dataSourceDnsRecordSetRead, + + Schema: map[string]*schema.Schema{ + "managed_zone": { + Type: schema.TypeString, + Required: true, + }, + + "name": { + Type: schema.TypeString, + Required: true, + }, + + "rrdatas": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "ttl": { + Type: schema.TypeInt, + Computed: true, + }, + + "type": { + Type: schema.TypeString, + Required: true, + }, + + "project": { + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func dataSourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + + zone := d.Get("managed_zone").(string) + name := d.Get("name").(string) + dnsType := d.Get("type").(string) + d.SetId(fmt.Sprintf("projects/%s/managedZones/%s/rrsets/%s/%s", project, zone, name, dnsType)) + + resp, err := config.NewDnsClient(userAgent).ResourceRecordSets.List(project, zone).Name(name).Type(dnsType).Do() + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("dataSourceDnsRecordSet %q", name)) + } + if len(resp.Rrsets) != 1 { + return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets)) + } + + if err := d.Set("rrdatas", resp.Rrsets[0].Rrdatas); err != nil { + return fmt.Errorf("Error setting rrdatas: %s", err) + } + if err := d.Set("ttl", resp.Rrsets[0].Ttl); err != nil { + return fmt.Errorf("Error setting ttl: %s", err) + } + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error setting project: %s", err) + } + + return nil +} diff --git a/mmv1/third_party/terraform/tests/data_source_dns_record_set_test.go b/mmv1/third_party/terraform/tests/data_source_dns_record_set_test.go new file mode 100644 index 000000000000..403f3ff013e1 --- /dev/null +++ b/mmv1/third_party/terraform/tests/data_source_dns_record_set_test.go @@ -0,0 +1,51 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAcccDataSourceDnsRecordSet_basic(t *testing.T) { + t.Parallel() + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDnsRecordSetDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceDnsRecordSet_basic(randString(t, 10), randString(t, 10)), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState("data.google_dns_record_set.rs", "google_dns_record_set.rs"), + ), + }, + }, + }) +} + +func testAccDataSourceDnsRecordSet_basic(zoneName, recordSetName string) string { + return fmt.Sprintf(` +resource "google_dns_managed_zone" "zone" { + name = "test-zone" + dns_name = "%s.hashicorptest.com." +} + +resource "google_dns_record_set" "rs" { + managed_zone = google_dns_managed_zone.zone.name + name = "%s.${google_dns_managed_zone.zone.dns_name}" + type = "A" + ttl = 300 + rrdatas = [ + "192.168.1.0", + ] +} + +data "google_dns_record_set" "rs" { + managed_zone = google_dns_record_set.rs.managed_zone + name = google_dns_record_set.rs.name + type = google_dns_record_set.rs.type +} +`, zoneName, recordSetName) +} diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index db7c6cc482ee..8774d1285068 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -254,6 +254,7 @@ func Provider() *schema.Provider { "google_container_registry_repository": dataSourceGoogleContainerRepo(), "google_dns_keys": dataSourceDNSKeys(), "google_dns_managed_zone": dataSourceDnsManagedZone(), + "google_dns_record_set": dataSourceDnsRecordSet(), "google_game_services_game_server_deployment_rollout": dataSourceGameServicesGameServerDeploymentRollout(), "google_iam_policy": dataSourceGoogleIamPolicy(), "google_iam_role": dataSourceGoogleIamRole(), diff --git a/mmv1/third_party/terraform/website/docs/d/dns_record_set.markdown b/mmv1/third_party/terraform/website/docs/d/dns_record_set.markdown new file mode 100644 index 000000000000..e64c9c0fc4b5 --- /dev/null +++ b/mmv1/third_party/terraform/website/docs/d/dns_record_set.markdown @@ -0,0 +1,48 @@ +--- +subcategory: "Cloud DNS" +layout: "google" +page_title: "Google: google_dns_record_set" +sidebar_current: "docs-google-datasource-dns-record-set" +description: |- + Get a DNS record set within Google Cloud DNS +--- + +# google\_dns\_record\_set + +Get a DNS record set within Google Cloud DNS +For more information see +[the official documentation](https://cloud.google.com/dns/docs/records) +and +[API](https://cloud.google.com/dns/docs/reference/v1/resourceRecordSets) + +## Example Usage + +```tf +data "google_dns_managed_zone" "sample" { + name = "sample-zone" +} + +data "google_dns_record_set" "rs" { + managed_zone = data.google_dns_managed_zone.sample.name + name = "my-record.${data.google_dns_managed_zone.sample.dns_name}" + type = "A" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `managed_zone` - (Required) The Name of the zone. + +* `name` - (Required) The DNS name for the resource. + +* `project` - (Optional) The ID of the project for the Google Cloud. + +## Attributes Reference + +The following attributes are exported: + +* `rrdatas` - The string data for the records in this record set. + +* `ttl` - The time-to-live of this record set (seconds).