2015-04-18 22:58:08 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-05 01:46:27 +02:00
|
|
|
"strings"
|
2015-04-18 22:58:08 +02:00
|
|
|
"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 TestAccAWSRole_basic(t *testing.T) {
|
2015-04-18 22:58:08 +02:00
|
|
|
var conf iam.GetRoleOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSRoleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-14 10:33:53 +01:00
|
|
|
{
|
2015-04-18 22:58:08 +02:00
|
|
|
Config: testAccAWSRoleConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSRoleExists("aws_iam_role.role", &conf),
|
|
|
|
testAccCheckAWSRoleAttributes(&conf),
|
2016-11-14 10:33:53 +01:00
|
|
|
resource.TestCheckResourceAttrSet(
|
|
|
|
"aws_iam_role.role", "create_date",
|
|
|
|
),
|
2015-04-18 22:58:08 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:46:27 +02:00
|
|
|
func TestAccAWSRole_namePrefix(t *testing.T) {
|
|
|
|
var conf iam.GetRoleOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_iam_role.role",
|
|
|
|
IDRefreshIgnore: []string{"name_prefix"},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSRoleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-14 10:33:53 +01:00
|
|
|
{
|
2016-06-05 01:46:27 +02:00
|
|
|
Config: testAccAWSRolePrefixNameConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSRoleExists("aws_iam_role.role", &conf),
|
|
|
|
testAccCheckAWSRoleGeneratedNamePrefix(
|
|
|
|
"aws_iam_role.role", "test-role-"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-01 22:47:14 +02:00
|
|
|
func TestAccAWSRole_testNameChange(t *testing.T) {
|
|
|
|
var conf iam.GetRoleOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSRoleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-14 10:33:53 +01:00
|
|
|
{
|
2015-06-01 22:47:14 +02:00
|
|
|
Config: testAccAWSRolePre,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSRoleExists("aws_iam_role.role_update_test", &conf),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
2016-11-14 10:33:53 +01:00
|
|
|
{
|
2015-06-01 22:47:14 +02:00
|
|
|
Config: testAccAWSRolePost,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSRoleExists("aws_iam_role.role_update_test", &conf),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-18 22:58:08 +02:00
|
|
|
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
|
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 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:46:27 +02:00
|
|
|
func testAccCheckAWSRoleGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
r, ok := s.RootModule().Resources[resource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Resource not found")
|
|
|
|
}
|
|
|
|
name, ok := r.Primary.Attributes["name"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(name, prefix) {
|
|
|
|
return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 22:58:08 +02:00
|
|
|
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" {
|
2016-11-14 10:33:53 +01:00
|
|
|
name = "test-role"
|
|
|
|
path = "/"
|
|
|
|
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
`
|
2015-06-01 22:47:14 +02:00
|
|
|
|
2016-06-05 01:46:27 +02:00
|
|
|
const testAccAWSRolePrefixNameConfig = `
|
|
|
|
resource "aws_iam_role" "role" {
|
2016-11-14 10:33:53 +01:00
|
|
|
name_prefix = "test-role-"
|
|
|
|
path = "/"
|
|
|
|
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
|
2016-06-05 01:46:27 +02:00
|
|
|
}
|
|
|
|
`
|
2016-08-10 18:18:03 +02:00
|
|
|
|
2015-06-01 22:47:14 +02:00
|
|
|
const testAccAWSRolePre = `
|
|
|
|
resource "aws_iam_role" "role_update_test" {
|
|
|
|
name = "tf_old_name"
|
|
|
|
path = "/test/"
|
2016-08-10 18:18:03 +02:00
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "ec2.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
2015-06-01 22:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "role_update_test" {
|
|
|
|
name = "role_update_test"
|
|
|
|
role = "${aws_iam_role.role_update_test.id}"
|
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": [
|
|
|
|
"s3:GetBucketLocation",
|
|
|
|
"s3:ListAllMyBuckets"
|
|
|
|
],
|
|
|
|
"Resource": "arn:aws:s3:::*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_instance_profile" "role_update_test" {
|
|
|
|
name = "role_update_test"
|
|
|
|
path = "/test/"
|
|
|
|
roles = ["${aws_iam_role.role_update_test.name}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAWSRolePost = `
|
|
|
|
resource "aws_iam_role" "role_update_test" {
|
|
|
|
name = "tf_new_name"
|
|
|
|
path = "/test/"
|
2016-08-10 18:18:03 +02:00
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "ec2.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
2015-06-01 22:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "role_update_test" {
|
|
|
|
name = "role_update_test"
|
|
|
|
role = "${aws_iam_role.role_update_test.id}"
|
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": [
|
|
|
|
"s3:GetBucketLocation",
|
|
|
|
"s3:ListAllMyBuckets"
|
|
|
|
],
|
|
|
|
"Resource": "arn:aws:s3:::*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_instance_profile" "role_update_test" {
|
|
|
|
name = "role_update_test"
|
|
|
|
path = "/test/"
|
|
|
|
roles = ["${aws_iam_role.role_update_test.name}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|