diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 446561d33..27064f0b7 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -2511,6 +2511,17 @@ class DHCPOptionsSetBackend(object): raise InvalidDHCPOptionsIdError(options_id) return True + def get_all_dhcp_options(self, dhcp_options_ids=None, filters=None): + dhcp_options_sets = self.dhcp_options_sets.values() + + if dhcp_options_ids: + dhcp_options_sets = [dhcp_options_set for dhcp_options_set in dhcp_options_sets if dhcp_options_set.id in dhcp_options_ids] + if len(dhcp_options_sets) != len(dhcp_options_ids): + invalid_id = list(set(dhcp_options_ids).difference(set([dhcp_options_set.id for dhcp_options_set in dhcp_options_sets])))[0] + raise InvalidDHCPOptionsIdError(invalid_id) + + return generic_filter(filters, dhcp_options_sets) + class NetworkAclBackend(object): def __init__(self): diff --git a/moto/ec2/responses/dhcp_options.py b/moto/ec2/responses/dhcp_options.py index a3a454a86..55542e069 100644 --- a/moto/ec2/responses/dhcp_options.py +++ b/moto/ec2/responses/dhcp_options.py @@ -1,8 +1,9 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse from moto.ec2.utils import ( - dhcp_configuration_from_querystring, - sequence_from_querystring) + filters_from_querystring, + dhcp_opt_ids_from_querystring, + dhcp_configuration_from_querystring) class DHCPOptions(BaseResponse): @@ -47,15 +48,12 @@ class DHCPOptions(BaseResponse): return template.render(delete_status=delete_status) def describe_dhcp_options(self): - if "Filter.1.Name" in self.querystring: + dhcp_opt_ids = dhcp_opt_ids_from_querystring(self.querystring) + if filters_from_querystring(self.querystring): raise NotImplementedError("Filtering not supported in describe_dhcp_options.") - elif "DhcpOptionsId.1" in self.querystring: - dhcp_opt_ids = sequence_from_querystring("DhcpOptionsId", self.querystring) - dhcp_opt = self.ec2_backend.describe_dhcp_options(dhcp_opt_ids) - else: - dhcp_opt = self.ec2_backend.describe_dhcp_options() + dhcp_opts = self.ec2_backend.get_all_dhcp_options(dhcp_opt_ids, None) template = self.response_template(DESCRIBE_DHCP_OPTIONS_RESPONSE) - return template.render(dhcp_options=dhcp_opt) + return template.render(dhcp_options=dhcp_opts) CREATE_DHCP_OPTIONS_RESPONSE = u""" @@ -104,16 +102,16 @@ DELETE_DHCP_OPTIONS_RESPONSE = u""" DESCRIBE_DHCP_OPTIONS_RESPONSE = u""" 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE - - {% for dhcp_options_set in dhcp_options %} - + + {% for dhcp_options_set in dhcp_options %} + {{ dhcp_options_set.id }} {% for key, values in dhcp_options_set.options.items() %} {{ values }} {% if values %} - {{key}} + {{ key }} {% for value in values %} @@ -135,9 +133,9 @@ DESCRIBE_DHCP_OPTIONS_RESPONSE = u""" {% endfor %} - - {% endfor %} - + + {% endfor %} + """ diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index b16c363ea..ebad2b82a 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -185,6 +185,14 @@ def network_acl_ids_from_querystring(querystring_dict): return network_acl_ids +def dhcp_opt_ids_from_querystring(querystring_dict): + dhcp_opt_ids = [] + for key, value in querystring_dict.items(): + if 'DhcpOptionsId' in key: + dhcp_opt_ids.append(value[0]) + return dhcp_opt_ids + + def vpc_ids_from_querystring(querystring_dict): vpc_ids = [] for key, value in querystring_dict.items():