Merge "Neutron LBaaS: Listeners Client"
This commit is contained in:
153
cloudcafe/networking/lbaas/lbaas_api/clients/listener/client.py
Normal file
153
cloudcafe/networking/lbaas/lbaas_api/clients/listener/client.py
Normal file
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
|
||||
from cloudcafe.networking.lbaas.common.client import BaseLoadBalancersClient
|
||||
from cloudcafe.networking.lbaas.lbaas_api.models.request.listener import \
|
||||
CreateListener, UpdateListener
|
||||
from cloudcafe.networking.lbaas.lbaas_api.models.response.listener import \
|
||||
Listener, Listeners
|
||||
|
||||
|
||||
class ListenersClient(BaseLoadBalancersClient):
|
||||
"""
|
||||
Listeners Client
|
||||
|
||||
@summary: Listeners represent a single listening port and can optionally
|
||||
provide TLS termination.
|
||||
"""
|
||||
_LISTENERS_URL = "{base_url}/listeners"
|
||||
_LISTENER_URL = "{base_url}/listeners/{listener_id}"
|
||||
|
||||
def create_listener(self, name, load_balancer_id, tenant_id,
|
||||
default_pool_id, protocol, protocol_port,
|
||||
description=None, connection_limit=None,
|
||||
admin_state_up=None, requestslib_kwargs=None):
|
||||
"""Create Listener
|
||||
@summary: Creates an instance of a listener given the
|
||||
provided parameters
|
||||
@param name: Name of the listener that will be created
|
||||
@type name: String
|
||||
@param load_balancer_id: ID of a load balancer.
|
||||
@type load_balancer_id: String
|
||||
@param tenant_id: Tenant that will own the listener.
|
||||
@type tenant_id: String
|
||||
@param default_pool_id: ID of default pool. Must have compatible
|
||||
protocol with listener.
|
||||
@type default_pool_id: String
|
||||
@param protocol: Protocol to load balance: HTTP, HTTPS, TCP, UDP
|
||||
@type protocol: String
|
||||
@param protocol_port: TCP (or UDP) port to listen on.
|
||||
@type protocol_port: Integer
|
||||
@param description: Detailed description of the listener.
|
||||
@type description: String
|
||||
|
||||
@param connection_limit: Maximum connections the load balancer can
|
||||
have. Default is infinite.
|
||||
@type connection_limit: Integer
|
||||
@param admin_state_up: If set to false, listener will be created in an
|
||||
administratively down state
|
||||
@type admin_state_up: Boolean
|
||||
@return: Response Object containing response code and the
|
||||
listener domain object
|
||||
@rtype: Requests.response
|
||||
"""
|
||||
full_url = self._LISTENERS_URL.format(base_url=self.url)
|
||||
listener_request_object = CreateListener(
|
||||
name=name, load_balancer_id=load_balancer_id, tenant_id=tenant_id,
|
||||
default_pool_id=default_pool_id, protocol=protocol,
|
||||
protocol_port=protocol_port, description=description,
|
||||
connection_limit=connection_limit, admin_state_up=admin_state_up)
|
||||
return self.request('POST', full_url,
|
||||
response_entity_type=Listener,
|
||||
request_entity=listener_request_object,
|
||||
requestslib_kwargs=requestslib_kwargs)
|
||||
|
||||
def list_listeners(self, requestslib_kwargs=None):
|
||||
"""List Listeners
|
||||
@summary: List all listeners configured for the account.
|
||||
@rtype: Requests.response
|
||||
@note: This operation does not require a request body.
|
||||
"""
|
||||
full_url = self._LISTENERS_URL.format(base_url=self.url)
|
||||
return self.request('GET', full_url,
|
||||
response_entity_type=Listeners,
|
||||
requestslib_kwargs=requestslib_kwargs)
|
||||
|
||||
def update_listener(self, listener_id, name=None, description=None,
|
||||
default_pool_id=None, load_balancer_id=None,
|
||||
admin_state_up=None, requestslib_kwargs=None):
|
||||
"""Update Listener
|
||||
@summary: Update the properties of a listener given the
|
||||
provided parameters
|
||||
@param listener_id: ID of the listener to get details from.
|
||||
@type listener_id: str
|
||||
@param name: Name of the listener that will be created
|
||||
@type name: String
|
||||
@param description: Detailed description of the listener.
|
||||
@type description: String
|
||||
@param default_pool_id: ID of default pool. Must have compatible
|
||||
protocol with listener.
|
||||
@type default_pool_id: String
|
||||
@param load_balancer_id: ID of a load balancer.
|
||||
@type load_balancer_id: String
|
||||
@param admin_state_up: If set to false, listener will be created in an
|
||||
administratively down state
|
||||
@type admin_state_up: Boolean
|
||||
@return: Response Object containing response code.
|
||||
@rtype: Requests.response
|
||||
"""
|
||||
update_listener = UpdateListener(
|
||||
name=name, description=description,
|
||||
default_pool_id=default_pool_id,
|
||||
load_balancer_id=load_balancer_id,
|
||||
admin_state_up=admin_state_up)
|
||||
full_url = self._LISTENER_URL.format(base_url=self.url,
|
||||
listener_id=listener_id)
|
||||
return self.request('PUT', full_url,
|
||||
request_entity=update_listener,
|
||||
response_entity_type=Listener,
|
||||
requestslib_kwargs=requestslib_kwargs)
|
||||
|
||||
def get_listener(self, listener_id, requestslib_kwargs=None):
|
||||
"""Get Listener Details
|
||||
@summary: List details of the specified listener.
|
||||
@param listener_id: ID of the listener to get details from.
|
||||
@type listener_id: str
|
||||
@return: Response Object containing response code and the
|
||||
listener domain object.
|
||||
@rtype: Requests.response
|
||||
@note: This operation does not require a request body.
|
||||
"""
|
||||
full_url = self._LISTENER_URL.format(base_url=self.url,
|
||||
listener_id=listener_id)
|
||||
return self.request('GET', full_url,
|
||||
response_entity_type=Listener,
|
||||
requestslib_kwargs=requestslib_kwargs)
|
||||
|
||||
def delete_listener(self, listener_id, requestslib_kwargs=None):
|
||||
"""Delete Listener
|
||||
@summary: Remove a listener from the account.
|
||||
@param listener_id: ID of the listener to delete.
|
||||
@type listener_id: str
|
||||
@return: Response Object containing response code.
|
||||
@rtype: Requests.response
|
||||
@note: Returns an error if it's still in use by any pools.
|
||||
"""
|
||||
full_url = self._LISTENER_URL.format(
|
||||
base_url=self.url,
|
||||
listener_id=listener_id)
|
||||
return self.request('DELETE', full_url,
|
||||
requestslib_kwargs=requestslib_kwargs)
|
||||
156
metatests/networking/lbaas/lbaas_api/clients/listener/client.py
Normal file
156
metatests/networking/lbaas/lbaas_api/clients/listener/client.py
Normal file
@@ -0,0 +1,156 @@
|
||||
"""
|
||||
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 mock
|
||||
import unittest
|
||||
|
||||
from cloudcafe.networking.lbaas.lbaas_api.clients.listener.client import \
|
||||
ListenersClient
|
||||
from cloudcafe.networking.lbaas.lbaas_api.models.request.listener import \
|
||||
CreateListener, UpdateListener
|
||||
from cloudcafe.networking.lbaas.lbaas_api.models.response.listener import \
|
||||
Listener, Listeners
|
||||
|
||||
|
||||
class ListenersClientFixture(unittest.TestCase):
|
||||
"""
|
||||
@summary: Listener Client Tests
|
||||
"""
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(ListenersClientFixture, cls).setUpClass()
|
||||
|
||||
cls.auth_token = "fake_auth_token"
|
||||
cls.url = "http://fake.url.endpoint"
|
||||
cls.listener_id = "12345"
|
||||
cls.name = "Example HTTPS Listener"
|
||||
cls.load_balancer_id = "b8a35470-f65d-11e3-a3ac-0800200c9a66"
|
||||
cls.tenant_id = "352686b7-c4b2-44ec-a458-84239713f685"
|
||||
cls.default_pool_id = "8311446e-8a13-4c00-95b3-03a92f9759c7"
|
||||
cls.protocol = "https"
|
||||
cls.protocol_port = 443
|
||||
cls.description = "A very simple example of an HTTPS listener."
|
||||
cls.connection_limit = 200
|
||||
cls.admin_state_up = True
|
||||
|
||||
cls.full_url_listeners = (
|
||||
ListenersClient._LISTENERS_URL.format(
|
||||
base_url=cls.url))
|
||||
cls.full_url_listener = (
|
||||
ListenersClient._LISTENER_URL.format(
|
||||
base_url=cls.url,
|
||||
listener_id=cls.listener_id))
|
||||
|
||||
cls.listeners_client = ListenersClient(
|
||||
url=cls.url,
|
||||
auth_token=cls.auth_token,
|
||||
serialize_format=cls.SERIALIZE,
|
||||
deserialize_format=cls.DESERIALIZE)
|
||||
|
||||
|
||||
class ListenersClientTests(object):
|
||||
|
||||
@mock.patch.object(ListenersClient, 'request', autospec=True)
|
||||
def test_create_listener(self, mock_request):
|
||||
|
||||
create_listener_kwargs = (
|
||||
{'name': self.name,
|
||||
'load_balancer_id': self.load_balancer_id,
|
||||
'tenant_id': self.tenant_id,
|
||||
'default_pool_id': self.default_pool_id,
|
||||
'protocol': self.protocol,
|
||||
'protocol_port': self.protocol_port,
|
||||
'description': self.description,
|
||||
'connection_limit': self.connection_limit,
|
||||
'admin_state_up': self.admin_state_up})
|
||||
self.listeners_client.create_listener(
|
||||
**create_listener_kwargs)
|
||||
create_listener_request = CreateListener(
|
||||
**create_listener_kwargs)
|
||||
mock_request.assert_called_once_with(
|
||||
self.listeners_client,
|
||||
'POST',
|
||||
self.full_url_listeners,
|
||||
request_entity=create_listener_request,
|
||||
response_entity_type=Listener,
|
||||
requestslib_kwargs=None)
|
||||
|
||||
@mock.patch.object(ListenersClient, 'request', autospec=True)
|
||||
def test_list_listener(self, mock_request):
|
||||
|
||||
self.listeners_client.list_listeners()
|
||||
mock_request.assert_called_once_with(
|
||||
self.listeners_client,
|
||||
'GET',
|
||||
self.full_url_listeners,
|
||||
response_entity_type=Listeners,
|
||||
requestslib_kwargs=None)
|
||||
|
||||
@mock.patch.object(ListenersClient, 'request', autospec=True)
|
||||
def test_get_listener(self, mock_request):
|
||||
|
||||
self.listeners_client.get_listener(
|
||||
listener_id=self.listener_id)
|
||||
mock_request.assert_called_once_with(
|
||||
self.listeners_client,
|
||||
'GET',
|
||||
self.full_url_listener,
|
||||
response_entity_type=Listener,
|
||||
requestslib_kwargs=None)
|
||||
|
||||
@mock.patch.object(ListenersClient, 'request', autospec=True)
|
||||
def test_update_listener(self, mock_request):
|
||||
|
||||
update_listener_kwargs = (
|
||||
{'name': self.name,
|
||||
'description': self.description,
|
||||
'load_balancer_id': self.load_balancer_id,
|
||||
'default_pool_id': self.default_pool_id,
|
||||
'admin_state_up': self.admin_state_up})
|
||||
self.listeners_client.update_listener(
|
||||
listener_id=self.listener_id,
|
||||
**update_listener_kwargs)
|
||||
update_listener_request = UpdateListener(
|
||||
**update_listener_kwargs)
|
||||
mock_request.assert_called_once_with(
|
||||
self.listeners_client,
|
||||
'PUT',
|
||||
self.full_url_listener,
|
||||
request_entity=update_listener_request,
|
||||
response_entity_type=Listener,
|
||||
requestslib_kwargs=None)
|
||||
|
||||
@mock.patch.object(ListenersClient, 'request', autospec=True)
|
||||
def test_delete_listener(self, mock_request):
|
||||
|
||||
self.listeners_client.delete_listener(
|
||||
listener_id=self.listener_id)
|
||||
mock_request.assert_called_once_with(self.listeners_client,
|
||||
'DELETE',
|
||||
self.full_url_listener,
|
||||
requestslib_kwargs=None)
|
||||
|
||||
|
||||
class ListenersClientTestsXML(ListenersClientFixture,
|
||||
ListenersClientTests):
|
||||
SERIALIZE = 'xml'
|
||||
DESERIALIZE = 'xml'
|
||||
|
||||
|
||||
class ListenersClientTestsJSON(ListenersClientFixture,
|
||||
ListenersClientTests):
|
||||
SERIALIZE = 'json'
|
||||
DESERIALIZE = 'json'
|
||||
Reference in New Issue
Block a user