Skip to content

Commit

Permalink
bugfix: validation azure image definition params (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
sternik authored Dec 4, 2024
1 parent 2f78a59 commit 29254bc
Showing 1 changed file with 9 additions and 49 deletions.
58 changes: 9 additions & 49 deletions imagefactory/imagetemplate/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
package imagetemplate

import (
"fmt"
"regexp"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

Expand Down Expand Up @@ -60,59 +57,22 @@ var awsTemplateConfigResource = &schema.Resource{
},
}

func validateVMImageDefinitionParameter(min, max int) schema.SchemaValidateDiagFunc {
return func(val interface{}, path cty.Path) diag.Diagnostics {
var diags diag.Diagnostics

v, ok := val.(string)
if !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid value type",
Detail: "Field value must be of type string",
AttributePath: path,
})
}

if len(v) < min || len(v) > max {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Allowed values",
Detail: fmt.Sprintf("Expected length of the value to be in the range (%d - %d), got %s", min, max, v),
AttributePath: path,
})
}

if ok := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]$`).MatchString(v); !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid value",
Detail: "The value must contain only English letters, numbers, underscores and hyphens. " +
"The value cannot begin or end with underscores or hyphens.",
AttributePath: path,
})
}

return diags
}
}

var vmImageDefinitionAzureTemplateConfigResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateVMImageDefinitionParameter(2, 80), // nolint: gomnd
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(2, 80), // nolint: gomnd
},
"offer": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateVMImageDefinitionParameter(2, 64), // nolint: gomnd
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(2, 64), // nolint: gomnd
},
"sku": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateVMImageDefinitionParameter(2, 64), // nolint: gomnd
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(2, 64), // nolint: gomnd
},
},
}
Expand Down

0 comments on commit 29254bc

Please sign in to comment.