1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1004 from trotter/aws-launch-configuration-parameters

AWS AutoScaling
This commit is contained in:
Dylan Egan 2012-07-03 00:54:06 -07:00
commit 2b366e7722
22 changed files with 314 additions and 30 deletions

View file

@ -50,6 +50,8 @@ module Fog
collection :groups
model :instance
collection :instances
model :policy
collection :policies
class Real
include Fog::AWS::CredentialFetcher::ConnectionMethods
@ -165,6 +167,7 @@ module Fog
'PercentChangeInCapacity'
],
:auto_scaling_groups => {},
:scaling_policies => {},
:health_states => ['Healthy', 'Unhealthy'],
:launch_configurations => {},
:metric_collection_types => {

View file

@ -36,9 +36,37 @@ module Fog
class Mock
def initialize(options={})
def self.data
@data ||= Hash.new do |hash, region|
hash[region] = Hash.new do |region_hash, key|
region_hash[key] = {
:metric_alarms => {}
}
end
end
end
def self.reset
@data = nil
end
def initialize(options={})
@aws_access_key_id = options[:aws_access_key_id]
@region = options[:region] || 'us-east-1'
unless ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
raise ArgumentError, "Unknown region: #{@region.inspect}"
end
end
def data
self.class.data[@region][@aws_access_key_id]
end
def reset_data
self.class.data[@region].delete(@aws_access_key_id)
end
end
class Real

View file

@ -39,7 +39,9 @@ module Fog
requires :image_id
requires :instance_type
connection.create_launch_configuration(image_id, instance_type, id) #, listeners.map{|l| l.to_params})
options = Hash[self.class.aliases.map { |key, value| [key, send(value)] }]
options.delete_if { |key, value| value.nil? }
connection.create_launch_configuration(image_id, instance_type, id, options) #, listeners.map{|l| l.to_params})
# reload instead of merge attributes b/c some attrs (like HealthCheck)
# may be set, but only the DNS name is returned in the create_load_balance

View file

@ -4,7 +4,6 @@ module Fog
module AWS
class AutoScaling
class Group < Fog::Model
identity :id, :aliases => 'AutoScalingGroupName'
attribute :arn, :aliases => 'AutoScalingGroupARN'
attribute :availability_zones, :aliases => 'AvailabilityZones'
@ -22,6 +21,7 @@ module Fog
attribute :placement_group, :aliases => 'PlacementGroup'
attribute :suspended_processes, :aliases => 'SuspendedProcesses'
attribute :vpc_zone_identifier, :aliases => 'VPCZoneIdentifier'
attribute :tags, :aliases => 'Tags'
def initialize(attributes={})
attributes['DefaultCooldown'] ||= 0
@ -34,6 +34,7 @@ module Fog
attributes['MaxSize'] ||= 0
attributes['MinSize'] ||= 0
attributes['SuspendedProcesses'] ||= []
attributes['Tags'] ||= []
super
end
@ -112,7 +113,7 @@ module Fog
requires :max_size
requires :min_size
connection.create_auto_scaling_group(id, availability_zones, launch_configuration_name, max_size, min_size)
connection.create_auto_scaling_group(id, availability_zones, launch_configuration_name, max_size, min_size, options)
reload
end
@ -126,14 +127,17 @@ module Fog
connection.delete_auto_scaling_group(id)
end
private
def update(options)
def update
requires :id
connection.update_auto_scaling_group(id, options)
reload
end
def options
ret = Hash[self.class.aliases.map { |key, value| [key, send(value)] }]
ret.delete_if { |key, value| value.nil? }
ret
end
end
end
end

View 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

View 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

View file

@ -3,9 +3,57 @@ require 'fog/core/model'
module Fog
module AWS
class CloudWatch
class Alarm < Fog::Model
attribute :alarm_names, :aliases => 'AlarmNames'
identity :id, :aliases => 'AlarmName'
attribute :actions_enabled, :aliases => 'ActionsEnabled'
attribute :alarm_actions, :aliases => 'AlarmActions'
attribute :arn, :aliases => 'AlarmArn'
attribute :alarm_configuration_updated_timestamp, :aliases => 'AlarmConfigurationUpdatedTimestamp'
attribute :alarm_description, :aliases => 'AlarmDescription'
attribute :comparison_operator, :aliases => 'ComparisonOperator'
attribute :dimensions, :aliases => 'Dimensions'
attribute :evaluation_periods, :aliases => 'EvaluationPeriods'
attribute :insufficient_data_actions, :aliases => 'InsufficientDataActions'
attribute :metric_name, :aliases => 'MetricName'
attribute :namespace, :aliases => 'Namespace'
attribute :ok_actions, :aliases => 'OKActions'
attribute :period, :aliases => 'Period'
attribute :state_reason, :aliases => 'StateReason'
attribute :state_reason_data, :aliases => 'StateReasonData'
attribute :state_updated_timestamp, :aliases => 'StateUpdatedTimestamp'
attribute :state_value, :aliases => 'StateValue'
attribute :statistic, :aliases => 'Statistic'
attribute :threshold, :aliases => 'Threshold'
attribute :unit, :aliases => 'Unit'
def initialize(attributes)
attributes['EvaluationPeriods'] ||= 1
attributes['Namespace'] ||= 'AWS/EC2'
super
end
def save
requires :id
requires :comparison_operator
requires :evaluation_periods
requires :metric_name
requires :namespace
requires :period
requires :statistic
requires :threshold
options = Hash[self.class.aliases.map { |key, value| [key, send(value)] }]
options.delete_if { |key, value| value.nil? }
connection.put_metric_alarm(options)
reload
end
def destroy
requires :id
connection.delete_alarms(id)
end
end
end
end

View file

@ -7,7 +7,24 @@ module Fog
class Alarms < Fog::Collection
model Fog::AWS::CloudWatch::Alarm
def all
data = []
next_token = nil
loop do
result = connection.describe_alarms('NextToken' => next_token).body['DescribeAlarmsResult']
data += result['MetricAlarms']
next_token = result['NextToken']
break if next_token.nil?
end
load(data)
end
def get(identity)
data = connection.describe_alarms('AlarmNames' => identity).body['DescribeAlarmsResult']['MetricAlarms'].first
new(data) unless data.nil?
end
#alarm_names is an array of alarm names
def delete(alarm_names)
connection.delete_alarms(alarm_names)

View file

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

View file

@ -49,9 +49,17 @@ module Fog
#
def create_auto_scaling_group(auto_scaling_group_name, availability_zones, launch_configuration_name, max_size, min_size, options = {})
options.merge!(AWS.indexed_param('AvailabilityZones.member.%d', [*availability_zones]))
options.delete('AvailabilityZones')
if load_balancer_names = options.delete('LoadBalancerNames')
options.merge!(AWS.indexed_param('LoadBalancerNames.member.%d', [*load_balancer_names]))
end
if tags = options.delete('Tags')
tags.each_with_index do |tag, i|
tag.each do |key, value|
options["Tags.member.#{i + 1}.#{key.to_s.capitalize}"] = value
end
end
end
request({
'Action' => 'CreateAutoScalingGroup',
'AutoScalingGroupName' => auto_scaling_group_name,

View file

@ -78,9 +78,8 @@ module Fog
data[:launch_configurations][launch_configuration_name] = {
'BlockDeviceMappings' => [],
'CreatedTime' => Time.now.utc,
'IamInstanceProfile' => nil,
'ImageId' => image_id,
'InstanceMonitoring.Enabled' => true,
'InstanceMonitoring' => {'Enabled' => true},
'InstanceType' => instance_type,
'KernelId' => nil,
'KeyName' => nil,

View file

@ -35,9 +35,17 @@ module Fog
class Mock
def delete_auto_scaling_group(auto_scaling_group_name)
Fog::Mock.not_implemented
end
unless self.data[:auto_scaling_groups].delete(auto_scaling_group_name)
raise Fog::AWS::Autoscaling::NotFound, "The auto scaling group '#{auto_scaling_group_name}' does not exist."
end
response = Excon::Response.new
response.status = 200
response.body = {
'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
}
response
end
end
end

View file

@ -39,7 +39,7 @@ module Fog
def delete_launch_configuration(launch_configuration_name)
unless self.data[:launch_configurations].delete(launch_configuration_name)
raise Fog::AWS::AutoScaling::ValidationError, "Launch configuration name not found - Launch configuration #{launch_configuration_name} not found"
raise Fog::AWS::AutoScaling::NotFound, "The launch configuration '#{launch_configuration_name}' does not exist."
end
response = Excon::Response.new

View file

@ -37,7 +37,16 @@ module Fog
class Mock
def delete_policy(auto_scaling_group_name, policy_name)
Fog::Mock.not_implemented
unless data[:scaling_policies].delete(policy_name)
raise Fog::AWS::AutoScaling::NotFound, "The scaling policy '#{policy_name}' does not exist."
end
response = Excon::Response.new
response.status = 200
response.body = {
'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
}
response
end
end

View file

@ -73,7 +73,6 @@ module Fog
def describe_auto_scaling_instances(options = {})
results = { 'AutoScalingInstances' => [] }
data[:auto_scaling_groups].each do |asg_name, asg_data|
lc_name = data[asg_data][lc_name]
asg_data['Instances'].each do |instance|
results['AutoScalingInstances'] << {
'AutoScalingGroupName' => asg_name

View file

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

View file

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

View file

@ -62,7 +62,7 @@ module Fog
unless data[:auto_scaling_groups].has_key?(auto_scaling_group_name)
raise Fog::AWS::AutoScaling::ValidationError.new('AutoScalingGroup name not found - null')
end
data[:auto_scaling_group_name][auto_scaling_group_name].merge!(options)
data[:auto_scaling_groups][auto_scaling_group_name].merge!(options)
response = Excon::Response.new
response.status = 200

View file

@ -24,7 +24,25 @@ module Fog
:parser => Fog::Parsers::AWS::CloudWatch::DeleteAlarms.new
}.merge(options))
end
end
end
class Mock
def delete_alarms(alarm_names)
[*alarm_names].each do |alarm_name|
unless data[:metric_alarms].has_key?(alarm_name)
raise Fog::AWS::AutoScaling::NotFound, "The alarm '#{alarm_name}' does not exist."
end
end
[*alarm_names].each { |alarm_name| data[:metric_alarms].delete(alarm_name) }
response = Excon::Response.new
response.status = 200
response.body = {
'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
}
response
end
end
end
end
end

View file

@ -1,7 +1,7 @@
module Fog
module AWS
class CloudWatch
class Real
class Real
require 'fog/aws/parsers/cloud_watch/describe_alarms'
@ -32,7 +32,25 @@ module Fog
:parser => Fog::Parsers::AWS::CloudWatch::DescribeAlarms.new
}.merge(options))
end
end
end
class Mock
def describe_alarms(options={})
results = { 'MetricAlarms' => [] }
data[:metric_alarms].each do |alarm_name, alarm_data|
results['MetricAlarms'] << {
'AlarmName' => alarm_name
}.merge!(alarm_data)
end
response = Excon::Response.new
response.status = 200
response.body = {
'DescribeAlarmsResult' => results,
'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
}
response
end
end
end
end
end

View file

@ -71,6 +71,15 @@ module Fog
end
end
data[:metric_alarms][options['AlarmName']] = {
'AlarmARN' => "arn:aws:cloudwatch:eu-west-1:000000000000:metricAlarm:00000000-0000-0000-0000-000000000000:alarmName/#{options['AlarmName']}",
'ActionsEnabled' => false,
'AlarmActions' => [],
'AlarmConfigurationUpdatedTimestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
'Dimensions' => [],
'OKActions' => [],
}.merge!(options)
response = Excon::Response.new
response.status = 200
response.body = {

View file

@ -3,8 +3,6 @@ Shindo.tests('AWS::AutoScaling | auto_scaling_tests', ['aws', 'auto_scaling']) d
@lc_name = 'fog-test-lc'
tests('success') do
pending if Fog.mocking?
tests("#describe_adjustment_types").formats(AWS::AutoScaling::Formats::DESCRIBE_ADJUSTMENT_TYPES) do
Fog::AWS[:auto_scaling].describe_adjustment_types.body
end
@ -54,15 +52,19 @@ Shindo.tests('AWS::AutoScaling | auto_scaling_tests', ['aws', 'auto_scaling']) d
end
tests("#describe_scaling_activities").formats(AWS::AutoScaling::Formats::DESCRIBE_SCALING_ACTIVITIES) do
pending if Fog.mocking?
Fog::AWS[:auto_scaling].describe_scaling_activities().body
end
tests("#describe_scaling_activities").formats(AWS::AutoScaling::Formats::DESCRIBE_SCALING_ACTIVITIES) do
pending if Fog.mocking?
Fog::AWS[:auto_scaling].describe_scaling_activities('ActivityIds' => '1').body
end
tests("#describe_scaling_activities").formats(AWS::AutoScaling::Formats::DESCRIBE_SCALING_ACTIVITIES) do
pending if Fog.mocking?
Fog::AWS[:auto_scaling].describe_scaling_activities('ActivityIds' => ['1', '2']).body
end
tests("#describe_scaling_activities").formats(AWS::AutoScaling::Formats::DESCRIBE_SCALING_ACTIVITIES) do
pending if Fog.mocking?
Fog::AWS[:auto_scaling].describe_scaling_activities('AutoScalingGroupName' => @asg_name).body
end