github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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  	roleName := fmt.Sprintf("tf-ops-user-profile-%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  			resource.TestStep{
    24  				Config: testAccAwsOpsworksUserProfileCreate(rName, roleName),
    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  	})
    41  }
    42  
    43  func testAccCheckAWSOpsworksUserProfileExists(
    44  	n, username string) resource.TestCheckFunc {
    45  	return func(s *terraform.State) error {
    46  		rs, ok := s.RootModule().Resources[n]
    47  		if !ok {
    48  			return fmt.Errorf("Not found: %s", n)
    49  		}
    50  
    51  		if rs.Primary.ID == "" {
    52  			return fmt.Errorf("No ID is set")
    53  		}
    54  
    55  		if _, ok := rs.Primary.Attributes["user_arn"]; !ok {
    56  			return fmt.Errorf("User Profile user arn is missing, should be set.")
    57  		}
    58  
    59  		conn := testAccProvider.Meta().(*AWSClient).opsworksconn
    60  
    61  		params := &opsworks.DescribeUserProfilesInput{
    62  			IamUserArns: []*string{aws.String(rs.Primary.Attributes["user_arn"])},
    63  		}
    64  		resp, err := conn.DescribeUserProfiles(params)
    65  
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		if v := len(resp.UserProfiles); v != 1 {
    71  			return fmt.Errorf("Expected 1 response returned, got %d", v)
    72  		}
    73  
    74  		opsuserprofile := *resp.UserProfiles[0]
    75  
    76  		if *opsuserprofile.AllowSelfManagement {
    77  			return fmt.Errorf("Unnexpected allowSelfManagement: %t",
    78  				*opsuserprofile.AllowSelfManagement)
    79  		}
    80  
    81  		if *opsuserprofile.Name != username {
    82  			return fmt.Errorf("Unnexpected name: %s", *opsuserprofile.Name)
    83  		}
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  func testAccCheckAwsOpsworksUserProfileDestroy(s *terraform.State) error {
    90  	client := testAccProvider.Meta().(*AWSClient).opsworksconn
    91  
    92  	for _, rs := range s.RootModule().Resources {
    93  		if rs.Type != "aws_opsworks_user_profile" {
    94  			continue
    95  		}
    96  
    97  		req := &opsworks.DescribeUserProfilesInput{
    98  			IamUserArns: []*string{aws.String(rs.Primary.Attributes["user_arn"])},
    99  		}
   100  		resp, err := client.DescribeUserProfiles(req)
   101  
   102  		if err == nil {
   103  			if len(resp.UserProfiles) > 0 {
   104  				return fmt.Errorf("OpsWorks User Profiles still exist.")
   105  			}
   106  		}
   107  
   108  		if awserr, ok := err.(awserr.Error); ok {
   109  			if awserr.Code() != "ResourceNotFoundException" {
   110  				return err
   111  			}
   112  		}
   113  	}
   114  	return nil
   115  }
   116  
   117  func testAccAwsOpsworksUserProfileCreate(rn, roleName string) string {
   118  	return fmt.Sprintf(`
   119  resource "aws_opsworks_user_profile" "user" {
   120    user_arn = "${aws_iam_user.user.arn}"
   121    ssh_username = "${aws_iam_user.user.name}"
   122  }
   123  
   124  resource "aws_iam_user" "user" {
   125  	name = "%s"
   126  	path = "/"
   127  }
   128  
   129  %s
   130  	`, rn, testAccAwsOpsworksStackConfigNoVpcCreate(roleName))
   131  }