2016-07-21 01:29:33 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2016-11-04 15:33:07 +01:00
|
|
|
"fmt"
|
2016-07-21 01:29:33 +02:00
|
|
|
"testing"
|
|
|
|
|
2016-12-18 12:55:40 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/opsworks"
|
2016-11-04 15:33:07 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-07-21 01:29:33 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2016-12-18 12:55:40 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2016-07-21 01:29:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSOpsworksUserProfile(t *testing.T) {
|
2016-11-04 15:33:07 +01:00
|
|
|
rName := fmt.Sprintf("test-user-%d", acctest.RandInt())
|
2017-03-13 10:05:22 +01:00
|
|
|
updateRName := fmt.Sprintf("test-user-%d", acctest.RandInt())
|
2016-07-21 01:29:33 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-12-18 12:55:40 +01:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAwsOpsworksUserProfileDestroy,
|
2016-07-21 01:29:33 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-15 15:17:53 +01:00
|
|
|
{
|
2017-03-13 10:05:22 +01:00
|
|
|
Config: testAccAwsOpsworksUserProfileCreate(rName),
|
2016-07-21 01:29:33 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-12-18 12:55:40 +01:00
|
|
|
testAccCheckAWSOpsworksUserProfileExists(
|
|
|
|
"aws_opsworks_user_profile.user", rName),
|
2016-07-21 01:29:33 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_user_profile.user", "ssh_public_key", "",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-11-04 15:33:07 +01:00
|
|
|
"aws_opsworks_user_profile.user", "ssh_username", rName,
|
2016-07-21 01:29:33 +02:00
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_user_profile.user", "allow_self_management", "false",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2017-03-15 15:17:53 +01:00
|
|
|
{
|
2017-03-13 10:05:22 +01:00
|
|
|
Config: testAccAwsOpsworksUserProfileUpdate(rName, updateRName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSOpsworksUserProfileExists(
|
|
|
|
"aws_opsworks_user_profile.user", updateRName),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_user_profile.user", "ssh_public_key", "",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_user_profile.user", "ssh_username", updateRName,
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_user_profile.user", "allow_self_management", "false",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2016-07-21 01:29:33 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-18 12:55:40 +01:00
|
|
|
func testAccCheckAWSOpsworksUserProfileExists(
|
|
|
|
n, username string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := rs.Primary.Attributes["user_arn"]; !ok {
|
|
|
|
return fmt.Errorf("User Profile user arn is missing, should be set.")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).opsworksconn
|
|
|
|
|
|
|
|
params := &opsworks.DescribeUserProfilesInput{
|
|
|
|
IamUserArns: []*string{aws.String(rs.Primary.Attributes["user_arn"])},
|
|
|
|
}
|
|
|
|
resp, err := conn.DescribeUserProfiles(params)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := len(resp.UserProfiles); v != 1 {
|
|
|
|
return fmt.Errorf("Expected 1 response returned, got %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
opsuserprofile := *resp.UserProfiles[0]
|
|
|
|
|
|
|
|
if *opsuserprofile.AllowSelfManagement {
|
2017-01-13 23:31:44 +01:00
|
|
|
return fmt.Errorf("Unnexpected allowSelfManagement: %t",
|
2016-12-18 12:55:40 +01:00
|
|
|
*opsuserprofile.AllowSelfManagement)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *opsuserprofile.Name != username {
|
|
|
|
return fmt.Errorf("Unnexpected name: %s", *opsuserprofile.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAwsOpsworksUserProfileDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*AWSClient).opsworksconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_opsworks_user_profile" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &opsworks.DescribeUserProfilesInput{
|
|
|
|
IamUserArns: []*string{aws.String(rs.Primary.Attributes["user_arn"])},
|
|
|
|
}
|
|
|
|
resp, err := client.DescribeUserProfiles(req)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(resp.UserProfiles) > 0 {
|
|
|
|
return fmt.Errorf("OpsWorks User Profiles still exist.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if awserr, ok := err.(awserr.Error); ok {
|
|
|
|
if awserr.Code() != "ResourceNotFoundException" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-13 10:05:22 +01:00
|
|
|
func testAccAwsOpsworksUserProfileCreate(rn string) string {
|
2016-11-04 15:33:07 +01:00
|
|
|
return fmt.Sprintf(`
|
2016-07-21 01:29:33 +02:00
|
|
|
resource "aws_opsworks_user_profile" "user" {
|
|
|
|
user_arn = "${aws_iam_user.user.arn}"
|
|
|
|
ssh_username = "${aws_iam_user.user.name}"
|
|
|
|
}
|
2016-10-19 16:22:27 +02:00
|
|
|
|
|
|
|
resource "aws_iam_user" "user" {
|
2016-11-04 15:33:07 +01:00
|
|
|
name = "%s"
|
2016-10-19 16:22:27 +02:00
|
|
|
path = "/"
|
|
|
|
}
|
2017-03-13 10:05:22 +01:00
|
|
|
`, rn)
|
|
|
|
}
|
2016-11-04 15:33:07 +01:00
|
|
|
|
2017-03-13 10:05:22 +01:00
|
|
|
func testAccAwsOpsworksUserProfileUpdate(rn, updateRn string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_opsworks_user_profile" "user" {
|
|
|
|
user_arn = "${aws_iam_user.new-user.arn}"
|
|
|
|
ssh_username = "${aws_iam_user.new-user.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_user" "user" {
|
|
|
|
name = "%s"
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_user" "new-user" {
|
|
|
|
name = "%s"
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
`, rn, updateRn)
|
2016-11-04 15:33:07 +01:00
|
|
|
}
|