Skip to content

Commit

Permalink
Support regional records (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilson Lin authored Sep 19, 2023
1 parent a4d824b commit c61daf9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/zone_record.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The following arguments are supported:
* `type` - (Required) The type of the record
* `ttl` - (Optional) The TTL of the record - defaults to 3600
* `priority` - (Optional) The priority of the record - only useful for some record types
* `regions` - (Optional) A list of regions to serve the record from. You can find a list of supported values in our [developer documentation](https://developer.dnsimple.com/v2/zones/records/).


## Attributes Reference
Expand Down
11 changes: 11 additions & 0 deletions internal/framework/resources/zone_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ZoneRecordResourceModel struct {
Name types.String `tfsdk:"name"`
QualifiedName types.String `tfsdk:"qualified_name"`
Type types.String `tfsdk:"type"`
Regions types.List `tfsdk:"regions"`
Value types.String `tfsdk:"value"`
TTL types.Int64 `tfsdk:"ttl"`
Priority types.Int64 `tfsdk:"priority"`
Expand Down Expand Up @@ -81,6 +82,10 @@ func (r *ZoneRecordResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringplanmodifier.RequiresReplace(),
},
},
"regions": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
},
"value": schema.StringAttribute{
Required: true,
},
Expand Down Expand Up @@ -125,6 +130,8 @@ func (r *ZoneRecordResource) Create(ctx context.Context, req resource.CreateRequ

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
regions := make([]string, len(data.Regions.Elements()))
resp.Diagnostics.Append(data.Regions.ElementsAs(ctx, &regions, false)...)

if resp.Diagnostics.HasError() {
return
Expand All @@ -134,6 +141,7 @@ func (r *ZoneRecordResource) Create(ctx context.Context, req resource.CreateRequ
Name: dnsimple.String(data.Name.ValueString()),
Type: data.Type.ValueString(),
Content: data.Value.ValueString(),
Regions: regions,
TTL: int(data.TTL.ValueInt64()),
}

Expand Down Expand Up @@ -248,6 +256,8 @@ func (r *ZoneRecordResource) Update(ctx context.Context, req resource.UpdateRequ

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
regions := make([]string, len(data.Regions.Elements()))
resp.Diagnostics.Append(data.Regions.ElementsAs(ctx, &regions, false)...)

if resp.Diagnostics.HasError() {
return
Expand All @@ -257,6 +267,7 @@ func (r *ZoneRecordResource) Update(ctx context.Context, req resource.UpdateRequ
Name: dnsimple.String(data.Name.ValueString()),
Type: data.Type.ValueString(),
Content: data.Value.ValueString(),
Regions: regions,
TTL: int(data.TTL.ValueInt64()),
}

Expand Down
32 changes: 32 additions & 0 deletions internal/framework/resources/zone_record_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources_test

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -30,9 +31,23 @@ func TestAccZoneRecordResource(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "zone_name", domainName),
resource.TestCheckResourceAttr(resourceName, "qualified_name", "terraform."+domainName),
resource.TestCheckResourceAttr(resourceName, "ttl", "2800"),
resource.TestCheckResourceAttr(resourceName, "value", "192.168.0.10"),
resource.TestCheckResourceAttrSet(resourceName, "id"),
),
},
{
Config: testAccZoneRecordResourceStandardWithRegionsConfig(domainName, []string{"IAD", "SYD"}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "zone_name", domainName),
resource.TestCheckResourceAttr(resourceName, "qualified_name", "terraform."+domainName),
resource.TestCheckResourceAttr(resourceName, "ttl", "4000"),
resource.TestCheckResourceAttr(resourceName, "value", "192.168.0.11"),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "#regions", "2"),
resource.TestCheckResourceAttr(resourceName, "regions.0", "IAD"),
resource.TestCheckResourceAttr(resourceName, "regions.1", "SYD"),
),
},
{
Config: testAccZoneRecordResourceStandardWithDefaultsConfig(domainName),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -250,6 +265,23 @@ resource "dnsimple_zone_record" "test" {
}`, domainName)
}

func testAccZoneRecordResourceStandardWithRegionsConfig(domainName string, regions []string) string {
regionsRaw, err := json.Marshal(regions)
if err != nil {
panic(err)
}
return fmt.Sprintf(`
resource "dnsimple_zone_record" "test" {
zone_name = %[1]q
name = "terraform"
value = "192.168.0.11"
type = "A"
ttl = 4000
regions = %[2]s
}`, domainName, regionsRaw)
}

func testAccZoneRecordResourceStandardWithDefaultsConfig(domainName string) string {
return fmt.Sprintf(`
resource "dnsimple_zone_record" "test" {
Expand Down

0 comments on commit c61daf9

Please sign in to comment.