Skip to content

Commit 4424e0c

Browse files
committed
Support RD for rrset resource
1 parent d5cfdfb commit 4424e0c

File tree

10 files changed

+308
-0
lines changed

10 files changed

+308
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
- Support for `mount_type_details`, `mount_type`, `nfs_server` and `nfs_server_export` attributes in `oci_database_backup_destination` resource
1111
- Support resource discovery for `ons` resources
1212
- Support resource discovery for `analytics` resources
13+
- Support resource discovery for `dns` resources
14+
- Support datasource for `oci_dns_rrset`
15+
1316
### Fixed
1417
- Fix issue where discovering object storage buckets without lifecycle policies, results in an error
1518

examples/dns/rrset.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ resource "oci_dns_rrset" "rrset-cname" {
4343
ttl = 86400
4444
}
4545
}
46+
47+
data "oci_dns_rrset" "test_rrset" {
48+
domain = "el.${oci_dns_zone.zone3.name}"
49+
rtype = "CNAME"
50+
zone_name_or_id = "${oci_dns_zone.zone3.id}"
51+
}

oci/dns_rrset_data_source.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package oci
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform/helper/schema"
10+
oci_dns "github.com/oracle/oci-go-sdk/dns"
11+
)
12+
13+
func init() {
14+
RegisterDatasource("oci_dns_rrset", DnsRrsetDataSource())
15+
}
16+
17+
func DnsRrsetDataSource() *schema.Resource {
18+
fieldMap := make(map[string]*schema.Schema)
19+
fieldMap["compartment_id"] = &schema.Schema{
20+
Type: schema.TypeString,
21+
Optional: true,
22+
}
23+
fieldMap["domain"] = &schema.Schema{
24+
Type: schema.TypeString,
25+
Required: true,
26+
}
27+
fieldMap["rtype"] = &schema.Schema{
28+
Type: schema.TypeString,
29+
Required: true,
30+
}
31+
fieldMap["zone_name_or_id"] = &schema.Schema{
32+
Type: schema.TypeString,
33+
Required: true,
34+
}
35+
fieldMap["zone_version"] = &schema.Schema{
36+
Type: schema.TypeString,
37+
Optional: true,
38+
}
39+
return GetSingularDataSourceItemSchema(DnsRrsetResource(), fieldMap, readSingularDnsRrset)
40+
}
41+
42+
func readSingularDnsRrset(d *schema.ResourceData, m interface{}) error {
43+
sync := &DnsRrsetDataSourceCrud{}
44+
sync.D = d
45+
sync.Client = m.(*OracleClients).dnsClient()
46+
47+
return ReadResource(sync)
48+
}
49+
50+
type DnsRrsetDataSourceCrud struct {
51+
D *schema.ResourceData
52+
Client *oci_dns.DnsClient
53+
Res *oci_dns.GetRRSetResponse
54+
}
55+
56+
func (s *DnsRrsetDataSourceCrud) VoidState() {
57+
s.D.SetId("")
58+
}
59+
60+
func (s *DnsRrsetDataSourceCrud) Get() error {
61+
request := oci_dns.GetRRSetRequest{}
62+
63+
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
64+
tmp := compartmentId.(string)
65+
request.CompartmentId = &tmp
66+
}
67+
68+
if domain, ok := s.D.GetOkExists("domain"); ok {
69+
tmp := domain.(string)
70+
request.Domain = &tmp
71+
}
72+
73+
if rtype, ok := s.D.GetOkExists("rtype"); ok {
74+
tmp := rtype.(string)
75+
request.Rtype = &tmp
76+
}
77+
78+
if zoneNameOrId, ok := s.D.GetOkExists("zone_name_or_id"); ok {
79+
tmp := zoneNameOrId.(string)
80+
request.ZoneNameOrId = &tmp
81+
}
82+
83+
if zoneVersion, ok := s.D.GetOkExists("zone_version"); ok {
84+
tmp := zoneVersion.(string)
85+
request.ZoneVersion = &tmp
86+
}
87+
88+
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "dns")
89+
90+
response, err := s.Client.GetRRSet(context.Background(), request)
91+
if err != nil {
92+
return err
93+
}
94+
95+
s.Res = &response
96+
return nil
97+
}
98+
99+
func (s *DnsRrsetDataSourceCrud) SetData() error {
100+
if s.Res == nil {
101+
return nil
102+
}
103+
104+
s.D.SetId(GenerateDataSourceID())
105+
106+
items := []interface{}{}
107+
for _, item := range s.Res.Items {
108+
items = append(items, RecordToMap(item))
109+
}
110+
s.D.Set("items", items)
111+
112+
return nil
113+
}

