Merge "Refactored rescue models up to meeting coding standards"

This commit is contained in:
Jenkins 2013-08-26 18:20:19 +00:00 committed by Gerrit Code Review
commit 9498801575
5 changed files with 83 additions and 17 deletions

View File

@ -22,30 +22,26 @@ from cloudcafe.compute.common.constants import Constants
class RescueMode(AutoMarshallingModel):
ROOT_TAG = 'rescue'
def _obj_to_json(self):
ret = self._auto_to_dict()
return json.dumps(ret)
return json.dumps({'rescue': {}})
def _obj_to_xml(self):
xml = Constants.XML_HEADER
element = ET.Element(self.ROOT_TAG)
element = ET.Element('rescue')
element.set('xmlns', Constants.XML_API_RESCUE)
xml += ET.tostring(element)
return xml
class ExitRescueMode(AutoMarshallingModel):
ROOT_TAG = 'unrescue'
def _obj_to_json(self):
ret = self._auto_to_dict()
return json.dumps(ret)
return json.dumps({'unrescue': {}})
def _obj_to_xml(self):
xml = Constants.XML_HEADER
element = ET.Element(self.ROOT_TAG)
element = ET.Element('unrescue')
element.set('xmlns', Constants.XML_API_UNRESCUE)
xml += ET.tostring(element)
return xml

View File

@ -15,13 +15,16 @@ limitations under the License.
"""
import json
import xml.etree.ElementTree as ET
from cafe.engine.models.base import AutoMarshallingModel
from cloudcafe.compute.common.constants import Constants
class RescueResponse(AutoMarshallingModel):
def __init__(self, admin_pass):
super(RescueResponse, self).__init__()
self.admin_pass = admin_pass
def __repr__(self):
@ -33,16 +36,12 @@ class RescueResponse(AutoMarshallingModel):
@classmethod
def _json_to_obj(cls, serialized_str):
json_dict = json.loads(serialized_str)
return cls._dict_to_obj(json_dict)
@classmethod
def _dict_to_obj(cls, json_dict):
return RescueResponse(json_dict.get('adminPass'))
@classmethod
def _xml_to_obj(cls, serialized_str):
raise NotImplemented
@classmethod
def _xml_ele_to_obj(cls, xml_ele):
raise NotImplemented
element = ET.fromstring(serialized_str)
cls._remove_xml_etree_namespace(element, Constants.XML_API_NAMESPACE)
cls._remove_xml_etree_namespace(element,
Constants.XML_API_ATOM_NAMESPACE)
return RescueResponse(element.text)

View File

@ -0,0 +1,15 @@
"""
Copyright 2013 Rackspace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

View File

@ -0,0 +1,15 @@
"""
Copyright 2013 Rackspace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

View File

@ -0,0 +1,41 @@
"""
Copyright 2013 Rackspace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import unittest2 as unittest
from cloudcafe.compute.extensions.rescue_api.models.responses \
import RescueResponse
class RescueResponseModelTest(object):
def test_admin_password(self):
self.assertEqual(self.rescue.admin_pass, 'MySecretPass')
class RescueResponseXMLModelTest(unittest.TestCase, RescueResponseModelTest):
@classmethod
def setUpClass(cls):
cls.rescue_xml = '<adminPass>MySecretPass</adminPass>'
cls.rescue = RescueResponse.deserialize(cls.rescue_xml, 'xml')
class RescueResponseJSONModelTest(unittest.TestCase, RescueResponseModelTest):
@classmethod
def setUpClass(cls):
cls.rescue_json = '{"adminPass": "MySecretPass"}'
cls.rescue = RescueResponse.deserialize(cls.rescue_json, 'json')