class Applb::DSL::EC2::LoadBalancer::Listeners::Listener::Rules::Rule::Result

Constants

ATTRIBUTES

Public Class Methods

new(context, listener) click to toggle source
# File lib/applb/dsl/rule.rb, line 15
def initialize(context, listener)
  @context = context
  @options = context.options
  @listener = listener
end

Public Instance Methods

aws(aws_rule) click to toggle source
# File lib/applb/dsl/rule.rb, line 21
def aws(aws_rule)
  @aws_rule = aws_rule
  self
end
create() click to toggle source
# File lib/applb/dsl/rule.rb, line 30
def create
  Applb.logger.info("Create rule #{conditions.first[:values].first}")
  return if @options[:dry_run]

  Applb.logger.debug("create rule with option blow.")
  Applb.logger.debug(create_option.pretty_inspect)
  rule = client.create_rule(create_option).rules.first
  rule_arn = rule.rule_arn
  rule
end
modify() click to toggle source
# File lib/applb/dsl/rule.rb, line 41
def modify
  dsl_hash = to_diff_h
  aws_hash = to_diff_h_aws
  result = nil

  # modify rule
  if dsl_hash != aws_hash
    Applb.logger.info("Modify rule #{@aws_rule.rule_arn}")
    Applb.logger.info("<diff>\n#{Applb::Utils.diff(aws_hash, dsl_hash, color: @options[:color])}")

    unless @options[:dry_run]
      result = client.modify_rule(modify_option).rules.first
    end
  end

  # modify rule priority
  if priority.to_s != @aws_rule.priority
    Applb.logger.info("Modify priority #{@aws_rule.priority} to #{priority}")
    Applb.logger.info("<diff>\n#{Applb::Utils.diff(@aws_rule.priority, priority, color: @options[:color])}")

    unless @options[:dry_run]
      rule_priority_option = {
        rule_priorities: [{ rule_arn: @aws_rule.rule_arn, priority: priority}]
      }
      result = client.set_rule_priorities(rule_priority_option).rules.first
    end
  end
  result
end
to_h() click to toggle source
# File lib/applb/dsl/rule.rb, line 26
def to_h
  Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }]
end

Private Instance Methods

client() click to toggle source
# File lib/applb/dsl/rule.rb, line 103
def client
  @client ||= Applb::ClientWrapper.new(@options)
end
create_option() click to toggle source
# File lib/applb/dsl/rule.rb, line 73
def create_option
  options = to_h.reject { |k, v| k == :rule_arn }
  options[:actions].first.delete(:target_group_name)
  options
end
modify_option() click to toggle source
# File lib/applb/dsl/rule.rb, line 79
def modify_option
  options = to_h.reject { |k, v| k == :priority }
  options[:rule_arn] = @aws_rule.rule_arn
  options[:actions].first.delete(:target_group_name)
  options.delete(:listener_arn)
  options
end
needs_modify?() click to toggle source
# File lib/applb/dsl/rule.rb, line 99
def needs_modify?
  to_diff_h != to_diff_h_aws
end
to_diff_h() click to toggle source
# File lib/applb/dsl/rule.rb, line 87
def to_diff_h
  Applb::Utils.normalize_hash(to_h).reject do |k, v|
    %i/:priority listener_arn rule_arn priority/.include?(k)
  end.tap { |h| h[:actions].first.delete(:target_group_name) }
end
to_diff_h_aws() click to toggle source
# File lib/applb/dsl/rule.rb, line 93
def to_diff_h_aws
  Applb::Utils.normalize_hash(@aws_rule.to_h).reject do |k, v|
    %i/priority is_default rule_arn/.include?(k)
  end
end