diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index 4093c6ede..b9e62d7c3 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.core.utils import camelcase_to_underscores from .models import autoscaling_backends @@ -11,26 +10,6 @@ class AutoScalingResponse(BaseResponse): def autoscaling_backend(self): return autoscaling_backends[self.region] - def _get_int_param(self, param_name): - value = self._get_param(param_name) - if value is not None: - return int(value) - - def _get_list_prefix(self, param_prefix): - results = [] - param_index = 1 - while True: - index_prefix = "{0}.{1}.".format(param_prefix, param_index) - new_items = {} - for key, value in self.querystring.items(): - if key.startswith(index_prefix): - new_items[camelcase_to_underscores(key.replace(index_prefix, ""))] = value[0] - if not new_items: - break - results.append(new_items) - param_index += 1 - return results - def create_launch_configuration(self): instance_monitoring_string = self._get_param('InstanceMonitoring.Enabled') if instance_monitoring_string == 'true': diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index 3b2b70d27..6c990b84f 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -1,31 +1,9 @@ from moto.core.responses import BaseResponse -from moto.core.utils import camelcase_to_underscores from .models import cloudwatch_backend class CloudWatchResponse(BaseResponse): - def _get_param(self, param_name): - return self.querystring.get(param_name, [None])[0] - - def _get_multi_param(self, param_prefix): - return [value[0] for key, value in self.querystring.items() if key.startswith(param_prefix)] - - def _get_list_prefix(self, param_prefix): - results = [] - param_index = 1 - while True: - index_prefix = "{0}.{1}.".format(param_prefix, param_index) - new_items = {} - for key, value in self.querystring.items(): - if key.startswith(index_prefix): - new_items[camelcase_to_underscores(key.replace(index_prefix, ""))] = value[0] - if not new_items: - break - results.append(new_items) - param_index += 1 - return results - def put_metric_alarm(self): name = self._get_param('AlarmName') comparison_operator = self._get_param('ComparisonOperator') @@ -35,9 +13,9 @@ class CloudWatchResponse(BaseResponse): statistic = self._get_param('Statistic') description = self._get_param('AlarmDescription') dimensions = self._get_list_prefix('Dimensions.member') - alarm_actions = self._get_multi_param('AlarmActions') - ok_actions = self._get_multi_param('OKActions') - insufficient_data_actions = self._get_multi_param("InsufficientDataActions") + alarm_actions = self._get_multi_param('AlarmActions.member') + ok_actions = self._get_multi_param('OKActions.member') + insufficient_data_actions = self._get_multi_param("InsufficientDataActions.member") unit = self._get_param('Unit') alarm = cloudwatch_backend.put_metric_alarm(name, comparison_operator, evaluation_periods, period, diff --git a/moto/core/responses.py b/moto/core/responses.py index d951d143e..a99ac78c8 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -166,6 +166,10 @@ class BaseResponse(_TemplateEnvironmentMixin): return False def _get_multi_param(self, param_prefix): + """ + Given a querystring of ?LaunchConfigurationNames.member.1=my-test-1&LaunchConfigurationNames.member.2=my-test-2 + this will return ['my-test-1', 'my-test-2'] + """ if param_prefix.endswith("."): prefix = param_prefix else: @@ -181,6 +185,63 @@ class BaseResponse(_TemplateEnvironmentMixin): index += 1 return values + def _get_dict_param(self, param_prefix): + """ + Given a parameter dict of + { + 'Instances.SlaveInstanceType': ['m1.small'], + 'Instances.InstanceCount': ['1'] + } + + returns + { + "SlaveInstanceType": "m1.small", + "InstanceCount": "1", + } + """ + params = {} + for key, value in self.querystring.items(): + if key.startswith(param_prefix): + params[camelcase_to_underscores(key.replace(param_prefix, ""))] = value[0] + return params + + def _get_list_prefix(self, param_prefix): + """ + Given a query dict like + { + 'Steps.member.1.Name': ['example1'], + 'Steps.member.1.ActionOnFailure': ['TERMINATE_JOB_FLOW'], + 'Steps.member.1.HadoopJarStep.Jar': ['streaming1.jar'], + 'Steps.member.2.Name': ['example2'], + 'Steps.member.2.ActionOnFailure': ['TERMINATE_JOB_FLOW'], + 'Steps.member.2.HadoopJarStep.Jar': ['streaming2.jar'], + } + + returns + [{ + 'name': u'example1', + 'action_on_failure': u'TERMINATE_JOB_FLOW', + 'hadoop_jar_step._jar': u'streaming1.jar', + }, { + 'name': u'example2', + 'action_on_failure': u'TERMINATE_JOB_FLOW', + 'hadoop_jar_step._jar': u'streaming2.jar', + }] + """ + results = [] + param_index = 1 + while True: + index_prefix = "{0}.{1}.".format(param_prefix, param_index) + new_items = {} + for key, value in self.querystring.items(): + if key.startswith(index_prefix): + new_items[camelcase_to_underscores(key.replace(index_prefix, ""))] = value[0] + if not new_items: + break + results.append(new_items) + param_index += 1 + return results + def metadata_response(request, full_url, headers): """ diff --git a/moto/ec2/responses/spot_instances.py b/moto/ec2/responses/spot_instances.py index 30f683e07..29e55792b 100644 --- a/moto/ec2/responses/spot_instances.py +++ b/moto/ec2/responses/spot_instances.py @@ -4,13 +4,6 @@ from moto.ec2.utils import filters_from_querystring class SpotInstances(BaseResponse): - def _get_param(self, param_name): - return self.querystring.get(param_name, [None])[0] - - def _get_int_param(self, param_name): - value = self._get_param(param_name) - if value is not None: - return int(value) def cancel_spot_instance_requests(self): request_ids = self._get_multi_param('SpotInstanceRequestId') diff --git a/moto/emr/responses.py b/moto/emr/responses.py index 44f2f0890..b8e6d2fb7 100644 --- a/moto/emr/responses.py +++ b/moto/emr/responses.py @@ -1,40 +1,11 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from moto.core.utils import camelcase_to_underscores from .models import emr_backend class ElasticMapReduceResponse(BaseResponse): - def _get_param(self, param_name): - return self.querystring.get(param_name, [None])[0] - - def _get_multi_param(self, param_prefix): - return [value[0] for key, value in self.querystring.items() if key.startswith(param_prefix)] - - def _get_dict_param(self, param_prefix): - params = {} - for key, value in self.querystring.items(): - if key.startswith(param_prefix): - params[camelcase_to_underscores(key.replace(param_prefix, ""))] = value[0] - return params - - def _get_list_prefix(self, param_prefix): - results = [] - param_index = 1 - while True: - index_prefix = "{0}.{1}.".format(param_prefix, param_index) - new_items = {} - for key, value in self.querystring.items(): - if key.startswith(index_prefix): - new_items[camelcase_to_underscores(key.replace(index_prefix, ""))] = value[0] - if not new_items: - break - results.append(new_items) - param_index += 1 - return results - def add_job_flow_steps(self): job_flow_id = self._get_param('JobFlowId') steps = self._get_list_prefix('Steps.member') diff --git a/moto/iam/responses.py b/moto/iam/responses.py index d84780267..e95494ce4 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -6,9 +6,6 @@ from .models import iam_backend class IamResponse(BaseResponse): - def _get_param(self, param_name): - return self.querystring.get(param_name, [None])[0] - def create_role(self): role_name = self._get_param('RoleName') path = self._get_param('Path')