Skip to content

Commit

Permalink
azurerm_container_app_job Fix parameter validation
Browse files Browse the repository at this point in the history
  • Loading branch information
oWretch committed May 22, 2024
1 parent 4e0a530 commit 97b0455
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containerapps/helpers"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containerapps/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)
Expand Down Expand Up @@ -69,7 +70,7 @@ func (r ContainerAppJobResource) Arguments() map[string]*schema.Schema {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: validate.ContainerAppJobName,
},

"resource_group_name": commonschema.ResourceGroupName(),
Expand Down
2 changes: 1 addition & 1 deletion internal/services/containerapps/helpers/container_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2948,7 +2948,7 @@ func CustomScaleRuleSchema() *pluginsdk.Schema {
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: validate.LowerCaseAlphaNumericWithHyphensAndPeriods,
},

"metadata": {
Expand Down
48 changes: 48 additions & 0 deletions internal/services/containerapps/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,56 @@ func ContainerAppContainerName(i interface{}, k string) (warnings []string, erro

if matched := regexp.MustCompile(`^([a-zA-Z0-9])[a-zA-Z0-9-.]{0,254}[a-z]?$`).Match([]byte(v)); !matched || strings.HasSuffix(v, "-") {
errors = append(errors, fmt.Errorf("%q must consist of lower case alphanumeric characters, '-', or '.', start with an alphabetic character, and end with an alphanumeric character. The length must not be more than 60 characters", k))
}

return
}

func ContainerAppJobName(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
}

if len(v) == 1 {
if matched := regexp.MustCompile(`^[a-z0-9]$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q must consist of lower case alphanumeric characters, '-', or '.', start and end with an alphanumeric character", k))
}
} else {
if matched := regexp.MustCompile(`^([a-z0-9])[a-z0-9-]*[a-z0-9]$`).Match([]byte(v)); !matched || strings.HasSuffix(v, "-") {
errors = append(errors, fmt.Errorf("%q must consist of lower case alphanumeric characters, or '-', start and end with an alphanumeric character", k))
}
}

if len(v) > 32 {
errors = append(errors, fmt.Errorf("%q must not exceed 32 characters", k))
}

if strings.Contains(v, "--") {
errors = append(errors, fmt.Errorf("%q must not contain --", k))
}

return
}

func LowerCaseAlphaNumericWithHyphensAndPeriods(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
}

if len(v) == 1 {
if matched := regexp.MustCompile(`^[a-z0-9]$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q must consist of lower case alphanumeric characters, '-', or '.', start and end with an alphanumeric character", k))
}
} else {
if matched := regexp.MustCompile(`^([a-z0-9])[a-z0-9-.]*[a-z0-9]$`).Match([]byte(v)); !matched || strings.HasSuffix(v, "-") {
errors = append(errors, fmt.Errorf("%q must consist of lower case alphanumeric characters, '-', or '.', start and end with an alphanumeric character", k))
}
}

return
}

Expand Down

0 comments on commit 97b0455

Please sign in to comment.