Fix filter wildcards. Closes #910.

This commit is contained in:
Steve Pulec 2017-05-10 21:33:30 -04:00
parent 6ef2f366f6
commit 408a70992c
2 changed files with 23 additions and 3 deletions

View File

@ -1,4 +1,6 @@
from __future__ import unicode_literals
import fnmatch
import random
import re
import six
@ -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)
@ -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)

View File

@ -5,11 +5,12 @@ from nose.tools import assert_raises
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
@ -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]