github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/import_aws_iam_policy_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/iam" 10 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func testAccAwsIamPolicyConfig(suffix string) string { 16 return fmt.Sprintf(` 17 resource "aws_iam_policy" "test_%[1]s" { 18 name = "test_policy_%[1]s" 19 path = "/" 20 description = "My test policy" 21 policy = <<EOF 22 { 23 "Version": "2012-10-17", 24 "Statement": [ 25 { 26 "Action": [ 27 "ec2:Describe*" 28 ], 29 "Effect": "Allow", 30 "Resource": "*" 31 } 32 ] 33 } 34 EOF 35 } 36 `, suffix) 37 } 38 39 func TestAccAWSIAMPolicy_importBasic(t *testing.T) { 40 suffix := randomString(10) 41 resourceName := fmt.Sprintf("aws_iam_policy.test_%s", suffix) 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckAWSPolicyDestroy, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: testAccAwsIamPolicyConfig(suffix), 50 }, 51 52 resource.TestStep{ 53 ResourceName: resourceName, 54 ImportState: true, 55 ImportStateVerify: true, 56 }, 57 }, 58 }) 59 } 60 61 func testAccCheckAWSPolicyDestroy(s *terraform.State) error { 62 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 63 64 for _, rs := range s.RootModule().Resources { 65 if rs.Type != "aws_iam_policy" { 66 continue 67 } 68 69 // Try to get group 70 _, err := iamconn.GetPolicy(&iam.GetPolicyInput{ 71 PolicyArn: aws.String(rs.Primary.ID), 72 }) 73 if err == nil { 74 return fmt.Errorf("still exist.") 75 } 76 77 // Verify the error is what we want 78 ec2err, ok := err.(awserr.Error) 79 if !ok { 80 return err 81 } 82 if ec2err.Code() != "NoSuchEntity" { 83 return err 84 } 85 } 86 87 return nil 88 }