Merge pull request #456 from achiku/master

Implement JSON responses for EMR (for Boto3)
This commit is contained in:
Steve Pulec 2015-11-28 08:48:53 -05:00
commit 4f172dcf41
4 changed files with 78 additions and 3 deletions

View File

@ -131,7 +131,7 @@ class BaseResponse(_TemplateEnvironmentMixin):
else:
self.region = self.default_region
self.headers = dict(request.headers)
self.headers = request.headers
self.response_headers = headers
return self.call_action()

View File

@ -1,4 +1,5 @@
from __future__ import unicode_literals
import json
from moto.core.responses import BaseResponse
from .models import emr_backends
@ -11,6 +12,10 @@ class ElasticMapReduceResponse(BaseResponse):
def backend(self):
return emr_backends[self.region]
@property
def boto3_request(self):
return 'json' in self.headers.get('Content-Type', [])
def add_job_flow_steps(self):
job_flow_id = self._get_param('JobFlowId')
steps = self._get_list_prefix('Steps.member')
@ -35,6 +40,11 @@ class ElasticMapReduceResponse(BaseResponse):
if instance_groups:
self.backend.add_instance_groups(job_flow.id, instance_groups)
if self.boto3_request:
return json.dumps({
"JobFlowId": job_flow.id
})
template = self.response_template(RUN_JOB_FLOW_TEMPLATE)
return template.render(job_flow=job_flow)
@ -79,6 +89,24 @@ class ElasticMapReduceResponse(BaseResponse):
def list_clusters(self):
clusters = self.backend.list_clusters()
if self.boto3_request:
return json.dumps({
"Clusters": [
{
"Id": cluster.id,
"Name": cluster.name,
"Status": {
"State": cluster.state,
"StatusChangeReason": {},
"TimeLine": {},
},
"NormalizedInstanceHours": cluster.normalized_instance_hours,
} for cluster in clusters
],
"Marker": ""
})
template = self.response_template(LIST_CLUSTERS_TEMPLATE)
return template.render(clusters=clusters)

View File

@ -39,8 +39,9 @@ class KmsResponse(BaseResponse):
try:
key = self.kms_backend.describe_key(key_id)
except KeyError:
self.headers['status'] = 404
return "{}", self.headers
headers = dict(self.headers)
headers['status'] = 404
return "{}", headers
return json.dumps(key.to_dict())
def list_keys(self):

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import boto3
import sure # noqa
from moto import mock_emr
@mock_emr
def test_run_job_flow():
client = boto3.client('emr', region_name='us-east-1')
cluster_id = client.run_job_flow(
Name='cluster',
Instances={
'MasterInstanceType': 'c3.xlarge',
'SlaveInstanceType': 'c3.xlarge',
'InstanceCount': 3,
'Placement': {'AvailabilityZone': 'us-east-1a'},
'KeepJobFlowAliveWhenNoSteps': True,
},
VisibleToAllUsers=True,
)
cluster_id.should.have.key('JobFlowId')
@mock_emr
def test_list_clusters():
client = boto3.client('emr', region_name='us-east-1')
client.run_job_flow(
Name='cluster',
Instances={
'MasterInstanceType': 'c3.xlarge',
'SlaveInstanceType': 'c3.xlarge',
'InstanceCount': 3,
'Placement': {'AvailabilityZone': 'us-east-1a'},
'KeepJobFlowAliveWhenNoSteps': True,
},
VisibleToAllUsers=True,
)
summary = client.list_clusters()
clusters = summary['Clusters']
clusters.should.have.length_of(1)
cluster = clusters[0]
cluster['NormalizedInstanceHours'].should.equal(0)
cluster['Status']['State'].should.equal("RUNNING")