From 41873b59f702d9ea12b61a0b640de08dbc24e4a9 Mon Sep 17 00:00:00 2001 From: earthmant Date: Mon, 23 Nov 2015 15:16:46 +0200 Subject: [PATCH 1/2] Adding Support for Get All DHCP Options add DescribeDhcpOptionsResponse support filtering describe_dhcp_options add get_all_dhcp_options --- moto/ec2/models.py | 11 +++++++++++ moto/ec2/responses/dhcp_options.py | 30 ++++++++++++++---------------- moto/ec2/utils.py | 8 ++++++++ 3 files changed, 33 insertions(+), 16 deletions(-) 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(): From c5bf9d8c94743aaf982ff73990a1346bbed05734 Mon Sep 17 00:00:00 2001 From: earthmant Date: Thu, 3 Dec 2015 13:49:08 +0200 Subject: [PATCH 2/2] replacing the usage of dhcp_opt_ids_from_querystring with sequence_from_querystring --- moto/ec2/responses/dhcp_options.py | 4 ++-- moto/ec2/utils.py | 8 -------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/moto/ec2/responses/dhcp_options.py b/moto/ec2/responses/dhcp_options.py index 55542e069..57d16a015 100644 --- a/moto/ec2/responses/dhcp_options.py +++ b/moto/ec2/responses/dhcp_options.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse from moto.ec2.utils import ( filters_from_querystring, - dhcp_opt_ids_from_querystring, + sequence_from_querystring, dhcp_configuration_from_querystring) @@ -48,7 +48,7 @@ class DHCPOptions(BaseResponse): return template.render(delete_status=delete_status) def describe_dhcp_options(self): - dhcp_opt_ids = dhcp_opt_ids_from_querystring(self.querystring) + dhcp_opt_ids = sequence_from_querystring("DhcpOptionsId", self.querystring) if filters_from_querystring(self.querystring): raise NotImplementedError("Filtering not supported in describe_dhcp_options.") dhcp_opts = self.ec2_backend.get_all_dhcp_options(dhcp_opt_ids, None) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index ebad2b82a..b16c363ea 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -185,14 +185,6 @@ 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():