Skip to content

Commit

Permalink
Fix redshift responses to work with json or xml.
Browse files Browse the repository at this point in the history
  • Loading branch information
spulec committed Mar 16, 2017
1 parent 09a4d17 commit 3cdb4af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
34 changes: 21 additions & 13 deletions moto/redshift/responses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

import json
import dicttoxml

from moto.core.responses import BaseResponse
from .models import redshift_backends
Expand All @@ -12,6 +13,13 @@ class RedshiftResponse(BaseResponse):
def redshift_backend(self):
return redshift_backends[self.region]

def get_response(self, response):
if self.request_json:
return json.dumps(response)
else:
xml = dicttoxml.dicttoxml(response, attr_type=False, root=False)
return xml

def create_cluster(self):
cluster_kwargs = {
"cluster_identifier": self._get_param('ClusterIdentifier'),
Expand All @@ -37,7 +45,7 @@ def create_cluster(self):
}
cluster = self.redshift_backend.create_cluster(**cluster_kwargs)

return json.dumps({
return self.get_response({
"CreateClusterResponse": {
"CreateClusterResult": {
"Cluster": cluster.to_json(),
Expand All @@ -52,7 +60,7 @@ def describe_clusters(self):
cluster_identifier = self._get_param("ClusterIdentifier")
clusters = self.redshift_backend.describe_clusters(cluster_identifier)

return json.dumps({
return self.get_response({
"DescribeClustersResponse": {
"DescribeClustersResult": {
"Clusters": [cluster.to_json() for cluster in clusters]
Expand Down Expand Up @@ -84,7 +92,7 @@ def modify_cluster(self):
}
cluster = self.redshift_backend.modify_cluster(**cluster_kwargs)

return json.dumps({
return self.get_response({
"ModifyClusterResponse": {
"ModifyClusterResult": {
"Cluster": cluster.to_json(),
Expand All @@ -99,7 +107,7 @@ def delete_cluster(self):
cluster_identifier = self._get_param("ClusterIdentifier")
cluster = self.redshift_backend.delete_cluster(cluster_identifier)

return json.dumps({
return self.get_response({
"DeleteClusterResponse": {
"DeleteClusterResult": {
"Cluster": cluster.to_json()
Expand All @@ -121,7 +129,7 @@ def create_cluster_subnet_group(self):
subnet_ids=subnet_ids,
)

return json.dumps({
return self.get_response({
"CreateClusterSubnetGroupResponse": {
"CreateClusterSubnetGroupResult": {
"ClusterSubnetGroup": subnet_group.to_json(),
Expand All @@ -137,7 +145,7 @@ def describe_cluster_subnet_groups(self):
subnet_groups = self.redshift_backend.describe_cluster_subnet_groups(
subnet_identifier)

return json.dumps({
return self.get_response({
"DescribeClusterSubnetGroupsResponse": {
"DescribeClusterSubnetGroupsResult": {
"ClusterSubnetGroups": [subnet_group.to_json() for subnet_group in subnet_groups]
Expand All @@ -152,7 +160,7 @@ def delete_cluster_subnet_group(self):
subnet_identifier = self._get_param("ClusterSubnetGroupName")
self.redshift_backend.delete_cluster_subnet_group(subnet_identifier)

return json.dumps({
return self.get_response({
"DeleteClusterSubnetGroupResponse": {
"ResponseMetadata": {
"RequestId": "384ac68d-3775-11df-8963-01868b7c937a",
Expand All @@ -170,7 +178,7 @@ def create_cluster_security_group(self):
description=description,
)

return json.dumps({
return self.get_response({
"CreateClusterSecurityGroupResponse": {
"CreateClusterSecurityGroupResult": {
"ClusterSecurityGroup": security_group.to_json(),
Expand All @@ -187,7 +195,7 @@ def describe_cluster_security_groups(self):
security_groups = self.redshift_backend.describe_cluster_security_groups(
cluster_security_group_name)

return json.dumps({
return self.get_response({
"DescribeClusterSecurityGroupsResponse": {
"DescribeClusterSecurityGroupsResult": {
"ClusterSecurityGroups": [security_group.to_json() for security_group in security_groups]
Expand All @@ -203,7 +211,7 @@ def delete_cluster_security_group(self):
self.redshift_backend.delete_cluster_security_group(
security_group_identifier)

return json.dumps({
return self.get_response({
"DeleteClusterSecurityGroupResponse": {
"ResponseMetadata": {
"RequestId": "384ac68d-3775-11df-8963-01868b7c937a",
Expand All @@ -222,7 +230,7 @@ def create_cluster_parameter_group(self):
description,
)

return json.dumps({
return self.get_response({
"CreateClusterParameterGroupResponse": {
"CreateClusterParameterGroupResult": {
"ClusterParameterGroup": parameter_group.to_json(),
Expand All @@ -238,7 +246,7 @@ def describe_cluster_parameter_groups(self):
parameter_groups = self.redshift_backend.describe_cluster_parameter_groups(
cluster_parameter_group_name)

return json.dumps({
return self.get_response({
"DescribeClusterParameterGroupsResponse": {
"DescribeClusterParameterGroupsResult": {
"ParameterGroups": [parameter_group.to_json() for parameter_group in parameter_groups]
Expand All @@ -254,7 +262,7 @@ def delete_cluster_parameter_group(self):
self.redshift_backend.delete_cluster_parameter_group(
cluster_parameter_group_name)

return json.dumps({
return self.get_response({
"DeleteClusterParameterGroupResponse": {
"ResponseMetadata": {
"RequestId": "384ac68d-3775-11df-8963-01868b7c937a",
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"cookies",
"requests>=2.0",
"xmltodict",
"dicttoxml",
"six",
"werkzeug",
"pytz",
Expand Down
17 changes: 16 additions & 1 deletion tests/test_redshift/test_redshift.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

import boto
import boto3
from boto.redshift.exceptions import (
ClusterNotFound,
ClusterParameterGroupNotFound,
Expand All @@ -10,7 +11,21 @@
)
import sure # noqa

from moto import mock_ec2_deprecated, mock_redshift_deprecated
from moto import mock_ec2_deprecated, mock_redshift_deprecated, mock_redshift


@mock_redshift
def test_create_cluster_boto3():
client = boto3.client('redshift', region_name='us-east-1')
response = client.create_cluster(
DBName='test',
ClusterIdentifier='test',
ClusterType='single-node',
NodeType='ds2.xlarge',
MasterUsername='user',
MasterUserPassword='password',
)
response['Cluster']['NodeType'].should.equal('ds2.xlarge')


@mock_redshift_deprecated
Expand Down

0 comments on commit 3cdb4af

Please sign in to comment.