-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
2b14888
commit 9fda04a
Showing
6 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-datasource | ||
`google_dns_record_set` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters