Skip to content

Commit

Permalink
Fix validator #minor
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Kara <mkara@opsheaven.space>
  • Loading branch information
mustafaahmetkara committed Mar 4, 2024
1 parent f9cb26e commit 1abc560
Showing 1 changed file with 0 additions and 25 deletions.
25 changes: 0 additions & 25 deletions internal/hetzner/dns/record_types.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package dns

import (
"context"
"fmt"
"slices"
"strings"

dsSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
rSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"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"
"github.com/opsheaven/gohetznerdns"
)
Expand Down Expand Up @@ -76,9 +73,6 @@ var RecordResourceSchema = rSchema.Schema{
"type": rSchema.StringAttribute{
MarkdownDescription: fmt.Sprintf("Record Type. Supported values: [ %s ]", strings.Join(allowedRecordTypes, ",")),
Required: true,
Validators: []validator.String{
&recordTypeValidator{},
},
},
"zone_id": rSchema.StringAttribute{
MarkdownDescription: "Zone identifier that record belongs to",
Expand Down Expand Up @@ -145,22 +139,3 @@ func (r *Records) mapFromHetznerRecords(hetznerRecords []*gohetznerdns.Record) d
}

var allowedRecordTypes = []string{"A", "AAAA", "NS", "MX", "CNAME", "RP", "TXT", "SOA", "HINFO", "SRV", "DANE", "TLSA", "DS", "CAA"}

type recordTypeValidator struct {
}

func (r *recordTypeValidator) Description(context.Context) string {
return "Type of the Type field"
}

func (r *recordTypeValidator) MarkdownDescription(context.Context) string {
return fmt.Sprintf("Must be one of the values - [%s]", strings.Join(allowedRecordTypes, ","))
}

func (r *recordTypeValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
resp.Diagnostics.AddError(fmt.Sprintf("%s is unknown", req.Path.String()), fmt.Sprintf("Please provide supported value for the field. Supported values: [%s]", strings.Join(allowedRecordTypes, ",")))
} else if !slices.Contains(allowedRecordTypes, req.ConfigValue.ValueString()) {
resp.Diagnostics.AddError(fmt.Sprintf("Invalid Value for %s", req.Path.String()), fmt.Sprintf("Please provide supported value for the field. Current Value: %s, Supported values: [%s]", req.ConfigValue.ValueString(), strings.Join(allowedRecordTypes, ",")))
}
}

0 comments on commit 1abc560

Please sign in to comment.