class Cumulus::AutoScaling::PolicyConfig

Public: A class that encapsulates data about the way a scaling policy is configured.

Attributes

adjustment[R]
adjustment_type[R]
alarms[R]
cooldown[R]
min_adjustment[R]
name[R]

Public Class Methods

new(json = nil) click to toggle source

Public: Constructor

json - a hash representing the JSON configuration for this scaling policy

# File lib/autoscaling/models/PolicyConfig.rb, line 23
def initialize(json = nil)
  @@cloudwatch ||= Aws::CloudWatch::Client.new(Configuration.instance.client)

  if !json.nil?
    @name = json["name"]
    @adjustment_type = json["adjustment-type"]
    @adjustment = json["adjustment"]
    @cooldown = json["cooldown"]
    @min_adjustment = json["min-adjustment-step"]
    @alarms = {}
    if !json["alarms"].nil?
      @alarms = Hash[json["alarms"].map { |alarm| [alarm["name"], AlarmConfig.new(alarm)] }]
    end
  end
end

Public Instance Methods

diff(aws) click to toggle source

Public: Produce the differences between this local configuration and the configuration in AWS

aws - the scaling policy in AWS

# File lib/autoscaling/models/PolicyConfig.rb, line 57
def diff(aws)
  diffs = []

  if @adjustment_type != aws.adjustment_type
    diffs << PolicyDiff.new(PolicyChange::ADJUSTMENT_TYPE, aws, self)
  end
  if @adjustment != aws.scaling_adjustment
    diffs << PolicyDiff.new(PolicyChange::ADJUSTMENT, aws, self)
  end
  if @cooldown != aws.cooldown
    diffs << PolicyDiff.new(PolicyChange::COOLDOWN, aws, self)
  end
  if @min_adjustment != aws.min_adjustment_step
    diffs << PolicyDiff.new(PolicyChange::MIN_ADJUSTMENT, aws, self)
  end

  # get all cloudwatch alarms that trigger this policy as their action
  aws_alarms = @@cloudwatch.describe_alarms({
    action_prefix: aws.policy_arn
  }).metric_alarms
  alarm_diffs = diff_alarms(aws_alarms, aws.policy_arn)
  if !alarm_diffs.empty?
    diffs << PolicyDiff.alarms(alarm_diffs, self, aws.policy_arn)
  end

  diffs
end
hash() click to toggle source

Public: Get the configuration as a hash

Returns the hash

# File lib/autoscaling/models/PolicyConfig.rb, line 42
def hash
  {
    "name" => @name,
    "adjustment-type" => @adjustment_type,
    "adjustment" => @adjustment,
    "cooldown" => @cooldown,
    "min-adjustment-step" => @min_adjustment,
    "alarms" => @alarms.map { |a| a.hash }
  }.reject { |k, v| v.nil? }
end
populate(resource) click to toggle source

Public: Populate the PolicyConfig from an existing AWS scaling policy

resource - the aws resource to populate from

# File lib/autoscaling/models/PolicyConfig.rb, line 88
def populate(resource)
  @name = resource.policy_name
  @adjustment_type = resource.adjustment_type
  @adjustment = resource.scaling_adjustment
  @cooldown = resource.cooldown
  @min_adjustment = resource.min_adjustment_step

  alarms = @@cloudwatch.describe_alarms({
    action_prefix: resource.policy_arn
  }).metric_alarms
  @alarms = alarms.map do |alarm|
    config = AlarmConfig.new()
    config.populate(resource.policy_arn, alarm)
    config
  end
end

Private Instance Methods

diff_alarms(aws_alarms, policy_arn) click to toggle source

Internal: Determine changes in alarms

aws_alarms - the Cloudwatch alarms in AWS policy_arn - the policy arn is the action the alarms for this policy should

take

Returns an array of AlarmDiff's that represent differences between local and AWS configuration.

# File lib/autoscaling/models/PolicyConfig.rb, line 115
def diff_alarms(aws_alarms, policy_arn)
  diffs = []

  aws_alarms = Hash[aws_alarms.map { |a| [a.alarm_name, a] }]
  aws_alarms.reject { |k, v| @alarms.include?(k) }.each do |name, aws|
    diffs << AlarmDiff.unmanaged(aws)
  end
  @alarms.each do |name, local|
    if !aws_alarms.include?(name)
      diffs << AlarmDiff.added(local)
    else
      diffs << local.diff(aws_alarms[name], policy_arn)
    end
  end

  diffs.flatten
end