github.com/meteor/terraform@v0.6.15-0.20210412225145-79ec4bc057c6/builtin/providers/aws/resource_aws_iam_account_alias_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/service/iam" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSIAMAccountAlias(t *testing.T) { 15 testCases := map[string]map[string]func(t *testing.T){ 16 "Basic": { 17 "basic": testAccAWSIAMAccountAlias_basic_with_datasource, 18 }, 19 "Import": { 20 "import": testAccAWSIAMAccountAlias_importBasic, 21 }, 22 } 23 24 for group, m := range testCases { 25 m := m 26 t.Run(group, func(t *testing.T) { 27 for name, tc := range m { 28 tc := tc 29 t.Run(name, func(t *testing.T) { 30 tc(t) 31 }) 32 } 33 }) 34 } 35 } 36 37 func testAccAWSIAMAccountAlias_basic_with_datasource(t *testing.T) { 38 var account_alias string 39 40 rstring := acctest.RandString(5) 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckAWSIAMAccountAliasDestroy, 46 Steps: []resource.TestStep{ 47 { 48 Config: testAccAWSIAMAccountAliasConfig(rstring), 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckAWSIAMAccountAliasExists("aws_iam_account_alias.test", &account_alias), 51 ), 52 }, 53 { 54 Config: testAccAWSIAMAccountAliasConfig_with_datasource(rstring), 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckAWSIAMAccountAliasExists("aws_iam_account_alias.test", &account_alias), 57 testAccCheckAWSIAMAccountAliasDataExists("data.aws_iam_account_alias.current", &account_alias), 58 ), 59 // We expect a non-empty plan due to the way data sources and depends_on 60 // work, or don't work. See https://github.com/hashicorp/terraform/issues/11139#issuecomment-275121893 61 // We accept this limitation and feel this test is OK because of the 62 // explicity check above 63 ExpectNonEmptyPlan: true, 64 }, 65 }, 66 }) 67 } 68 69 func testAccCheckAWSIAMAccountAliasDestroy(s *terraform.State) error { 70 conn := testAccProvider.Meta().(*AWSClient).iamconn 71 72 for _, rs := range s.RootModule().Resources { 73 if rs.Type != "aws_iam_account_alias" { 74 continue 75 } 76 77 params := &iam.ListAccountAliasesInput{} 78 79 resp, err := conn.ListAccountAliases(params) 80 81 if err != nil || resp == nil { 82 return nil 83 } 84 85 if len(resp.AccountAliases) > 0 { 86 return fmt.Errorf("Bad: Account alias still exists: %q", rs.Primary.ID) 87 } 88 } 89 90 return nil 91 92 } 93 94 func testAccCheckAWSIAMAccountAliasDataExists(n string, a *string) resource.TestCheckFunc { 95 return func(s *terraform.State) error { 96 rs, ok := s.RootModule().Resources[n] 97 if !ok { 98 return fmt.Errorf("Not found: %s", n) 99 } 100 101 if rs.Primary.Attributes["account_alias"] != *a { 102 return fmt.Errorf("Data Source account_alias didn't match, expected (%s), got (%s)", *a, rs.Primary.Attributes["account_alias"]) 103 } 104 105 return nil 106 } 107 } 108 109 func testAccCheckAWSIAMAccountAliasExists(n string, a *string) resource.TestCheckFunc { 110 return func(s *terraform.State) error { 111 rs, ok := s.RootModule().Resources[n] 112 if !ok { 113 return fmt.Errorf("Not found: %s", n) 114 } 115 116 conn := testAccProvider.Meta().(*AWSClient).iamconn 117 params := &iam.ListAccountAliasesInput{} 118 119 resp, err := conn.ListAccountAliases(params) 120 121 if err != nil || resp == nil { 122 return nil 123 } 124 125 if len(resp.AccountAliases) == 0 { 126 return fmt.Errorf("Bad: Account alias %q does not exist", rs.Primary.ID) 127 } 128 129 *a = aws.StringValue(resp.AccountAliases[0]) 130 131 return nil 132 } 133 } 134 135 func testAccCheckAwsIamAccountAlias(n string) resource.TestCheckFunc { 136 return func(s *terraform.State) error { 137 rs, ok := s.RootModule().Resources[n] 138 if !ok { 139 return fmt.Errorf("Can't find Account Alias resource: %s", n) 140 } 141 142 if rs.Primary.Attributes["account_alias"] == "" { 143 return fmt.Errorf("Missing Account Alias") 144 } 145 146 return nil 147 } 148 } 149 150 func testAccAWSIAMAccountAliasConfig_with_datasource(rstring string) string { 151 return fmt.Sprintf(` 152 resource "aws_iam_account_alias" "test" { 153 account_alias = "terraform-%s-alias" 154 } 155 156 data "aws_iam_account_alias" "current" { 157 depends_on = ["aws_iam_account_alias.test"] 158 }`, rstring) 159 } 160 161 func testAccAWSIAMAccountAliasConfig(rstring string) string { 162 return fmt.Sprintf(` 163 resource "aws_iam_account_alias" "test" { 164 account_alias = "terraform-%s-alias" 165 }`, rstring) 166 }