Skip to content

Commit

Permalink
[#47156875] Decouple ElasticIP and KeyPair removal from VPC removal.
Browse files Browse the repository at this point in the history
So even when we don't have any VPCs, it is possible to remove ElasticIPs and KeyPairs.
  • Loading branch information
Jeffrey Peckham and Vinicius Fuentes committed Apr 2, 2013
1 parent 5249f15 commit 4d81cfa
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
29 changes: 26 additions & 3 deletions bosh_aws_bootstrap/lib/bosh/cli/commands/aws.rb
Expand Up @@ -162,6 +162,8 @@ def destroy(config_file = nil)
delete_all_rds_dbs(config_file)
delete_all_s3(config_file)
delete_all_vpcs(config_file)
delete_all_key_pairs(config_file)
delete_all_elastic_ips(config_file)
delete_all_security_groups(config_file)
delete_all_route53_records(config_file)
end
Expand Down Expand Up @@ -292,15 +294,36 @@ def delete_all_vpcs(config_file)
vpc.delete_vpc
end
dhcp_options.uniq(&:id).map(&:delete)

ec2.remove_all_key_pairs
ec2.release_all_elastic_ips
end
else
say("No VPCs found")
end
end

usage "aws delete key_pairs"
desc "delete key pairs"
def delete_all_key_pairs(config_file)
config = load_yaml_file(config_file)
ec2 = Bosh::Aws::EC2.new(config["aws"])

if confirmed?("Are you sure you want to delete all SSH Keypairs?")
say "Deleting all key pairs..."
ec2.remove_all_key_pairs
end
end

usage "aws delete elastic_ips"
desc "delete elastic ips"
def delete_all_elastic_ips(config_file)
config = load_yaml_file(config_file)
ec2 = Bosh::Aws::EC2.new(config["aws"])

if confirmed?("Are you sure you want to delete all Elastic IPs?")
say "Releasing all elastic IPs..."
ec2.release_all_elastic_ips
end
end

usage "aws create key_pairs"
desc "create key pairs"
def create_key_pairs(config_file)
Expand Down
69 changes: 65 additions & 4 deletions bosh_aws_bootstrap/spec/functional/aws_spec.rb
Expand Up @@ -94,6 +94,8 @@ def stub_required_environment_variables
aws.should_receive(:delete_all_rds_dbs).with(config_file)
aws.should_receive(:delete_all_s3).with(config_file)
aws.should_receive(:delete_all_vpcs).with(config_file)
aws.should_receive(:delete_all_key_pairs).with(config_file)
aws.should_receive(:delete_all_elastic_ips).with(config_file)
aws.should_receive(:delete_all_security_groups).with(config_file)
aws.should_receive(:delete_all_route53_records).with(config_file)
aws.should_receive(:delete_all_elbs).with(config_file)
Expand All @@ -106,6 +108,8 @@ def stub_required_environment_variables
aws.should_receive(:delete_all_rds_dbs).with(default_config_filename)
aws.should_receive(:delete_all_s3).with(default_config_filename)
aws.should_receive(:delete_all_vpcs).with(default_config_filename)
aws.should_receive(:delete_all_key_pairs).with(default_config_filename)
aws.should_receive(:delete_all_elastic_ips).with(default_config_filename)
aws.should_receive(:delete_all_security_groups).with(default_config_filename)
aws.should_receive(:delete_all_route53_records).with(default_config_filename)
aws.should_receive(:delete_all_elbs).with(default_config_filename)
Expand All @@ -116,7 +120,6 @@ def stub_required_environment_variables
describe "aws delete_all security_groups" do
let(:config_file) { asset "config.yml" }
let(:fake_ec2) { mock('EC2') }
let(:fake_vpc) { mock('VPC', id: 'vpc-1234') }

before do
Bosh::Aws::EC2.stub(:new).and_return(fake_ec2)
Expand Down Expand Up @@ -145,7 +148,6 @@ def stub_required_environment_variables
aws.delete_all_security_groups(config_file)
end
end

end

describe "aws create vpc" do
Expand Down Expand Up @@ -317,6 +319,67 @@ def make_fake_vpc!(overrides = {})
end
end

describe "aws destroy key pairs" do
let(:config_file) { asset "config.yml" }
let(:fake_ec2) { mock('EC2') }

before do
Bosh::Aws::EC2.stub(:new).and_return(fake_ec2)
end

context "when non-interactive" do
before do
aws.should_receive(:confirmed?).and_return(true)
end

it "should remove the key pairs" do
fake_ec2.should_receive(:remove_all_key_pairs).and_return(true)
aws.delete_all_key_pairs(config_file)
end
end

context "when interactive and bailing out" do
before do
aws.should_receive(:confirmed?).and_return(false)
end

it 'should not delete the key pairs' do
fake_ec2.should_not_receive(:remove_all_key_pairs)
aws.delete_all_key_pairs(config_file)
end
end
end
describe "aws destroy elastic ips" do
let(:config_file) { asset "config.yml" }
let(:fake_ec2) { mock('EC2') }

before do
Bosh::Aws::EC2.stub(:new).and_return(fake_ec2)
end

context "when non-interactive" do
before do
aws.should_receive(:confirmed?).and_return(true)
end

it "should remove the key pairs" do
fake_ec2.should_receive(:release_all_elastic_ips).and_return(true)
aws.delete_all_elastic_ips(config_file)
end
end

context "when interactive and bailing out" do
before do
aws.should_receive(:confirmed?).and_return(false)
end

it 'should not delete the key pairs' do
fake_ec2.should_not_receive(:release_all_elastic_ips)
aws.delete_all_elastic_ips(config_file)
end
end
end

describe "aws delete all vpcs" do
let(:fake_ec2) { mock('EC2') }
let(:fake_vpc) { mock('VPC', id: 'vpc-1234') }
Expand All @@ -331,8 +394,6 @@ def make_fake_vpc!(overrides = {})
aws.should_receive(:confirmed?).and_return(false)

Bosh::Aws::VPC.should_not_receive(:find)
fake_ec2.should_not_receive(:remove_all_key_pairs)
fake_ec2.should_not_receive(:release_all_elastic_ips)

aws.delete_all_vpcs(asset('config.yml'))
end
Expand Down

0 comments on commit 4d81cfa

Please sign in to comment.