forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is intended to behave the same way as name_prefix does in aws_iam_role. Fixes hashicorp#10176
- Loading branch information
Showing
3 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
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 | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters