github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_iam_user_policy_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/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSIAMUserPolicy_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckIAMUserPolicyDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccIAMUserPolicyConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckIAMUserPolicy(
    23  						"aws_iam_user.user",
    24  						"aws_iam_user_policy.foo",
    25  					),
    26  				),
    27  			},
    28  			resource.TestStep{
    29  				Config: testAccIAMUserPolicyConfigUpdate,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckIAMUserPolicy(
    32  						"aws_iam_user.user",
    33  						"aws_iam_user_policy.bar",
    34  					),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func testAccCheckIAMUserPolicyDestroy(s *terraform.State) error {
    42  	if len(s.RootModule().Resources) > 0 {
    43  		return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources)
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func testAccCheckIAMUserPolicy(
    50  	iamUserResource string,
    51  	iamUserPolicyResource string) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[iamUserResource]
    54  		if !ok {
    55  			return fmt.Errorf("Not Found: %s", iamUserResource)
    56  		}
    57  
    58  		if rs.Primary.ID == "" {
    59  			return fmt.Errorf("No ID is set")
    60  		}
    61  
    62  		policy, ok := s.RootModule().Resources[iamUserPolicyResource]
    63  		if !ok {
    64  			return fmt.Errorf("Not Found: %s", iamUserPolicyResource)
    65  		}
    66  
    67  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    68  		username, name := resourceAwsIamUserPolicyParseId(policy.Primary.ID)
    69  		_, err := iamconn.GetUserPolicy(&iam.GetUserPolicyInput{
    70  			UserName:   aws.String(username),
    71  			PolicyName: aws.String(name),
    72  		})
    73  
    74  		if err != nil {
    75  			return err
    76  		}
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  const testAccIAMUserPolicyConfig = `
    83  resource "aws_iam_user" "user" {
    84  	name = "test_user"
    85  	path = "/"
    86  }
    87  
    88  resource "aws_iam_user_policy" "foo" {
    89  	name = "foo_policy"
    90  	user = "${aws_iam_user.user.name}"
    91  	policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
    92  }
    93  `
    94  
    95  const testAccIAMUserPolicyConfigUpdate = `
    96  resource "aws_iam_user" "user" {
    97  	name = "test_user"
    98  	path = "/"
    99  }
   100  
   101  resource "aws_iam_user_policy" "foo" {
   102  	name = "foo_policy"
   103  	user = "${aws_iam_user.user.name}"
   104  	policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
   105  }
   106  
   107  resource "aws_iam_user_policy" "bar" {
   108  	name = "bar_policy"
   109  	user = "${aws_iam_user.user.name}"
   110  	policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
   111  }
   112  `