github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_basic(t *testing.T) {
    15  	var account_alias string
    16  
    17  	rstring := acctest.RandString(5)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSIAMAccountAliasDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccAWSIAMAccountAliasConfig(rstring),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSIAMAccountAliasExists("aws_iam_account_alias.test", &account_alias),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func testAccCheckAWSIAMAccountAliasDestroy(s *terraform.State) error {
    35  	conn := testAccProvider.Meta().(*AWSClient).iamconn
    36  
    37  	for _, rs := range s.RootModule().Resources {
    38  		if rs.Type != "aws_iam_account_alias" {
    39  			continue
    40  		}
    41  
    42  		params := &iam.ListAccountAliasesInput{}
    43  
    44  		resp, err := conn.ListAccountAliases(params)
    45  
    46  		if err != nil || resp == nil {
    47  			return nil
    48  		}
    49  
    50  		if len(resp.AccountAliases) > 0 {
    51  			return fmt.Errorf("Bad: Account alias still exists: %q", rs.Primary.ID)
    52  		}
    53  	}
    54  
    55  	return nil
    56  
    57  }
    58  
    59  func testAccCheckAWSIAMAccountAliasExists(n string, a *string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		rs, ok := s.RootModule().Resources[n]
    62  		if !ok {
    63  			return fmt.Errorf("Not found: %s", n)
    64  		}
    65  
    66  		conn := testAccProvider.Meta().(*AWSClient).iamconn
    67  		params := &iam.ListAccountAliasesInput{}
    68  
    69  		resp, err := conn.ListAccountAliases(params)
    70  
    71  		if err != nil || resp == nil {
    72  			return nil
    73  		}
    74  
    75  		if len(resp.AccountAliases) == 0 {
    76  			return fmt.Errorf("Bad: Account alias %q does not exist", rs.Primary.ID)
    77  		}
    78  
    79  		*a = aws.StringValue(resp.AccountAliases[0])
    80  
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccAWSIAMAccountAliasConfig(rstring string) string {
    86  	return fmt.Sprintf(`
    87  resource "aws_iam_account_alias" "test" {
    88    account_alias = "terraform-%s-alias"
    89  }
    90  `, rstring)
    91  }