Skip to content

Commit 7468334

Browse files
committed
Modify DNS Record resource code to use PatchRRSetRequest instead of PatchZoneRecordsRequest
1 parent 814b21b commit 7468334

File tree

4 files changed

+54
-43
lines changed

4 files changed

+54
-43
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Enable regional Subnets by making Availability Domain optional when creating a S
44
### Added
55
- Adding description property to rules in Steering Policies in DNS
66

7+
### Fixed
8+
- DNS Record now requires domain and rtype as mandatory arguments. Managing DNS record resources now requires DNS_RECORD* level policy entitlements instead of DNS_ZONE*. [Permissions List](https://docs.cloud.oracle.com/iaas/Content/Identity/Reference/dnspolicyreference.htm)
9+
710
## 3.15.0 (February 12, 2019)
811

912
### Added

oci/dns_record_resource.go

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ package provider
1111

1212
import (
1313
"context"
14+
"fmt"
1415

1516
"github.com/hashicorp/terraform/helper/schema"
1617

17-
"fmt"
1818
"net"
1919
"regexp"
2020

@@ -45,7 +45,8 @@ func DnsRecordResource() *schema.Resource {
4545
// Optional
4646
"domain": {
4747
Type: schema.TypeString,
48-
Optional: true,
48+
ForceNew: true,
49+
Required: true,
4950
},
5051
"rdata": {
5152
Type: schema.TypeString,
@@ -57,7 +58,8 @@ func DnsRecordResource() *schema.Resource {
5758
},
5859
"rtype": {
5960
Type: schema.TypeString,
60-
Optional: true,
61+
ForceNew: true,
62+
Required: true,
6163
},
6264
"ttl": {
6365
Type: schema.TypeInt,
@@ -126,32 +128,30 @@ func (s *DnsRecordResourceCrud) ID() string {
126128
}
127129

128130
func (s *DnsRecordResourceCrud) Create() error {
129-
request := oci_dns.PatchZoneRecordsRequest{}
131+
request := oci_dns.PatchRRSetRequest{}
130132
ro := oci_dns.RecordOperation{Operation: oci_dns.RecordOperationOperationAdd}
131133

132134
zoneNameOrId := s.D.Get("zone_name_or_id").(string)
133135
request.ZoneNameOrId = &zoneNameOrId
134136

137+
domain := s.D.Get("domain").(string)
138+
request.Domain = &domain
139+
ro.Domain = &domain
140+
141+
rtype := s.D.Get("rtype").(string)
142+
request.Rtype = &rtype
143+
ro.Rtype = &rtype
144+
135145
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
136146
tmp := compartmentId.(string)
137147
request.CompartmentId = &tmp
138148
}
139149

140-
if domain, ok := s.D.GetOkExists("domain"); ok {
141-
tmp := domain.(string)
142-
ro.Domain = &tmp
143-
}
144-
145150
if rdata, ok := s.D.GetOkExists("rdata"); ok {
146151
tmp := rdata.(string)
147152
ro.Rdata = &tmp
148153
}
149154

150-
if rtype, ok := s.D.GetOkExists("rtype"); ok {
151-
tmp := rtype.(string)
152-
ro.Rtype = &tmp
153-
}
154-
155155
if ttl, ok := s.D.GetOkExists("ttl"); ok {
156156
tmp := ttl.(int)
157157
ro.Ttl = &tmp
@@ -160,14 +160,14 @@ func (s *DnsRecordResourceCrud) Create() error {
160160
request.Items = []oci_dns.RecordOperation{ro}
161161

162162
request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "dns")
163-
response, err := s.Client.PatchZoneRecords(context.Background(), request)
163+
response, err := s.Client.PatchRRSet(context.Background(), request)
164164
if err != nil {
165165
return err
166166
}
167167

168168
// The patch operation can add a record, but it returns ALL records, so there is no absolute way to map to this new item.
169169
// Since there cant be duplicate records, try to match on the rType and rData that was just used
170-
item, err := findItem(&response.RecordCollection, s.D)
170+
item, err := findItem(&response.Items, s.D)
171171

172172
if err != nil {
173173
//PatchZoneRecords only returns 50 records we need to do a Get with pagination to make sure we can't find the item
@@ -192,11 +192,17 @@ func (s *DnsRecordResourceCrud) Create() error {
192192
}
193193

194194
func (s *DnsRecordResourceCrud) Get() error {
195-
request := oci_dns.GetZoneRecordsRequest{}
195+
request := oci_dns.GetRRSetRequest{}
196196

197197
zoneNameOrId := s.D.Get("zone_name_or_id").(string)
198198
request.ZoneNameOrId = &zoneNameOrId
199199

200+
domain := s.D.Get("domain").(string)
201+
request.Domain = &domain
202+
203+
rtype := s.D.Get("rtype").(string)
204+
request.Rtype = &rtype
205+
200206
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
201207
tmp := compartmentId.(string)
202208
request.CompartmentId = &tmp
@@ -206,11 +212,11 @@ func (s *DnsRecordResourceCrud) Get() error {
206212

207213
var err error
208214
for true {
209-
response, err := s.Client.GetZoneRecords(context.Background(), request)
215+
response, err := s.Client.GetRRSet(context.Background(), request)
210216
if err != nil {
211217
return err
212218
}
213-
item, err := findItem(&response.RecordCollection, s.D)
219+
item, err := findItem(&response.Items, s.D)
214220
if err == nil {
215221
s.Res = item
216222
return nil
@@ -225,9 +231,11 @@ func (s *DnsRecordResourceCrud) Get() error {
225231

226232
func (s *DnsRecordResourceCrud) Update() error {
227233
zoneNameOrId := s.D.Get("zone_name_or_id").(string)
228-
request := oci_dns.PatchZoneRecordsRequest{ZoneNameOrId: &zoneNameOrId}
234+
domain := s.D.Get("domain").(string)
235+
rtype := s.D.Get("rtype").(string)
236+
request := oci_dns.PatchRRSetRequest{ZoneNameOrId: &zoneNameOrId, Domain: &domain, Rtype: &rtype}
229237

230-
// "Update" using PatchZoneRecords requires removing the target record then adding the updated version.
238+
// "Update" using PatchRRSetRequest requires removing the target record then adding the updated version.
231239
recordHash := s.D.Get("record_hash").(string)
232240
removeOp := oci_dns.RecordOperation{Operation: oci_dns.RecordOperationOperationRemove, RecordHash: &recordHash}
233241
addOp := oci_dns.RecordOperation{Operation: oci_dns.RecordOperationOperationAdd}
@@ -237,21 +245,14 @@ func (s *DnsRecordResourceCrud) Update() error {
237245
request.CompartmentId = &tmp
238246
}
239247

240-
if domain, ok := s.D.GetOkExists("domain"); ok {
241-
tmp := domain.(string)
242-
addOp.Domain = &tmp
243-
}
248+
addOp.Domain = &domain
249+
addOp.Rtype = &rtype
244250

245251
if rdata, ok := s.D.GetOkExists("rdata"); ok {
246252
tmp := rdata.(string)
247253
addOp.Rdata = &tmp
248254
}
249255

250-
if rtype, ok := s.D.GetOkExists("rtype"); ok {
251-
tmp := rtype.(string)
252-
addOp.Rtype = &tmp
253-
}
254-
255256
if ttl, ok := s.D.GetOkExists("ttl"); ok {
256257
tmp := ttl.(int)
257258
addOp.Ttl = &tmp
@@ -260,11 +261,11 @@ func (s *DnsRecordResourceCrud) Update() error {
260261
request.Items = []oci_dns.RecordOperation{removeOp, addOp}
261262

262263
request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "dns")
263-
response, err := s.Client.PatchZoneRecords(context.Background(), request)
264+
response, err := s.Client.PatchRRSet(context.Background(), request)
264265
if err != nil {
265266
return err
266267
}
267-
item, err := findItem(&response.RecordCollection, s.D)
268+
item, err := findItem(&response.Items, s.D)
268269

269270
if err != nil {
270271
//PatchZoneRecords only returns 50 records we need to do a Get with pagination to make sure we can't find the item
@@ -280,12 +281,18 @@ func (s *DnsRecordResourceCrud) Update() error {
280281
}
281282

282283
func (s *DnsRecordResourceCrud) Delete() error {
283-
request := oci_dns.PatchZoneRecordsRequest{}
284+
request := oci_dns.PatchRRSetRequest{}
284285
ro := oci_dns.RecordOperation{Operation: oci_dns.RecordOperationOperationRemove}
285286

286287
zoneNameOrId := s.D.Get("zone_name_or_id").(string)
287288
request.ZoneNameOrId = &zoneNameOrId
288289

290+
domain := s.D.Get("domain").(string)
291+
request.Domain = &domain
292+
293+
rtype := s.D.Get("rtype").(string)
294+
request.Rtype = &rtype
295+
289296
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
290297
tmp := compartmentId.(string)
291298
request.CompartmentId = &tmp
@@ -299,7 +306,7 @@ func (s *DnsRecordResourceCrud) Delete() error {
299306
request.Items = []oci_dns.RecordOperation{ro}
300307

301308
request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "dns")
302-
_, err := s.Client.PatchZoneRecords(context.Background(), request)
309+
_, err := s.Client.PatchRRSet(context.Background(), request)
303310
return err
304311
}
305312

@@ -337,15 +344,15 @@ func (s *DnsRecordResourceCrud) SetData() error {
337344
return nil
338345
}
339346

340-
func findItem(rc *oci_dns.RecordCollection, r *schema.ResourceData) (*oci_dns.Record, error) {
347+
func findItem(rc *[]oci_dns.Record, r *schema.ResourceData) (*oci_dns.Record, error) {
341348
rType := r.Get("rtype").(string)
342349
rData := r.Get("rdata").(string)
343350
rDomain := r.Get("domain").(string)
344351
rTtl := r.Get("ttl").(int)
345352
rData = normalizeRData(rType, rData)
346353
rHash, rHashOk := r.GetOk("record_hash")
347354

348-
for _, item := range rc.Items {
355+
for _, item := range *rc {
349356
// prefer exact match by record hash
350357
if rHashOk && rHash == *item.RecordHash {
351358
return &item, nil
@@ -357,7 +364,7 @@ func findItem(rc *oci_dns.RecordCollection, r *schema.ResourceData) (*oci_dns.Re
357364
}
358365
}
359366

360-
return nil, fmt.Errorf("target %s record could not be matched against data %s\nfrom set %+v", rType, rData, rc.Items)
367+
return nil, fmt.Errorf("target %s record could not be matched against data %s\nfrom set %+v", rType, rData, rc)
361368
}
362369

363370
// Match dns service transforms of rdata

oci/dns_record_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"fmt"
88
"testing"
99

10+
"github.com/hashicorp/terraform/helper/schema"
11+
1012
"regexp"
1113

1214
"github.com/hashicorp/terraform/helper/resource"
13-
"github.com/hashicorp/terraform/helper/schema"
1415
"github.com/hashicorp/terraform/terraform"
1516
"github.com/oracle/oci-go-sdk/common"
1617
oci_dns "github.com/oracle/oci-go-sdk/dns"
@@ -369,7 +370,7 @@ func testAccCheckDnsRecordDestroy(s *terraform.State) error {
369370
recordCollection.Items = append(recordCollection.Items, listResponse.Items...)
370371
request.Page = listResponse.OpcNextPage
371372
}
372-
_, err = findItem(&response.RecordCollection, resourceData)
373+
_, err = findItem(&response.Items, resourceData)
373374
if err == nil {
374375
return fmt.Errorf("resource still exists")
375376
}

website/docs/r/dns_record.html.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ request body, the record will be removed from the zone.
2222
resource "oci_dns_record" "test_record" {
2323
#Required
2424
zone_name_or_id = "${oci_dns_zone_name_or.test_zone_name_or.id}"
25+
domain = "${var.record_items_domain}"
26+
rtype = "${var.record_items_rtype}"
2527
2628
#Optional
2729
compartment_id = "${var.compartment_id}"
28-
domain = "${var.record_items_domain}"
2930
rdata = "${var.record_items_rdata}"
30-
rtype = "${var.record_items_rtype}"
3131
ttl = "${var.record_items_ttl}"
3232
}
3333
```
@@ -37,9 +37,9 @@ resource "oci_dns_record" "test_record" {
3737
The following arguments are supported:
3838

3939
* `compartment_id` - (Optional) (Updatable) The OCID of the compartment the resource belongs to. If supplied, it must match the Zone's compartment ocid.
40-
* `domain` - (Optional) (Updatable) The fully qualified domain name where the record can be located.
40+
* `domain` - (Required) The fully qualified domain name where the record can be located.
4141
* `rdata` - (Optional) (Updatable) 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)
42-
* `rtype` - (Optional) (Updatable) 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).
42+
* `rtype` - (Required) 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).
4343
* `ttl` - (Optional) (Updatable) The Time To Live for the record, in seconds.
4444
* `zone_name_or_id` - (Required) The name or OCID of the target zone.
4545

0 commit comments

Comments
 (0)