127 lines
2.7 KiB
Go
127 lines
2.7 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAWSPolicy_namePrefix(t *testing.T) {
|
|
var out iam.GetPolicyOutput
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckAWSPolicyDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccAWSPolicyPrefixNameConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckAWSPolicyExists("aws_iam_policy.policy", &out),
|
|
testAccCheckAWSPolicyGeneratedNamePrefix(
|
|
"aws_iam_policy.policy", "test-policy-"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckAWSPolicyDestroy(s *terraform.State) error {
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "aws_iam_policy" {
|
|
continue
|
|
}
|
|
|
|
// Try to get policy
|
|
_, err := iamconn.GetPolicy(&iam.GetPolicyInput{
|
|
PolicyArn: aws.String(rs.Primary.Attributes["arn"]),
|
|
})
|
|
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 testAccCheckAWSPolicyExists(resource string, res *iam.GetPolicyOutput) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[resource]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", resource)
|
|
}
|
|
|
|
if rs.Primary.ID == "" {
|
|
return fmt.Errorf("No Policy name is set")
|
|
}
|
|
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
resp, err := iamconn.GetPolicy(&iam.GetPolicyInput{
|
|
PolicyArn: aws.String(rs.Primary.Attributes["arn"]),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*res = *resp
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckAWSPolicyGeneratedNamePrefix(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
|
|
}
|
|
}
|
|
|
|
const testAccAWSPolicyPrefixNameConfig = `
|
|
resource "aws_iam_policy" "policy" {
|
|
name_prefix = "test-policy-"
|
|
path = "/"
|
|
policy = <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Action": [
|
|
"ec2:Describe*"
|
|
],
|
|
"Effect": "Allow",
|
|
"Resource": "*"
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
}
|
|
`
|