mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-01 16:21:06 +00:00
Validate account_id length in google_service_account (#793)
* Add validation method for RFC1035 names
This commit is contained in:
parent
f441685699
commit
d4c5a718a5
@ -36,6 +36,7 @@ func resourceGoogleServiceAccount() *schema.Resource {
|
|||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
|
ValidateFunc: validateRFC1035Name(6, 30),
|
||||||
},
|
},
|
||||||
"display_name": &schema.Schema{
|
"display_name": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
|
@ -20,6 +20,7 @@ func resourceGoogleServiceAccountKey() *schema.Resource {
|
|||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
|
ValidateFunc: validateRFC1035Name(6, 30),
|
||||||
},
|
},
|
||||||
// Optional
|
// Optional
|
||||||
"key_algorithm": &schema.Schema{
|
"key_algorithm": &schema.Schema{
|
||||||
|
@ -79,3 +79,19 @@ func validateRFC3339Time(v interface{}, k string) (warnings []string, errors []e
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateRFC1035Name(min, max int) schema.SchemaValidateFunc {
|
||||||
|
if min < 2 || max < min {
|
||||||
|
return func(i interface{}, k string) (s []string, errors []error) {
|
||||||
|
if min < 2 {
|
||||||
|
errors = append(errors, fmt.Errorf("min must be at least 2. Got: %d", min))
|
||||||
|
}
|
||||||
|
if max < min {
|
||||||
|
errors = append(errors, fmt.Errorf("max must greater than min. Got [%d, %d]", min, max))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return validateRegexp(fmt.Sprintf(`^[a-z]([-a-z0-9]{%d,%d}[a-z0-9])$`, min-2, max-2))
|
||||||
|
}
|
||||||
|
@ -74,6 +74,38 @@ func TestValidateRFC3339Time(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateRFC1035Name(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
TestName string
|
||||||
|
Value string
|
||||||
|
Min, Max int
|
||||||
|
ExpectError bool
|
||||||
|
}{
|
||||||
|
{TestName: "valid", Min: 6, Max: 30, Value: "a-valid-name0"},
|
||||||
|
{TestName: "valid lower bound", Min: 12, Max: 30, Value: "a-valid-name"},
|
||||||
|
{TestName: "valid upper bound", Min: 6, Max: 12, Value: "a-valid-name"},
|
||||||
|
{TestName: "valid with numbers", Min: 6, Max: 30, Value: "valid000-name"},
|
||||||
|
{TestName: "must start with a letter", Min: 6, Max: 10, Value: "0invalid", ExpectError: true},
|
||||||
|
{TestName: "cannot end with a dash", Min: 6, Max: 10, Value: "invalid-", ExpectError: true},
|
||||||
|
{TestName: "too short", Min: 6, Max: 10, Value: "short", ExpectError: true},
|
||||||
|
{TestName: "too long", Min: 6, Max: 10, Value: "toolooooong", ExpectError: true},
|
||||||
|
{TestName: "min too small", Min: 1, Max: 10, Value: "", ExpectError: true},
|
||||||
|
{TestName: "min < max", Min: 6, Max: 5, Value: "", ExpectError: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
errors := testStringValidation(StringValidationTestCase{
|
||||||
|
TestName: c.TestName,
|
||||||
|
Value: c.Value,
|
||||||
|
ExpectError: c.ExpectError,
|
||||||
|
}, validateRFC1035Name(c.Min, c.Max))
|
||||||
|
|
||||||
|
if len(errors) > 0 {
|
||||||
|
t.Errorf("%s failed; %v", c.TestName, errors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type StringValidationTestCase struct {
|
type StringValidationTestCase struct {
|
||||||
TestName string
|
TestName string
|
||||||
Value string
|
Value string
|
||||||
|
Loading…
Reference in New Issue
Block a user