mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Add better ScalingPolicy support
This commit is contained in:
parent
4b697b619a
commit
eab5d489ed
6 changed files with 122 additions and 7 deletions
|
@ -49,6 +49,8 @@ module Fog
|
|||
collection :groups
|
||||
model :instance
|
||||
collection :instances
|
||||
model :policy
|
||||
collection :policies
|
||||
|
||||
class Real
|
||||
|
||||
|
@ -153,6 +155,7 @@ module Fog
|
|||
'PercentChangeInCapacity'
|
||||
],
|
||||
:auto_scaling_groups => {},
|
||||
:scaling_policies => {},
|
||||
:health_states => ['Healthy', 'Unhealthy'],
|
||||
:launch_configurations => {},
|
||||
:metric_collection_types => {
|
||||
|
|
33
lib/fog/aws/models/auto_scaling/policies.rb
Normal file
33
lib/fog/aws/models/auto_scaling/policies.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
require 'fog/aws/models/auto_scaling/policy'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class AutoScaling
|
||||
class Policies < Fog::Collection
|
||||
model Fog::AWS::AutoScaling::Policy
|
||||
|
||||
# Creates a new scaling policy.
|
||||
def initialize(attributes={})
|
||||
super
|
||||
end
|
||||
|
||||
def all
|
||||
data = []
|
||||
next_token = nil
|
||||
loop do
|
||||
result = connection.describe_policies('NextToken' => next_token).body['DescribePoliciesResult']
|
||||
data += result['ScalingPolicies']
|
||||
next_token = result['NextToken']
|
||||
break if next_token.nil?
|
||||
end
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(identity, auto_scaling_group = nil)
|
||||
data = connection.describe_policies('PolicyNames' => identity, 'AutoScalingGroupName' => auto_scaling_group).body['DescribePoliciesResult']['ScalingPolicies'].first
|
||||
new(data) unless data.nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
46
lib/fog/aws/models/auto_scaling/policy.rb
Normal file
46
lib/fog/aws/models/auto_scaling/policy.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class AutoScaling
|
||||
class Policy < Fog::Model
|
||||
identity :id, :aliases => 'PolicyName'
|
||||
attribute :arn, :aliases => 'PolicyARN'
|
||||
attribute :adjustment_type, :aliases => 'AdjustmentType'
|
||||
attribute :alarms, :aliases => 'Alarms'
|
||||
attribute :auto_scaling_group_name, :aliases => 'AutoScalingGroupName'
|
||||
attribute :cooldown, :aliases => 'Cooldown'
|
||||
attribute :min_adjustment_step, :aliases => 'MinAdjustmentStep'
|
||||
attribute :scaling_adjustment, :aliases => 'ScalingAdjustment'
|
||||
|
||||
def initialize(attributes)
|
||||
attributes['AdjustmentType'] = 'ChangeInCapacity'
|
||||
attributes['ScalingAdjustment'] = 1
|
||||
super
|
||||
end
|
||||
|
||||
# TODO: implement #alarms
|
||||
# TODO: implement #auto_scaling_group
|
||||
|
||||
def save
|
||||
requires :id
|
||||
requires :adjustment_type
|
||||
requires :auto_scaling_group_name
|
||||
requires :scaling_adjustment
|
||||
|
||||
options = Hash[self.class.aliases.map { |key, value| [key, send(value)] }]
|
||||
options.delete_if { |key, value| value.nil? }
|
||||
|
||||
connection.put_scaling_policy(adjustment_type, auto_scaling_group_name, id, scaling_adjustment, options)
|
||||
reload
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
requires :auto_scaling_group_name
|
||||
connection.delete_policy(auto_scaling_group_name, id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -49,13 +49,15 @@ module Fog
|
|||
@response['DescribePoliciesResult'] = @results
|
||||
|
||||
when 'Alarms'
|
||||
if @in_alarms == true
|
||||
@in_alarms = false
|
||||
when 'member'
|
||||
if @in_alarms
|
||||
@scaling_policy['Alarms'] << @alarm
|
||||
reset_alarm
|
||||
end
|
||||
when 'member'
|
||||
@results['ScalingPolicies'] << @scaling_policy
|
||||
reset_scaling_policy
|
||||
else
|
||||
@results['ScalingPolicies'] << @scaling_policy
|
||||
reset_scaling_policy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -75,7 +75,19 @@ module Fog
|
|||
class Mock
|
||||
|
||||
def describe_policies(options = {})
|
||||
Fog::Mock.not_implemented
|
||||
results = { 'ScalingPolicies' => [] }
|
||||
data[:scaling_policies].each do |asp_name, asp_data|
|
||||
results['ScalingPolicies'] << {
|
||||
'PolicyName' => asp_name
|
||||
}.merge!(asp_data)
|
||||
end
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'DescribePoliciesResult' => results,
|
||||
'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
|
||||
}
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -56,7 +56,26 @@ module Fog
|
|||
class Mock
|
||||
|
||||
def put_scaling_policy(adjustment_type, auto_scaling_group_name, policy_name, scaling_adjustment, options = {})
|
||||
Fog::Mock.not_implemented
|
||||
unless data[:auto_scaling_groups].has_key?(auto_scaling_group_name)
|
||||
raise Fog::AWS::AutoScaling::ValidationError.new('Auto Scaling Group name not found - null')
|
||||
end
|
||||
data[:scaling_policies][policy_name] = {
|
||||
'AdjustmentType' => adjustment_type,
|
||||
'Alarms' => [],
|
||||
'AutoScalingGroupName' => auto_scaling_group_name,
|
||||
'Cooldown' => 0,
|
||||
'MinAdjustmentStep' => 0,
|
||||
'PolicyARN' => "arn:aws:autoscaling:eu-west-1:000000000000:scalingPolicy:00000000-0000-0000-0000-000000000000:autoScalingGroupName/#{auto_scaling_group_name}:policyName/#{policy_name}",
|
||||
'PolicyName' => policy_name,
|
||||
'ScalingAdjustment' => scaling_adjustment
|
||||
}.merge!(options)
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
|
||||
}
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue