diff --git a/httpretty.py b/httpretty.py index 297c9a3a1..6b3bc137f 100644 --- a/httpretty.py +++ b/httpretty.py @@ -470,19 +470,19 @@ class Entry(Py3kObject): self.method = method self.uri = uri - if isinstance(body, types.FunctionType): + if callable(body): self.dynamic_response = True else: self.dynamic_response = False self.body = body + self.streaming = streaming if self.dynamic_response or self.streaming: self.body_length = 0 else: self.body_length = len(self.body or '') - self.streaming = streaming self.adding_headers = adding_headers or {} self.forcing_headers = forcing_headers or {} self.status = int(status) diff --git a/moto/ec2/responses/__init__.py b/moto/ec2/responses/__init__.py new file mode 100644 index 000000000..61769ef01 --- /dev/null +++ b/moto/ec2/responses/__init__.py @@ -0,0 +1,25 @@ +from urlparse import parse_qs + +from moto.ec2.utils import method_namess_from_class + +from .instances import InstanceResponse + + +class EC2Response(object): + + sub_responses = [InstanceResponse,] + + def dispatch(self, uri, body, headers): + if body: + querystring = parse_qs(body) + else: + querystring = parse_qs(headers) + + action = querystring['Action'][0] + + for sub_response in self.sub_responses: + method_names = method_namess_from_class(sub_response) + if action in method_names: + response = sub_response(querystring) + method = getattr(response, action) + return method() diff --git a/moto/ec2/responses.py b/moto/ec2/responses/instances.py similarity index 89% rename from moto/ec2/responses.py rename to moto/ec2/responses/instances.py index 7be47253a..a7ac7213a 100644 --- a/moto/ec2/responses.py +++ b/moto/ec2/responses/instances.py @@ -1,69 +1,65 @@ -import boto - -from urlparse import parse_qs - from jinja2 import Template -from .models import ec2_backend -from .utils import instance_ids_from_querystring, camelcase_to_underscores +from moto.ec2.models import ec2_backend +from moto.ec2.utils import instance_ids_from_querystring, camelcase_to_underscores, method_namess_from_class -def instances(uri, body, headers): - if body: - querystring = parse_qs(body) - else: - querystring = parse_qs(headers) +class InstanceResponse(object): + def __init__(self, querystring): + self.querystring = querystring + self.instance_ids = instance_ids_from_querystring(querystring) - action = querystring['Action'][0] - instance_ids = instance_ids_from_querystring(querystring) - - if action == 'DescribeInstances': + def DescribeInstances(self): template = Template(EC2_DESCRIBE_INSTANCES) return template.render(reservations=ec2_backend.all_reservations()) - elif action == 'RunInstances': - min_count = int(querystring.get('MinCount', ['1'])[0]) + + def RunInstances(self): + min_count = int(self.querystring.get('MinCount', ['1'])[0]) new_reservation = ec2_backend.add_instances(min_count) template = Template(EC2_RUN_INSTANCES) return template.render(reservation=new_reservation) - elif action == 'TerminateInstances': - instances = ec2_backend.terminate_instances(instance_ids) + + def TerminateInstances(self): + instances = ec2_backend.terminate_instances(self.instance_ids) template = Template(EC2_TERMINATE_INSTANCES) return template.render(instances=instances) - elif action == 'RebootInstances': - instances = ec2_backend.reboot_instances(instance_ids) + + def RebootInstances(self): + instances = ec2_backend.reboot_instances(self.instance_ids) template = Template(EC2_REBOOT_INSTANCES) return template.render(instances=instances) - elif action == 'StopInstances': - instances = ec2_backend.stop_instances(instance_ids) + + def StopInstances(self): + instances = ec2_backend.stop_instances(self.instance_ids) template = Template(EC2_STOP_INSTANCES) return template.render(instances=instances) - elif action == 'StartInstances': - instances = ec2_backend.start_instances(instance_ids) + + def StartInstances(self): + instances = ec2_backend.start_instances(self.instance_ids) template = Template(EC2_START_INSTANCES) return template.render(instances=instances) - elif action == 'DescribeInstanceAttribute': + + def DescribeInstanceAttribute(self): # TODO this and modify below should raise IncorrectInstanceState if instance not in stopped state - attribute = querystring.get("Attribute")[0] + attribute = self.querystring.get("Attribute")[0] normalized_attribute = camelcase_to_underscores(attribute) - instance_id = instance_ids[0] + instance_id = self.instance_ids[0] instance = ec2_backend.get_instance(instance_id) value = getattr(instance, normalized_attribute) template = Template(EC2_DESCRIBE_INSTANCE_ATTRIBUTE) return template.render(instance=instance, attribute=attribute, value=value) - elif action == 'ModifyInstanceAttribute': - for key, value in querystring.iteritems(): + + def ModifyInstanceAttribute(self): + for key, value in self.querystring.iteritems(): if '.Value' in key: break - value = querystring.get(key)[0] + value = self.querystring.get(key)[0] normalized_attribute = camelcase_to_underscores(key.split(".")[0]) - instance_id = instance_ids[0] + instance_id = self.instance_ids[0] instance = ec2_backend.get_instance(instance_id) setattr(instance, normalized_attribute, value) return EC2_MODIFY_INSTANCE_ATTRIBUTE - else: - import pdb;pdb.set_trace() - return EC2_REBOOT_INSTANCE EC2_RUN_INSTANCES = """ diff --git a/moto/ec2/urls.py b/moto/ec2/urls.py index 3d981805c..f2c02238b 100644 --- a/moto/ec2/urls.py +++ b/moto/ec2/urls.py @@ -1,5 +1,5 @@ -from .responses import instances +from .responses import EC2Response urls = { - "https://ec2.us-east-1.amazonaws.com/": instances, + "https://ec2.us-east-1.amazonaws.com/": EC2Response().dispatch, } diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 93dba95ad..f63fbef72 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -1,3 +1,4 @@ +import inspect import random @@ -35,3 +36,7 @@ def camelcase_to_underscores(argument): result += "_" result += char.lower() return result + + +def method_namess_from_class(clazz): + return [x[0] for x in inspect.getmembers(clazz, predicate=inspect.ismethod)]