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

[aws|compute] fleshing out tag support

This commit is contained in:
geemus 2010-10-12 11:00:59 -07:00
parent 7f2ecbf44c
commit f2fd5d30b7
14 changed files with 214 additions and 44 deletions

View file

@ -31,14 +31,16 @@ module Fog
request :associate_address
request :attach_volume
request :authorize_security_group_ingress
request :create_image
request :create_key_pair
request :create_security_group
request :create_snapshot
request :create_image
request :create_tags
request :create_volume
request :delete_key_pair
request :delete_security_group
request :delete_snapshot
request :delete_tags
request :delete_volume
request :deregister_image
request :describe_addresses

View file

@ -21,6 +21,7 @@ module Fog
attribute :ramdisk_id, :aliases => 'ramdiskId'
attribute :root_device_type, :aliases => 'rootDeviceType'
attribute :root_device_name, :aliases => 'rootDeviceName'
attribute :tags, :aliases => 'tagSet'
def deregister(delete_snapshot = false)
connection.deregister_image(@id)
@ -37,12 +38,6 @@ module Fog
return true
end
def tags
requires :id
connection.tags.all('resource-id' => identity)
end
end
end

View file

@ -12,7 +12,7 @@ module Fog
model Fog::AWS::Compute::Image
def initialize(attributes)
@filters ||= []
@filters ||= {}
super
end

View file

@ -32,6 +32,7 @@ module Fog
attribute :state, :aliases => 'instanceState'
attribute :state_reason, :aliases => 'stateReason'
attribute :subnet_id, :aliases => 'subnetId'
attribute :tags, :aliases => 'tagSet'
attribute :user_data
attr_accessor :password, :username
@ -189,11 +190,6 @@ module Fog
true
end
def tags
requires :id
connection.tags.all('resource-id' => identity)
end
def username
@username ||= 'root'
end

View file

@ -15,6 +15,7 @@ module Fog
attribute :created_at, :aliases => 'startTime'
attribute :owner_id, :aliases => 'ownerId'
attribute :state, :aliases => 'status'
attribute :tags, :aliases => 'tagSet'
attribute :volume_id, :aliases => 'volumeId'
attribute :volume_size, :aliases => 'volumeSize'
@ -39,11 +40,6 @@ module Fog
true
end
def tags
requires :id
connection.tags.all('resource-id' => identity)
end
def volume
requires :id
connection.describe_volumes(@volume_id)

View file

@ -18,6 +18,7 @@ module Fog
attribute :size
attribute :snapshot_id, :aliases => 'snapshotId'
attribute :state, :aliases => 'status'
attribute :tags, :aliases => 'tagSet'
def initialize(attributes = {})
# assign server first to prevent race condition with new_record?
@ -62,11 +63,6 @@ module Fog
connection.snapshots(:volume => self)
end
def tags
requires :id
connection.tags.all('resource-id' => identity)
end
private
def attachmentSet=(new_attachment_set)

View file

@ -7,8 +7,9 @@ module Fog
def reset
@block_device_mapping = {}
@image = { 'blockDeviceMapping' => [], 'productCodes' => [] }
@image = { 'blockDeviceMapping' => [], 'productCodes' => [], 'tagSet' => {} }
@response = { 'imagesSet' => [] }
@tag = {}
end
def start_element(name, attrs = [])
@ -18,6 +19,8 @@ module Fog
@in_product_codes = true
when 'blockDeviceMapping'
@in_block_device_mapping = true
when 'tagSet'
@in_tag_set = true
end
end
@ -25,6 +28,8 @@ module Fog
case name
when 'architecture', 'imageId', 'imageLocation', 'imageOwnerId', 'imageState', 'imageType', 'kernelId', 'platform', 'ramdiskId', 'rootDeviceType','rootDeviceName'
@image[name] = @value
when 'blockDeviceMapping'
@in_block_device_mapping = false
when 'deviceName', 'virtualName', 'snapshotId', 'volumeSize', 'deleteOnTermination'
@block_device_mapping[name] = @value
when 'isPublic'
@ -37,16 +42,19 @@ module Fog
if @in_block_device_mapping
@image['blockDeviceMapping'] << @block_device_mapping
@block_device_mapping = {}
elsif @in_tag_set
@image['tagSet'][@tag['key']] = @tag['value']
@tag = {}
elsif !@in_product_codes
@response['imagesSet'] << @image
@image = { 'blockDeviceMapping' => [], 'productCodes' => [] }
@image = { 'blockDeviceMapping' => [], 'productCodes' => [], 'tagSet' => {} }
end
when 'key', 'value'
@tag[name] = @value
when 'productCode'
@image['productCodes'] << @value
when 'productCodes'
@in_product_codes = false
when 'blockDeviceMapping'
@in_block_device_mapping = false
when 'requestId'
@response[name] = @value
when 'volumeSize'

