Move filter logic from responses.InstanceResponse to models.InstanceBackend
This commit is contained in:
parent
88684f10f2
commit
aa3cf9c806
@ -85,6 +85,7 @@ from .utils import (
|
||||
simple_aws_filter_to_re,
|
||||
is_valid_cidr,
|
||||
filter_internet_gateways,
|
||||
filter_reservations,
|
||||
)
|
||||
|
||||
|
||||
@ -588,7 +589,7 @@ class InstanceBackend(object):
|
||||
if instance.id == instance_id:
|
||||
return instance
|
||||
|
||||
def get_reservations_by_instance_ids(self, instance_ids):
|
||||
def get_reservations_by_instance_ids(self, instance_ids, filters=None):
|
||||
""" Go through all of the reservations and filter to only return those
|
||||
associated with the given instance_ids.
|
||||
"""
|
||||
@ -605,15 +606,20 @@ class InstanceBackend(object):
|
||||
if len(found_instance_ids) != len(instance_ids):
|
||||
invalid_id = list(set(instance_ids).difference(set(found_instance_ids)))[0]
|
||||
raise InvalidInstanceIdError(invalid_id)
|
||||
if filters is not None:
|
||||
reservations = filter_reservations(reservations, filters)
|
||||
return reservations
|
||||
|
||||
def all_reservations(self, make_copy=False):
|
||||
def all_reservations(self, make_copy=False, filters=None):
|
||||
if make_copy:
|
||||
# Return copies so that other functions can modify them with changing
|
||||
# the originals
|
||||
return [copy.deepcopy(reservation) for reservation in self.reservations.values()]
|
||||
reservations = [copy.deepcopy(reservation) for reservation in self.reservations.values()]
|
||||
else:
|
||||
return [reservation for reservation in self.reservations.values()]
|
||||
reservations = [reservation for reservation in self.reservations.values()]
|
||||
if filters is not None:
|
||||
reservations = filter_reservations(reservations, filters)
|
||||
return reservations
|
||||
|
||||
|
||||
class KeyPairBackend(object):
|
||||
|
@ -3,20 +3,18 @@ from jinja2 import Template
|
||||
|
||||
from moto.core.responses import BaseResponse
|
||||
from moto.core.utils import camelcase_to_underscores
|
||||
from moto.ec2.utils import instance_ids_from_querystring, filters_from_querystring, filter_reservations, \
|
||||
from moto.ec2.utils import instance_ids_from_querystring, filters_from_querystring, \
|
||||
dict_from_querystring, optional_from_querystring
|
||||
|
||||
|
||||
class InstanceResponse(BaseResponse):
|
||||
def describe_instances(self):
|
||||
filter_dict = filters_from_querystring(self.querystring)
|
||||
instance_ids = instance_ids_from_querystring(self.querystring)
|
||||
if instance_ids:
|
||||
reservations = self.ec2_backend.get_reservations_by_instance_ids(instance_ids)
|
||||
reservations = self.ec2_backend.get_reservations_by_instance_ids(instance_ids, filters=filter_dict)
|
||||
else:
|
||||
reservations = self.ec2_backend.all_reservations(make_copy=True)
|
||||
|
||||
filter_dict = filters_from_querystring(self.querystring)
|
||||
reservations = filter_reservations(reservations, filter_dict)
|
||||
reservations = self.ec2_backend.all_reservations(make_copy=True, filters=filter_dict)
|
||||
|
||||
template = Template(EC2_DESCRIBE_INSTANCES)
|
||||
return template.render(reservations=reservations)
|
||||
|
Loading…
Reference in New Issue
Block a user