github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_opsworks_user_profile_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/opsworks" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSOpsworksUserProfile(t *testing.T) { 16 rName := fmt.Sprintf("test-user-%d", acctest.RandInt()) 17 updateRName := fmt.Sprintf("test-user-%d", acctest.RandInt()) 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAwsOpsworksUserProfileDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccAwsOpsworksUserProfileCreate(rName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSOpsworksUserProfileExists( 27 "aws_opsworks_user_profile.user", rName), 28 resource.TestCheckResourceAttr( 29 "aws_opsworks_user_profile.user", "ssh_public_key", "", 30 ), 31 resource.TestCheckResourceAttr( 32 "aws_opsworks_user_profile.user", "ssh_username", rName, 33 ), 34 resource.TestCheckResourceAttr( 35 "aws_opsworks_user_profile.user", "allow_self_management", "false", 36 ), 37 ), 38 }, 39 { 40 Config: testAccAwsOpsworksUserProfileUpdate(rName, updateRName), 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckAWSOpsworksUserProfileExists( 43 "aws_opsworks_user_profile.user", updateRName), 44 resource.TestCheckResourceAttr( 45 "aws_opsworks_user_profile.user", "ssh_public_key", "", 46 ), 47 resource.TestCheckResourceAttr( 48 "aws_opsworks_user_profile.user", "ssh_username", updateRName, 49 ), 50 resource.TestCheckResourceAttr( 51 "aws_opsworks_user_profile.user", "allow_self_management", "false", 52 ), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func testAccCheckAWSOpsworksUserProfileExists( 60 n, username string) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 rs, ok := s.RootModule().Resources[n] 63 if !ok { 64 return fmt.Errorf("Not found: %s", n) 65 } 66 67 if rs.Primary.ID == "" { 68 return fmt.Errorf("No ID is set") 69 } 70 71 if _, ok := rs.Primary.Attributes["user_arn"]; !ok { 72 return fmt.Errorf("User Profile user arn is missing, should be set.") 73 } 74 75 conn := testAccProvider.Meta().(*AWSClient).opsworksconn 76 77 params := &opsworks.DescribeUserProfilesInput{ 78 IamUserArns: []*string{aws.String(rs.Primary.Attributes["user_arn"])}, 79 } 80 resp, err := conn.DescribeUserProfiles(params) 81 82 if err != nil { 83 return err 84 } 85 86 if v := len(resp.UserProfiles); v != 1 { 87 return fmt.Errorf("Expected 1 response returned, got %d", v) 88 } 89 90 opsuserprofile := *resp.UserProfiles[0] 91 92 if *opsuserprofile.AllowSelfManagement { 93 return fmt.Errorf("Unnexpected allowSelfManagement: %t", 94 *opsuserprofile.AllowSelfManagement) 95 } 96 97 if *opsuserprofile.Name != username { 98 return fmt.Errorf("Unnexpected name: %s", *opsuserprofile.Name) 99 } 100 101 return nil 102 } 103 } 104 105 func testAccCheckAwsOpsworksUserProfileDestroy(s *terraform.State) error { 106 client := testAccProvider.Meta().(*AWSClient).opsworksconn 107 108 for _, rs := range s.RootModule().Resources { 109 if rs.Type != "aws_opsworks_user_profile" { 110 continue 111 } 112 113 req := &opsworks.DescribeUserProfilesInput{ 114 IamUserArns: []*string{aws.String(rs.Primary.Attributes["user_arn"])}, 115 } 116 resp, err := client.DescribeUserProfiles(req) 117 118 if err == nil { 119 if len(resp.UserProfiles) > 0 { 120 return fmt.Errorf("OpsWorks User Profiles still exist.") 121 } 122 } 123 124 if awserr, ok := err.(awserr.Error); ok { 125 if awserr.Code() != "ResourceNotFoundException" { 126 return err 127 } 128 } 129 } 130 return nil 131 } 132 133 func testAccAwsOpsworksUserProfileCreate(rn string) string { 134 return fmt.Sprintf(` 135 resource "aws_opsworks_user_profile" "user" { 136 user_arn = "${aws_iam_user.user.arn}" 137 ssh_username = "${aws_iam_user.user.name}" 138 } 139 140 resource "aws_iam_user" "user" { 141 name = "%s" 142 path = "/" 143 } 144 `, rn) 145 } 146 147 func testAccAwsOpsworksUserProfileUpdate(rn, updateRn string) string { 148 return fmt.Sprintf(` 149 resource "aws_opsworks_user_profile" "user" { 150 user_arn = "${aws_iam_user.new-user.arn}" 151 ssh_username = "${aws_iam_user.new-user.name}" 152 } 153 154 resource "aws_iam_user" "user" { 155 name = "%s" 156 path = "/" 157 } 158 159 resource "aws_iam_user" "new-user" { 160 name = "%s" 161 path = "/" 162 } 163 `, rn, updateRn) 164 }