View file

@ -7,9 +7,10 @@ module Fog
def reset
@block_device_mapping = {}
@instance = { 'blockDeviceMapping' => [], 'instanceState' => {}, 'monitoring' => {}, 'placement' => {}, 'productCodes' => [], 'stateReason' => {} }
@instance = { 'blockDeviceMapping' => [], 'instanceState' => {}, 'monitoring' => {}, 'placement' => {}, 'productCodes' => [], 'stateReason' => {}, 'tagSet' => {} }
@reservation = { 'groupSet' => [], 'instancesSet' => [] }
@response = { 'reservationSet' => [] }
@tag = {}
end
def start_element(name, attrs = [])
@ -25,6 +26,8 @@ module Fog
@in_instance_state = true
when 'stateReason'
@in_state_reason = true
when 'tagSet'
@in_tag_set = true
end
end
@ -71,11 +74,16 @@ module Fog
@block_device_mapping = {}
elsif @in_instances_set
@reservation['instancesSet'] << @instance
@instance = { 'blockDeviceMapping' => [], 'instanceState' => {}, 'monitoring' => {}, 'placement' => {}, 'productCodes' => [], 'stateReason' => {} }
@instance = { 'blockDeviceMapping' => [], 'instanceState' => {}, 'monitoring' => {}, 'placement' => {}, 'productCodes' => [], 'stateReason' => {}, 'tagSet' => {} }
elsif @in_tag_set
@instance['tagSet'][@tag['key']] = @tag['value']
@tag = {}
elsif !@in_subset
@response['reservationSet'] << @reservation
@reservation = { 'groupSet' => [], 'instancesSet' => [] }
end
when 'key', 'value'
@tag[name] = @value
when 'launchTime'
@instance[name] = Time.parse(@value)
when 'name'
@ -98,6 +106,8 @@ module Fog
end
when 'stateReason'
@in_state_reason = false
when 'tagSet'
@in_tag_set = false
end
end

View file

@ -7,22 +7,42 @@ module Fog
def reset
@response = { 'snapshotSet' => [] }
@snapshot = {}
@snapshot = { 'tagSet' => {} }
@tag = {}
end
def start_element(name, attrs = [])
super
if name == 'tagSet'
@in_tag_set = true
end
end
def end_element(name)
case name
when 'item'
@response['snapshotSet'] << @snapshot
@snapshot = {}
when 'description', 'ownerId', 'progress', 'snapshotId', 'status', 'volumeId'
@snapshot[name] ||= @value
when 'requestId'
@response[name] = @value
when 'startTime'
@snapshot[name] = Time.parse(@value)
when 'volumeSize'
@snapshot[name] = @value.to_i
if @in_tag_set
case name
when 'item'
@snapshot['tagSet'][@tag['key']] = @tag['value']
@tag = {}
when 'key', 'value'
@tag[key] = value
when 'tagSet'
@in_tag_set = false
end
else
case name
when 'item'
@response['snapshotSet'] << @snapshot
@snapshot = { 'tagSet' => {} }
when 'description', 'ownerId', 'progress', 'snapshotId', 'status', 'volumeId'
@snapshot[name] ||= @value
when 'requestId'
@response[name] = @value
when 'startTime'
@snapshot[name] = Time.parse(@value)
when 'volumeSize'
@snapshot[name] = @value.to_i
end
end
end

