Skip to content

Commit

Permalink
Support resource aws_key_pair
Browse files Browse the repository at this point in the history
  • Loading branch information
jckuester committed Jan 20, 2018
1 parent 83042a5 commit 4b5bfea
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 16 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ AWSweeper can currently delete many but not [all of the existing types of AWS re
- aws_iam_user
- aws_instance
- aws_internet_gateway
- aws_key_pair (***new***)
- aws_kms_alias
- aws_kms_key
- aws_launch_configuration
Expand Down
31 changes: 16 additions & 15 deletions all.yml
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:
1 change: 0 additions & 1 deletion command_wipe/command_wipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (c *WipeCommand) Run(args []string) int {

if len(args) == 1 {
data, err := afero.ReadFile(OsFs, args[0])
//data, err := ioutil.ReadFile(args[0])
check(err)
err = yaml.Unmarshal([]byte(data), &c.deleteCfg)
check(err)
Expand Down
8 changes: 8 additions & 0 deletions command_wipe/resource_infos.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func getResourceInfos(c *WipeCommand) []ResourceInfo {
&ec2.DescribeInstancesInput{},
c.deleteInstances,
},
{
"aws_key_pair",
"KeyPairs",
"KeyName",
c.client.ec2conn.DescribeKeyPairs,
&ec2.DescribeKeyPairsInput{},
c.deleteGeneric,
},
{
"aws_elb",
"LoadBalancerDescriptions",
Expand Down
146 changes: 146 additions & 0 deletions test.acceptance/key_pair_test.go
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)
}

0 comments on commit 4b5bfea

Please sign in to comment.