From 408ad4307dc15c52e29c8af7ecbecdc2b3e1a719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=A4dorf?= Date: Tue, 14 Nov 2023 19:27:39 +0100 Subject: [PATCH 1/4] feat: add priority field for MX and SRV records --- docs/resources/record.md | 4 +- .../resources/hostingde_record/resource.tf | 11 ++++ flake.nix | 6 ++- hostingde/record_resource.go | 38 +++++++++++--- hostingde/record_resource_test.go | 52 +++++++++++++++++++ 5 files changed, 101 insertions(+), 10 deletions(-) diff --git a/docs/resources/record.md b/docs/resources/record.md index 41858c2..493198c 100644 --- a/docs/resources/record.md +++ b/docs/resources/record.md @@ -19,6 +19,7 @@ resource "hostingde_record" "example" { name = "test.example.test" type = "CNAME" content = "www.example.com" + ttl = 300 } ``` @@ -34,7 +35,8 @@ resource "hostingde_record" "example" { ### Optional -- `ttl` (Number) TTL of the DNS record in seconds. +- `priority` (Number) Priority of MX and SRV records. +- `ttl` (Number) TTL of the DNS record in seconds. Minimum is 60, maximum is 31556926. ### Read-Only diff --git a/examples/resources/hostingde_record/resource.tf b/examples/resources/hostingde_record/resource.tf index 76a4afc..0172796 100644 --- a/examples/resources/hostingde_record/resource.tf +++ b/examples/resources/hostingde_record/resource.tf @@ -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 } diff --git a/flake.nix b/flake.nix index 12da179..29e1460 100644 --- a/flake.nix +++ b/flake.nix @@ -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"; @@ -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 { @@ -28,6 +28,8 @@ golangci-lint ]; bash.extra = '' + export GOPATH=~/.local/share/go + export PATH=$GOPATH:$PATH ''; }; }; diff --git a/hostingde/record_resource.go b/hostingde/record_resource.go index aa38b5d..fceb9a0 100644 --- a/hostingde/record_resource.go +++ b/hostingde/record_resource.go @@ -37,6 +37,7 @@ type recordResourceModel struct { 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. @@ -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.", Computed: true, Required: false, Optional: true, Default: int64default.StaticInt64(3600), }, + "priority": schema.Int64Attribute{ + Description: "Priority of MX and SRV records.", + Computed: true, + Required: false, + Optional: true, + Default: int64default.StaticInt64(10), + }, }, } } @@ -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{ @@ -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...) @@ -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) @@ -203,6 +221,7 @@ func (r *recordResource) Update(ctx context.Context, req resource.UpdateRequest, Type: plan.Type.ValueString(), Content: plan.Content.ValueString(), TTL: int(plan.TTL.ValueInt64()), + Priority: int(plan.Priority.ValueInt64()), } recordReq := RecordsUpdateRequest{ @@ -235,6 +254,11 @@ 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() { diff --git a/hostingde/record_resource_test.go b/hostingde/record_resource_test.go index 8346656..b88d651 100644 --- a/hostingde/record_resource_test.go +++ b/hostingde/record_resource_test.go @@ -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", @@ -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 }, }) From 53e295db3c9687d0d550bfa2cad6a6a23ea2dd31 Mon Sep 17 00:00:00 2001 From: teutat3s <10206665+teutat3s@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:59:57 +0100 Subject: [PATCH 2/4] docs: regenerate docs, add example for MX record Command used: make docs --- docs/resources/record.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/resources/record.md b/docs/resources/record.md index 493198c..c7c4795 100644 --- a/docs/resources/record.md +++ b/docs/resources/record.md @@ -21,6 +21,16 @@ resource "hostingde_record" "example" { 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 +} ``` From 7cef1a0dddb03f1417fcb41dd463de91d23fa48a Mon Sep 17 00:00:00 2001 From: teutat3s <10206665+teutat3s@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:01:15 +0100 Subject: [PATCH 3/4] chore: fix formatting and double newline --- hostingde/record_resource.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/hostingde/record_resource.go b/hostingde/record_resource.go index fceb9a0..aaf00a6 100644 --- a/hostingde/record_resource.go +++ b/hostingde/record_resource.go @@ -31,13 +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"` - Priority types.Int64 `tfsdk:"priority"` + 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. @@ -215,13 +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()), - Priority: int(plan.Priority.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{ @@ -258,7 +258,6 @@ func (r *recordResource) Update(ctx context.Context, req resource.UpdateRequest, plan.Priority = types.Int64Value(int64(returnedRecord.Priority)) } - diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { From 40e87f8490afd52a7175bf30d94db0b98a16d29d Mon Sep 17 00:00:00 2001 From: teutat3s <10206665+teutat3s@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:06:14 +0100 Subject: [PATCH 4/4] docs: add defaults for ttl and priority --- docs/resources/record.md | 4 ++-- hostingde/record_resource.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/resources/record.md b/docs/resources/record.md index c7c4795..f54aa98 100644 --- a/docs/resources/record.md +++ b/docs/resources/record.md @@ -45,8 +45,8 @@ resource "hostingde_record" "example" { ### Optional -- `priority` (Number) Priority of MX and SRV records. -- `ttl` (Number) TTL of the DNS record in seconds. Minimum is 60, maximum is 31556926. +- `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 diff --git a/hostingde/record_resource.go b/hostingde/record_resource.go index aaf00a6..e14e88b 100644 --- a/hostingde/record_resource.go +++ b/hostingde/record_resource.go @@ -73,14 +73,14 @@ func (r *recordResource) Schema(_ context.Context, _ resource.SchemaRequest, res Required: true, }, "ttl": schema.Int64Attribute{ - Description: "TTL of the DNS record in seconds. Minimum is 60, maximum is 31556926.", + 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.", + Description: "Priority of MX and SRV records. Defaults to 10.", Computed: true, Required: false, Optional: true,