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

[aws|auto_scaling] tags support

- implement create_or_update_tags
- implement delete_tags
- implement describe_tags
- don't capitalise keys in create_auto_scaling_group
This commit is contained in:
Nick Osborn 2012-09-23 20:53:36 +01:00
parent 2202119703
commit f2dbf7efb6
8 changed files with 314 additions and 3 deletions

View file

@ -15,11 +15,13 @@ module Fog
request_path 'fog/aws/requests/auto_scaling'
request :create_auto_scaling_group
request :create_launch_configuration
request :create_or_update_tags
request :delete_auto_scaling_group
request :delete_launch_configuration
request :delete_notification_configuration
request :delete_policy
request :delete_scheduled_action
request :delete_tags
request :describe_adjustment_types
request :describe_auto_scaling_groups
request :describe_auto_scaling_instances
@ -31,6 +33,7 @@ module Fog
request :describe_scaling_activities
request :describe_scaling_process_types
request :describe_scheduled_actions
request :describe_tags
request :describe_termination_policy_types
request :disable_metrics_collection
request :enable_metrics_collection

View file

@ -0,0 +1,46 @@
module Fog
module Parsers
module AWS
module AutoScaling
class DescribeTags < Fog::Parsers::Base
def reset
reset_tag
@results = { 'Tags' => [] }
@response = { 'DescribeTagsResult' => {}, 'ResponseMetadata' => {} }
end
def reset_tag
@tag = {}
end
def end_element(name)
case name
when 'member'
@results['Tags'] << @tag
reset_tag
when 'Key', 'ResourceId', 'ResourceType', 'Value'
@tag[name] = value
when 'PropagateAtLaunch'
@tag[name] = (value == 'true')
when 'NextToken'
@results[name] = value
when 'RequestId'
@response['ResponseMetadata'][name] = value
when 'DescribeTagsResponse'
@response['DescribeTagsResult'] = @results
end
end
end
end
end
end
end

View file

@ -72,7 +72,7 @@ module Fog
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
options["Tags.member.#{i+1}.#{key}"] = value unless value.nil?
end
end
end

View file

@ -0,0 +1,63 @@
module Fog
module AWS
class AutoScaling
class Real
require 'fog/aws/parsers/auto_scaling/basic'
# Creates new tags or updates existing tags for an Auto Scaling group.
#
# ==== Parameters
# * tags<~Array>:
# * tag<~Hash>:
# * Key<~String> - The key of the tag.
# * PropagateAtLaunch<~Boolean> - Specifies whether the new tag
# will be applied to instances launched after the tag is created.
# The same behavior applies to updates: If you change a tag, the
# changed tag will be applied to all instances launched after you
# made the change.
# * ResourceId<~String> - The name of the Auto Scaling group.
# * ResourceType<~String> - The kind of resource to which the tag
# is applied. Currently, Auto Scaling supports the
# auto-scaling-group resource type.
# * Value<~String> - The value of the tag.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'ResponseMetadata'<~Hash>:
# * 'RequestId'<~String> - Id of request
#
# ==== See Also
# http://docs.amazonwebservices.com/AutoScaling/latest/APIReference/API_CreateOrUpdateTags.html
#
def create_or_update_tags(tags)
params = {}
tags.each_with_index do |tag, i|
tag.each do |key, value|
params["Tags.member.#{i+1}.#{key}"] = value unless value.nil?
end
end
request({
'Action' => 'CreateOrUpdateTags',
:parser => Fog::Parsers::AWS::AutoScaling::Basic.new
}.merge!(params))
end
end
class Mock
def create_or_update_tags(tags)
if tags.to_a.empty?
raise Fog::AWS::AutoScaling::ValidationError.new("1 validation error detected: Value null at 'tags' failed to satisfy constraint: Member must not be null")
end
raise Fog::Mock::NotImplementedError
end
end
end
end
end

View file

@ -0,0 +1,63 @@
module Fog
module AWS
class AutoScaling
class Real
require 'fog/aws/parsers/auto_scaling/basic'
# Removes the specified tags or a set of tags from a set of resources.
#
# ==== Parameters
# * tags<~Array>:
# * tag<~Hash>:
# * Key<~String> - The key of the tag.
# * PropagateAtLaunch<~Boolean> - Specifies whether the new tag
# will be applied to instances launched after the tag is created.
# The same behavior applies to updates: If you change a tag, the
# changed tag will be applied to all instances launched after you
# made the change.
# * ResourceId<~String> - The name of the Auto Scaling group.
# * ResourceType<~String> - The kind of resource to which the tag
# is applied. Currently, Auto Scaling supports the
# auto-scaling-group resource type.
# * Value<~String> - The value of the tag.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'ResponseMetadata'<~Hash>:
# * 'RequestId'<~String> - Id of request
#
# ==== See Also
# http://docs.amazonwebservices.com/AutoScaling/latest/APIReference/API_DeleteTags.html
#
def delete_tags(tags)
params = {}
tags.each_with_index do |tag, i|
tag.each do |key, value|
params["Tags.member.#{i+1}.#{key}"] = value unless value.nil?
end
end
request({
'Action' => 'DeleteTags',
:parser => Fog::Parsers::AWS::AutoScaling::Basic.new
}.merge!(params))
end
end
class Mock
def delete_tags(tags)
if tags.to_a.empty?
raise Fog::AWS::AutoScaling::ValidationError.new("1 validation error detected: Value null at 'tags' failed to satisfy constraint: Member must not be null")
end
raise Fog::Mock::NotImplementedError
end
end
end
end
end

