provider/aws: Reworked validateArn function to handle empty values (#10833)

This commit is contained in:
Ninir 2016-12-27 21:52:22 +01:00 committed by Paul Stack
parent bdad7c2784
commit 5dbc66012e
3 changed files with 15 additions and 4 deletions

View File

@ -32,10 +32,11 @@ func resourceAwsAmiCopy() *schema.Resource {
}
resourceSchema["kms_key_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validateArn,
}
return &schema.Resource{

View File

@ -292,6 +292,10 @@ func validateAwsAccountId(v interface{}, k string) (ws []string, errors []error)
func validateArn(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value == "" {
return
}
// http://docs.aws.amazon.com/lambda/latest/dg/API_AddPermission.html
pattern := `^arn:aws:([a-zA-Z0-9\-])+:([a-z]{2}-[a-z]+-\d{1})?:(\d{12})?:(.*)$`
if !regexp.MustCompile(pattern).MatchString(value) {

View File

@ -189,6 +189,12 @@ func TestValidateAwsAccountId(t *testing.T) {
}
func TestValidateArn(t *testing.T) {
v := ""
_, errors := validateArn(v, "arn")
if len(errors) != 0 {
t.Fatalf("%q should not be validated as an ARN: %q", v, errors)
}
validNames := []string{
"arn:aws:elasticbeanstalk:us-east-1:123456789012:environment/My App/MyEnvironment", // Beanstalk
"arn:aws:iam::123456789012:user/David", // IAM User