-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
16 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -1,23 +1,24 @@ | ||
aws_autoscaling_group: | ||
aws_launch_configuration: | ||
aws_instance: | ||
aws_elb: | ||
aws_vpc_endpoint: | ||
aws_nat_gateway: | ||
aws_cloudformation_stack: | ||
aws_route53_zone: | ||
aws_efs_file_system: | ||
aws_eip: | ||
aws_elb: | ||
aws_iam_instance_profile: | ||
aws_iam_policy: | ||
aws_iam_role: | ||
aws_iam_user: | ||
aws_instance: | ||
aws_internet_gateway: | ||
aws_efs_file_system: | ||
aws_key_pair: | ||
aws_kms_alias: | ||
aws_kms_key: | ||
aws_launch_configuration: | ||
aws_nat_gateway: | ||
aws_network_acl: | ||
aws_network_interface: | ||
aws_subnet: | ||
aws_route53_zone: | ||
aws_route_table: | ||
aws_network_acl: | ||
aws_security_group: | ||
aws_subnet: | ||
aws_vpc: | ||
aws_iam_user: | ||
aws_iam_role: | ||
aws_iam_policy: | ||
aws_iam_instance_profile: | ||
aws_kms_alias: | ||
aws_kms_key: | ||
aws_vpc_endpoint: |
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
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,146 @@ | ||
package test_acceptance | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/cloudetc/awsweeper/command_wipe" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
func TestAccKeyPair_deleteByIds(t *testing.T) { | ||
var kp1, kp2 ec2.KeyPairInfo | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccKeyPairConfig, | ||
ExpectNonEmptyPlan: true, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckKeyPairExists("aws_key_pair.foo", &kp1), | ||
testAccCheckKeyPairExists("aws_key_pair.bar", &kp2), | ||
testMainKeyPairIds(argsDryRun, &kp1), | ||
testKeyPairExists(&kp1), | ||
testKeyPairExists(&kp2), | ||
testMainKeyPairIds(argsForceDelete, &kp1), | ||
testKeyPairDeleted(&kp1), | ||
testKeyPairExists(&kp2), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckKeyPairExists(n string, kp *ec2.KeyPairInfo) 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 key pair ID is set") | ||
} | ||
|
||
conn := client.ec2conn | ||
opts := &ec2.DescribeKeyPairsInput{ | ||
KeyNames: []*string{aws.String(rs.Primary.ID)}, | ||
} | ||
resp, err := conn.DescribeKeyPairs(opts) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.KeyPairs) == 0 { | ||
return fmt.Errorf("Key pair not found") | ||
} | ||
|
||
*kp = *resp.KeyPairs[0] | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testMainKeyPairIds(args []string, kp *ec2.KeyPairInfo) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
command_wipe.OsFs = afero.NewMemMapFs() | ||
afero.WriteFile(command_wipe.OsFs, "config.yml", []byte(testAccKeyPairAWSweeperIdsConfig(kp)), 0644) | ||
os.Args = args | ||
|
||
command_wipe.WrappedMain() | ||
return nil | ||
} | ||
} | ||
|
||
func testKeyPairExists(kp *ec2.KeyPairInfo) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := client.ec2conn | ||
opts := &ec2.DescribeKeyPairsInput{ | ||
KeyNames: []*string{kp.KeyName}, | ||
} | ||
resp, err := conn.DescribeKeyPairs(opts) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.KeyPairs) == 0 { | ||
return fmt.Errorf("Key pair has been deleted") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testKeyPairDeleted(kp *ec2.KeyPairInfo) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := client.ec2conn | ||
opts := &ec2.DescribeKeyPairsInput{ | ||
KeyNames: []*string{kp.KeyName}, | ||
} | ||
resp, err := conn.DescribeKeyPairs(opts) | ||
if err != nil { | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
return err | ||
} | ||
if ec2err.Code() == "InvalidKeyPair.NotFound" { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if len(resp.KeyPairs) != 0 { | ||
return fmt.Errorf("Key pair hasn't been deleted") | ||
|
||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccKeyPairConfig = ` | ||
resource "aws_key_pair" "foo" { | ||
key_name = "awsweeper-testacc-foo" | ||
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com" | ||
} | ||
resource "aws_key_pair" "bar" { | ||
key_name = "awsweeper-testacc-bar" | ||
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com" | ||
} | ||
` | ||
|
||
func testAccKeyPairAWSweeperIdsConfig(kp *ec2.KeyPairInfo) string { | ||
id := kp.KeyName | ||
return fmt.Sprintf(` | ||
aws_key_pair: | ||
ids: | ||
- %s | ||
`, *id) | ||
} |