View file

@ -0,0 +1,74 @@
module Fog
module AWS
class AutoScaling
class Real
require 'fog/aws/parsers/auto_scaling/describe_tags'
# Lists the Auto Scaling group tags.
#
# ==== Parameters
# * options<~Hash>:
# * tag<~Hash>:
# * Key<~String> - The key of the tag.
# * PropagateAtLaunch<~Boolean> - Specifies whether the new tag
# will be applied to instances launched after the tag is created.
# The same behavior applies to updates: If you change a tag,
# changed tag will be applied to all instances launched after you
# made the change.
# * ResourceId<~String> - The name of the Auto Scaling group.
# * ResourceType<~String> - The kind of resource to which the tag
# is applied. Currently, Auto Scaling supports the
# auto-scaling-group resource type.
# * Value<~String> - The value of the tag.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'ResponseMetadata'<~Hash>:
# * 'RequestId'<~String> - Id of request
# * 'DescribeTagsResult'<~Hash>:
# * 'NextToken'<~String> - A string used to mark the start of the
# next batch of returned results.
# * 'Tags'<~Hash>:
# * tagDescription<~Hash>:
# * 'Key'<~String> - The key of the tag.
# * 'PropagateAtLaunch'<~Boolean> - Specifies whether the new
# tag will be applied to instances launched after the tag
# is created. The same behavior applies to updates: If you
# change a tag, the changed tag will be applied to all
# instances launched after you made the change.
# * 'ResourceId'<~String> - The name of the Auto Scaling
# group.
# * 'ResourceType'<~String> - The kind of resource to which
# the tag is applied. Currently, Auto Scaling supports the
# auto-scaling-group resource type.
# * 'Value'<~String> - The value of the tag.
#
# ==== See Also
# http://docs.amazonwebservices.com/AutoScaling/latest/APIReference/API_DescribeTags.html
#
def describe_tags(options={})
if filters = options.delete('Filters')
options.merge!(Fog::AWS.indexed_filters(filters))
end
request({
'Action' => 'DescribeTags',
:parser => Fog::Parsers::AWS::AutoScaling::DescribeTags.new
}.merge!(options))
end
end
class Mock
def describe_tags(options={})
raise Fog::Mock::NotImplementedError
end
end
end
end
end

View file

@ -75,8 +75,8 @@ class AWS
TAG_DESCRIPTION = {
'Key' => String,
'PropagateAtLaunch' => Fog::Boolean,
'ResourceId' => 'String',
'ResourceType' => 'String',
'ResourceId' => String,
'ResourceType' => String,
'Value' => Fog::Nullable::String
}

View file

@ -0,0 +1,62 @@
Shindo.tests('AWS::AutoScaling | tag requests', ['aws', 'auto_scaling']) do
image_id = { # Ubuntu 12.04 LTS 64-bit EBS
'ap-northeast-1' => 'ami-60c77761',
'ap-southeast-1' => 'ami-a4ca8df6',
'eu-west-1' => 'ami-e1e8d395',
'sa-east-1' => 'ami-8cd80691',
'us-east-1' => 'ami-a29943cb',
'us-west-1' => 'ami-87712ac2',
'us-west-2' => 'ami-20800c10'
}
now = Time.now.utc.to_i
lc_name = "fog-test-#{now}"
asg_name = "fog-test-#{now}"
asg_tag = {
'Key' => 'Name',
'PropagateAtLaunch' => true,
'ResourceId' => asg_name,
'ResourceType' => 'auto-scaling-group',
'Value' => asg_name
}
lc = Fog::AWS[:auto_scaling].create_launch_configuration(image_id[Fog::AWS[:auto_scaling].region], 't1.micro', lc_name)
asg = Fog::AWS[:auto_scaling].create_auto_scaling_group(asg_name, "#{Fog::AWS[:auto_scaling].region}a", lc_name, 0, 0, 'Tags' => [asg_tag])
tests('raises') do
tests("#create_or_update_tags(empty)").raises(Fog::AWS::AutoScaling::ValidationError) do
Fog::AWS[:auto_scaling].create_or_update_tags([])
end
tests("#delete_tags(empty)").raises(Fog::AWS::AutoScaling::ValidationError) do
Fog::AWS[:auto_scaling].delete_tags([])
end
end
tests('success') do
tests("#describe_auto_scaling_groups(#{asg_name}").formats(AWS::AutoScaling::Formats::DESCRIBE_AUTO_SCALING_GROUPS) do
body = Fog::AWS[:auto_scaling].describe_auto_scaling_groups('AutoScalingGroupNames' => asg_name).body
auto_scaling_group = body['DescribeAutoScalingGroupsResult']['AutoScalingGroups'].first
returns(true) { auto_scaling_group.has_key?('Tags') }
returns(true) { auto_scaling_group['Tags'].size == 1 }
returns(true) { auto_scaling_group['Tags'].first == asg_tag }
body
end
tests("#describe_tags").formats(AWS::AutoScaling::Formats::DESCRIBE_TAGS) do
pending if Fog.mocking?
body = Fog::AWS[:auto_scaling].describe_tags.body
tags = body['DescribeTagsResult']['Tags']
returns(true) { tags.any? {|tag| tag == asg_tag} }
body
end
# TODO: more tests!
end
Fog::AWS[:auto_scaling].delete_auto_scaling_group(asg_name)
Fog::AWS[:auto_scaling].delete_launch_configuration(lc_name)
end