Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Quotas service_code/quota_code validation #17992

Merged
merged 2 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions aws/resource_aws_servicequotas_service_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func resourceAwsServiceQuotasServiceQuota() *schema.Resource {
Computed: true,
},
"quota_code": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceQuotasServiceQuotaQuotaCode,
},
"quota_name": {
Type: schema.TypeString,
Expand All @@ -51,9 +52,10 @@ func resourceAwsServiceQuotasServiceQuota() *schema.Resource {
Computed: true,
},
"service_code": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceQuotasServiceQuotaServiceCode,
},
"service_name": {
Type: schema.TypeString,
Expand Down
32 changes: 32 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,38 @@ func validateSecretManagerSecretName(v interface{}, k string) (ws []string, erro
return
}

func validateServiceQuotasServiceQuotaServiceCode(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 128 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 128 characters: %q", k, value))
}

pattern := `^[a-zA-Z0-9-]*$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}

func validateServiceQuotasServiceQuotaQuotaCode(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 128 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 128 characters: %q", k, value))
}

pattern := `^[a-zA-Z0-9-]*$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}

func validateLbTargetGroupNamePrefix(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
prefixMaxLength := 32 - resource.UniqueIDSuffixLength
Expand Down
50 changes: 50 additions & 0 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,56 @@ func TestValidateSecurityGroupRuleDescription(t *testing.T) {
}
}

func TestValidateServiceQuotasServiceQuotaServiceCode(t *testing.T) {
validServiceCodes := []string{
"AWSCloudMap",
"acm-pca",
}
for _, v := range validServiceCodes {
_, errors := validateServiceQuotasServiceQuotaServiceCode(v, "service_code")
if len(errors) != 0 {
t.Fatalf("%q should be a valid service service code: %q", v, errors)
}
}

invalidServiceCodes := []string{
" vpc",
"123456789012345678921234567893123456789412345678951234567896123456789712345678981234567899123456789012345678911234567892123456789",
`\`,
}
for _, v := range invalidServiceCodes {
_, errors := validateServiceQuotasServiceQuotaServiceCode(v, "service_code")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid service code", v)
}
}
}

func TestValidateServiceQuotasServiceQuotaQuotaCode(t *testing.T) {
validQuotaCodes := []string{
"L-123ABC",
"L-123aBc",
}
for _, v := range validQuotaCodes {
_, errors := validateServiceQuotasServiceQuotaQuotaCode(v, "quota_code")
if len(errors) != 0 {
t.Fatalf("%q should be a valid service quota code: %q", v, errors)
}
}

invalidQuotaCodes := []string{
" L-123ABC",
"123456789012345678921234567893123456789412345678951234567896123456789712345678981234567899123456789012345678911234567892123456789",
`\`,
}
for _, v := range invalidQuotaCodes {
_, errors := validateServiceQuotasServiceQuotaQuotaCode(v, "quota_code")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid quota code", v)
}
}
}

func TestValidateCognitoRoles(t *testing.T) {
validValues := []map[string]interface{}{
{"authenticated": "hoge"},
Expand Down