class Awful::Alb

Constants

COLORS

Public Instance Methods

add_tag(name, key, value) click to toggle source
# File lib/awful/alb.rb, line 57
def add_tag(name, key, value)
  alb.add_tags(resource_arns: [get_arn(name)], tags: [{key: key, value: value}])
end
alb() click to toggle source
# File lib/awful/alb.rb, line 24
def alb
  @alb ||= Aws::ElasticLoadBalancingV2::Client.new
end
color(string) click to toggle source
# File lib/awful/alb.rb, line 20
def color(string)
  set_color(string, COLORS.fetch(string.to_sym, :yellow))
end
describe_load_balancers(*names) click to toggle source
# File lib/awful/alb.rb, line 28
def describe_load_balancers(*names)
  next_marker = nil
  albs = []
  loop do
    response = alb.describe_load_balancers(names: names, marker: next_marker)
    albs += response.load_balancers
    next_marker = response.next_marker
    break unless next_marker
  end
  albs
end
dump(*names) click to toggle source
# File lib/awful/alb.rb, line 82
def dump(*names)
  describe_load_balancers(*names).output do |albs|
    albs.each do |alb|
      puts YAML.dump(stringify_keys(alb.to_hash))
    end
  end
end
get_arn(name_or_arn) click to toggle source

return ARN for named ALB

# File lib/awful/alb.rb, line 41
def get_arn(name_or_arn)
  if name_or_arn.start_with?('arn:')
    name_or_arn           # it is already an arn
  else
    describe_load_balancers(name_or_arn).first.load_balancer_arn
  end
end
get_tag(name, key) click to toggle source
# File lib/awful/alb.rb, line 49
def get_tag(name, key)
  alb.describe_tags(resource_arns: [get_arn(name)]).tag_descriptions.first.tags.find do |tag|
    tag.key == key
  end.output do |tag|
    puts tag.value if tag
  end
end
instances(name) click to toggle source
# File lib/awful/alb.rb, line 120
def instances(name)
  alb.describe_target_groups(load_balancer_arn: get_arn(name)).target_groups.map do |tg|
    alb.describe_target_health(target_group_arn: tg.target_group_arn).target_health_descriptions
  end.flatten(1).output do |targets|
    if options[:long]
      print_table targets.map { |t|
        [t.target.id, t.target.port, color(t.target_health.state), t.target_health.reason, t.target_health.description]
      }
    else
      puts targets.map{ |t| t.target.id }
    end
  end
end
listeners(name) click to toggle source
# File lib/awful/alb.rb, line 92
def listeners(name)
  alb.describe_listeners(load_balancer_arn: get_arn(name)).listeners.output do |listeners|
    if options[:long]
      print_table listeners.map { |l|
        [l.protocol, l.port, l.ssl_policy, l.certificates.join(','), l.listener_arn]
      }.sort
    else
      puts listeners.map(&:listener_arn)
    end
  end
end
ls(*names) click to toggle source
# File lib/awful/alb.rb, line 69
def ls(*names)
  describe_load_balancers(*names).tap do |albs|
    albs.select! { |a| a.load_balancer_name.match(options[:matching]) } if options[:matching]
  end.output do |list|
    if options[:long]
      print_table list.map { |a| [a.load_balancer_name, a.dns_name, color(a.state.code), a.vpc_id, a.created_time] }
    else
      puts list.map(&:load_balancer_name)
    end
  end
end
remove_tags(name, *keys) click to toggle source
# File lib/awful/alb.rb, line 61
def remove_tags(name, *keys)
  alb.remove_tags(resource_arns: [get_arn(name)], tag_keys: keys)
end
rules(listener) click to toggle source
# File lib/awful/alb.rb, line 136
def rules(listener)
  alb.describe_rules(listener_arn: listener).rules.output do |rules|
    if options[:long]
      print_table rules.map { |r|
        [r.priority, r.rule_arn]
      }
    else
      puts rules.map(&:rule_arn)
    end
  end
end
tag(name, key, value = nil) click to toggle source
# File lib/awful/alb.rb, line 159
def tag(name, key, value = nil)
  if options[:delete]
    remove_tags(name, key)
  elsif value
    add_tag(name, key, value)
  else
    get_tag(name, key)
  end
end
tags(*names) click to toggle source
# File lib/awful/alb.rb, line 149
def tags(*names)
  alb.describe_tags(resource_arns: names.map(&method(:get_arn))).tag_descriptions.output do |albs|
    albs.each do |alb|
      print_table alb.tags.map{ |t| [t.key, t.value] }
    end
  end
end
targets(name) click to toggle source
# File lib/awful/alb.rb, line 106
def targets(name)
  alb.describe_target_groups(load_balancer_arn: get_arn(name)).target_groups.output do |target_groups|
    if options[:long]
      print_table target_groups.map { |t|
        [t.target_group_name, t.port, t.protocol, t.vpc_id]
      }
    else
      puts target_groups.map(&:target_group_name)
    end
  end
end