From d9b2221d72f1936825385c23acd15b2aad244b7e Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 8 Feb 2019 17:08:22 +0000 Subject: [PATCH] validation for the name field --- azurerm/data_source_api_management_product.go | 5 +---- azurerm/helpers/azure/api_management.go | 17 +++++++++++++++++ azurerm/helpers/validate/api_management.go | 11 +++++++++++ azurerm/resource_arm_api_management_product.go | 12 +----------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/azurerm/data_source_api_management_product.go b/azurerm/data_source_api_management_product.go index 295b1b837f31..abddc0d43ebe 100644 --- a/azurerm/data_source_api_management_product.go +++ b/azurerm/data_source_api_management_product.go @@ -14,10 +14,7 @@ func dataSourceApiManagementProduct() *schema.Resource { Read: dataSourceApiManagementProductRead, Schema: map[string]*schema.Schema{ - "product_id": { - Type: schema.TypeString, - Required: true, - }, + "product_id": azure.SchemaApiManagementProductDataSourceName(), "api_management_name": azure.SchemaApiManagementDataSourceName(), diff --git a/azurerm/helpers/azure/api_management.go b/azurerm/helpers/azure/api_management.go index 00ec2a505654..f364e4211a2f 100644 --- a/azurerm/helpers/azure/api_management.go +++ b/azurerm/helpers/azure/api_management.go @@ -21,3 +21,20 @@ func SchemaApiManagementDataSourceName() *schema.Schema { ValidateFunc: validate.ApiManagementServiceName, } } + +func SchemaApiManagementProductName() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.ApiManagementProductName, + } +} + +func SchemaApiManagementProductDataSourceName() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.ApiManagementProductName, + } +} diff --git a/azurerm/helpers/validate/api_management.go b/azurerm/helpers/validate/api_management.go index b0780799a52c..58f7a4f875a2 100644 --- a/azurerm/helpers/validate/api_management.go +++ b/azurerm/helpers/validate/api_management.go @@ -5,6 +5,17 @@ import ( "regexp" ) +func ApiManagementProductName(v interface{}, k string) (warnings []string, errors []error) { + value := v.(string) + + // from the portal: `The field may contain only numbers, letters, and dash (-) sign when preceded and followed by number or a letter.` + if matched := regexp.MustCompile(`(^[a-zA-Z0-9])([a-zA-Z0-9-]{1,78})([a-zA-Z0-9]$)`).Match([]byte(value)); !matched { + errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes up to 80 characters in length", k)) + } + + return warnings, errors +} + func ApiManagementServiceName(v interface{}, k string) (warnings []string, errors []error) { value := v.(string) diff --git a/azurerm/resource_arm_api_management_product.go b/azurerm/resource_arm_api_management_product.go index 7d3067cdbf69..067a4088552b 100644 --- a/azurerm/resource_arm_api_management_product.go +++ b/azurerm/resource_arm_api_management_product.go @@ -22,17 +22,7 @@ func resourceArmApiManagementProduct() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "product_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - /* - Constraints: []validation.Constraint{{Target: "productID", Name: validation.MaxLength, Rule: 80, Chain: nil}, - {Target: "productID", Name: validation.MinLength, Rule: 1, Chain: nil}, - {Target: "productID", Name: validation.Pattern, Rule: `(^[\w]+$)|(^[\w][\w\-]+[\w]$)`, Chain: nil}}}, - */ - // TODO: validation - }, + "product_id": azure.SchemaApiManagementProductName(), "api_management_name": azure.SchemaApiManagementName(),