108 lines
2.4 KiB
Go
108 lines
2.4 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
"github.com/awslabs/aws-sdk-go/aws/awserr"
|
|
"github.com/awslabs/aws-sdk-go/service/iam"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccAWSRole_normal(t *testing.T) {
|
|
var conf iam.GetRoleOutput
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckAWSRoleDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccAWSRoleConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckAWSRoleExists("aws_iam_role.role", &conf),
|
|
testAccCheckAWSRoleAttributes(&conf),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckAWSRoleDestroy(s *terraform.State) error {
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "aws_iam_role" {
|
|
continue
|
|
}
|
|
|
|
// Try to get role
|
|
_, err := iamconn.GetRole(&iam.GetRoleInput{
|
|
RoleName: aws.String(rs.Primary.ID),
|
|
})
|
|
if err == nil {
|
|
return fmt.Errorf("still exist.")
|
|
}
|
|
|
|
// Verify the error is what we want
|
|
ec2err, ok := err.(awserr.Error)
|
|
if !ok {
|
|
return err
|
|
}
|
|
if ec2err.Code() != "NoSuchEntity" {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckAWSRoleExists(n string, res *iam.GetRoleOutput) 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 Role name is set")
|
|
}
|
|
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
resp, err := iamconn.GetRole(&iam.GetRoleInput{
|
|
RoleName: aws.String(rs.Primary.ID),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*res = *resp
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckAWSRoleAttributes(role *iam.GetRoleOutput) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
if *role.Role.RoleName != "test-role" {
|
|
return fmt.Errorf("Bad name: %s", *role.Role.RoleName)
|
|
}
|
|
|
|
if *role.Role.Path != "/" {
|
|
return fmt.Errorf("Bad path: %s", *role.Role.Path)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
const testAccAWSRoleConfig = `
|
|
resource "aws_iam_role" "role" {
|
|
name = "test-role"
|
|
path = "/"
|
|
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
|
|
}
|
|
`
|