Class: Bosh::Aws::VpcDestroyer

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_cli_plugin_aws/vpc_destroyer.rb

Instance Method Summary collapse

Constructor Details

#initialize(ui, config) ⇒ VpcDestroyer

Returns a new instance of VpcDestroyer.



3
4
5
6
# File 'lib/bosh_cli_plugin_aws/vpc_destroyer.rb', line 3

def initialize(ui, config)
  @ui = ui
  @credentials = config['aws']
end

Instance Method Details

#delete_allObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bosh_cli_plugin_aws/vpc_destroyer.rb', line 8

def delete_all
  vpc_ids = ec2.vpcs.map(&:id)
  if vpc_ids.empty?
    @ui.say('No VPCs found')
    return
  end

  @ui.say("THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".make_red)
  @ui.say("VPCs:\n\t#{vpc_ids.join("\n\t")}")
  return unless @ui.confirmed?('Are you sure you want to delete all VPCs?')

  dhcp_options = []

  vpc_ids.each do |vpc_id|
    vpc = Bosh::Aws::VPC.find(ec2, vpc_id)
    if vpc.instances_count > 0
      raise "#{vpc.instances_count} instance(s) running in #{vpc.vpc_id} - delete them first"
    end

    dhcp_options << vpc.dhcp_options

    vpc.delete_network_interfaces
    vpc.delete_security_groups
    ec2.delete_internet_gateways(ec2.internet_gateway_ids)
    vpc.delete_subnets
    vpc.delete_route_tables
    vpc.delete_vpc
  end

  dhcp_options.uniq(&:id).map(&:delete)
end