Skip to content

Commit

Permalink
CR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Apr 16, 2021
1 parent 5b927c5 commit 7b7997a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 90 deletions.
3 changes: 3 additions & 0 deletions .changelog/17992.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_servicequotas_service_quota: Add plan time validation to `quota_code` and `service_code`
```
26 changes: 18 additions & 8 deletions aws/resource_aws_servicequotas_service_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package aws
import (
"fmt"
"log"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicequotas"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceAwsServiceQuotasServiceQuota() *schema.Resource {
Expand All @@ -34,10 +36,14 @@ func resourceAwsServiceQuotasServiceQuota() *schema.Resource {
Computed: true,
},
"quota_code": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceQuotasServiceQuotaQuotaCode,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 128),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z]`), "must begin with alphabetic character"),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9-]+$`), "must contain only alphanumeric and hyphen characters"),
),
},
"quota_name": {
Type: schema.TypeString,
Expand All @@ -52,10 +58,14 @@ func resourceAwsServiceQuotasServiceQuota() *schema.Resource {
Computed: true,
},
"service_code": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceQuotasServiceQuotaServiceCode,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 63),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z]`), "must begin with alphabetic character"),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9-]+$`), "must contain only alphanumeric and hyphen characters"),
),
},
"service_name": {
Type: schema.TypeString,
Expand Down
32 changes: 0 additions & 32 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2357,38 +2357,6 @@ 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: 0 additions & 50 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2568,56 +2568,6 @@ 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

0 comments on commit 7b7997a

Please sign in to comment.