Cleanup parameter parsing.

This commit is contained in:
Steve Pulec 2014-12-31 13:23:08 -05:00
parent fceeed0c9f
commit 56007660d8
6 changed files with 64 additions and 85 deletions

View File

@ -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':

View File

@ -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,

View File

@ -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):
"""

View File

@ -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')

View File

@ -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')

View File

@ -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')