Neutron_LBaaS Session Persistence Request Model

* request model used in setting request model
* also included are associated metatests for de/serialization

Change-Id: I4afb1cded969d27b758b5611a4113055fe813257
This commit is contained in:
Franklin Naval
2014-07-23 17:37:59 -05:00
parent f4fa0517f6
commit 812d7f4ad9
2 changed files with 150 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
"""
Copyright 2014 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 json
import xml.etree.ElementTree as ET
from cafe.engine.models.base import AutoMarshallingModel
from cloudcafe.networking.lbaas.common.constants import Constants
class SetSessionPersistence(AutoMarshallingModel):
""" Set Session Persistence Request Model
@summary: An object that represents the the request data of a
Session Persistence. This is used with Pools.
json ex:
{
"sessionPersistence": {
"type": "COOKIE",
"cookie_name": "session_persistence_cookie",
}
}
xml ex:
<sessionPersistence xmlns=""
type="COOKIE"
cookie_name="session_persistence_cookie"
</sessionPersistence>
"""
ROOT_TAG = 'session_persistence'
def __init__(self, type_=None, cookie_name=None):
"""
@summary: Set Session Persistence Request Object Model
@param type_: Type of session persistence to set.
@type type_: String
@param cookie_name: Name of cookie to set.
@type cookie_name: String
@return: Set Session Persistence Request Object
@rtype: SetSessionPersistence
"""
self.type_ = type_
self.cookie_name = cookie_name
self.attr_dict = {
'type': self.type_,
'cookie_name': self.cookie_name
}
def _obj_to_json(self):
body = self._remove_empty_values(self.attr_dict)
return json.dumps({self.ROOT_TAG: body})
def _obj_to_xml(self):
xml = Constants.XML_HEADER
element = ET.Element(self.ROOT_TAG)
element.set('xmlns', Constants.XML_API_NAMESPACE)
element = self._set_xml_etree_element(element, self.attr_dict)
xml = "{0}{1}".format(xml, ET.tostring(element))
return xml

View File

@@ -0,0 +1,76 @@
"""
Copyright 2014 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.
@summary: Unit tests for the load balancer "session persistence" request model.
SetSessionPersistenceRequestTest
"""
import json
import unittest
from cloudcafe.networking.lbaas.common.constants import Constants
from cloudcafe.networking.lbaas.lbaas_api.models.request.session_persistence \
import SetSessionPersistence
class SessionPersistenceRequestTestBase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.XML_HEADER = Constants.XML_HEADER
cls.XML_NS = Constants.XML_API_NAMESPACE
cls.ROOT_TAG = SetSessionPersistence.ROOT_TAG
cls.type_ = "COOKIE"
cls.cookie_name = "session_persistence_cookie"
cls.set_session_persistence_obj = SetSessionPersistence(
type_=cls.type_,
cookie_name=cls.cookie_name
)
class SetSessionPersistenceRequestTest(SessionPersistenceRequestTestBase):
def test_set_session_persistence_json(self):
actual_json = json.loads(
self.set_session_persistence_obj.serialize('json'))
expected_json = json.loads(
"""
{{
"{root_tag}":
{{
"type": "{type_}",
"cookie_name": "{cookie_name}"
}}
}}
""".format(root_tag=self.ROOT_TAG,
type_=self.type_,
cookie_name=self.cookie_name))
self.assertEqual(expected_json, actual_json)
def test_set_session_persistence_xml(self):
actual_xml = self.set_session_persistence_obj.serialize('xml')
expected_xml = (
'{xml_header}<{root_tag} '
'cookie_name="{cookie_name}" '
'type="{type_}" xmlns="{xmlns}" />').format(
xml_header=self.XML_HEADER,
xmlns=self.XML_NS,
root_tag=self.ROOT_TAG,
type_=self.type_, cookie_name=self.cookie_name)
self.assertEqual(expected_xml, actual_xml)
if __name__ == "__main__":
unittest.main()