这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix DNS Record resource #64

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

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/resources/dns_record.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ subcategory: ""
description: |-
Provides a DNS Record resource.
DNS records are instructions that live in authoritative DNS servers and provide information about a domain.
~> The value field must be specified on all DNS record types except SRV. When using SRV DNS records, the srv field must be specified.
For more detailed information, please see the Vercel documentation https://vercel.com/docs/concepts/projects/custom-domains#dns-records
---

Expand All @@ -14,6 +15,8 @@ Provides a DNS Record resource.

DNS records are instructions that live in authoritative DNS servers and provide information about a domain.

~> The `value` field must be specified on all DNS record types except `SRV`. When using `SRV` DNS records, the `srv` field must be specified.

For more detailed information, please see the [Vercel documentation](https://vercel.com/docs/concepts/projects/custom-domains#dns-records)

## Example Usage
Expand Down Expand Up @@ -97,7 +100,7 @@ resource "vercel_dns_record" "txt" {

- `domain` (String) The domain name, or zone, that the DNS record should be created beneath.
- `name` (String) The subdomain name of the record. This should be an empty string if the rercord is for the root domain.
- `type` (String) The type of DNS record.
- `type` (String) The type of DNS record. Available types: `A`, `AAAA`, `ALIAS`, `CAA`, `CNAME`, `MX`, `NS`, `SRV`, `TXT`.

### Optional

Expand Down
13 changes: 12 additions & 1 deletion vercel/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Provides a DNS Record resource.

DNS records are instructions that live in authoritative DNS servers and provide information about a domain.

~> The ` + "`value` field" + ` must be specified on all DNS record types except ` + "`SRV`" + `. When using ` + "`SRV`" + ` DNS records, the ` + "`srv`" + ` field must be specified.

For more detailed information, please see the [Vercel documentation](https://vercel.com/docs/concepts/projects/custom-domains#dns-records)
`,
Attributes: map[string]tfsdk.Attribute{
Expand All @@ -48,7 +50,7 @@ For more detailed information, please see the [Vercel documentation](https://ver
Type: types.StringType,
},
"type": {
Description: "The type of DNS record.",
Description: "The type of DNS record. Available types: " + "`A`" + ", " + "`AAAA`" + ", " + "`ALIAS`" + ", " + "`CAA`" + ", " + "`CNAME`" + ", " + "`MX`" + ", " + "`NS`" + ", " + "`SRV`" + ", " + "`TXT`" + ".",
PlanModifiers: tfsdk.AttributePlanModifiers{resource.RequiresReplace()},
Required: true,
Type: types.StringType,
Expand All @@ -65,6 +67,7 @@ For more detailed information, please see the [Vercel documentation](https://ver
"ttl": {
Description: "The TTL value in seconds. Must be a number between 60 and 2147483647. If unspecified, it will default to 60 seconds.",
Optional: true,
Computed: true,
Type: types.Int64Type,
Validators: []tfsdk.AttributeValidator{
int64GreaterThan(60),
Expand Down Expand Up @@ -141,13 +144,21 @@ func (r resourceDNSRecord) ValidateConfig(ctx context.Context, req resource.Vali
if resp.Diagnostics.HasError() {
return
}

if config.Type.Value == "SRV" && config.SRV == nil {
resp.Diagnostics.AddError(
"DNS Record Invalid",
"A DNS Record type of 'SRV' requires the `srv` attribute to be set",
)
}

if config.Type.Value != "SRV" && config.Value.Null {
resp.Diagnostics.AddError(
"DNS Record Invalid",
fmt.Sprintf("The `value` attribute must be set on records of `type` '%s'", config.Type.Value),
)
}

if config.Type.Value == "SRV" && !config.Value.Null {
resp.Diagnostics.AddError(
"DNS Record Invalid",
Expand Down
1 change: 1 addition & 0 deletions vercel/resource_dns_record_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (d DNSRecord) toCreateDNSRecordRequest() client.CreateDNSRecordRequest {
Weight: d.SRV.Weight.Value,
}
}

return client.CreateDNSRecordRequest{
Domain: d.Domain.Value,
MXPriority: d.MXPriority.Value,
Expand Down
24 changes: 24 additions & 0 deletions vercel/resource_dns_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestAcc_DNSRecord(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: resource.ComposeAggregateTestCheckFunc(
testAccDNSRecordDestroy("vercel_dns_record.a_without_ttl", ""),
testAccDNSRecordDestroy("vercel_dns_record.a", ""),
testAccDNSRecordDestroy("vercel_dns_record.aaaa", ""),
testAccDNSRecordDestroy("vercel_dns_record.alias", ""),
Expand All @@ -76,6 +77,11 @@ func TestAcc_DNSRecord(t *testing.T) {
{
Config: testAccDNSRecordConfig(testDomain(), nameSuffix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccDNSRecordExists("vercel_dns_record.a_without_ttl", ""),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "domain", testDomain()),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "type", "A"),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "ttl", "60"),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "value", "127.0.0.1"),
testAccDNSRecordExists("vercel_dns_record.a", ""),
resource.TestCheckResourceAttr("vercel_dns_record.a", "domain", testDomain()),
resource.TestCheckResourceAttr("vercel_dns_record.a", "type", "A"),
Expand Down Expand Up @@ -137,6 +143,11 @@ func TestAcc_DNSRecord(t *testing.T) {
{
Config: testAccDNSRecordConfigUpdated(testDomain(), nameSuffix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccDNSRecordExists("vercel_dns_record.a_without_ttl", ""),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "domain", testDomain()),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "type", "A"),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "ttl", "120"),
resource.TestCheckResourceAttr("vercel_dns_record.a_without_ttl", "value", "127.0.0.1"),
testAccDNSRecordExists("vercel_dns_record.a", ""),
resource.TestCheckResourceAttr("vercel_dns_record.a", "domain", testDomain()),
resource.TestCheckResourceAttr("vercel_dns_record.a", "type", "A"),
Expand Down Expand Up @@ -234,6 +245,12 @@ func TestAcc_DNSRecord(t *testing.T) {

func testAccDNSRecordConfig(testDomain, nameSuffix string) string {
return fmt.Sprintf(`
resource "vercel_dns_record" "a_without_ttl" {
domain = "%[1]s"
name = "test-acc-%[2]s-a-without-ttl-record"
type = "A"
value = "127.0.0.1"
}
resource "vercel_dns_record" "a" {
domain = "%[1]s"
name = "test-acc-%[2]s-a-record"
Expand Down Expand Up @@ -320,6 +337,13 @@ resource "vercel_dns_record" "ns" {

func testAccDNSRecordConfigUpdated(testDomain, nameSuffix string) string {
return fmt.Sprintf(`
resource "vercel_dns_record" "a_without_ttl" {
domain = "%[1]s"
name = "test-acc-%[2]s-a-without-ttl-record"
type = "A"
ttl = 120
value = "127.0.0.1"
}
resource "vercel_dns_record" "a" {
domain = "%[1]s"
name = "test-acc-%[2]s-a-record-updated"
Expand Down