Class: Cumulus::AutoScaling::PolicyConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/autoscaling/models/PolicyConfig.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = nil) ⇒ PolicyConfig

Public: Constructor

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#adjustmentObject (readonly)

Returns the value of attribute adjustment.



13
14
15
# File 'lib/autoscaling/models/PolicyConfig.rb', line 13

def adjustment
  @adjustment
end

#adjustment_typeObject (readonly)

Returns the value of attribute adjustment_type.



14
15
16
# File 'lib/autoscaling/models/PolicyConfig.rb', line 14

def adjustment_type
  @adjustment_type
end

#alarmsObject (readonly)

Returns the value of attribute alarms.



15
16
17
# File 'lib/autoscaling/models/PolicyConfig.rb', line 15

def alarms
  @alarms
end

#cooldownObject (readonly)

Returns the value of attribute cooldown.



16
17
18
# File 'lib/autoscaling/models/PolicyConfig.rb', line 16

def cooldown
  @cooldown
end

#min_adjustmentObject (readonly)

Returns the value of attribute min_adjustment.



17
18
19
# File 'lib/autoscaling/models/PolicyConfig.rb', line 17

def min_adjustment
  @min_adjustment
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/autoscaling/models/PolicyConfig.rb', line 18

def name
  @name
end

Instance Method Details

#diff(aws) ⇒ Object

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

aws - the scaling policy in AWS



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
# 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

#hashObject

Public: Get the configuration as a hash

Returns the hash



42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

Public: Populate the PolicyConfig from an existing AWS scaling policy

resource - the aws resource to populate from



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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