From f07c1e84647dc0b20292c3087c4503b63f765b78 Mon Sep 17 00:00:00 2001 From: Thomas Falkenberg Date: Mon, 29 Jan 2024 15:42:56 +0100 Subject: [PATCH 1/4] extending the record datasource by adding the option to filter by "content" to be able to avoid the problem of having multiple records returned. --- internal/sdkv2provider/data_source_record.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/sdkv2provider/data_source_record.go b/internal/sdkv2provider/data_source_record.go index 8fef93ba37..209de2053f 100644 --- a/internal/sdkv2provider/data_source_record.go +++ b/internal/sdkv2provider/data_source_record.go @@ -36,6 +36,11 @@ func dataSourceCloudflareRecord() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{"A", "AAAA", "CAA", "CNAME", "TXT", "SRV", "LOC", "MX", "NS", "SPF", "CERT", "DNSKEY", "DS", "NAPTR", "SMIMEA", "SSHFP", "TLSA", "URI", "PTR", "HTTPS", "SVCB"}, false), Description: "DNS record type to filter record results on.", }, + "content": { + Type: schema.TypeString, + Optional: true, + Description: "Content to filter record results on.", + }, "priority": { Type: schema.TypeInt, Optional: true, @@ -81,8 +86,9 @@ func dataSourceCloudflareRecordRead(ctx context.Context, d *schema.ResourceData, zoneID := d.Get(consts.ZoneIDSchemaKey).(string) searchRecord := cloudflare.ListDNSRecordsParams{ - Name: d.Get("hostname").(string), - Type: d.Get("type").(string), + Name: d.Get("hostname").(string), + Type: d.Get("type").(string), + Content: d.Get("content").(string), } records, _, err := client.ListDNSRecords(ctx, cloudflare.ZoneIdentifier(zoneID), searchRecord) @@ -96,6 +102,7 @@ func dataSourceCloudflareRecordRead(ctx context.Context, d *schema.ResourceData, if len(records) != 1 && !contains([]string{"MX", "URI"}, searchRecord.Type) { return diag.Errorf("only wanted 1 DNS record. Got %d records", len(records)) + } else { var p uint16 if priority, ok := d.GetOkExists("priority"); ok { From 6db7ec42780125899f08688e7e563c24d7ce396d Mon Sep 17 00:00:00 2001 From: Thomas Falkenberg Date: Mon, 29 Jan 2024 16:06:04 +0100 Subject: [PATCH 2/4] updated the docs for the new optional field "content" for the dns record datasource. --- docs/data-sources/record.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/data-sources/record.md b/docs/data-sources/record.md index e0d50eae3a..15b1ee5cc3 100644 --- a/docs/data-sources/record.md +++ b/docs/data-sources/record.md @@ -27,6 +27,7 @@ data "cloudflare_record" "example" { ### Optional +- `content` (String) Content to filter record results on. - `priority` (Number) DNS priority to filter record results on. - `type` (String) DNS record type to filter record results on. Defaults to `A`. From db7bae5e51458edec5e800c16dd4ede5724e820a Mon Sep 17 00:00:00 2001 From: Thomas Falkenberg Date: Mon, 29 Jan 2024 16:30:33 +0100 Subject: [PATCH 3/4] adding changelog entry. --- .changelog/3084.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/3084.txt diff --git a/.changelog/3084.txt b/.changelog/3084.txt new file mode 100644 index 0000000000..7a5433cf48 --- /dev/null +++ b/.changelog/3084.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +datasource/cloudflare_record: Add the option to filter by "content" +``` \ No newline at end of file From ae4c383d02de34a081e058a404863b618cb4cf13 Mon Sep 17 00:00:00 2001 From: Thomas Falkenberg <152178147+tfalkenberg1@users.noreply.github.com> Date: Tue, 30 Jan 2024 09:20:00 +0100 Subject: [PATCH 4/4] Update internal/sdkv2provider/data_source_record.go removing empty line Co-authored-by: Jacob Bednarz --- internal/sdkv2provider/data_source_record.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/sdkv2provider/data_source_record.go b/internal/sdkv2provider/data_source_record.go index 209de2053f..4852dc1952 100644 --- a/internal/sdkv2provider/data_source_record.go +++ b/internal/sdkv2provider/data_source_record.go @@ -102,7 +102,6 @@ func dataSourceCloudflareRecordRead(ctx context.Context, d *schema.ResourceData, if len(records) != 1 && !contains([]string{"MX", "URI"}, searchRecord.Type) { return diag.Errorf("only wanted 1 DNS record. Got %d records", len(records)) - } else { var p uint16 if priority, ok := d.GetOkExists("priority"); ok {