mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[compute|aws] Add mocking for describe_tags
This commit is contained in:
parent
5385dbcef8
commit
d5a75dbe04
1 changed files with 72 additions and 1 deletions
|
@ -29,8 +29,79 @@ module Fog
|
|||
:parser => Fog::Parsers::Compute::AWS::DescribeTags.new
|
||||
}.merge!(params))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def describe_tags(filters = {})
|
||||
response = Excon::Response.new
|
||||
|
||||
tag_set = deep_clone(self.data[:tags])
|
||||
|
||||
aliases = {
|
||||
'key' => 'key',
|
||||
'resource-id' => 'resourceId',
|
||||
'resource-type' => 'resourceType',
|
||||
'value' => 'value'
|
||||
}
|
||||
|
||||
for filter_key, filter_value in filters
|
||||
filter_attribute = aliases[filter_key]
|
||||
case filter_attribute
|
||||
when 'key'
|
||||
tag_set.reject! { |k,_| k != filter_value }
|
||||
when 'value'
|
||||
tag_set.each { |k,values| values.reject! { |v, _| v != filter_value } }
|
||||
when 'resourceId'
|
||||
filter_resources(tag_set, 'resourceId', filter_value)
|
||||
when 'resourceType'
|
||||
filter_resources(tag_set, 'resourceType', filter_value)
|
||||
end
|
||||
end
|
||||
|
||||
tagged_resources = []
|
||||
tag_set.each do |key, values|
|
||||
values.each do |value, resources|
|
||||
resources.each do |resource|
|
||||
tagged_resources << resource.merge({
|
||||
'key' => key,
|
||||
'value' => value
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'requestId' => Fog::AWS::Mock.request_id,
|
||||
'tagSet' => tagged_resources
|
||||
}
|
||||
response
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def filter_resources(tag_set, filter, value)
|
||||
value_hash_list = tag_set.values
|
||||
value_hash_list.each do |value_hash|
|
||||
value_hash.each do |_, resource_list|
|
||||
resource_list.reject! { |resource| resource[filter] != value }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deep_clone(obj)
|
||||
case obj
|
||||
when Hash
|
||||
obj.inject({}) { |h, pair| h[pair.first] = deep_clone(pair.last); h }
|
||||
when Array
|
||||
obj.map { |o| deep_clone(o) }
|
||||
else
|
||||
obj
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue