diff --git a/builtin/providers/aws/resource_aws_iam_policy.go b/builtin/providers/aws/resource_aws_iam_policy.go index e983dc726..b3fdf1c5f 100644 --- a/builtin/providers/aws/resource_aws_iam_policy.go +++ b/builtin/providers/aws/resource_aws_iam_policy.go @@ -24,24 +24,24 @@ func resourceAwsIamPolicy() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "description": &schema.Schema{ + "description": { Type: schema.TypeString, ForceNew: true, Optional: true, }, - "path": &schema.Schema{ + "path": { Type: schema.TypeString, Optional: true, Default: "/", ForceNew: true, }, - "policy": &schema.Schema{ + "policy": { Type: schema.TypeString, Required: true, - ValidateFunc: validateJsonString, + ValidateFunc: validateIAMPolicyJson, DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs, }, - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Optional: true, Computed: true, @@ -79,7 +79,7 @@ func resourceAwsIamPolicy() *schema.Resource { return }, }, - "arn": &schema.Schema{ + "arn": { Type: schema.TypeString, Computed: true, }, diff --git a/builtin/providers/aws/resource_aws_iam_policy_test.go b/builtin/providers/aws/resource_aws_iam_policy_test.go index ce52d5067..a63a75eb6 100644 --- a/builtin/providers/aws/resource_aws_iam_policy_test.go +++ b/builtin/providers/aws/resource_aws_iam_policy_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "strings" "testing" @@ -19,7 +20,7 @@ func TestAWSPolicy_namePrefix(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckAWSPolicyDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: testAccAWSPolicyPrefixNameConfig, Check: resource.ComposeTestCheckFunc( testAccCheckAWSPolicyExists("aws_iam_policy.policy", &out), @@ -31,6 +32,20 @@ func TestAWSPolicy_namePrefix(t *testing.T) { }) } +func TestAWSPolicy_invalidJson(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSPolicyInvalidJsonConfig, + ExpectError: regexp.MustCompile("invalid JSON"), + }, + }, + }) +} + func testAccCheckAWSPolicyExists(resource string, res *iam.GetPolicyOutput) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resource] @@ -94,3 +109,23 @@ resource "aws_iam_policy" "policy" { EOF } ` +const testAccAWSPolicyInvalidJsonConfig = ` +resource "aws_iam_policy" "policy" { + name_prefix = "test-policy-" + path = "/" + policy = <