diff --git a/.gitignore b/.gitignore index e17800a77..923d72edd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,8 @@ dist/* .tox .coverage *.pyc +*~ .noseids build/ +.idea/ + diff --git a/.travis.yml b/.travis.yml index 037501a5b..e7fe9847c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ python: env: matrix: #- BOTO_VERSION=2.13.3 + - BOTO_VERSION=2.19.0 - BOTO_VERSION=2.12.0 - BOTO_VERSION=2.11.0 - BOTO_VERSION=2.10.0 diff --git a/AUTHORS.md b/AUTHORS.md index 8a0831dcd..e7283eceb 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -12,3 +12,5 @@ Moto is written by Steve Pulec with contributions from: * [Konstantinos Koukopoulos](https://github.com/kouk) * [attili](https://github.com/attili) * [JJ Zeng](https://github.com/jjofseattle) +* [Jon Haddad](https://github.com/rustyrazorblade) +* [Andres Riancho](https://github.com/andresriancho) diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index a367ba297..a9447974b 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -83,6 +83,8 @@ class FakeAutoScalingGroup(object): self.launch_config = autoscaling_backend.launch_configurations[launch_config_name] self.launch_config_name = launch_config_name self.vpc_zone_identifier = vpc_zone_identifier + self.health_check_period = health_check_period + self.health_check_type = health_check_type self.set_desired_capacity(desired_capacity) diff --git a/moto/core/models.py b/moto/core/models.py index 17238fcb0..c5c23d155 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -7,9 +7,13 @@ from .utils import convert_regex_to_flask_path class MockAWS(object): + nested_count = 0 + def __init__(self, backend): self.backend = backend - HTTPretty.reset() + + if self.__class__.nested_count == 0: + HTTPretty.reset() def __call__(self, func): return self.decorate_callable(func) @@ -21,8 +25,11 @@ class MockAWS(object): self.stop() def start(self): + self.__class__.nested_count += 1 self.backend.reset() - HTTPretty.enable() + + if not HTTPretty.is_enabled(): + HTTPretty.enable() for method in HTTPretty.METHODS: for key, value in self.backend.urls.iteritems(): @@ -40,7 +47,13 @@ class MockAWS(object): ) def stop(self): - HTTPretty.disable() + self.__class__.nested_count -= 1 + + if self.__class__.nested_count < 0: + raise RuntimeError('Called stop() before start().') + + if self.__class__.nested_count == 0: + HTTPretty.disable() def decorate_callable(self, func): def wrapper(*args, **kwargs): @@ -97,6 +110,13 @@ class BaseBackend(object): return paths + @property + def url_bases(self): + """ + A list containing the url_bases extracted from urls.py + """ + return self._url_module.url_bases + @property def flask_paths(self): """ diff --git a/moto/core/responses.py b/moto/core/responses.py index 3d8fa138c..4a0515c4b 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -9,18 +9,29 @@ from moto.core.utils import camelcase_to_underscores, method_names_from_class class BaseResponse(object): def dispatch(self, request, full_url, headers): + querystring = {} + if hasattr(request, 'body'): # Boto self.body = request.body else: # Flask server + + # FIXME: At least in Flask==0.10.1, request.data is an empty string + # and the information we want is in request.form. Keeping self.body + # definition for back-compatibility self.body = request.data - querystring = parse_qs(urlparse(full_url).query) + querystring = {} + for key, value in request.form.iteritems(): + querystring[key] = [value, ] + if not querystring: - querystring = parse_qs(self.body) + querystring.update(parse_qs(urlparse(full_url).query)) if not querystring: - querystring = headers + querystring.update(parse_qs(self.body)) + if not querystring: + querystring.update(headers) self.uri = full_url self.path = urlparse(full_url).path @@ -64,7 +75,13 @@ def metadata_response(request, full_url, headers): Expiration=tomorrow.strftime("%Y-%m-%dT%H:%M:%SZ") ) - path = parsed_url.path.lstrip("/latest/meta-data/") + path = parsed_url.path + + meta_data_prefix = "/latest/meta-data/" + # Strip prefix if it is there + if path.startswith(meta_data_prefix): + path = path[len(meta_data_prefix):] + if path == '': result = 'iam' elif path == 'iam': diff --git a/moto/dynamodb/models.py b/moto/dynamodb/models.py index 198b6dc38..67f5152d6 100644 --- a/moto/dynamodb/models.py +++ b/moto/dynamodb/models.py @@ -3,10 +3,10 @@ import datetime import json try: - from collections import OrderedDict + from collections import OrderedDict except ImportError: - # python 2.6 or earlier, use backport - from ordereddict import OrderedDict + # python 2.6 or earlier, use backport + from ordereddict import OrderedDict from moto.core import BaseBackend diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 0a391c5d7..4e05ceda8 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1,4 +1,5 @@ import copy +import itertools from collections import defaultdict from boto.ec2.instance import Instance as BotoInstance, Reservation @@ -35,23 +36,23 @@ class Instance(BotoInstance): self._state = InstanceState("running", 16) self.user_data = user_data - def start(self): + def start(self, *args, **kwargs): self._state.name = "running" self._state.code = 16 - def stop(self): + def stop(self, *args, **kwargs): self._state.name = "stopped" self._state.code = 80 - def terminate(self): + def terminate(self, *args, **kwargs): self._state.name = "terminated" self._state.code = 48 - def reboot(self): + def reboot(self, *args, **kwargs): self._state.name = "running" self._state.code = 16 - def get_tags(self): + def get_tags(self, *args, **kwargs): tags = ec2_backend.describe_tags(self.id) return tags @@ -300,45 +301,50 @@ class SecurityRule(object): class SecurityGroup(object): - def __init__(self, group_id, name, description): + def __init__(self, group_id, name, description, vpc_id=None): self.id = group_id self.name = name self.description = description self.ingress_rules = [] self.egress_rules = [] + self.vpc_id = vpc_id class SecurityGroupBackend(object): def __init__(self): - self.groups = {} + # the key in the dict group is the vpc_id or None (non-vpc) + self.groups = defaultdict(dict) super(SecurityGroupBackend, self).__init__() - def create_security_group(self, name, description, force=False): + def create_security_group(self, name, description, vpc_id=None, force=False): group_id = random_security_group_id() if not force: - existing_group = self.get_security_group_from_name(name) + existing_group = self.get_security_group_from_name(name, vpc_id) if existing_group: return None - group = SecurityGroup(group_id, name, description) - self.groups[group_id] = group + group = SecurityGroup(group_id, name, description, vpc_id=vpc_id) + + self.groups[vpc_id][group_id] = group return group def describe_security_groups(self): - return self.groups.values() + return itertools.chain(*[x.values() for x in self.groups.values()]) - def delete_security_group(self, name_or_group_id): - if name_or_group_id in self.groups: - # Group Id - return self.groups.pop(name_or_group_id) - else: - # Group Name - group = self.get_security_group_from_name(name_or_group_id) + def delete_security_group(self, name=None, group_id=None): + if group_id: + # loop over all the SGs, find the right one + for vpc in self.groups.values(): + if group_id in vpc: + return vpc.pop(group_id) + elif name: + # Group Name. Has to be in standard EC2, VPC needs to be identified by group_id + group = self.get_security_group_from_name(name, None) if group: - return self.groups.pop(group.id) + return self.groups[None].pop(group.id) - def get_security_group_from_name(self, name): - for group_id, group in self.groups.iteritems(): + def get_security_group_from_name(self, name, vpc_id): + for group_id, group in self.groups[vpc_id].iteritems(): if group.name == name: return group @@ -347,20 +353,20 @@ class SecurityGroupBackend(object): default_group = ec2_backend.create_security_group("default", "The default security group", force=True) return default_group - def authorize_security_group_ingress(self, group_name, ip_protocol, from_port, to_port, ip_ranges=None, source_group_names=None): - group = self.get_security_group_from_name(group_name) + def authorize_security_group_ingress(self, group_name, ip_protocol, from_port, to_port, ip_ranges=None, source_group_names=None, vpc_id=None): + group = self.get_security_group_from_name(group_name, vpc_id) source_groups = [] for source_group_name in source_group_names: - source_groups.append(self.get_security_group_from_name(source_group_name)) + source_groups.append(self.get_security_group_from_name(source_group_name, vpc_id)) security_rule = SecurityRule(ip_protocol, from_port, to_port, ip_ranges, source_groups) group.ingress_rules.append(security_rule) - def revoke_security_group_ingress(self, group_name, ip_protocol, from_port, to_port, ip_ranges=None, source_group_names=None): - group = self.get_security_group_from_name(group_name) + def revoke_security_group_ingress(self, group_name, ip_protocol, from_port, to_port, ip_ranges=None, source_group_names=None, vpc_id=None): + group = self.get_security_group_from_name(group_name, vpc_id) source_groups = [] for source_group_name in source_group_names: - source_groups.append(self.get_security_group_from_name(source_group_name)) + source_groups.append(self.get_security_group_from_name(source_group_name, vpc_id)) security_rule = SecurityRule(ip_protocol, from_port, to_port, ip_ranges, source_groups) if security_rule in group.ingress_rules: @@ -536,12 +542,12 @@ class SpotInstanceRequest(object): self.security_groups = [] if security_groups: for group_name in security_groups: - group = ec2_backend.get_security_group_from_name(group_name) + group = ec2_backend.get_security_group_from_name(group_name, None) if group: self.security_groups.append(group) else: # If not security groups, add the default - default_group = ec2_backend.get_security_group_from_name("default") + default_group = ec2_backend.get_security_group_from_name("default", None) self.security_groups.append(default_group) @@ -556,7 +562,7 @@ class SpotRequestBackend(object): instance_type, placement, kernel_id, ramdisk_id, monitoring_enabled, subnet_id): requests = [] - for index in range(count): + for _ in range(count): spot_request_id = random_spot_request_id() request = SpotInstanceRequest( spot_request_id, price, image_id, type, valid_from, valid_until, @@ -578,7 +584,7 @@ class SpotRequestBackend(object): return requests -class ElasticAddress(): +class ElasticAddress(object): def __init__(self, domain): self.public_ip = random_ip() self.allocation_id = random_eip_allocation_id() if domain == "vpc" else None diff --git a/moto/ec2/responses/__init__.py b/moto/ec2/responses/__init__.py index 690419438..cebff3eba 100644 --- a/moto/ec2/responses/__init__.py +++ b/moto/ec2/responses/__init__.py @@ -1,5 +1,3 @@ -from moto.core.responses import BaseResponse - from .amazon_dev_pay import AmazonDevPay from .amis import AmisResponse from .availability_zones_and_regions import AvailabilityZonesAndRegions @@ -31,7 +29,6 @@ from .windows import Windows class EC2Response( - BaseResponse, AmazonDevPay, AmisResponse, AvailabilityZonesAndRegions, diff --git a/moto/ec2/responses/amazon_dev_pay.py b/moto/ec2/responses/amazon_dev_pay.py index b5a0cf646..d0ef8893b 100644 --- a/moto/ec2/responses/amazon_dev_pay.py +++ b/moto/ec2/responses/amazon_dev_pay.py @@ -1,9 +1,6 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class AmazonDevPay(object): +class AmazonDevPay(BaseResponse): def confirm_product_instance(self): raise NotImplementedError('AmazonDevPay.confirm_product_instance is not yet implemented') diff --git a/moto/ec2/responses/amis.py b/moto/ec2/responses/amis.py index 10936e635..74375e1b0 100644 --- a/moto/ec2/responses/amis.py +++ b/moto/ec2/responses/amis.py @@ -1,10 +1,11 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend from moto.ec2.utils import instance_ids_from_querystring, image_ids_from_querystring -class AmisResponse(object): +class AmisResponse(BaseResponse): def create_image(self): name = self.querystring.get('Name')[0] if "Description" in self.querystring: diff --git a/moto/ec2/responses/availability_zones_and_regions.py b/moto/ec2/responses/availability_zones_and_regions.py index f216a644f..1e1b482aa 100644 --- a/moto/ec2/responses/availability_zones_and_regions.py +++ b/moto/ec2/responses/availability_zones_and_regions.py @@ -1,9 +1,10 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend -class AvailabilityZonesAndRegions(object): +class AvailabilityZonesAndRegions(BaseResponse): def describe_availability_zones(self): zones = ec2_backend.describe_availability_zones() template = Template(DESCRIBE_ZONES_RESPONSE) diff --git a/moto/ec2/responses/customer_gateways.py b/moto/ec2/responses/customer_gateways.py index 06c590e4e..1f30a9d16 100644 --- a/moto/ec2/responses/customer_gateways.py +++ b/moto/ec2/responses/customer_gateways.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class CustomerGateways(object): +class CustomerGateways(BaseResponse): def create_customer_gateway(self): raise NotImplementedError('CustomerGateways(AmazonVPC).create_customer_gateway is not yet implemented') diff --git a/moto/ec2/responses/dhcp_options.py b/moto/ec2/responses/dhcp_options.py index 23e73b317..f94abd9be 100644 --- a/moto/ec2/responses/dhcp_options.py +++ b/moto/ec2/responses/dhcp_options.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class DHCPOptions(object): +class DHCPOptions(BaseResponse): def associate_dhcp_options(self): raise NotImplementedError('DHCPOptions(AmazonVPC).associate_dhcp_options is not yet implemented') diff --git a/moto/ec2/responses/elastic_block_store.py b/moto/ec2/responses/elastic_block_store.py index 64ad65b30..b179ad13a 100644 --- a/moto/ec2/responses/elastic_block_store.py +++ b/moto/ec2/responses/elastic_block_store.py @@ -1,9 +1,10 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend -class ElasticBlockStore(object): +class ElasticBlockStore(BaseResponse): def attach_volume(self): volume_id = self.querystring.get('VolumeId')[0] instance_id = self.querystring.get('InstanceId')[0] diff --git a/moto/ec2/responses/elastic_ip_addresses.py b/moto/ec2/responses/elastic_ip_addresses.py index 60cdbcf5e..5553ef956 100644 --- a/moto/ec2/responses/elastic_ip_addresses.py +++ b/moto/ec2/responses/elastic_ip_addresses.py @@ -1,11 +1,11 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend from moto.ec2.utils import sequence_from_querystring - -class ElasticIPAddresses(object): +class ElasticIPAddresses(BaseResponse): def allocate_address(self): if "Domain" in self.querystring: domain = self.querystring.get('Domain')[0] diff --git a/moto/ec2/responses/elastic_network_interfaces.py b/moto/ec2/responses/elastic_network_interfaces.py index f3624070d..985fc334c 100644 --- a/moto/ec2/responses/elastic_network_interfaces.py +++ b/moto/ec2/responses/elastic_network_interfaces.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class ElasticNetworkInterfaces(object): +class ElasticNetworkInterfaces(BaseResponse): def attach_network_interface(self): raise NotImplementedError('ElasticNetworkInterfaces(AmazonVPC).attach_network_interface is not yet implemented') diff --git a/moto/ec2/responses/general.py b/moto/ec2/responses/general.py index 5353bb99a..77636655f 100644 --- a/moto/ec2/responses/general.py +++ b/moto/ec2/responses/general.py @@ -1,10 +1,11 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend from moto.ec2.utils import instance_ids_from_querystring -class General(object): +class General(BaseResponse): def get_console_output(self): self.instance_ids = instance_ids_from_querystring(self.querystring) instance_id = self.instance_ids[0] diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index f230dcf49..cc2c002bf 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -1,12 +1,13 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores from moto.ec2.models import ec2_backend from moto.ec2.utils import instance_ids_from_querystring, filters_from_querystring, filter_reservations from moto.ec2.exceptions import InvalidIdError -class InstanceResponse(object): +class InstanceResponse(BaseResponse): def describe_instances(self): instance_ids = instance_ids_from_querystring(self.querystring) if instance_ids: @@ -67,12 +68,16 @@ class InstanceResponse(object): return template.render(instance=instance, attribute=attribute, value=value) def modify_instance_attribute(self): + attribute_key = None for key, value in self.querystring.iteritems(): if '.Value' in key: + attribute_key = key break - value = self.querystring.get(key)[0] - normalized_attribute = camelcase_to_underscores(key.split(".")[0]) + if not attribute_key: + return + 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] ec2_backend.modify_instance_attribute(instance_id, normalized_attribute, value) diff --git a/moto/ec2/responses/internet_gateways.py b/moto/ec2/responses/internet_gateways.py index 772623bc6..c9b24922f 100644 --- a/moto/ec2/responses/internet_gateways.py +++ b/moto/ec2/responses/internet_gateways.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class InternetGateways(object): +class InternetGateways(BaseResponse): def attach_internet_gateway(self): raise NotImplementedError('InternetGateways(AmazonVPC).attach_internet_gateway is not yet implemented') diff --git a/moto/ec2/responses/ip_addresses.py b/moto/ec2/responses/ip_addresses.py index b88a5462e..7d79d2a75 100644 --- a/moto/ec2/responses/ip_addresses.py +++ b/moto/ec2/responses/ip_addresses.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class IPAddresses(object): +class IPAddresses(BaseResponse): def assign_private_ip_addresses(self): raise NotImplementedError('IPAddresses.assign_private_ip_addresses is not yet implemented') diff --git a/moto/ec2/responses/key_pairs.py b/moto/ec2/responses/key_pairs.py index 23e438bfc..fd0a35fe6 100644 --- a/moto/ec2/responses/key_pairs.py +++ b/moto/ec2/responses/key_pairs.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class KeyPairs(object): +class KeyPairs(BaseResponse): def create_key_pair(self): raise NotImplementedError('KeyPairs.create_key_pair is not yet implemented') diff --git a/moto/ec2/responses/monitoring.py b/moto/ec2/responses/monitoring.py index e3fc1611a..40a2a7434 100644 --- a/moto/ec2/responses/monitoring.py +++ b/moto/ec2/responses/monitoring.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class Monitoring(object): +class Monitoring(BaseResponse): def monitor_instances(self): raise NotImplementedError('Monitoring.monitor_instances is not yet implemented') diff --git a/moto/ec2/responses/network_acls.py b/moto/ec2/responses/network_acls.py index abcf1ab6e..56ac36c38 100644 --- a/moto/ec2/responses/network_acls.py +++ b/moto/ec2/responses/network_acls.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class NetworkACLs(object): +class NetworkACLs(BaseResponse): def create_network_acl(self): raise NotImplementedError('NetworkACLs(AmazonVPC).create_network_acl is not yet implemented') diff --git a/moto/ec2/responses/placement_groups.py b/moto/ec2/responses/placement_groups.py index 7f7b881df..2260b8852 100644 --- a/moto/ec2/responses/placement_groups.py +++ b/moto/ec2/responses/placement_groups.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class PlacementGroups(object): +class PlacementGroups(BaseResponse): def create_placement_group(self): raise NotImplementedError('PlacementGroups.create_placement_group is not yet implemented') diff --git a/moto/ec2/responses/reserved_instances.py b/moto/ec2/responses/reserved_instances.py index faf413426..bab7967fd 100644 --- a/moto/ec2/responses/reserved_instances.py +++ b/moto/ec2/responses/reserved_instances.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class ReservedInstances(object): +class ReservedInstances(BaseResponse): def cancel_reserved_instances_listing(self): raise NotImplementedError('ReservedInstances.cancel_reserved_instances_listing is not yet implemented') diff --git a/moto/ec2/responses/route_tables.py b/moto/ec2/responses/route_tables.py index 6266742ed..b9dcfcceb 100644 --- a/moto/ec2/responses/route_tables.py +++ b/moto/ec2/responses/route_tables.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class RouteTables(object): +class RouteTables(BaseResponse): def associate_route_table(self): raise NotImplementedError('RouteTables(AmazonVPC).associate_route_table is not yet implemented') diff --git a/moto/ec2/responses/security_groups.py b/moto/ec2/responses/security_groups.py index 69d32d5fb..163faa1d9 100644 --- a/moto/ec2/responses/security_groups.py +++ b/moto/ec2/responses/security_groups.py @@ -1,5 +1,6 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend @@ -20,7 +21,7 @@ def process_rules_from_querystring(querystring): return (name, ip_protocol, from_port, to_port, ip_ranges, source_groups) -class SecurityGroups(object): +class SecurityGroups(BaseResponse): def authorize_security_group_egress(self): raise NotImplementedError('SecurityGroups.authorize_security_group_egress is not yet implemented') @@ -31,7 +32,8 @@ class SecurityGroups(object): def create_security_group(self): name = self.querystring.get('GroupName')[0] description = self.querystring.get('GroupDescription')[0] - group = ec2_backend.create_security_group(name, description) + vpc_id = self.querystring.get("VpcId", [None])[0] + group = ec2_backend.create_security_group(name, description, vpc_id=vpc_id) if not group: # There was an exisitng group return "There was an existing security group with name {0}".format(name), dict(status=409) @@ -40,9 +42,16 @@ class SecurityGroups(object): def delete_security_group(self): # TODO this should raise an error if there are instances in the group. See http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DeleteSecurityGroup.html - name = self.querystring.get('GroupName')[0] - group = ec2_backend.delete_security_group(name) + name = self.querystring.get('GroupName') + sg_id = self.querystring.get('GroupId') + + if name: + group = ec2_backend.delete_security_group(name[0]) + elif sg_id: + group = ec2_backend.delete_security_group(group_id=sg_id[0]) + + # needs name or group now if not group: # There was no such group return "There was no security group with name {0}".format(name), dict(status=404) @@ -83,7 +92,7 @@ DESCRIBE_SECURITY_GROUPS_RESPONSE = """ {% for rule in group.ingress_rules %} diff --git a/moto/ec2/responses/spot_instances.py b/moto/ec2/responses/spot_instances.py index 759914989..814bbcbd6 100644 --- a/moto/ec2/responses/spot_instances.py +++ b/moto/ec2/responses/spot_instances.py @@ -1,9 +1,10 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend -class SpotInstances(object): +class SpotInstances(BaseResponse): def _get_param(self, param_name): return self.querystring.get(param_name, [None])[0] diff --git a/moto/ec2/responses/subnets.py b/moto/ec2/responses/subnets.py index 761f492e5..754982214 100644 --- a/moto/ec2/responses/subnets.py +++ b/moto/ec2/responses/subnets.py @@ -1,9 +1,10 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend -class Subnets(object): +class Subnets(BaseResponse): def create_subnet(self): vpc_id = self.querystring.get('VpcId')[0] cidr_block = self.querystring.get('CidrBlock')[0] diff --git a/moto/ec2/responses/tags.py b/moto/ec2/responses/tags.py index dd8dce8e8..6a12cf00c 100644 --- a/moto/ec2/responses/tags.py +++ b/moto/ec2/responses/tags.py @@ -1,10 +1,11 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend from moto.ec2.utils import resource_ids_from_querystring -class TagResponse(object): +class TagResponse(BaseResponse): def create_tags(self): resource_ids = resource_ids_from_querystring(self.querystring) diff --git a/moto/ec2/responses/virtual_private_gateways.py b/moto/ec2/responses/virtual_private_gateways.py index 7f4c8292d..e2bdac7db 100644 --- a/moto/ec2/responses/virtual_private_gateways.py +++ b/moto/ec2/responses/virtual_private_gateways.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class VirtualPrivateGateways(object): +class VirtualPrivateGateways(BaseResponse): def attach_vpn_gateway(self): raise NotImplementedError('VirtualPrivateGateways(AmazonVPC).attach_vpn_gateway is not yet implemented') diff --git a/moto/ec2/responses/vm_export.py b/moto/ec2/responses/vm_export.py index 466c627a6..8a88a709d 100644 --- a/moto/ec2/responses/vm_export.py +++ b/moto/ec2/responses/vm_export.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class VMExport(object): +class VMExport(BaseResponse): def cancel_export_task(self): raise NotImplementedError('VMExport.cancel_export_task is not yet implemented') diff --git a/moto/ec2/responses/vm_import.py b/moto/ec2/responses/vm_import.py index 7bed7c8da..a30cf706c 100644 --- a/moto/ec2/responses/vm_import.py +++ b/moto/ec2/responses/vm_import.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class VMImport(object): +class VMImport(BaseResponse): def cancel_conversion_task(self): raise NotImplementedError('VMImport.cancel_conversion_task is not yet implemented') diff --git a/moto/ec2/responses/vpcs.py b/moto/ec2/responses/vpcs.py index c2b16f9cd..1decdb33c 100644 --- a/moto/ec2/responses/vpcs.py +++ b/moto/ec2/responses/vpcs.py @@ -1,9 +1,10 @@ from jinja2 import Template +from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend -class VPCs(object): +class VPCs(BaseResponse): def create_vpc(self): cidr_block = self.querystring.get('CidrBlock')[0] vpc = ec2_backend.create_vpc(cidr_block) diff --git a/moto/ec2/responses/vpn_connections.py b/moto/ec2/responses/vpn_connections.py index 1a4dbb75a..61c34336b 100644 --- a/moto/ec2/responses/vpn_connections.py +++ b/moto/ec2/responses/vpn_connections.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class VPNConnections(object): +class VPNConnections(BaseResponse): def create_vpn_connection(self): raise NotImplementedError('VPNConnections(AmazonVPC).create_vpn_connection is not yet implemented') diff --git a/moto/ec2/responses/windows.py b/moto/ec2/responses/windows.py index 4c600c9a3..809c82a1c 100644 --- a/moto/ec2/responses/windows.py +++ b/moto/ec2/responses/windows.py @@ -1,10 +1,7 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring +from moto.core.responses import BaseResponse -class Windows(object): +class Windows(BaseResponse): def bundle_instance(self): raise NotImplementedError('Windows.bundle_instance is not yet implemented') diff --git a/moto/emr/models.py b/moto/emr/models.py index 5dca3f62e..6c2045622 100644 --- a/moto/emr/models.py +++ b/moto/emr/models.py @@ -100,7 +100,7 @@ class FakeJobFlow(object): def master_instance_type(self): groups = self.instance_groups if groups: - groups[0].type + return groups[0].type else: return self.initial_master_instance_type @@ -108,7 +108,7 @@ class FakeJobFlow(object): def slave_instance_type(self): groups = self.instance_groups if groups: - groups[0].type + return groups[0].type else: return self.initial_slave_instance_type diff --git a/moto/route53/models.py b/moto/route53/models.py index d901996fa..1b2067504 100644 --- a/moto/route53/models.py +++ b/moto/route53/models.py @@ -2,7 +2,7 @@ from moto.core import BaseBackend from moto.core.utils import get_random_hex -class FakeZone: +class FakeZone(object): def __init__(self, name, id): self.name = name diff --git a/moto/s3bucket_path/models.py b/moto/s3bucket_path/models.py index 2b7e99539..a32fe18e6 100644 --- a/moto/s3bucket_path/models.py +++ b/moto/s3bucket_path/models.py @@ -2,6 +2,6 @@ from moto.s3.models import S3Backend class S3BucketPathBackend(S3Backend): - True + pass s3bucket_path_backend = S3BucketPathBackend() diff --git a/moto/server.py b/moto/server.py index 9ef135359..c1ecef074 100644 --- a/moto/server.py +++ b/moto/server.py @@ -1,16 +1,57 @@ +import re import sys import argparse +from threading import Lock + from flask import Flask from werkzeug.routing import BaseConverter +from werkzeug.serving import run_simple from moto.backends import BACKENDS from moto.core.utils import convert_flask_to_httpretty_response -app = Flask(__name__) HTTP_METHODS = ["GET", "POST", "PUT", "DELETE", "HEAD"] +class DomainDispatcherApplication(object): + """ + Dispatch requests to different applications based on the "Host:" header + value. We'll match the host header value with the url_bases of each backend. + """ + + def __init__(self, create_app, service=None): + self.create_app = create_app + self.lock = Lock() + self.app_instances = {} + self.service = service + + def get_backend_for_host(self, host): + if self.service: + return self.service + + for backend_name, backend in BACKENDS.iteritems(): + for url_base in backend.url_bases: + if re.match(url_base, 'http://%s' % host): + return backend_name + + raise RuntimeError('Invalid host: "%s"' % host) + + def get_application(self, host): + host = host.split(':')[0] + with self.lock: + backend = self.get_backend_for_host(host) + app = self.app_instances.get(backend, None) + if app is None: + app = self.create_app(backend) + self.app_instances[backend] = app + return app + + def __call__(self, environ, start_response): + backend_app = self.get_application(environ['HTTP_HOST']) + return backend_app(environ, start_response) + + class RegexConverter(BaseConverter): # http://werkzeug.pocoo.org/docs/routing/#custom-converters def __init__(self, url_map, *items): @@ -18,25 +59,34 @@ class RegexConverter(BaseConverter): self.regex = items[0] -def configure_urls(service): - backend = BACKENDS[service] +def create_backend_app(service): from werkzeug.routing import Map + + # Create the backend_app + backend_app = Flask(__name__) + backend_app.debug = True + # Reset view functions to reset the app - app.view_functions = {} - app.url_map = Map() - app.url_map.converters['regex'] = RegexConverter + backend_app.view_functions = {} + backend_app.url_map = Map() + backend_app.url_map.converters['regex'] = RegexConverter + + backend = BACKENDS[service] for url_path, handler in backend.flask_paths.iteritems(): - app.route(url_path, methods=HTTP_METHODS)(convert_flask_to_httpretty_response(handler)) + backend_app.route(url_path, methods=HTTP_METHODS)(convert_flask_to_httpretty_response(handler)) + + return backend_app def main(argv=sys.argv[1:]): - available_services = BACKENDS.keys() - parser = argparse.ArgumentParser() + + # Keep this for backwards compat parser.add_argument( - 'service', type=str, - choices=available_services, - help='Choose which mechanism you want to run') + "service", + type=str, + nargs='?', # http://stackoverflow.com/a/4480202/731592 + default=None) parser.add_argument( '-H', '--host', type=str, help='Which host to bind', @@ -48,10 +98,11 @@ def main(argv=sys.argv[1:]): args = parser.parse_args(argv) - configure_urls(args.service) + # Wrap the main application + main_app = DomainDispatcherApplication(create_backend_app, service=args.service) + main_app.debug = True - app.testing = True - app.run(host=args.host, port=args.port) + run_simple(args.host, args.port, main_app) if __name__ == '__main__': main() diff --git a/moto/sqs/models.py b/moto/sqs/models.py index 5c6d04fe7..f82dc3208 100644 --- a/moto/sqs/models.py +++ b/moto/sqs/models.py @@ -100,7 +100,7 @@ class SQSBackend(BaseBackend): def receive_messages(self, queue_name, count): queue = self.get_queue(queue_name) result = [] - for index in range(count): + for _ in range(count): if queue.messages: result.append(queue.messages.pop(0)) return result diff --git a/moto/sqs/urls.py b/moto/sqs/urls.py index 80fe27595..bbc6a2733 100644 --- a/moto/sqs/urls.py +++ b/moto/sqs/urls.py @@ -1,7 +1,7 @@ from .responses import QueueResponse, QueuesResponse url_bases = [ - "https?://(.*).amazonaws.com" + "https?://(.*?)(queue|sqs)(.*?).amazonaws.com" ] url_paths = { diff --git a/setup.py b/setup.py index 4a5806ca9..999afba42 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ if sys.version_info < (2, 7): setup( name='moto', - version='0.2.11', + version='0.2.15', description='A library that allows your python tests to easily' ' mock out the boto library', author='Steve Pulec', @@ -28,6 +28,6 @@ setup( 'moto_server = moto.server:main', ], }, - packages=find_packages(), + packages=find_packages(exclude=("tests", "tests.*")), install_requires=install_requires, ) diff --git a/tests/test_autoscaling/test_server.py b/tests/test_autoscaling/test_server.py index d3ca05cd5..72a38750d 100644 --- a/tests/test_autoscaling/test_server.py +++ b/tests/test_autoscaling/test_server.py @@ -5,11 +5,12 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("autoscaling") def test_describe_autoscaling_groups(): - test_client = server.app.test_client() + backend = server.create_backend_app("autoscaling") + test_client = backend.test_client() + res = test_client.get('/?Action=DescribeLaunchConfigurations') res.data.should.contain('(.*)", res.data) diff --git a/tests/test_elb/test_server.py b/tests/test_elb/test_server.py index 9fc172dd6..c8f13e8d0 100644 --- a/tests/test_elb/test_server.py +++ b/tests/test_elb/test_server.py @@ -5,11 +5,12 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("elb") def test_elb_describe_instances(): - test_client = server.app.test_client() + backend = server.create_backend_app("elb") + test_client = backend.test_client() + res = test_client.get('/?Action=DescribeLoadBalancers') res.data.should.contain('DescribeLoadBalancersResponse') diff --git a/tests/test_emr/test_server.py b/tests/test_emr/test_server.py index 85ba7c4db..07aed3bc5 100644 --- a/tests/test_emr/test_server.py +++ b/tests/test_emr/test_server.py @@ -5,11 +5,12 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("emr") def test_describe_jobflows(): - test_client = server.app.test_client() + backend = server.create_backend_app("emr") + test_client = backend.test_client() + res = test_client.get('/?Action=DescribeJobFlows') res.data.should.contain('') diff --git a/tests/test_s3/test_server.py b/tests/test_s3/test_server.py index d2f38cb07..ee1429819 100644 --- a/tests/test_s3/test_server.py +++ b/tests/test_s3/test_server.py @@ -5,18 +5,21 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("s3") def test_s3_server_get(): - test_client = server.app.test_client() + backend = server.create_backend_app("s3") + test_client = backend.test_client() + res = test_client.get('/') res.data.should.contain('ListAllMyBucketsResult') def test_s3_server_bucket_create(): - test_client = server.app.test_client() + backend = server.create_backend_app("s3") + test_client = backend.test_client() + res = test_client.put('/', 'http://foobar.localhost:5000/') res.status_code.should.equal(200) @@ -36,7 +39,9 @@ def test_s3_server_bucket_create(): def test_s3_server_post_to_bucket(): - test_client = server.app.test_client() + backend = server.create_backend_app("s3") + test_client = backend.test_client() + res = test_client.put('/', 'http://foobar.localhost:5000/') res.status_code.should.equal(200) diff --git a/tests/test_s3bucket_path/test_bucket_path_server.py b/tests/test_s3bucket_path/test_bucket_path_server.py index 943615767..b8eea77e1 100644 --- a/tests/test_s3bucket_path/test_bucket_path_server.py +++ b/tests/test_s3bucket_path/test_bucket_path_server.py @@ -5,18 +5,21 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("s3bucket_path") def test_s3_server_get(): - test_client = server.app.test_client() + backend = server.create_backend_app("s3bucket_path") + test_client = backend.test_client() + res = test_client.get('/') res.data.should.contain('ListAllMyBucketsResult') def test_s3_server_bucket_create(): - test_client = server.app.test_client() + backend = server.create_backend_app("s3bucket_path") + test_client = backend.test_client() + res = test_client.put('/foobar', 'http://localhost:5000') res.status_code.should.equal(200) @@ -36,7 +39,9 @@ def test_s3_server_bucket_create(): def test_s3_server_post_to_bucket(): - test_client = server.app.test_client() + backend = server.create_backend_app("s3bucket_path") + test_client = backend.test_client() + res = test_client.put('/foobar', 'http://localhost:5000/') res.status_code.should.equal(200) diff --git a/tests/test_ses/test_server.py b/tests/test_ses/test_server.py index 876fa1240..06a71d137 100644 --- a/tests/test_ses/test_server.py +++ b/tests/test_ses/test_server.py @@ -5,10 +5,11 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("ses") def test_ses_list_identities(): - test_client = server.app.test_client() + backend = server.create_backend_app("ses") + test_client = backend.test_client() + res = test_client.get('/?Action=ListIdentities') res.data.should.contain("ListIdentitiesResponse") diff --git a/tests/test_sqs/test_server.py b/tests/test_sqs/test_server.py index 8934dcecc..4e5102edf 100644 --- a/tests/test_sqs/test_server.py +++ b/tests/test_sqs/test_server.py @@ -6,11 +6,12 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("sqs") def test_sqs_list_identities(): - test_client = server.app.test_client() + backend = server.create_backend_app("sqs") + test_client = backend.test_client() + res = test_client.get('/?Action=ListQueues') res.data.should.contain("ListQueuesResponse") diff --git a/tests/test_sts/test_server.py b/tests/test_sts/test_server.py index 9a505422f..a1f428caf 100644 --- a/tests/test_sts/test_server.py +++ b/tests/test_sts/test_server.py @@ -5,11 +5,12 @@ import moto.server as server ''' Test the different server responses ''' -server.configure_urls("sts") def test_sts_get_session_token(): - test_client = server.app.test_client() + backend = server.create_backend_app("sts") + test_client = backend.test_client() + res = test_client.get('/?Action=GetSessionToken') res.status_code.should.equal(200) res.data.should.contain("SessionToken")