Class: Applb::Client

Inherits:
Object
  • Object
show all
Includes:
Filterable
Defined in:
lib/applb/client.rb

Defined Under Namespace

Classes: TargetGroupResolveError

Constant Summary collapse

MAGIC_COMMENT =
<<-EOS
# -*- mode: ruby -*-
# vi: set ft=ruby :
EOS

Instance Method Summary collapse

Methods included from Filterable

#target?

Constructor Details

#initialize(filepath, options = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
22
# File 'lib/applb/client.rb', line 19

def initialize(filepath, options = {})
  @filepath = filepath
  @options = options
end

Instance Method Details

#applyObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/applb/client.rb', line 24

def apply
  dsl = load_file(@filepath)

  dsl_ec2s = dsl.ec2s
  aws_ec2s = client.load_balancers.group_by(&:vpc_id)

  dsl.ec2s.each do |vpc_id, dsl_ec2|
    aws_ec2 = aws_ec2s[vpc_id] || []

    traverse_ec2(vpc_id, dsl_ec2, aws_ec2)
  end
end

#exportObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/applb/client.rb', line 37

def export
  result = {}

  lbs = client.load_balancers
  tags_by_arn = describe_tags(lbs)
  
  lbs.each do |lb|
    attributes = client.load_balancer_attributes(load_balancer_arn: lb.load_balancer_arn)
    target_groups = describe_target_groups(lb)
    listeners = describe_listeners(lb)
    rules_by_listener_arn = listeners.each_with_object({}) do |listener, rules_by_listener_arn|
      rules_by_listener_arn[listener.listener_arn] = describe_rules(listener)
    end
    (result[lb.vpc_id] ||= {})[lb.load_balancer_name] = export_lb(
      lb,
      attributes,
      target_groups,
      listeners,
      rules_by_listener_arn,
    )
  end

  path = Pathname.new(@filepath)
  base_dir = path.parent
  if @options[:split_more]
    result.each do |vpc_id, lbs_by_name|
      lbs_by_name.each do |name, lbs|
        Converter.new({vpc_id => {name => lbs}}, tags_by_arn).convert do |vpc_id, dsl|
          alb_base_dir = base_dir.join("#{vpc_id}")
          FileUtils.mkdir_p(alb_base_dir)
          alb_file = alb_base_dir.join("#{name}.alb")
          Applb.logger.info("export #{alb_file}")
          open(alb_file, 'wb') do |f|
            f.puts MAGIC_COMMENT
            f.puts dsl
          end
        end
      end
    end
  elsif @options[:split]
    Converter.new(result, tags_by_arn).convert do |vpc_id, dsl|
      FileUtils.mkdir_p(base_dir)
      alb_file = base_dir.join("#{vpc_id}.alb")
      Applb.logger.info("export #{alb_file}")
      open(alb_file, 'wb') do |f|
        f.puts MAGIC_COMMENT
        f.puts dsl
      end
    end
  else
    dsls = []
    Converter.new(result, tags_by_arn).convert do |vpc_id, dsl|
      dsls << dsl
    end

    FileUtils.mkdir_p(base_dir)
    Applb.logger.info("export #{path}")
    open(path, 'wb') do |f|
      f.puts MAGIC_COMMENT
      f.puts dsls.join("\n")
    end
  end
end