View file

@ -14,8 +14,11 @@ module Fog
def start_element(name, attrs = [])
super
if name == 'attachmentSet'
case name
when 'attachmentSet'
@in_attachment_set = true
when 'tagSet'
@in_tag_set = true
end
end
@ -32,6 +35,15 @@ module Fog
@volume['attachmentSet'] << @attachment
@attachment = {}
end
elsif @in_tag_set
case name
when 'key', 'value'
@tag[name] = @value
when 'item'
@volume['tagSet'][@tag['key']] = @tag['value']
when 'tagSet'
@in_tag_set = false
end
else
case name
when 'availabilityZone', 'snapshotId', 'status', 'volumeId'

View file

@ -0,0 +1,45 @@
module Fog
module AWS
class Compute
class Real
# Adds tags to resources
#
# ==== Parameters
# * resources<~String> - One or more resources to tag
# * tags<~String> - hash of key value tag pairs to assign
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - success?
def create_tags(resources, tags)
resources = [*resources]
for key, value in tags
if value.nil?
tags[key] = ''
end
end
params = {}
params.merge!(AWS.indexed_param('ResourceId', resources))
params.merge!(AWS.indexed_param('Tag.%d.Key', tags.keys))
params.merge!(AWS.indexed_param('Tag.%d.Value', tags.values))
request({
'Action' => 'CreateTags',
:parser => Fog::Parsers::AWS::Compute::Basic.new
}.merge!(params))
end
end
class Mock
def create_tags(resources, tags)
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,45 @@
module Fog
module AWS
class Compute
class Real
# Remove tags from resources
#
# ==== Parameters
# * resources<~String> - One or more resources to tag
# * tags<~String> - hash of key value tag pairs to assign
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - success?
def delete_tags(resources, tags)
resources = [*resources]
for key, value in tags
if value.nil?
tags[key] = ''
end
end
params = {}
params.merge!(AWS.indexed_param('ResourceId', resources))
params.merge!(AWS.indexed_param('Tag.%d.Key', tags.keys))
params.merge!(AWS.indexed_param('Tag.%d.Value', tags.values))
request({
'Action' => 'CreateTags',
:parser => Fog::Parsers::AWS::Compute::Basic.new
}.merge!(params))
end
end
class Mock
def delete_tags(resources, tags)
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -37,7 +37,7 @@ module Fog
def describe_images(filters = {})
options = {}
for key in ['ExecutableBy', 'ImageId', 'Owner']
if filters.key?(key)
if filters.is_a?(Hash) && filters.key?(key)
options[key] = filters[key]
end
end

View file

@ -0,0 +1,45 @@
Shindo.tests('AWS::Compute | tag requests', ['aws']) do
@tags_format = {
'tagSet' => [{
'key' => String,
'resourceId' => String,
'resourceType' => String,
'value' => String
}],
'requestId' => String
}
@volume = AWS[:compute].volumes.create(:availability_zone => 'us-east-1a', :size => 1)
tests('success') do
tests("#create_tags('#{@volume.identity}', 'foo' => 'bar')").formats(AWS::Compute::Formats::BASIC) do
AWS[:compute].create_tags(@volume.identity, 'foo' => 'bar').body
end
tests('#describe_tags').formats(@tags_format) do
AWS[:compute].describe_tags.body
end
tests("#delete_tags('#{@volume.identity}', 'foo' => 'bar')").formats(AWS::Compute::Formats::BASIC) do
AWS[:compute].delete_tags(@volume.identity, 'foo' => 'bar').body
end
end
tests('failure') do
tests("#create_tags('vol-00000000', 'baz' => 'qux')").raises(Fog::Service::NotFound) do
AWS[:compute].create_tags('vol-00000000', 'baz' => 'qux')
end
tests("#delete_tags('vol-00000000', 'baz' => 'qux')").raises(Fog::Service::NotFound) do
AWS[:compute].delete_tags('vol-00000000', 'baz' => 'qux')
end
end
@volume.destroy
end