github.com/mkuzmin/terraform@v0.3.7-0.20161118171027-ec4c00ff92a9/builtin/providers/aws/resource_aws_iam_policy_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/iam" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAWSPolicy_namePrefix(t *testing.T) { 16 var out iam.GetPolicyOutput 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSPolicyDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAWSPolicyPrefixNameConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSPolicyExists("aws_iam_policy.policy", &out), 27 testAccCheckAWSPolicyGeneratedNamePrefix( 28 "aws_iam_policy.policy", "test-policy-"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckAWSPolicyDestroy(s *terraform.State) error { 36 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "aws_iam_policy" { 40 continue 41 } 42 43 // Try to get policy 44 _, err := iamconn.GetPolicy(&iam.GetPolicyInput{ 45 PolicyArn: aws.String(rs.Primary.Attributes["arn"]), 46 }) 47 if err == nil { 48 return fmt.Errorf("still exist.") 49 } 50 51 // Verify the error is what we want 52 ec2err, ok := err.(awserr.Error) 53 if !ok { 54 return err 55 } 56 if ec2err.Code() != "NoSuchEntity" { 57 return err 58 } 59 } 60 61 return nil 62 } 63 64 func testAccCheckAWSPolicyExists(resource string, res *iam.GetPolicyOutput) resource.TestCheckFunc { 65 return func(s *terraform.State) error { 66 rs, ok := s.RootModule().Resources[resource] 67 if !ok { 68 return fmt.Errorf("Not found: %s", resource) 69 } 70 71 if rs.Primary.ID == "" { 72 return fmt.Errorf("No Policy name is set") 73 } 74 75 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 76 77 resp, err := iamconn.GetPolicy(&iam.GetPolicyInput{ 78 PolicyArn: aws.String(rs.Primary.Attributes["arn"]), 79 }) 80 if err != nil { 81 return err 82 } 83 84 *res = *resp 85 86 return nil 87 } 88 } 89 90 func testAccCheckAWSPolicyGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc { 91 return func(s *terraform.State) error { 92 r, ok := s.RootModule().Resources[resource] 93 if !ok { 94 return fmt.Errorf("Resource not found") 95 } 96 name, ok := r.Primary.Attributes["name"] 97 if !ok { 98 return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes) 99 } 100 if !strings.HasPrefix(name, prefix) { 101 return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix) 102 } 103 return nil 104 } 105 } 106 107 const testAccAWSPolicyPrefixNameConfig = ` 108 resource "aws_iam_policy" "policy" { 109 name_prefix = "test-policy-" 110 path = "/" 111 policy = <<EOF 112 { 113 "Version": "2012-10-17", 114 "Statement": [ 115 { 116 "Action": [ 117 "ec2:Describe*" 118 ], 119 "Effect": "Allow", 120 "Resource": "*" 121 } 122 ] 123 } 124 EOF 125 } 126 `