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/aws/awserr"
|
|
|
|
"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:04:38 +02:00
|
|
|
func TestAccAWSIAMGroup_basic(t *testing.T) {
|
2015-04-18 22:58:08 +02:00
|
|
|
var conf iam.GetGroupOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSGroupConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSGroupExists("aws_iam_group.group", &conf),
|
2015-10-31 17:04:54 +01:00
|
|
|
testAccCheckAWSGroupAttributes(&conf, "test-group", "/"),
|
2015-04-18 22:58:08 +02:00
|
|
|
),
|
|
|
|
},
|
2015-09-15 07:00:22 +02:00
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSGroupConfig2,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-23 04:10:31 +02:00
|
|
|
testAccCheckAWSGroupExists("aws_iam_group.group2", &conf),
|
2015-10-31 17:04:54 +01:00
|
|
|
testAccCheckAWSGroupAttributes(&conf, "test-group2", "/funnypath/"),
|
2015-09-15 07:00:22 +02:00
|
|
|
),
|
|
|
|
},
|
2015-04-18 22:58:08 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSGroupDestroy(s *terraform.State) error {
|
|
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_iam_group" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get group
|
|
|
|
_, err := iamconn.GetGroup(&iam.GetGroupInput{
|
|
|
|
GroupName: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2015-04-18 22:58:08 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "NoSuchEntity" {
|
2015-04-18 22:58:08 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) 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 Group name is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
|
|
|
|
resp, err := iamconn.GetGroup(&iam.GetGroupInput{
|
|
|
|
GroupName: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*res = *resp
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 17:04:54 +01:00
|
|
|
func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput, name string, path string) resource.TestCheckFunc {
|
2015-04-18 22:58:08 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-10-31 17:04:54 +01:00
|
|
|
if *group.Group.GroupName != name {
|
|
|
|
return fmt.Errorf("Bad name: %s when %s was expected", *group.Group.GroupName, name)
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
|
2015-10-31 17:04:54 +01:00
|
|
|
if *group.Group.Path != path {
|
|
|
|
return fmt.Errorf("Bad path: %s when %s was expected", *group.Group.Path, path)
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSGroupConfig = `
|
|
|
|
resource "aws_iam_group" "group" {
|
|
|
|
name = "test-group"
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
`
|
2015-09-15 07:00:22 +02:00
|
|
|
const testAccAWSGroupConfig2 = `
|
2016-06-23 04:10:31 +02:00
|
|
|
resource "aws_iam_group" "group2" {
|
2015-09-15 07:00:22 +02:00
|
|
|
name = "test-group2"
|
|
|
|
path = "/funnypath/"
|
|
|
|
}
|
|
|
|
`
|