mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-08 15:13:56 -06:00
89 lines
1.7 KiB
Go
89 lines
1.7 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"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 testAccAwsIamPolicyConfig(suffix string) string {
|
|
return fmt.Sprintf(`
|
|
resource "aws_iam_policy" "test_%[1]s" {
|
|
name = "test_policy_%[1]s"
|
|
path = "/"
|
|
description = "My test policy"
|
|
policy = <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Action": [
|
|
"ec2:Describe*"
|
|
],
|
|
"Effect": "Allow",
|
|
"Resource": "*"
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
}
|
|
`, suffix)
|
|
}
|
|
|
|
func TestAccAWSIAMPolicy_importBasic(t *testing.T) {
|
|
suffix := randomString(10)
|
|
resourceName := fmt.Sprintf("aws_iam_policy.test_%s", suffix)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckAWSPolicyDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccAwsIamPolicyConfig(suffix),
|
|
},
|
|
|
|
resource.TestStep{
|
|
ResourceName: resourceName,
|
|
ImportState: true,
|
|
ImportStateVerify: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
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 group
|
|
_, err := iamconn.GetPolicy(&iam.GetPolicyInput{
|
|
PolicyArn: 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
|
|
}
|