2015-04-18 22:58:08 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2015-04-18 22:58:08 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccAWSIAMGroupPolicy_basic(t *testing.T) {
|
2015-04-18 22:58:08 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckIAMGroupPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccIAMGroupPolicyConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckIAMGroupPolicy(
|
|
|
|
"aws_iam_group.group",
|
|
|
|
"aws_iam_group_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccIAMGroupPolicyConfigUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckIAMGroupPolicy(
|
|
|
|
"aws_iam_group.group",
|
|
|
|
"aws_iam_group_policy.bar",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckIAMGroupPolicyDestroy(s *terraform.State) error {
|
|
|
|
if len(s.RootModule().Resources) > 0 {
|
|
|
|
return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckIAMGroupPolicy(
|
|
|
|
iamGroupResource string,
|
|
|
|
iamGroupPolicyResource string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[iamGroupResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not Found: %s", iamGroupResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, ok := s.RootModule().Resources[iamGroupPolicyResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not Found: %s", iamGroupPolicyResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
group, name := resourceAwsIamGroupPolicyParseId(policy.Primary.ID)
|
|
|
|
_, err := iamconn.GetGroupPolicy(&iam.GetGroupPolicyInput{
|
|
|
|
GroupName: aws.String(group),
|
|
|
|
PolicyName: aws.String(name),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccIAMGroupPolicyConfig = `
|
|
|
|
resource "aws_iam_group" "group" {
|
|
|
|
name = "test_group"
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_group_policy" "foo" {
|
|
|
|
name = "foo_policy"
|
|
|
|
group = "${aws_iam_group.group.name}"
|
|
|
|
policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccIAMGroupPolicyConfigUpdate = `
|
|
|
|
resource "aws_iam_group" "group" {
|
|
|
|
name = "test_group"
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_group_policy" "foo" {
|
|
|
|
name = "foo_policy"
|
|
|
|
group = "${aws_iam_group.group.name}"
|
|
|
|
policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_group_policy" "bar" {
|
|
|
|
name = "bar_policy"
|
|
|
|
group = "${aws_iam_group.group.name}"
|
|
|
|
policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"
|
|
|
|
}
|
|
|
|
`
|