Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DNS record data source #1906

Merged
merged 12 commits into from Sep 21, 2022

Conversation

Cyb3r-Jak3
Copy link
Contributor

Adds a data source for an existing DNS record

Resolves #1904

@github-actions
Copy link
Contributor

github-actions bot commented Sep 12, 2022

changelog detected ✅

@Cyb3r-Jak3 Cyb3r-Jak3 marked this pull request as ready for review September 13, 2022 00:46
Copy link
Member

@jacobbednarz jacobbednarz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've added some notes inline; we probably need to work out how we want to search/filter for a single record before going any further.

.changelog/1906.txt Outdated Show resolved Hide resolved
@Cyb3r-Jak3
Copy link
Contributor Author

Cyb3r-Jak3 commented Sep 14, 2022

For the search, what about using the list DNS record API call (https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records) but applying a filter of hostname and a type of "A" unless set? Could even make the page response only be 1 record on 1 page.

The only case where I can see that not working would be MX records, given they have a priority with likely the same hostname.

@jacobbednarz
Copy link
Member

jacobbednarz commented Sep 15, 2022

For the search, what about using the list DNS record API call (api.cloudflare.com/#dns-records-for-a-zone-list-dns-records) but applying a filter of hostname and a type of "A" unless set? Could even make the page response only be 1 record on 1 page.

yep, where possible, offload the actual searching the Cloudflare API and don't do it in the data source itself. check out cloudflare_zone for an example of this.

@Cyb3r-Jak3
Copy link
Contributor Author

Cyb3r-Jak3 commented Sep 15, 2022

This is what I devised

records, err := client.DNSRecords(ctx, zoneID, searchRecord)
if err != nil {
	return diag.FromErr(fmt.Errorf("error listing DNS records: %w", err))
}

if len(records) == 0 {
	return diag.Errorf("didn't get any DNS records for hostname: %s", searchRecord.Name)
}

if len(records) != 1 && !contains([]string{"MX", "URI"}, searchRecord.Type) {
	return diag.Errorf("only wanted 1 DNS record. Got %d records", len(records))
} else {
	for _, record := range records {
		if record.Priority == searchRecord.Priority {
			records = []cloudflare.DNSRecord{record}
			break
		}
	}
	if len(records) != 1 {
		return diag.Errorf("unable to find single record for %s type %s", searchRecord.Name, searchRecord.Type)
	}
}

I can't do the page limiting as the cloudflare-go function only search with a DNS record and returns all matching records. The logic behind it is

  1. Find all DNS records that have the same hostname and type. If no records found, throw an error.
  2. If there is more than one record available, make sure that the type is either MX or URI (these are the only two with priority) or throw an error.
  3. Search records to find one that match priority. If no records found then throw an error.

@jacobbednarz jacobbednarz merged commit 16ebd1c into cloudflare:master Sep 21, 2022
@Cyb3r-Jak3 Cyb3r-Jak3 deleted the record-data-source branch September 21, 2022 00:24
@github-actions github-actions bot added this to the v3.24.0 milestone Sep 21, 2022
github-actions bot pushed a commit that referenced this pull request Sep 21, 2022
@github-actions
Copy link
Contributor

This functionality has been released in v3.24.0 of the Terraform Cloudflare Provider.

Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add cloudflare_record data source.
2 participants