Skip to content

Commit

Permalink
Fix filter wildcards. Closes #910.
Browse files Browse the repository at this point in the history
  • Loading branch information
spulec committed May 11, 2017
1 parent 6ef2f36 commit 408a709
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 7 additions & 2 deletions moto/ec2/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import unicode_literals

import fnmatch
import random
import re
import six
Expand Down Expand Up @@ -460,7 +462,11 @@ def is_filter_matching(obj, filter, filter_value):
value = obj.get_filter_value(filter)

if isinstance(value, six.string_types):
return value in filter_value
if not isinstance(filter_value, list):
filter_value = [filter_value]
if any(fnmatch.fnmatch(value, pattern) for pattern in filter_value):
return True
return False

try:
value = set(value)
Expand All @@ -479,7 +485,6 @@ def generic_filter(filters, objects):


def simple_aws_filter_to_re(filter_string):
import fnmatch
tmp_filter = filter_string.replace('\?', '[?]')
tmp_filter = tmp_filter.replace('\*', '[*]')
tmp_filter = fnmatch.translate(tmp_filter)
Expand Down
17 changes: 16 additions & 1 deletion tests/test_ec2/test_amis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import boto
import boto.ec2
import boto3
from boto.exception import EC2ResponseError, EC2ResponseError

import sure # noqa

from moto import mock_emr_deprecated
from moto import mock_emr_deprecated, mock_ec2
from tests.helpers import requires_boto_gte


Expand Down Expand Up @@ -558,3 +559,17 @@ def test_ami_attribute_error_cases():
cm.exception.code.should.equal('InvalidAMIID.NotFound')
cm.exception.status.should.equal(400)
cm.exception.request_id.should_not.be.none


"""
Boto3
"""

@mock_ec2
def test_ami_filter_wildcard():
ec2 = boto3.resource('ec2', region_name='us-west-1')
instance = ec2.create_instances(ImageId='ami-1234abcd', MinCount=1, MaxCount=1)[0]
image = instance.create_image(Name='test-image')
filter_result = list(ec2.images.filter(Owners=['111122223333'], Filters=[{'Name':'name', 'Values':['test*']}]))
assert filter_result == [image]

0 comments on commit 408a709

Please sign in to comment.