Skip to content

Commit

Permalink
Validate 'Display Name' for API Management's named values (hashicorp#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CSymes authored and rizkybiz committed Feb 29, 2024
1 parent a4948b3 commit e0f5bc9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/validate"
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -50,7 +51,7 @@ func resourceApiManagementNamedValue() *pluginsdk.Resource {
"display_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: validate.ApiManagementNamedValueDisplayName,
},

"value_from_key_vault": {
Expand Down
13 changes: 12 additions & 1 deletion internal/services/apimanagement/validate/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,21 @@ func ApiManagementApiPath(v interface{}, k string) (ws []string, es []error) {
func ApiManagementBackendName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

// From https://docs.microsoft.com/en-us/rest/api/apimanagement/2018-01-01/backend/createorupdate#uri-parameters
// From https://learn.microsoft.com/en-us/rest/api/apimanagement/backend/create-or-update#uri-parameters
if matched := regexp.MustCompile(`(^[\w]+$)|(^[\w][\w\-]+[\w]$)`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes up to 50 characters in length", k))
}

return warnings, errors
}

func ApiManagementNamedValueDisplayName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

// From the portal: `Name may contain only letters, digits, periods, dash, and underscore.`
if matched := regexp.MustCompile(`^[0-9a-zA-Z_.-]$`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters, periods, underscores and dashes", k))
}

return warnings, errors
}

0 comments on commit e0f5bc9

Please sign in to comment.