Skip to content

Commit

Permalink
[aws] Add tag filtering to mocks for snapshots, volumes, instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
betamatt committed Mar 25, 2011
1 parent 74c8566 commit bd506e0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
12 changes: 12 additions & 0 deletions lib/fog/compute/aws.rb
Expand Up @@ -146,6 +146,18 @@ def reset_data
@data = self.class.data[@region][@aws_access_key_id]
end

def apply_tag_filters(resources, filters)
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.reject{|r| r['tagSet'][tag_key] != tag_value}
end

resources
end
end

class Real
Expand Down
8 changes: 2 additions & 6 deletions lib/fog/compute/requests/aws/describe_instances.rb
Expand Up @@ -74,15 +74,11 @@ def describe_instances(filters = {})
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',
Expand Down
10 changes: 4 additions & 6 deletions lib/fog/compute/requests/aws/describe_snapshots.rb
Expand Up @@ -59,11 +59,6 @@ def describe_snapshots(filters = {}, options = {})
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
Expand All @@ -75,6 +70,8 @@ def describe_snapshots(filters = {}, options = {})
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',
Expand All @@ -85,11 +82,12 @@ def describe_snapshots(filters = {}, options = {})
'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'
Expand Down
8 changes: 2 additions & 6 deletions lib/fog/compute/requests/aws/describe_volumes.rb
Expand Up @@ -50,15 +50,11 @@ def describe_volumes(filters = {})
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',
Expand Down

0 comments on commit bd506e0

Please sign in to comment.