From fd63c770264fedc87c2f4e84709a04a619c6b4a5 Mon Sep 17 00:00:00 2001 From: Cary Symes Date: Fri, 2 Feb 2024 12:14:34 +1100 Subject: [PATCH] fix: validate display name for apim named values --- .../api_management_named_value_resource.go | 3 ++- .../apimanagement/validate/api_management.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/services/apimanagement/api_management_named_value_resource.go b/internal/services/apimanagement/api_management_named_value_resource.go index 7c6b87fee32b..7cf4b2c24b54 100644 --- a/internal/services/apimanagement/api_management_named_value_resource.go +++ b/internal/services/apimanagement/api_management_named_value_resource.go @@ -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" @@ -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": { diff --git a/internal/services/apimanagement/validate/api_management.go b/internal/services/apimanagement/validate/api_management.go index 5b130cc4de9b..91e49dea384f 100644 --- a/internal/services/apimanagement/validate/api_management.go +++ b/internal/services/apimanagement/validate/api_management.go @@ -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 +}