Skip to content

Commit

Permalink
Merge pull request #35 from pub-solar/fix/priority-only-for-mx-srv
Browse files Browse the repository at this point in the history
fix: priority should only be used for MX and SRV records
  • Loading branch information
teutat3s authored Jan 12, 2024
2 parents d4976b2 + c9d0887 commit c4d3b31
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 41 deletions.
52 changes: 35 additions & 17 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.18.0 // indirect
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ github.com/hashicorp/terraform-plugin-framework v1.4.2 h1:P7a7VP1GZbjc4rv921Xy5O
github.com/hashicorp/terraform-plugin-framework v1.4.2/go.mod h1:GWl3InPFZi2wVQmdVnINPKys09s9mLmTZr95/ngLnbY=
github.com/hashicorp/terraform-plugin-go v0.20.0 h1:oqvoUlL+2EUbKNsJbIt3zqqZ7wi6lzn4ufkn/UA51xQ=
github.com/hashicorp/terraform-plugin-go v0.20.0/go.mod h1:Rr8LBdMlY53a3Z/HpP+ZU3/xCDqtKNCkeI9qOyT10QE=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
github.com/hashicorp/terraform-plugin-go v0.19.1 h1:lf/jTGTeELcz5IIbn/94mJdmnTjRYm6S6ct/JqCSr50=
github.com/hashicorp/terraform-plugin-go v0.19.1/go.mod h1:5NMIS+DXkfacX6o5HCpswda5yjkSYfKzn1Nfl9l+qRs=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 h1:X7vB6vn5tON2b49ILa4W7mFAsndeqJ7bZFOGbVO+0Cc=
Expand Down
2 changes: 1 addition & 1 deletion hostingde/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type DNSRecord struct {
Type string `json:"type,omitempty"`
Content string `json:"content,omitempty"`
TTL int `json:"ttl,omitempty"`
Priority int `json:"priority,omitempty"`
Priority int `json:"priority"`
LastChangeDate string `json:"lastChangeDate,omitempty"`
}

Expand Down
73 changes: 52 additions & 21 deletions hostingde/record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package hostingde
import (
"context"

"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand Down Expand Up @@ -78,13 +80,15 @@ func (r *recordResource) Schema(_ context.Context, _ resource.SchemaRequest, res
Required: false,
Optional: true,
Default: int64default.StaticInt64(3600),
Validators: []validator.Int64{
int64validator.Between(60, 31556926),
},
},
"priority": schema.Int64Attribute{
Description: "Priority of MX and SRV records. Defaults to 10.",
Description: "Priority of MX and SRV records.",
Computed: true,
Required: false,
Optional: true,
Default: int64default.StaticInt64(10),
},
},
}
Expand All @@ -102,12 +106,12 @@ func (r *recordResource) Create(ctx context.Context, req resource.CreateRequest,

// Generate API request body from plan
record := DNSRecord{
Name: plan.Name.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(),
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 @@ -139,10 +143,7 @@ func (r *recordResource) Create(ctx context.Context, req resource.CreateRequest,
plan.Type = types.StringValue(returnedRecord.Type)
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))
}
plan.Priority = types.Int64Value(int64(returnedRecord.Priority))

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

returnedRecord := recordResp.Response.Data[0];
returnedRecord := recordResp.Response.Data[0]
// Overwrite DNS record with refreshed state
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))
}
state.Priority = types.Int64Value(int64(returnedRecord.Priority))

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -253,10 +251,7 @@ func (r *recordResource) Update(ctx context.Context, req resource.UpdateRequest,
plan.Type = types.StringValue(returnedRecord.Type)
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))
}
plan.Priority = types.Int64Value(int64(returnedRecord.Priority))

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -312,3 +307,39 @@ func (r *recordResource) ImportState(ctx context.Context, req resource.ImportSta
// Retrieve import ID and save to id attribute
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *recordResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
// Retrieve values from config
var configData recordResourceModel
diags := req.Config.Get(ctx, &configData)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

// If Type is MX or SRV, return without warning.
if configData.Type.ValueString() == "MX" || configData.Type.ValueString() == "SRV" {
if configData.Priority.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("Priority"),
"Missing attribute",
"Setting priority is required for records of type MX or SRV. "+
"Please add a priority to the resource, for example priority = 0.",
)
}
return
}

// If Priority is not configured, return without warning.
if configData.Priority.IsNull() || configData.Priority.IsUnknown() {
return
}

resp.Diagnostics.AddAttributeError(
path.Root("Type"),
"Unexpected combination of attributes",
"Priority is only relevant for records of type MX or SRV. "+
"Please remove priority from the resource or change its type.",
)
}
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"flag"
"log"

"github.com/pub-solar/terraform-provider-hostingde/hostingde"

Expand All @@ -13,7 +15,19 @@ import (

//nolint:errcheck
func main() {
providerserver.Serve(context.Background(), hostingde.New, providerserver.ServeOpts{
var debug bool

flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

opts := providerserver.ServeOpts{
Address: "registry.terraform.io/pub-solar/hostingde",
})
Debug: debug,
}

err := providerserver.Serve(context.Background(), hostingde.New, opts)

if err != nil {
log.Fatal(err.Error())
}
}

0 comments on commit c4d3b31

Please sign in to comment.