Fix redshift responses to work with json or xml.

This commit is contained in:
Steve Pulec 2017-03-15 21:58:37 -04:00
parent 09a4d177f5
commit 3cdb4afad0
3 changed files with 38 additions and 14 deletions

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import json import json
import dicttoxml
from moto.core.responses import BaseResponse from moto.core.responses import BaseResponse
from .models import redshift_backends from .models import redshift_backends
@ -12,6 +13,13 @@ class RedshiftResponse(BaseResponse):
def redshift_backend(self): def redshift_backend(self):
return redshift_backends[self.region] 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): def create_cluster(self):
cluster_kwargs = { cluster_kwargs = {
"cluster_identifier": self._get_param('ClusterIdentifier'), "cluster_identifier": self._get_param('ClusterIdentifier'),
@ -37,7 +45,7 @@ class RedshiftResponse(BaseResponse):
} }
cluster = self.redshift_backend.create_cluster(**cluster_kwargs) cluster = self.redshift_backend.create_cluster(**cluster_kwargs)
return json.dumps({ return self.get_response({
"CreateClusterResponse": { "CreateClusterResponse": {
"CreateClusterResult": { "CreateClusterResult": {
"Cluster": cluster.to_json(), "Cluster": cluster.to_json(),
@ -52,7 +60,7 @@ class RedshiftResponse(BaseResponse):
cluster_identifier = self._get_param("ClusterIdentifier") cluster_identifier = self._get_param("ClusterIdentifier")
clusters = self.redshift_backend.describe_clusters(cluster_identifier) clusters = self.redshift_backend.describe_clusters(cluster_identifier)
return json.dumps({ return self.get_response({
"DescribeClustersResponse": { "DescribeClustersResponse": {
"DescribeClustersResult": { "DescribeClustersResult": {
"Clusters": [cluster.to_json() for cluster in clusters] "Clusters": [cluster.to_json() for cluster in clusters]
@ -84,7 +92,7 @@ class RedshiftResponse(BaseResponse):
} }
cluster = self.redshift_backend.modify_cluster(**cluster_kwargs) cluster = self.redshift_backend.modify_cluster(**cluster_kwargs)
return json.dumps({ return self.get_response({
"ModifyClusterResponse": { "ModifyClusterResponse": {
"ModifyClusterResult": { "ModifyClusterResult": {
"Cluster": cluster.to_json(), "Cluster": cluster.to_json(),
@ -99,7 +107,7 @@ class RedshiftResponse(BaseResponse):
cluster_identifier = self._get_param("ClusterIdentifier") cluster_identifier = self._get_param("ClusterIdentifier")
cluster = self.redshift_backend.delete_cluster(cluster_identifier) cluster = self.redshift_backend.delete_cluster(cluster_identifier)
return json.dumps({ return self.get_response({
"DeleteClusterResponse": { "DeleteClusterResponse": {
"DeleteClusterResult": { "DeleteClusterResult": {
"Cluster": cluster.to_json() "Cluster": cluster.to_json()
@ -121,7 +129,7 @@ class RedshiftResponse(BaseResponse):
subnet_ids=subnet_ids, subnet_ids=subnet_ids,
) )
return json.dumps({ return self.get_response({
"CreateClusterSubnetGroupResponse": { "CreateClusterSubnetGroupResponse": {
"CreateClusterSubnetGroupResult": { "CreateClusterSubnetGroupResult": {
"ClusterSubnetGroup": subnet_group.to_json(), "ClusterSubnetGroup": subnet_group.to_json(),
@ -137,7 +145,7 @@ class RedshiftResponse(BaseResponse):
subnet_groups = self.redshift_backend.describe_cluster_subnet_groups( subnet_groups = self.redshift_backend.describe_cluster_subnet_groups(
subnet_identifier) subnet_identifier)
return json.dumps({ return self.get_response({
"DescribeClusterSubnetGroupsResponse": { "DescribeClusterSubnetGroupsResponse": {
"DescribeClusterSubnetGroupsResult": { "DescribeClusterSubnetGroupsResult": {
"ClusterSubnetGroups": [subnet_group.to_json() for subnet_group in subnet_groups] "ClusterSubnetGroups": [subnet_group.to_json() for subnet_group in subnet_groups]
@ -152,7 +160,7 @@ class RedshiftResponse(BaseResponse):
subnet_identifier = self._get_param("ClusterSubnetGroupName") subnet_identifier = self._get_param("ClusterSubnetGroupName")
self.redshift_backend.delete_cluster_subnet_group(subnet_identifier) self.redshift_backend.delete_cluster_subnet_group(subnet_identifier)
return json.dumps({ return self.get_response({
"DeleteClusterSubnetGroupResponse": { "DeleteClusterSubnetGroupResponse": {
"ResponseMetadata": { "ResponseMetadata": {
"RequestId": "384ac68d-3775-11df-8963-01868b7c937a", "RequestId": "384ac68d-3775-11df-8963-01868b7c937a",
@ -170,7 +178,7 @@ class RedshiftResponse(BaseResponse):
description=description, description=description,
) )
return json.dumps({ return self.get_response({
"CreateClusterSecurityGroupResponse": { "CreateClusterSecurityGroupResponse": {
"CreateClusterSecurityGroupResult": { "CreateClusterSecurityGroupResult": {
"ClusterSecurityGroup": security_group.to_json(), "ClusterSecurityGroup": security_group.to_json(),
@ -187,7 +195,7 @@ class RedshiftResponse(BaseResponse):
security_groups = self.redshift_backend.describe_cluster_security_groups( security_groups = self.redshift_backend.describe_cluster_security_groups(
cluster_security_group_name) cluster_security_group_name)
return json.dumps({ return self.get_response({
"DescribeClusterSecurityGroupsResponse": { "DescribeClusterSecurityGroupsResponse": {
"DescribeClusterSecurityGroupsResult": { "DescribeClusterSecurityGroupsResult": {
"ClusterSecurityGroups": [security_group.to_json() for security_group in security_groups] "ClusterSecurityGroups": [security_group.to_json() for security_group in security_groups]
@ -203,7 +211,7 @@ class RedshiftResponse(BaseResponse):
self.redshift_backend.delete_cluster_security_group( self.redshift_backend.delete_cluster_security_group(
security_group_identifier) security_group_identifier)
return json.dumps({ return self.get_response({
"DeleteClusterSecurityGroupResponse": { "DeleteClusterSecurityGroupResponse": {
"ResponseMetadata": { "ResponseMetadata": {
"RequestId": "384ac68d-3775-11df-8963-01868b7c937a", "RequestId": "384ac68d-3775-11df-8963-01868b7c937a",
@ -222,7 +230,7 @@ class RedshiftResponse(BaseResponse):
description, description,
) )
return json.dumps({ return self.get_response({
"CreateClusterParameterGroupResponse": { "CreateClusterParameterGroupResponse": {
"CreateClusterParameterGroupResult": { "CreateClusterParameterGroupResult": {
"ClusterParameterGroup": parameter_group.to_json(), "ClusterParameterGroup": parameter_group.to_json(),
@ -238,7 +246,7 @@ class RedshiftResponse(BaseResponse):
parameter_groups = self.redshift_backend.describe_cluster_parameter_groups( parameter_groups = self.redshift_backend.describe_cluster_parameter_groups(
cluster_parameter_group_name) cluster_parameter_group_name)
return json.dumps({ return self.get_response({
"DescribeClusterParameterGroupsResponse": { "DescribeClusterParameterGroupsResponse": {
"DescribeClusterParameterGroupsResult": { "DescribeClusterParameterGroupsResult": {
"ParameterGroups": [parameter_group.to_json() for parameter_group in parameter_groups] "ParameterGroups": [parameter_group.to_json() for parameter_group in parameter_groups]
@ -254,7 +262,7 @@ class RedshiftResponse(BaseResponse):
self.redshift_backend.delete_cluster_parameter_group( self.redshift_backend.delete_cluster_parameter_group(
cluster_parameter_group_name) cluster_parameter_group_name)
return json.dumps({ return self.get_response({
"DeleteClusterParameterGroupResponse": { "DeleteClusterParameterGroupResponse": {
"ResponseMetadata": { "ResponseMetadata": {
"RequestId": "384ac68d-3775-11df-8963-01868b7c937a", "RequestId": "384ac68d-3775-11df-8963-01868b7c937a",

View File

@ -8,6 +8,7 @@ install_requires = [
"cookies", "cookies",
"requests>=2.0", "requests>=2.0",
"xmltodict", "xmltodict",
"dicttoxml",
"six", "six",
"werkzeug", "werkzeug",
"pytz", "pytz",

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import boto import boto
import boto3
from boto.redshift.exceptions import ( from boto.redshift.exceptions import (
ClusterNotFound, ClusterNotFound,
ClusterParameterGroupNotFound, ClusterParameterGroupNotFound,
@ -10,7 +11,21 @@ from boto.redshift.exceptions import (
) )
import sure # noqa 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 @mock_redshift_deprecated