Class: Applb::DSL::EC2::LoadBalancer::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/applb/dsl/load_balancer.rb

Constant Summary collapse

ATTRIBUTES =
i/name instances scheme subnets security_groups tags ip_address_type attributes target_groups listeners load_balancer_arn/
CREATE_KEYS =
i/name subnets security_groups scheme tags ip_address_type/

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Result

Returns a new instance of Result.



17
18
19
20
# File 'lib/applb/dsl/load_balancer.rb', line 17

def initialize(context)
  @context = context
  @options = context.options
end

Instance Method Details

#aws(aws_lb) ⇒ Object



38
39
40
41
# File 'lib/applb/dsl/load_balancer.rb', line 38

def aws(aws_lb)
  @aws_lb = aws_lb
  self
end

#createObject



31
32
33
34
35
36
# File 'lib/applb/dsl/load_balancer.rb', line 31

def create
  Applb.logger.info "Create ELB v2 #{name}"
  return if @options[:dry_run]

  client.create_load_balancer(create_option).load_balancers.first
end

#create_optionObject



27
28
29
# File 'lib/applb/dsl/load_balancer.rb', line 27

def create_option
  to_h.select { |k, _| CREATE_KEYS.include?(k) }
end

#ip_address_type_updated?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/applb/dsl/load_balancer.rb', line 51

def ip_address_type_updated?
  ip_address_type != @aws_lb.ip_address_type
end

#modify_ip_address_typeObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/applb/dsl/load_balancer.rb', line 91

def modify_ip_address_type
  return unless ip_address_type_updated?

  Applb.logger.info "Modify #{name} ip_address_type"
  diff = Applb::Utils.diff(
    @aws_lb.ip_address_type,
    ip_address_type,
    color: @options[:color],
  )
  Applb.logger.info("<diff>\n#{diff}")

  return if @options[:dry_run]
  client.set_ip_address_type(
    load_balancer_arn: @aws_lb.load_balancer_arn,
    ip_address_type: ip_address_type,
  ).ip_address_type
end

#modify_load_balancer_attributesObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/applb/dsl/load_balancer.rb', line 109

def modify_load_balancer_attributes
  attrs = attributes.map do |attr|
    {key: attr[:key], value: attr[:value].to_s}
  end
  log_enabled = attrs.find { |attr| attr[:key] == 'access_logs.s3.enabled' }[:value]
  if log_enabled.to_s == 'false'
    attrs.reject! do |attr|
      %w/access_logs.s3.bucket access_logs.s3.prefix/.include?(attr[:key])
    end
  end
  dsl_hash = attrs.map { |a| a.to_h }.sort { |a, b| a[:key] <=> b[:key] }

  aws_attributes = client.describe_load_balancer_attributes(
    load_balancer_arn: @aws_lb.load_balancer_arn,
  ).attributes
  aws_hash = aws_attributes.map { |a| a.to_h }.sort { |a, b| a[:key] <=> b[:key] }
  aws_log_enabled = aws_attributes.find { |attr| attr[:key] == 'access_logs.s3.enabled' }[:value]
  if aws_log_enabled == 'false'
    aws_hash.reject! do |attr|
      %w/access_logs.s3.bucket access_logs.s3.prefix/.include?(attr[:key])
    end
  end

  return if dsl_hash == aws_hash

  Applb.logger.info "Modify #{name} load_balancer_attributes"
  Applb.logger.info("<diff>\n#{Applb::Utils.diff(aws_hash, dsl_hash, color: @options[:color])}")
  return if @options[:dry_run]

  client.modify_load_balancer_attributes(
    load_balancer_arn: @aws_lb.load_balancer_arn,
    attributes: attrs,
  ).attributes
end

#modify_security_groupsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/applb/dsl/load_balancer.rb', line 73

def modify_security_groups
  return unless security_groups_updated?

  Applb.logger.info "Modify #{name} security_groups"
  diff = Applb::Utils.diff(
    @aws_lb.security_groups.sort,
    security_groups.sort,
    color: @options[:color],
  )
  Applb.logger.info("<diff>\n#{diff}")
  return if @options[:dry_run]

  client.set_security_groups(
    load_balancer_arn: @aws_lb.load_balancer_arn,
    security_groups: security_groups,
  ).security_group_ids
end

#modify_subnetsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/applb/dsl/load_balancer.rb', line 55

def modify_subnets
  return unless subnets_updated?

  Applb.logger.info("Modify #{name} subnets")
  diff = Applb::Utils.diff(
    @aws_lb.availability_zones.map(&:subnet_id).sort,
    subnets.sort,
    color: @options[:color],
  )
  Applb.logger.info("<diff>\n#{diff}")
  return if @options[:dry_run]

  client.set_subnets(
    load_balancer_arn: @aws_lb.load_balancer_arn,
    subnets: subnets,
  ).availability_zones
end

#security_groups_updated?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/applb/dsl/load_balancer.rb', line 47

def security_groups_updated?
  security_groups.sort != @aws_lb.security_groups.sort
end

#subnets_updated?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/applb/dsl/load_balancer.rb', line 43

def subnets_updated?
  subnets.sort != @aws_lb.availability_zones.map(&:subnet_id).sort
end

#to_hObject



22
23
24
# File 'lib/applb/dsl/load_balancer.rb', line 22

def to_h
  Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }]
end