oci/dns_rrset_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ var (
2323
RrsetRequiredOnlyResource = RrsetResourceDependencies +
2424
generateResourceFromRepresentationMap("oci_dns_rrset", "test_rrset", Required, Create, rrsetRepresentation)
2525

26+
RrsetResourceConfig = RrsetResourceDependencies +
27+
generateResourceFromRepresentationMap("oci_dns_rrset", "test_rrset", Optional, Update, rrsetRepresentation)
28+
29+
rrsetSingularDataSourceRepresentation = map[string]interface{}{
30+
"domain": Representation{repType: Required, create: dnsDomainName},
31+
"rtype": Representation{repType: Required, create: `A`},
32+
"zone_name_or_id": Representation{repType: Required, create: `${oci_dns_zone.test_zone.id}`},
33+
"compartment_id": Representation{repType: Optional, create: `${var.compartment_id}`},
34+
}
35+
2636
dnsDomainName = randomString(5, charsetWithoutDigits) + ".token.oci-record-test"
2737
rrsetRepresentation = map[string]interface{}{
2838
"domain": Representation{repType: Required, create: dnsDomainName},
@@ -64,6 +74,8 @@ func TestDnsRrsetResource_basic(t *testing.T) {
6474

6575
resourceName := "oci_dns_rrset.test_rrset"
6676

77+
singularDatasourceName := "data.oci_dns_rrset.test_rrset"
78+
6779
var resId, resId2 string
6880

6981
resource.Test(t, resource.TestCase{
@@ -150,6 +162,34 @@ func TestDnsRrsetResource_basic(t *testing.T) {
150162
},
151163
),
152164
},
165+
// verify singular datasource
166+
{
167+
Config: config +
168+
generateDataSourceFromRepresentationMap("oci_dns_rrset", "test_rrset", Required, Create, rrsetSingularDataSourceRepresentation) +
169+
compartmentIdVariableStr + RrsetResourceConfig,
170+
Check: resource.ComposeAggregateTestCheckFunc(
171+
resource.TestCheckResourceAttr(singularDatasourceName, "domain", dnsDomainName),
172+
resource.TestCheckResourceAttr(singularDatasourceName, "rtype", "A"),
173+
resource.TestCheckResourceAttrSet(singularDatasourceName, "zone_name_or_id"),
174+
175+
resource.TestCheckResourceAttr(singularDatasourceName, "items.#", "1"),
176+
CheckResourceSetContainsElementWithProperties(singularDatasourceName, "items", map[string]string{
177+
"domain": dnsDomainName,
178+
"rdata": "77.77.77.77",
179+
"rtype": "A",
180+
"ttl": "1000",
181+
},
182+
[]string{
183+
"is_protected",
184+
"record_hash",
185+
"rrset_version",
186+
}),
187+
),
188+
},
189+
// remove singular datasource from previous step so that it doesn't conflict with import tests
190+
{
191+
Config: config + compartmentIdVariableStr + RrsetResourceConfig,
192+
},
153193
// verify resource import
154194
{
155195
Config: config,

oci/export_definitions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ var exportDnsTsigKeyHints = &TerraformResourceHints{
737737
},
738738
}
739739

740+
var exportDnsRrsetHints = &TerraformResourceHints{
741+
resourceClass: "oci_dns_rrset",
742+
datasourceClass: "oci_dns_rrset",
743+
resourceAbbreviation: "rrset",
744+
}
745+
740746
var exportDatacatalogCatalogHints = &TerraformResourceHints{
741747
resourceClass: "oci_datacatalog_catalog",
742748
datasourceClass: "oci_datacatalog_catalogs",

oci/export_graphs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ var dnsResourceGraph = TerraformResourceGraph{
339339
{TerraformResourceHints: exportDnsSteeringPolicyAttachmentHints},
340340
{TerraformResourceHints: exportDnsTsigKeyHints},
341341
},
342+
"oci_dns_zone": {
343+
{
344+
TerraformResourceHints: exportDnsRrsetHints,
345+
datasourceQueryParams: map[string]string{
346+
"zone_name_or_id": "id",
347+
},
348+
},
349+
},
342350
}
343351

344352
var datacatalogResourceGraph = TerraformResourceGraph{

oci/export_resource_helpers.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strconv"
1212
"strings"
1313

14+
oci_dns "github.com/oracle/oci-go-sdk/dns"
15+
1416
"github.com/hashicorp/hcl2/hclwrite"
1517

1618
"github.com/fatih/color"
@@ -382,6 +384,78 @@ func init() {
382384
exportKmsKeyHints.processDiscoveredResourcesFn = processKmsKey
383385

384386
exportKmsKeyVersionHints.getIdFn = getKmsKeyVersionId
387+
388+
exportDnsRrsetHints.getIdFn = getDnsRrsetId
389+
exportDnsRrsetHints.findResourcesOverrideFn = findDnsRrset
390+
exportDnsRrsetHints.processDiscoveredResourcesFn = processDnsRrset
391+
}
392+
393+
func processDnsRrset(clients *OracleClients, resources []*OCIResource) ([]*OCIResource, error) {
394+
395+
for _, record := range resources {
396+
// Populate config file from compositeId
397+
record.compartmentId = record.parent.compartmentId
398+
domain, rtype, zoneNameOrId, err := parseRrsetCompositeId(record.id)
399+
if err == nil {
400+
record.sourceAttributes["domain"] = domain
401+
record.sourceAttributes["rtype"] = rtype
402+
record.sourceAttributes["zone_name_or_id"] = zoneNameOrId
403+
}
404+
}
405+
return resources, nil
406+
}
407+
408+
func findDnsRrset(clients *OracleClients, tfMeta *TerraformResourceAssociation, parent *OCIResource) (resources []*OCIResource, err error) {
409+
// Rrset is singular datasource only
410+
// and need to use GetZoneRecordsRequest to list all records
411+
zoneId := parent.id
412+
request := oci_dns.GetZoneRecordsRequest{}
413+
request.ZoneNameOrId = &zoneId
414+
response, err := clients.dnsClient().GetZoneRecords(context.Background(), request)
415+
416+
if err != nil {
417+
return resources, err
418+
}
419+
420+
for _, record := range response.Items {
421+
recordResource := resourcesMap[tfMeta.resourceClass]
422+
d := recordResource.TestResourceData()
423+
zoneId := parent.id
424+
domain := record.Domain
425+
rtype := record.Rtype
426+
d.SetId(getRrsetCompositeId(*domain, *rtype, zoneId))
427+
if err := recordResource.Read(d, clients); err != nil {
428+
return resources, err
429+
}
430+
resource := &OCIResource{
431+
compartmentId: parent.compartmentId,
432+
sourceAttributes: convertResourceDataToMap(recordResource.Schema, d),
433+
rawResource: record,
434+
TerraformResource: TerraformResource{
435+
id: d.Id(),
436+
terraformClass: tfMeta.resourceClass,
437+
terraformName: fmt.Sprintf("%s_%s", parent.parent.terraformName, *record.RecordHash),
438+
},
439+
getHclStringFn: getHclStringFromGenericMap,
440+
parent: parent,
441+
}
442+
resources = append(resources, resource)
443+
}
444+
445+
return resources, err
446+
}
447+
448+
func getDnsRrsetId(resource *OCIResource) (string, error) {
449+
domain, ok := resource.sourceAttributes["domain"].(string)
450+
if !ok {
451+
return "", fmt.Errorf("[ERROR] unable to find domain for DnsRrset")
452+
}
453+
rtype, ok := resource.sourceAttributes["rtype"].(string)
454+
if !ok {
455+
return "", fmt.Errorf("[ERROR] unable to find rtype for DnsRrset")
456+
}
457+
zoneId := resource.parent.id
458+
return getRrsetCompositeId(domain, rtype, zoneId), nil
385459
}
386460

387461
func getDatacatalogDataAssetId(resource *OCIResource) (string, error) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
subcategory: "Dns"
3+
layout: "oci"
4+
page_title: "Oracle Cloud Infrastructure: oci_dns_rrset"
5+
sidebar_current: "docs-oci-datasource-dns-rrset"
6+
description: |-
7+
Provides details about a specific Rrset in Oracle Cloud Infrastructure Dns service
8+
---
9+
10+
# Data Source: oci_dns_rrset
11+
This data source provides details about a specific Rrset resource in Oracle Cloud Infrastructure Dns service.
12+
13+
Gets a list of all records in the specified RRSet. The results are
14+
sorted by `recordHash` by default.
15+
16+
17+
## Example Usage
18+
19+
```hcl
20+
data "oci_dns_rrset" "test_rrset" {
21+
#Required
22+
domain = "${var.rrset_domain}"
23+
rtype = "${var.rrset_rtype}"
24+
zone_name_or_id = "${oci_dns_zone.test_zone.id}"
25+
26+
#Optional
27+
compartment_id = "${var.compartment_id}"
28+
}
29+
```
30+
31+
## Argument Reference
32+
33+
The following arguments are supported:
34+
35+
* `compartment_id` - (Optional) The OCID of the compartment the resource belongs to.
36+
* `domain` - (Required) The target fully-qualified domain name (FQDN) within the target zone.
37+
* `rtype` - (Required) The type of the target RRSet within the target zone.
38+
* `zone_name_or_id` - (Required) The name or OCID of the target zone.
39+
* `zone_version` - (Optional) The version of the zone for which data is requested.
40+
41+
42+
## Attributes Reference
43+
44+
The following attributes are exported:
45+
46+
* `items` -
47+
* `domain` - The fully qualified domain name where the record can be located.
48+
* `is_protected` - A Boolean flag indicating whether or not parts of the record are unable to be explicitly managed.
49+
* `rdata` - The record's data, as whitespace-delimited tokens in type-specific presentation format. All RDATA is normalized and the returned presentation of your RDATA may differ from its initial input. For more information about RDATA, see [Supported DNS Resource Record Types](https://docs.cloud.oracle.com/iaas/Content/DNS/Reference/supporteddnsresource.htm)
50+
* `record_hash` - A unique identifier for the record within its zone.
51+
* `rrset_version` - The latest version of the record's zone in which its RRSet differs from the preceding version.
52+
* `rtype` - The canonical name for the record's type, such as A or CNAME. For more information, see [Resource Record (RR) TYPEs](https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4).
53+
* `ttl` - The Time To Live for the record, in seconds.
54+

website/docs/guides/resource_discovery.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ dns
326326
* oci\_dns\_steering\_policy
327327
* oci\_dns\_steering\_policy\_attachment
328328
* oci\_dns\_tsig\_key
329+
* oci\_dns\_rrset
329330

330331
email
331332

website/oci.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,9 @@
12041204
<li>
12051205
<a href="/docs/providers/oci/d/dns_records.html">oci_dns_records</a>
12061206
</li>
1207+
<li>
1208+
<a href="/docs/providers/oci/d/dns_rrset.html">oci_dns_rrset</a>
1209+
</li>
12071210
<li>
12081211
<a href="/docs/providers/oci/d/dns_steering_policies.html">oci_dns_steering_policies</a>
12091212
</li>

0 commit comments

Comments
 (0)