fix: validates the record conent based on record type (#267)

* fix:  validates the record conent based on record type

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>

* fix typo

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>

* apply feedback

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>

* apply feedback

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>

---------

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>
This commit is contained in:
Jorge Turrado Ferrero 2024-02-20 16:17:27 +01:00 committed by GitHub
parent a88688ce93
commit 32d176ee86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 196 additions and 1 deletions

View file

@ -10,7 +10,9 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
)
@ -73,6 +75,45 @@ func IP() *Validator {
}
}
func RecordSet() *Validator {
const typePath = "type"
return &Validator{
description: "value must be a valid record set",
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
recordType := basetypes.StringValue{}
req.Config.GetAttribute(ctx, path.Root(typePath), &recordType)
switch recordType.ValueString() {
case "A":
ip := net.ParseIP(req.ConfigValue.ValueString())
if ip == nil || ip.To4() == nil {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
"value must be an IPv4 address",
req.ConfigValue.ValueString(),
))
}
case "AAAA":
ip := net.ParseIP(req.ConfigValue.ValueString())
if ip == nil || ip.To4() != nil || ip.To16() == nil {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
"value must be an IPv6 address",
req.ConfigValue.ValueString(),
))
}
case "CNAME":
case "NS":
case "MX":
case "TXT":
case "ALIAS":
case "DNAME":
case "CAA":
default:
}
},
}
}
func NoSeparator() *Validator {
description := fmt.Sprintf("value must not contain identifier separator '%s'", core.Separator)