github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_iam_group_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 TestAccAWSIAMGroupPolicy_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckIAMGroupPolicyDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccIAMGroupPolicyConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckIAMGroupPolicy(
    23  						"aws_iam_group.group",
    24  						"aws_iam_group_policy.foo",
    25  					),
    26  				),
    27  			},
    28  			resource.TestStep{
    29  				Config: testAccIAMGroupPolicyConfigUpdate,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckIAMGroupPolicy(
    32  						"aws_iam_group.group",
    33  						"aws_iam_group_policy.bar",
    34  					),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func testAccCheckIAMGroupPolicyDestroy(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 testAccCheckIAMGroupPolicy(
    50  	iamGroupResource string,
    51  	iamGroupPolicyResource string) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[iamGroupResource]
    54  		if !ok {
    55  			return fmt.Errorf("Not Found: %s", iamGroupResource)
    56  		}
    57  
    58  		if rs.Primary.ID == "" {
    59  			return fmt.Errorf("No ID is set")
    60  		}
    61  
    62  		policy, ok := s.RootModule().Resources[iamGroupPolicyResource]
    63  		if !ok {
    64  			return fmt.Errorf("Not Found: %s", iamGroupPolicyResource)
    65  		}
    66  
    67  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    68  		group, name := resourceAwsIamGroupPolicyParseId(policy.Primary.ID)
    69  		_, err := iamconn.GetGroupPolicy(&iam.GetGroupPolicyInput{
    70  			GroupName:  aws.String(group),
    71  			PolicyName: aws.String(name),
    72  		})
    73  
    74  		if err != nil {
    75  			return err
    76  		}
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  const testAccIAMGroupPolicyConfig = `
    83  resource "aws_iam_group" "group" {
    84  	name = "test_group"
    85  	path = "/"
    86  }
    87  
    88  resource "aws_iam_group_policy" "foo" {
    89  	name = "foo_policy"
    90  	group = "${aws_iam_group.group.name}"
    91  	policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
    92  }
    93  `
    94  
    95  const testAccIAMGroupPolicyConfigUpdate = `
    96  resource "aws_iam_group" "group" {
    97  	name = "test_group"
    98  	path = "/"
    99  }
   100  
   101  resource "aws_iam_group_policy" "foo" {
   102  	name = "foo_policy"
   103  	group = "${aws_iam_group.group.name}"
   104  	policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
   105  }
   106  
   107  resource "aws_iam_group_policy" "bar" {
   108  	name = "bar_policy"
   109  	group = "${aws_iam_group.group.name}"
   110  	policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
   111  }
   112  `