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

feat: add priority field for MX and SRV records #25

Merged
merged 4 commits into from
Nov 20, 2023
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
14 changes: 13 additions & 1 deletion docs/resources/record.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ resource "hostingde_record" "example" {
name = "test.example.test"
type = "CNAME"
content = "www.example.com"
ttl = 300
}

# Manage example DNS MX record.
resource "hostingde_record" "example" {
zone_id = hostingde_zone.sample.id
name = "test.example.test"
type = "MX"
content = "mail.example.com"
ttl = 300
priority = 10
}
```

Expand All @@ -34,7 +45,8 @@ resource "hostingde_record" "example" {

### Optional

- `ttl` (Number) TTL of the DNS record in seconds.
- `priority` (Number) Priority of MX and SRV records. Defaults to 10.
- `ttl` (Number) TTL of the DNS record in seconds. Minimum is 60, maximum is 31556926. Defaults to 3600.

### Read-Only

Expand Down
11 changes: 11 additions & 0 deletions examples/resources/hostingde_record/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ resource "hostingde_record" "example" {
name = "test.example.test"
type = "CNAME"
content = "www.example.com"
ttl = 300
}

# Manage example DNS MX record.
resource "hostingde_record" "example" {
zone_id = hostingde_zone.sample.id
name = "test.example.test"
type = "MX"
content = "mail.example.com"
ttl = 300
priority = 10
}
6 changes: 4 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
description = "devs & ops environment for nix'ing with triton";
description = "hosting.de terraform provider";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
Expand All @@ -14,7 +14,7 @@
outputs = { self, flake-utils, devshell, nixpkgs }:
flake-utils.lib.simpleFlake {
inherit self nixpkgs;
name = "infra-project";
name = "hosting.de terraform provider";
overlay = devshell.overlays.default;
shell = { pkgs }:
pkgs.devshell.mkShell {
Expand All @@ -28,6 +28,8 @@
golangci-lint
];
bash.extra = ''
export GOPATH=~/.local/share/go
export PATH=$GOPATH:$PATH
'';
};
};
Expand Down
61 changes: 42 additions & 19 deletions hostingde/record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ type recordResource struct {

// recordResourceModel maps the DNSRecord resource schema data.
type recordResourceModel struct {
ID types.String `tfsdk:"id"`
ZoneID types.String `tfsdk:"zone_id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
Content types.String `tfsdk:"content"`
TTL types.Int64 `tfsdk:"ttl"`
ID types.String `tfsdk:"id"`
ZoneID types.String `tfsdk:"zone_id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
Content types.String `tfsdk:"content"`
TTL types.Int64 `tfsdk:"ttl"`
Priority types.Int64 `tfsdk:"priority"`
}

// Metadata returns the resource type name.
Expand Down Expand Up @@ -72,12 +73,19 @@ func (r *recordResource) Schema(_ context.Context, _ resource.SchemaRequest, res
Required: true,
},
"ttl": schema.Int64Attribute{
Description: "TTL of the DNS record in seconds.",
Description: "TTL of the DNS record in seconds. Minimum is 60, maximum is 31556926. Defaults to 3600.",
Computed: true,
Required: false,
Optional: true,
Default: int64default.StaticInt64(3600),
},
"priority": schema.Int64Attribute{
Description: "Priority of MX and SRV records. Defaults to 10.",
Computed: true,
Required: false,
Optional: true,
Default: int64default.StaticInt64(10),
},
},
}
}
Expand All @@ -99,6 +107,7 @@ func (r *recordResource) Create(ctx context.Context, req resource.CreateRequest,
Type: plan.Type.ValueString(),
Content: plan.Content.ValueString(),
TTL: int(plan.TTL.ValueInt64()),
Priority: int(plan.Priority.ValueInt64()),
}

recordReq := RecordsUpdateRequest{
Expand Down Expand Up @@ -131,6 +140,10 @@ func (r *recordResource) Create(ctx context.Context, req resource.CreateRequest,
plan.Content = types.StringValue(returnedRecord.Content)
plan.TTL = types.Int64Value(int64(returnedRecord.TTL))

if (returnedRecord.Type == "MX" || returnedRecord.Type == "SRV") {
plan.Priority = types.Int64Value(int64(returnedRecord.Priority))
}

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -169,13 +182,18 @@ func (r *recordResource) Read(ctx context.Context, req resource.ReadRequest, res
return
}

returnedRecord := recordResp.Response.Data[0];
// Overwrite DNS record with refreshed state
state.ZoneID = types.StringValue(recordResp.Response.Data[0].ZoneID)
state.ID = types.StringValue(recordResp.Response.Data[0].ID)
state.Name = types.StringValue(recordResp.Response.Data[0].Name)
state.Type = types.StringValue(recordResp.Response.Data[0].Type)
state.Content = types.StringValue(recordResp.Response.Data[0].Content)
state.TTL = types.Int64Value(int64(recordResp.Response.Data[0].TTL))
state.ZoneID = types.StringValue(returnedRecord.ZoneID)
state.ID = types.StringValue(returnedRecord.ID)
state.Name = types.StringValue(returnedRecord.Name)
state.Type = types.StringValue(returnedRecord.Type)
state.Content = types.StringValue(returnedRecord.Content)
state.TTL = types.Int64Value(int64(returnedRecord.TTL))

if (returnedRecord.Type == "MX" || returnedRecord.Type == "SRV") {
state.Priority = types.Int64Value(int64(returnedRecord.Priority))
}

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand All @@ -197,12 +215,13 @@ func (r *recordResource) Update(ctx context.Context, req resource.UpdateRequest,

// Generate API request body from plan
record := DNSRecord{
Name: plan.Name.ValueString(),
ID: plan.ID.ValueString(),
ZoneID: plan.ZoneID.ValueString(),
Type: plan.Type.ValueString(),
Content: plan.Content.ValueString(),
TTL: int(plan.TTL.ValueInt64()),
Name: plan.Name.ValueString(),
ID: plan.ID.ValueString(),
ZoneID: plan.ZoneID.ValueString(),
Type: plan.Type.ValueString(),
Content: plan.Content.ValueString(),
TTL: int(plan.TTL.ValueInt64()),
Priority: int(plan.Priority.ValueInt64()),
}

recordReq := RecordsUpdateRequest{
Expand Down Expand Up @@ -235,6 +254,10 @@ func (r *recordResource) Update(ctx context.Context, req resource.UpdateRequest,
plan.Content = types.StringValue(returnedRecord.Content)
plan.TTL = types.Int64Value(int64(returnedRecord.TTL))

if (returnedRecord.Type == "MX" || returnedRecord.Type == "SRV") {
plan.Priority = types.Int64Value(int64(returnedRecord.Priority))
}

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
52 changes: 52 additions & 0 deletions hostingde/record_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ resource "hostingde_record" "test" {
resource.TestCheckResourceAttrSet("hostingde_record.test", "zone_id"),
),
},
// Create and read MX testing
{
Config: providerConfig + `
resource "hostingde_zone" "test" {
name = "example2.test"
type = "NATIVE"
email = "hostmaster@example2.test"
}
resource "hostingde_record" "test_mx" {
zone_id = hostingde_zone.test.id
name = "example2.test"
type = "MX"
content = "mail.example2.test"
priority = 10
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
// Verify name attribute.
resource.TestCheckResourceAttr("hostingde_record.test_mx", "name", "example2.test"),
// Verify type attribute.
resource.TestCheckResourceAttr("hostingde_record.test_mx", "type", "MX"),
// Verify priority attribute.
resource.TestCheckResourceAttr("hostingde_record.test_mx", "priority", "10"),
// Verify email attribute.
resource.TestCheckResourceAttr("hostingde_record.test_mx", "content", "mail.example2.test"),
// Verify dynamic values have any value set in the state.
resource.TestCheckResourceAttrSet("hostingde_record.test_mx", "id"),
),
},
// ImportState testing
{
ResourceName: "hostingde_zone.test",
Expand All @@ -63,6 +92,29 @@ resource "hostingde_record" "test" {
resource.TestCheckResourceAttr("hostingde_record.test", "content", "www2.example.com"),
),
},
// Update and Read testing for MX records
{
Config: providerConfig + `
resource "hostingde_zone" "test" {
name = "example2.test"
type = "NATIVE"
email = "hostmaster@example2.test"
}
resource "hostingde_record" "test_mx" {
zone_id = hostingde_zone.test.id
name = "mail.example2.test"
type = "MX"
content = "mail2.example2.test"
priority = 20
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
// Verify content attribute.
resource.TestCheckResourceAttr("hostingde_record.test_mx", "content", "mail2.example2.test"),
// Verify content attribute.
resource.TestCheckResourceAttr("hostingde_record.test_mx", "priority", "20"),
),
},
// Delete testing automatically occurs in TestCase
},
})
Expand Down