Skip to content

Commit

Permalink
validation for the name field
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Feb 26, 2019
1 parent d7a3529 commit d9b2221
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
5 changes: 1 addition & 4 deletions azurerm/data_source_api_management_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
17 changes: 17 additions & 0 deletions azurerm/helpers/azure/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
11 changes: 11 additions & 0 deletions azurerm/helpers/validate/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 1 addition & 11 deletions azurerm/resource_arm_api_management_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down

0 comments on commit d9b2221

Please sign in to comment.