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

Merge remote branch 'Viximo/feature/mocks'

This commit is contained in:
geemus 2011-03-28 14:08:26 -07:00
commit 98ea62da06
6 changed files with 77 additions and 18 deletions

View file

@ -146,6 +146,30 @@ module Fog
@data = self.class.data[@region][@aws_access_key_id]
end
def apply_tag_filters(resources, filters)
# tag-key: match resources tagged with this key (any value)
if filters.has_key?('tag-key')
value = filters.delete('tag-key')
resources = resources.select{|r| r['tagSet'].has_key?(value)}
end
# tag-value: match resources tagged with this value (any key)
if filters.has_key?('tag-value')
value = filters.delete('tag-value')
resources = resources.select{|r| r['tagSet'].values.include?(value)}
end
# tag:key: match resources taged with a key-value pair. Value may be an array, which is OR'd.
tag_filters = {}
filters.keys.each do |key|
tag_filters[key.gsub('tag:', '')] = filters.delete(key) if /^tag:/ =~ key
end
for tag_key, tag_value in tag_filters
resources = resources.select{|r| tag_value.include?(r['tagSet'][tag_key])}
end
resources
end
end
class Real

View file

@ -62,6 +62,8 @@ module Fog
@data[:tags][key] ||= {}
@data[:tags][key][value] ||= []
@data[:tags][key][value] = @data[:tags][key][value] & tagged
tagged.each {|resource| @data[:"#{resource['resourceType']}s"][resource['resourceId']]['tagSet'][key] = value}
end
response = Excon::Response.new

View file

@ -37,6 +37,49 @@ module Fog
end
end
class Mock
def delete_tags(resources, tags)
tagged = resources.map do |resource_id|
type = case resource_id
when /^ami\-[a-z0-9]{8}$/i
'image'
when /^i\-[a-z0-9]{8}$/i
'instance'
when /^snap\-[a-z0-9]{8}$/i
'snapshot'
when /^vol\-[a-z0-9]{8}$/i
'volume'
end
if type && @data[:"#{type}s"][resource_id]
{ 'resourceId' => resource_id, 'resourceType' => type }
else
raise(Fog::Service::NotFound.new("The #{type} ID '#{resource_id}' does not exist"))
end
end
tags.each do |key, value|
@data[:tags][key][value] = @data[:tags][key][value] - tagged
end
tagged.each do |resource|
object = @data[:"#{resource['resourceType']}s"][resource['resourceId']]
tags.each do |key, value|
tagset = object['tagSet']
tagset.delete(key) if tagset.has_key?(key) && (value.nil? || tagset[key] == value)
end
end
response = Excon::Response.new
response.status = true
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
response
end
end
end
end
end

View file

@ -74,15 +74,11 @@ module Fog
filters = {'instance-id' => [*filters]}
end
if filters.keys.any? {|key| key =~ /^tag/}
Formatador.display_line("[yellow][WARN] describe_instances tag filters are not yet mocked[/] [light_black](#{caller.first})[/]")
Fog::Mock.not_implemented
end
response = Excon::Response.new
instance_set = @data[:instances].values
instance_set = apply_tag_filters(instance_set, filters)
aliases = {
'architecture' => 'architecture',
'availability-zone' => 'availabilityZone',

View file

@ -59,11 +59,6 @@ module Fog
Formatador.display_line("[yellow][WARN] describe_snapshots with a second param is deprecated, use describe_snapshots(options) instead[/] [light_black](#{caller.first})[/]")
end
if filters.keys.any? {|key| key =~ /^tag/}
Formatador.display_line("[yellow][WARN] describe_snapshots tag filters are not yet mocked[/] [light_black](#{caller.first})[/]")
Fog::Mock.not_implemented
end
response = Excon::Response.new
snapshot_set = @data[:snapshots].values
@ -75,6 +70,8 @@ module Fog
Formatador.display_line("[yellow][WARN] describe_snapshots with RestorableBy is not mocked[/] [light_black](#{caller.first})[/]")
end
snapshot_set = apply_tag_filters(snapshot_set, filters)
aliases = {
'description' => 'description',
'owner-id' => 'ownerId',
@ -85,11 +82,12 @@ module Fog
'volume-id' => 'volumeId',
'volume-size' => 'volumeSize'
}
for filter_key, filter_value in filters
aliased_key = aliases[filter_key]
snapshot_set = snapshot_set.reject{|snapshot| ![*filter_value].include?(snapshot[aliased_key])}
end
snapshot_set.each do |snapshot|
case snapshot['status']
when 'in progress', 'pending'

View file

@ -50,15 +50,11 @@ module Fog
filters = {'volume-id' => [*filters]}
end
if filters.keys.any? {|key| key =~ /^tag/}
Formatador.display_line("[yellow][WARN] describe_volumes tag filters are not yet mocked[/] [light_black](#{caller.first})[/]")
Fog::Mock.not_implemented
end
response = Excon::Response.new
volume_set = @data[:volumes].values
volume_set = apply_tag_filters(volume_set, filters)
aliases = {
'availability-zone' => 'availabilityZone',
'create-time' => 'createTime',