Skip to content

Commit

Permalink
Merge pull request #3699 from cloudflare/clean-up-record-validation
Browse files Browse the repository at this point in the history
refactor(cloudflare_record): remove custom validation in favour of `ExactlyOneOf`
  • Loading branch information
jacobbednarz authored Aug 20, 2024
2 parents d981d08 + 64c3677 commit 30dd8e7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .changelog/3699.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_record: refactor validation to use `ExactlyOneOf` instead of custom logic
```
6 changes: 3 additions & 3 deletions docs/resources/record.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ resource "cloudflare_record" "_sip_tls" {

- `allow_overwrite` (Boolean) 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. **This configuration is not recommended for most environments**. Defaults to `false`.
- `comment` (String) Comments or notes about the DNS record. This field has no effect on DNS responses.
- `content` (String) The content of the record. Conflicts with `data`.
- `data` (Block List, Max: 1) Map of attributes that constitute the record value. Conflicts with `value`. (see [below for nested schema](#nestedblock--data))
- `content` (String) The content of the record. Must provide only one of `data`, `content`, `value`.
- `data` (Block List, Max: 1) Map of attributes that constitute the record value. Must provide only one of `data`, `content`, `value`. (see [below for nested schema](#nestedblock--data))
- `priority` (Number) The priority of the record.
- `proxied` (Boolean) Whether the record gets Cloudflare's origin protection.
- `tags` (Set of String) Custom tags for the DNS record.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
- `ttl` (Number) The TTL of the record.
- `value` (String, Deprecated) The value of the record. Conflicts with `data`.
- `value` (String, Deprecated) The value of the record. Must provide only one of `data`, `content`, `value`.

### Read-Only

Expand Down
15 changes: 0 additions & 15 deletions internal/sdkv2provider/resource_cloudflare_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ func resourceCloudflareRecordCreate(ctx context.Context, d *schema.ResourceData,
newRecord.Content = content.(string)
}

if contentOk == valueOk {
return diag.FromErr(fmt.Errorf(
"'content' (present: %t) must not be set with 'value' (present: %t)",
contentOk, valueOk))
}
if valueOk {
contentOk = true
}

data, dataOk := d.GetOk("data")
tflog.Debug(ctx, fmt.Sprintf("Data found in config: %#v", data))

Expand All @@ -100,12 +91,6 @@ func resourceCloudflareRecordCreate(ctx context.Context, d *schema.ResourceData,
newRecord.Data = newDataMap
}

if contentOk == dataOk {
return diag.FromErr(fmt.Errorf(
"either 'content' (present: %t) or 'data' (present: %t) must be provided",
contentOk, dataOk))
}

if priority, ok := d.GetOkExists("priority"); ok {
p := uint16(priority.(int))
newRecord.Priority = &p
Expand Down
14 changes: 7 additions & 7 deletions internal/sdkv2provider/schema_cloudflare_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func resourceCloudflareRecordSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"data"},
ExactlyOneOf: []string{"data", "content", "value"},
DiffSuppressFunc: suppressTrailingDots,
Description: "The value of the record.",
Deprecated: "`value` is deprecated in favour of `content` and will be removed in the next major release.",
Expand All @@ -57,17 +57,17 @@ func resourceCloudflareRecordSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"data"},
ExactlyOneOf: []string{"data", "content", "value"},
DiffSuppressFunc: suppressTrailingDots,
Description: "The content of the record.",
},

"data": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"value"},
Description: "Map of attributes that constitute the record value.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ExactlyOneOf: []string{"data", "content", "value"},
Description: "Map of attributes that constitute the record value.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Properties present in several record types
Expand Down

0 comments on commit 30dd8e7

Please sign in to comment.