From 17d62d9266c29bb2db9e7282b2d15d47813f287a Mon Sep 17 00:00:00 2001 From: Nuwan Goonasekera Date: Sat, 16 Sep 2017 17:08:21 +0530 Subject: [PATCH] Replaced redundant sequence_from_querystring with inherited _get_multi_param --- moto/ec2/responses/amis.py | 12 ++-- moto/ec2/responses/dhcp_options.py | 4 +- moto/ec2/responses/elastic_ip_addresses.py | 6 +- .../responses/elastic_network_interfaces.py | 7 +- moto/ec2/responses/general.py | 4 +- moto/ec2/responses/instances.py | 26 ++++---- moto/ec2/responses/internet_gateways.py | 4 +- moto/ec2/responses/key_pairs.py | 4 +- moto/ec2/responses/network_acls.py | 5 +- moto/ec2/responses/route_tables.py | 4 +- moto/ec2/responses/tags.py | 8 +-- moto/ec2/responses/vpcs.py | 4 +- moto/ec2/responses/vpn_connections.py | 5 +- moto/ec2/utils.py | 64 ------------------- 14 files changed, 38 insertions(+), 119 deletions(-) diff --git a/moto/ec2/responses/amis.py b/moto/ec2/responses/amis.py index d9be77812..967f23e87 100755 --- a/moto/ec2/responses/amis.py +++ b/moto/ec2/responses/amis.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.ec2.utils import instance_ids_from_querystring, image_ids_from_querystring, \ - filters_from_querystring, sequence_from_querystring, executable_users_from_querystring +from moto.ec2.utils import filters_from_querystring class AmisResponse(BaseResponse): @@ -12,8 +11,7 @@ class AmisResponse(BaseResponse): description = self.querystring.get('Description')[0] else: description = "" - instance_ids = instance_ids_from_querystring(self.querystring) - instance_id = instance_ids[0] + instance_id = self._get_param('InstanceId') if self.is_not_dryrun('CreateImage'): image = self.ec2_backend.create_image( instance_id, name, description) @@ -41,9 +39,9 @@ class AmisResponse(BaseResponse): return template.render(success=str(success).lower()) def describe_images(self): - ami_ids = image_ids_from_querystring(self.querystring) + ami_ids = self._get_multi_param('ImageId') filters = filters_from_querystring(self.querystring) - exec_users = executable_users_from_querystring(self.querystring) + exec_users = self._get_multi_param('ExecutableBy') images = self.ec2_backend.describe_images( ami_ids=ami_ids, filters=filters, exec_users=exec_users) template = self.response_template(DESCRIBE_IMAGES_RESPONSE) @@ -60,7 +58,7 @@ class AmisResponse(BaseResponse): ami_id = self.querystring.get('ImageId')[0] operation_type = self.querystring.get('OperationType')[0] group = self.querystring.get('UserGroup.1', [None])[0] - user_ids = sequence_from_querystring('UserId', self.querystring) + user_ids = self._get_multi_param('UserId') if self.is_not_dryrun('ModifyImageAttribute'): if (operation_type == 'add'): self.ec2_backend.add_launch_permission( diff --git a/moto/ec2/responses/dhcp_options.py b/moto/ec2/responses/dhcp_options.py index 450ef1bf9..966d2e3ac 100644 --- a/moto/ec2/responses/dhcp_options.py +++ b/moto/ec2/responses/dhcp_options.py @@ -2,7 +2,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse from moto.ec2.utils import ( filters_from_querystring, - sequence_from_querystring, dhcp_configuration_from_querystring) @@ -49,8 +48,7 @@ class DHCPOptions(BaseResponse): return template.render(delete_status=delete_status) def describe_dhcp_options(self): - dhcp_opt_ids = sequence_from_querystring( - "DhcpOptionsId", self.querystring) + dhcp_opt_ids = self._get_multi_param("DhcpOptionsId") filters = filters_from_querystring(self.querystring) dhcp_opts = self.ec2_backend.get_all_dhcp_options( dhcp_opt_ids, filters) diff --git a/moto/ec2/responses/elastic_ip_addresses.py b/moto/ec2/responses/elastic_ip_addresses.py index 137f12ec6..1a60efcde 100644 --- a/moto/ec2/responses/elastic_ip_addresses.py +++ b/moto/ec2/responses/elastic_ip_addresses.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.ec2.utils import filters_from_querystring, sequence_from_querystring +from moto.ec2.utils import filters_from_querystring class ElasticIPAddresses(BaseResponse): @@ -51,8 +51,8 @@ class ElasticIPAddresses(BaseResponse): return template.render(address=eip) def describe_addresses(self): - allocation_ids = sequence_from_querystring('AllocationId', self.querystring) - public_ips = sequence_from_querystring('PublicIp', self.querystring) + allocation_ids = self._get_multi_param('AllocationId') + public_ips = self._get_multi_param('PublicIp') filters = filters_from_querystring(self.querystring) addresses = self.ec2_backend.describe_addresses( allocation_ids, public_ips, filters) diff --git a/moto/ec2/responses/elastic_network_interfaces.py b/moto/ec2/responses/elastic_network_interfaces.py index cbe76e306..ec99f2da9 100644 --- a/moto/ec2/responses/elastic_network_interfaces.py +++ b/moto/ec2/responses/elastic_network_interfaces.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.ec2.utils import sequence_from_querystring, filters_from_querystring +from moto.ec2.utils import filters_from_querystring class ElasticNetworkInterfaces(BaseResponse): @@ -9,7 +9,7 @@ class ElasticNetworkInterfaces(BaseResponse): subnet_id = self.querystring.get('SubnetId')[0] private_ip_address = self.querystring.get( 'PrivateIpAddress', [None])[0] - groups = sequence_from_querystring('SecurityGroupId', self.querystring) + groups = self._get_multi_param('SecurityGroupId') subnet = self.ec2_backend.get_subnet(subnet_id) if self.is_not_dryrun('CreateNetworkInterface'): eni = self.ec2_backend.create_network_interface( @@ -31,8 +31,7 @@ class ElasticNetworkInterfaces(BaseResponse): 'ElasticNetworkInterfaces(AmazonVPC).describe_network_interface_attribute is not yet implemented') def describe_network_interfaces(self): - eni_ids = sequence_from_querystring( - 'NetworkInterfaceId', self.querystring) + eni_ids = self._get_multi_param('NetworkInterfaceId') filters = filters_from_querystring(self.querystring) enis = self.ec2_backend.get_all_network_interfaces(eni_ids, filters) template = self.response_template(DESCRIBE_NETWORK_INTERFACES_RESPONSE) diff --git a/moto/ec2/responses/general.py b/moto/ec2/responses/general.py index bd95c1975..9add43d3e 100644 --- a/moto/ec2/responses/general.py +++ b/moto/ec2/responses/general.py @@ -1,13 +1,11 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.ec2.utils import instance_ids_from_querystring class General(BaseResponse): def get_console_output(self): - self.instance_ids = instance_ids_from_querystring(self.querystring) - instance_id = self.instance_ids[0] + instance_id = self._get_multi_param('InstanceId')[0] instance = self.ec2_backend.get_instance(instance_id) template = self.response_template(GET_CONSOLE_OUTPUT_RESULT) return template.render(instance=instance) diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index 20c04668e..b4bf531a8 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from boto.ec2.instancetype import InstanceType 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, \ +from moto.ec2.utils import filters_from_querystring, \ dict_from_querystring, optional_from_querystring @@ -10,7 +10,7 @@ class InstanceResponse(BaseResponse): def describe_instances(self): filter_dict = filters_from_querystring(self.querystring) - instance_ids = instance_ids_from_querystring(self.querystring) + instance_ids = self._get_multi_param('InstanceId') token = self._get_param("NextToken") if instance_ids: reservations = self.ec2_backend.get_reservations_by_instance_ids( @@ -62,35 +62,35 @@ class InstanceResponse(BaseResponse): return template.render(reservation=new_reservation) def terminate_instances(self): - instance_ids = instance_ids_from_querystring(self.querystring) + instance_ids = self._get_multi_param('InstanceId') if self.is_not_dryrun('TerminateInstance'): instances = self.ec2_backend.terminate_instances(instance_ids) template = self.response_template(EC2_TERMINATE_INSTANCES) return template.render(instances=instances) def reboot_instances(self): - instance_ids = instance_ids_from_querystring(self.querystring) + instance_ids = self._get_multi_param('InstanceId') if self.is_not_dryrun('RebootInstance'): instances = self.ec2_backend.reboot_instances(instance_ids) template = self.response_template(EC2_REBOOT_INSTANCES) return template.render(instances=instances) def stop_instances(self): - instance_ids = instance_ids_from_querystring(self.querystring) + instance_ids = self._get_multi_param('InstanceId') if self.is_not_dryrun('StopInstance'): instances = self.ec2_backend.stop_instances(instance_ids) template = self.response_template(EC2_STOP_INSTANCES) return template.render(instances=instances) def start_instances(self): - instance_ids = instance_ids_from_querystring(self.querystring) + instance_ids = self._get_multi_param('InstanceId') if self.is_not_dryrun('StartInstance'): instances = self.ec2_backend.start_instances(instance_ids) template = self.response_template(EC2_START_INSTANCES) return template.render(instances=instances) def describe_instance_status(self): - instance_ids = instance_ids_from_querystring(self.querystring) + instance_ids = self._get_multi_param('InstanceId') include_all_instances = optional_from_querystring('IncludeAllInstances', self.querystring) == 'true' @@ -116,8 +116,7 @@ class InstanceResponse(BaseResponse): # instance not in stopped state attribute = self.querystring.get("Attribute")[0] key = camelcase_to_underscores(attribute) - instance_ids = instance_ids_from_querystring(self.querystring) - instance_id = instance_ids[0] + instance_id = self._get_param('InstanceId') instance, value = self.ec2_backend.describe_instance_attribute( instance_id, key) @@ -171,8 +170,7 @@ class InstanceResponse(BaseResponse): del_on_term_value = True if 'true' == del_on_term_value_str else False device_name_value = self.querystring[mapping_device_name][0] - instance_ids = instance_ids_from_querystring(self.querystring) - instance_id = instance_ids[0] + instance_id = self._get_param('InstanceId') instance = self.ec2_backend.get_instance(instance_id) if self.is_not_dryrun('ModifyInstanceAttribute'): @@ -200,8 +198,7 @@ class InstanceResponse(BaseResponse): value = self.querystring.get(attribute_key)[0] normalized_attribute = camelcase_to_underscores( attribute_key.split(".")[0]) - instance_ids = instance_ids_from_querystring(self.querystring) - instance_id = instance_ids[0] + instance_id = self._get_param('InstanceId') self.ec2_backend.modify_instance_attribute( instance_id, normalized_attribute, value) return EC2_MODIFY_INSTANCE_ATTRIBUTE @@ -212,8 +209,7 @@ class InstanceResponse(BaseResponse): if 'GroupId.' in key: new_security_grp_list.append(self.querystring.get(key)[0]) - instance_ids = instance_ids_from_querystring(self.querystring) - instance_id = instance_ids[0] + instance_id = self._get_param('InstanceId') if self.is_not_dryrun('ModifyInstanceSecurityGroups'): self.ec2_backend.modify_instance_security_groups( instance_id, new_security_grp_list) diff --git a/moto/ec2/responses/internet_gateways.py b/moto/ec2/responses/internet_gateways.py index 4a3da0b34..d6a0b19de 100644 --- a/moto/ec2/responses/internet_gateways.py +++ b/moto/ec2/responses/internet_gateways.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse from moto.ec2.utils import ( - sequence_from_querystring, filters_from_querystring, ) @@ -32,8 +31,7 @@ class InternetGateways(BaseResponse): def describe_internet_gateways(self): filter_dict = filters_from_querystring(self.querystring) if "InternetGatewayId.1" in self.querystring: - igw_ids = sequence_from_querystring( - "InternetGatewayId", self.querystring) + igw_ids = self._get_multi_param("InternetGatewayId") igws = self.ec2_backend.describe_internet_gateways( igw_ids, filters=filter_dict) else: diff --git a/moto/ec2/responses/key_pairs.py b/moto/ec2/responses/key_pairs.py index 59268556e..2dab918a7 100644 --- a/moto/ec2/responses/key_pairs.py +++ b/moto/ec2/responses/key_pairs.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals import six from moto.core.responses import BaseResponse -from moto.ec2.utils import keypair_names_from_querystring, filters_from_querystring +from moto.ec2.utils import filters_from_querystring class KeyPairs(BaseResponse): @@ -21,7 +21,7 @@ class KeyPairs(BaseResponse): return self.response_template(DELETE_KEY_PAIR_RESPONSE).render(success=success) def describe_key_pairs(self): - names = keypair_names_from_querystring(self.querystring) + names = self._get_multi_param('KeyName') filters = filters_from_querystring(self.querystring) keypairs = self.ec2_backend.describe_key_pairs(names, filters) template = self.response_template(DESCRIBE_KEY_PAIRS_RESPONSE) diff --git a/moto/ec2/responses/network_acls.py b/moto/ec2/responses/network_acls.py index 440069edc..a0598c73b 100644 --- a/moto/ec2/responses/network_acls.py +++ b/moto/ec2/responses/network_acls.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.ec2.utils import filters_from_querystring, \ - network_acl_ids_from_querystring +from moto.ec2.utils import filters_from_querystring class NetworkACLs(BaseResponse): @@ -67,7 +66,7 @@ class NetworkACLs(BaseResponse): return template.render() def describe_network_acls(self): - network_acl_ids = network_acl_ids_from_querystring(self.querystring) + network_acl_ids = self._get_multi_param('NetworkAclId') filters = filters_from_querystring(self.querystring) network_acls = self.ec2_backend.get_all_network_acls( network_acl_ids, filters) diff --git a/moto/ec2/responses/route_tables.py b/moto/ec2/responses/route_tables.py index 6f68a6553..30eef8159 100644 --- a/moto/ec2/responses/route_tables.py +++ b/moto/ec2/responses/route_tables.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.ec2.utils import route_table_ids_from_querystring, filters_from_querystring, optional_from_querystring +from moto.ec2.utils import filters_from_querystring, optional_from_querystring class RouteTables(BaseResponse): @@ -55,7 +55,7 @@ class RouteTables(BaseResponse): return template.render() def describe_route_tables(self): - route_table_ids = route_table_ids_from_querystring(self.querystring) + route_table_ids = self._get_multi_param('RouteTableId') filters = filters_from_querystring(self.querystring) route_tables = self.ec2_backend.get_all_route_tables( route_table_ids, filters) diff --git a/moto/ec2/responses/tags.py b/moto/ec2/responses/tags.py index a747067fb..65d3da255 100644 --- a/moto/ec2/responses/tags.py +++ b/moto/ec2/responses/tags.py @@ -2,14 +2,13 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse from moto.ec2.models import validate_resource_ids -from moto.ec2.utils import sequence_from_querystring, tags_from_query_string, filters_from_querystring +from moto.ec2.utils import tags_from_query_string, filters_from_querystring class TagResponse(BaseResponse): def create_tags(self): - resource_ids = sequence_from_querystring( - 'ResourceId', self.querystring) + resource_ids = self._get_multi_param('ResourceId') validate_resource_ids(resource_ids) self.ec2_backend.do_resources_exist(resource_ids) tags = tags_from_query_string(self.querystring) @@ -18,8 +17,7 @@ class TagResponse(BaseResponse): return CREATE_RESPONSE def delete_tags(self): - resource_ids = sequence_from_querystring( - 'ResourceId', self.querystring) + resource_ids = self._get_multi_param('ResourceId') validate_resource_ids(resource_ids) tags = tags_from_query_string(self.querystring) if self.is_not_dryrun('DeleteTags'): diff --git a/moto/ec2/responses/vpcs.py b/moto/ec2/responses/vpcs.py index 129f91a3b..17309fe05 100644 --- a/moto/ec2/responses/vpcs.py +++ b/moto/ec2/responses/vpcs.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores -from moto.ec2.utils import filters_from_querystring, vpc_ids_from_querystring +from moto.ec2.utils import filters_from_querystring class VPCs(BaseResponse): @@ -21,7 +21,7 @@ class VPCs(BaseResponse): return template.render(vpc=vpc) def describe_vpcs(self): - vpc_ids = vpc_ids_from_querystring(self.querystring) + vpc_ids = self._get_multi_param('VpcId') filters = filters_from_querystring(self.querystring) vpcs = self.ec2_backend.get_all_vpcs(vpc_ids=vpc_ids, filters=filters) template = self.response_template(DESCRIBE_VPCS_RESPONSE) diff --git a/moto/ec2/responses/vpn_connections.py b/moto/ec2/responses/vpn_connections.py index 2a4a7ef99..e24515ae6 100644 --- a/moto/ec2/responses/vpn_connections.py +++ b/moto/ec2/responses/vpn_connections.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.ec2.utils import filters_from_querystring, sequence_from_querystring +from moto.ec2.utils import filters_from_querystring class VPNConnections(BaseResponse): @@ -23,8 +23,7 @@ class VPNConnections(BaseResponse): return template.render(vpn_connection=vpn_connection) def describe_vpn_connections(self): - vpn_connection_ids = sequence_from_querystring( - 'VpnConnectionId', self.querystring) + vpn_connection_ids = self._get_multi_param('VpnConnectionId') filters = filters_from_querystring(self.querystring) vpn_connections = self.ec2_backend.get_all_vpn_connections( vpn_connection_ids=vpn_connection_ids, filters=filters) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 8f86d0a8d..543ac4a99 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -174,62 +174,6 @@ def split_route_id(route_id): return values[0], values[1] -def instance_ids_from_querystring(querystring_dict): - instance_ids = [] - for key, value in querystring_dict.items(): - if 'InstanceId' in key: - instance_ids.append(value[0]) - return instance_ids - - -def image_ids_from_querystring(querystring_dict): - image_ids = [] - for key, value in querystring_dict.items(): - if 'ImageId' in key: - image_ids.append(value[0]) - return image_ids - - -def executable_users_from_querystring(querystring_dict): - user_ids = [] - for key, value in querystring_dict.items(): - if 'ExecutableBy' in key: - user_ids.append(value[0]) - return user_ids - - -def route_table_ids_from_querystring(querystring_dict): - route_table_ids = [] - for key, value in querystring_dict.items(): - if 'RouteTableId' in key: - route_table_ids.append(value[0]) - return route_table_ids - - -def network_acl_ids_from_querystring(querystring_dict): - network_acl_ids = [] - for key, value in querystring_dict.items(): - if 'NetworkAclId' in key: - network_acl_ids.append(value[0]) - return network_acl_ids - - -def vpc_ids_from_querystring(querystring_dict): - vpc_ids = [] - for key, value in querystring_dict.items(): - if 'VpcId' in key: - vpc_ids.append(value[0]) - return vpc_ids - - -def sequence_from_querystring(parameter, querystring_dict): - parameter_values = [] - for key, value in querystring_dict.items(): - if parameter in key: - parameter_values.append(value[0]) - return parameter_values - - def tags_from_query_string(querystring_dict): prefix = 'Tag' suffix = 'Key' @@ -319,14 +263,6 @@ def dict_from_querystring(parameter, querystring_dict): return use_dict -def keypair_names_from_querystring(querystring_dict): - keypair_names = [] - for key, value in querystring_dict.items(): - if 'KeyName' in key: - keypair_names.append(value[0]) - return keypair_names - - def get_object_value(obj, attr): keys = attr.split('.') val = obj