Class: Balancer::Destroy

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsService, SecurityGroup
Defined in:
lib/balancer/destroy.rb

Instance Method Summary collapse

Methods included from SecurityGroup

#authorize_elb_port, #aws_cli_command, #create_security_group, #destroy_security_group, #find_security_group, #option_transformer, #param, #pretty_display, #sg_vpc_id

Methods included from AwsService

#ec2, #elb

Constructor Details

#initialize(options) ⇒ Destroy

Returns a new instance of Destroy.



7
8
9
10
# File 'lib/balancer/destroy.rb', line 7

def initialize(options)
  @options = options
  @name = options[:name]
end

Instance Method Details

#runObject



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
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/balancer/destroy.rb', line 12

def run
  puts "Destroying ELB and target groups associated with: #{@name}"
  return if @options[:noop]

  begin
    resp = elb.describe_load_balancers(names: [@name])
  rescue Aws::ElasticLoadBalancingV2::Errors::LoadBalancerNotFound
    puts "Load balancer '#{@name}' not found. Exiting.".colorize(:red)
    exit 1
  end

  load_balancer = resp.load_balancers.first

  # Must load resources to be deleted into memory to delete them later since there
  # are dependencies and they won't be available to query after deleting some of the
  # resources.
  resp = elb.describe_listeners(load_balancer_arn: load_balancer.load_balancer_arn)
  listeners = resp.listeners
  resp = elb.describe_target_groups(load_balancer_arn: load_balancer.load_balancer_arn)
  groups = resp.target_groups

  listeners.each do |listener|
    elb.delete_listener(listener_arn: listener.listener_arn)
    puts "Deleted listener: #{listener.listener_arn}"
  end

  groups.each do |group|
    elb.delete_target_group(target_group_arn: group.target_group_arn)
    puts "Deleted target group: #{group.target_group_arn}"
  end

  resp = elb.delete_load_balancer(
    load_balancer_arn: load_balancer.load_balancer_arn,
  )
  puts "Deleted load balancer: #{load_balancer.load_balancer_arn}"

  destroy_security_group
end