Skip to content

Commit

Permalink
Merge pull request #1124 from cloudflare/support-allow-overwrite-dns-…
Browse files Browse the repository at this point in the history
…records
  • Loading branch information
jacobbednarz authored Jul 6, 2021
2 parents b9c16e0 + 7072f8b commit 4b4712b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cloudflare/resource_cloudflare_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ func resourceCloudflareRecord() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"allow_overwrite": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Second),
Expand Down Expand Up @@ -369,6 +374,27 @@ func resourceCloudflareRecordCreate(d *schema.ResourceData, meta interface{}) er
r, err := client.CreateDNSRecord(context.Background(), newRecord.ZoneID, newRecord)
if err != nil {
if strings.Contains(err.Error(), "already exist") {

if d.Get("allow_overwrite").(bool) {
log.Printf("[DEBUG] Cloudflare Record already exists however we are overwriting it")
zone, _ := client.ZoneDetails(context.Background(), d.Get("zone_id").(string))
r := cloudflare.DNSRecord{
Name: d.Get("name").(string) + "." + zone.Name,
Type: d.Get("type").(string),
}
rs, _ := client.DNSRecords(context.Background(), d.Get("zone_id").(string), r)

if len(rs) != 1 {
return resource.RetryableError(fmt.Errorf("attempted to override existing record however didn't find an exact match"))
}

// Here we need to set the ID as the state will not have one and in order
// for Terraform to operate on it, we need an anchor.
d.SetId(rs[0].ID)

return resource.NonRetryableError(resourceCloudflareRecordUpdate(d, meta))
}

return resource.RetryableError(fmt.Errorf("expected DNS record to not already be present but already exists"))
}

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/record.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The following arguments are supported:
* `ttl` - (Optional) The TTL of the record ([automatic: '1'](https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record))
* `priority` - (Optional) The priority of the record
* `proxied` - (Optional) Whether the record gets Cloudflare's origin protection; defaults to `false`.
* `allow_overwrite` - (Optional) Allow creation of this record in Terraform to overwrite an existing record, if any. This does not affect the ability to update the record in Terraform and does not prevent other resources within Terraform or manual changes outside Terraform from overwriting this record. `false` by default. **This configuration is not recommended for most environments**.

## Attributes Reference

Expand Down

0 comments on commit 4b4712b

Please sign in to comment.