class Kelbim::ELBWrapper::LoadBalancerCollection::LoadBalancer::ListenerCollection::Listener::PolicyCollection

Public Class Methods

create_mock_policy(dsl, listener) click to toggle source
# File lib/kelbim/wrapper/policy-collection.rb, line 11
def create_mock_policy(dsl, listener)
  dsl_type, dsl_name_or_attrs = dsl
  policy_type = PolicyTypes.symbol_to_string(dsl_type)
  plcy = OpenStruct.new(:type => policy_type)

  if PolicyTypes.name?(dsl_name_or_attrs)
    plcy.name = dsl_name_or_attrs
    plcy.attribute = {'<new policy attribute name>' => ['<new policy attribute value>']}
  else
    plcy.name = create_policy_name(listener, policy_type).sub(/\w+-\w+-\w+-\w+-\w+\Z/, '...')
    plcy.attributes = PolicyTypes.unexpand(dsl_type, dsl_name_or_attrs)
  end

  return plcy
end
create_policy_name(listener, policy_type) click to toggle source
# File lib/kelbim/wrapper/policy-collection.rb, line 27
def create_policy_name(listener, policy_type)
  [
    listener.load_balancer.vpc_id || :classic,
    listener.load_balancer.name,
    listener.protocol,
    listener.port,
    listener.instance_protocol,
    listener.instance_port,
    policy_type,
    UUID.new.generate,
  ].join('-').gsub(/\s/, '_')
end
new(policies, listener, options) click to toggle source
# File lib/kelbim/wrapper/policy-collection.rb, line 41
def initialize(policies, listener, options)
  @policies = policies
  @listener = listener
  @options = options
end

Public Instance Methods

create(dsl) click to toggle source
# File lib/kelbim/wrapper/policy-collection.rb, line 53
def create(dsl)
  dsl_type, dsl_name_or_attrs = dsl
  log_id = "#{@listener.load_balancer.vpc_id || :classic} > #{@listener.load_balancer.name} > #{PolicyTypes.symbol_to_string(dsl_type)}: "
  log_id += PolicyTypes.name?(dsl_name_or_attrs) ? dsl_name_or_attrs : dsl_name_or_attrs.inspect
  log(:info, 'Create Policy', :cyan, log_id)

  plcy = @options.dry_run ? self.class.create_mock_policy(dsl, @listener) : create_policy(dsl)
  Policy.new(plcy, @listener, @options)
end
each() { |policy| ... } click to toggle source
# File lib/kelbim/wrapper/policy-collection.rb, line 47
def each
  @policies.each do |plcy|
    yield(Policy.new(plcy, @listener, @options))
  end
end

Private Instance Methods

create_policy(dsl) click to toggle source
# File lib/kelbim/wrapper/policy-collection.rb, line 64
def create_policy(dsl)
  dsl_type, dsl_name_or_attrs = dsl
  policy_type = PolicyTypes.symbol_to_string(dsl_type)

  if PolicyTypes.name?(dsl_name_or_attrs)
    plcy = @listener.load_balancer.policies[dsl_name_or_attrs]

    unless plcy
      raise "Can't find Policy: #{dsl_name_or_attrs} in #{@listener.load_balancer.vpc_id || :classic} > #{@listener.load_balancer.name}"
    end
  else
    plcy = @listener.load_balancer.policies.create(
      self.class.create_policy_name(@listener, policy_type),
      policy_type,
      PolicyTypes.unexpand(dsl_type, dsl_name_or_attrs)
    )
  end

  @options.updated = true

  return plcy
end