From 41d6d33339d430a5ed033837dd844a36d251129c Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Wed, 30 Jan 2019 08:45:49 -0800 Subject: [PATCH] Add Octavia (load_balancer) load balancer failover This patch adds a Octavia (load_balancer) load balancer failover method. It also corrects the resource documentation for the Octavia stats resource classes. Depends-On: https://review.openstack.org/634344 Change-Id: I7384351a5b59b2d68d6a45c543449815adfe52f2 --- doc/source/user/proxies/load_balancer_v2.rst | 1 + .../resources/load_balancer/v2/listener.rst | 9 +++++ .../load_balancer/v2/load_balancer.rst | 18 +++++++++ openstack/load_balancer/v2/_proxy.py | 9 +++++ openstack/load_balancer/v2/load_balancer.py | 37 ++++++++++++++++++- .../load_balancer/v2/test_load_balancer.py | 7 ++++ .../unit/load_balancer/test_load_balancer.py | 13 +++++++ .../tests/unit/load_balancer/test_proxy.py | 7 ++++ ...-octavia-lb-failover-9a34c9577d78ad34.yaml | 4 ++ 9 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/add-octavia-lb-failover-9a34c9577d78ad34.yaml diff --git a/doc/source/user/proxies/load_balancer_v2.rst b/doc/source/user/proxies/load_balancer_v2.rst index 520274948..d60756010 100644 --- a/doc/source/user/proxies/load_balancer_v2.rst +++ b/doc/source/user/proxies/load_balancer_v2.rst @@ -22,6 +22,7 @@ Load Balancer Operations .. automethod:: openstack.load_balancer.v2._proxy.Proxy.get_load_balancer_statistics .. automethod:: openstack.load_balancer.v2._proxy.Proxy.load_balancers .. automethod:: openstack.load_balancer.v2._proxy.Proxy.update_load_balancer + .. automethod:: openstack.load_balancer.v2._proxy.Proxy.failover_load_balancer Listener Operations ^^^^^^^^^^^^^^^^^^^ diff --git a/doc/source/user/resources/load_balancer/v2/listener.rst b/doc/source/user/resources/load_balancer/v2/listener.rst index 099caf7d0..b94bc73b2 100644 --- a/doc/source/user/resources/load_balancer/v2/listener.rst +++ b/doc/source/user/resources/load_balancer/v2/listener.rst @@ -10,3 +10,12 @@ The ``Listener`` class inherits from :class:`~openstack.resource.Resource`. .. autoclass:: openstack.load_balancer.v2.listener.Listener :members: + +The ListenerStats Class +----------------------- + +The ``ListenerStats`` class inherits from +:class:`~openstack.resource.Resource`. + +.. autoclass:: openstack.load_balancer.v2.listener.ListenerStats + :members: diff --git a/doc/source/user/resources/load_balancer/v2/load_balancer.rst b/doc/source/user/resources/load_balancer/v2/load_balancer.rst index 0622c7cb4..9e1456029 100644 --- a/doc/source/user/resources/load_balancer/v2/load_balancer.rst +++ b/doc/source/user/resources/load_balancer/v2/load_balancer.rst @@ -10,3 +10,21 @@ The ``LoadBalancer`` class inherits from :class:`~openstack.resource.Resource`. .. autoclass:: openstack.load_balancer.v2.load_balancer.LoadBalancer :members: + +The LoadBalancerStats Class +--------------------------- + +The ``LoadBalancerStats`` class inherits from +:class:`~openstack.resource.Resource`. + +.. autoclass:: openstack.load_balancer.v2.load_balancer.LoadBalancerStats + :members: + +The LoadBalancerFailover Class +------------------------------ + +The ``LoadBalancerFailover`` class inherits from +:class:`~openstack.resource.Resource`. + +.. autoclass:: openstack.load_balancer.v2.load_balancer.LoadBalancerFailover + :members: diff --git a/openstack/load_balancer/v2/_proxy.py b/openstack/load_balancer/v2/_proxy.py index bc0fb9034..db4b74675 100644 --- a/openstack/load_balancer/v2/_proxy.py +++ b/openstack/load_balancer/v2/_proxy.py @@ -126,6 +126,15 @@ class Proxy(proxy.Proxy): return resource.wait_for_status(self, lb, status, failures, interval, wait, attribute='provisioning_status') + def failover_load_balancer(self, name_or_id, **attrs): + """Failover a load balancer + + :param name_or_id: The name or ID of a load balancer + + :returns: ``None`` + """ + return self._update(_lb.LoadBalancerFailover, lb_id=name_or_id) + def create_listener(self, **attrs): """Create a new listener from attributes diff --git a/openstack/load_balancer/v2/load_balancer.py b/openstack/load_balancer/v2/load_balancer.py index 9d7cc407c..77f8cb61a 100644 --- a/openstack/load_balancer/v2/load_balancer.py +++ b/openstack/load_balancer/v2/load_balancer.py @@ -33,7 +33,7 @@ class LoadBalancer(resource.Resource, resource.TagMixin): **resource.TagMixin._tag_query_parameters ) - #: Properties + # Properties #: The administrative state of the load balancer *Type: bool* is_admin_state_up = resource.Body('admin_state_up', type=bool) #: Timestamp when the load balancer was created @@ -113,3 +113,38 @@ class LoadBalancerStats(resource.Resource): request_errors = resource.Body('request_errors', type=int) #: The total connections handled. total_connections = resource.Body('total_connections', type=int) + + +class LoadBalancerFailover(resource.Resource): + base_path = '/lbaas/loadbalancers/%(lb_id)s/failover' + + # capabilities + allow_create = False + allow_fetch = False + allow_commit = True + allow_delete = False + allow_list = False + + requires_id = False + + # Properties + #: The ID of the load balancer. + lb_id = resource.URI('lb_id') + + # The parent commit method assumes there is a header or body change, + # which we do not have here. The default _update code path also has no + # way to pass has_body into this function, so overriding the method here. + def commit(self, session, base_path=None): + kwargs = {} + request = self._prepare_request(prepend_key=False, + base_path=base_path, + **kwargs) + session = self._get_session(session) + kwargs = {} + microversion = self._get_microversion_for(session, 'commit') + response = session.put(request.url, json=request.body, + headers=request.headers, + microversion=microversion, **kwargs) + self.microversion = microversion + self._translate_response(response, has_body=False) + return self diff --git a/openstack/tests/functional/load_balancer/v2/test_load_balancer.py b/openstack/tests/functional/load_balancer/v2/test_load_balancer.py index 9a12c564b..a06b8e820 100644 --- a/openstack/tests/functional/load_balancer/v2/test_load_balancer.py +++ b/openstack/tests/functional/load_balancer/v2/test_load_balancer.py @@ -228,6 +228,13 @@ class TestLoadBalancer(base.BaseFunctionalTest): test_lb = self.conn.load_balancer.get_load_balancer(self.LB_ID) self.assertEqual(self.LB_NAME, test_lb.name) + def test_lb_failover(self): + self.conn.load_balancer.failover_load_balancer(self.LB_ID) + self.conn.load_balancer.wait_for_load_balancer( + self.LB_ID, wait=self._wait_for_timeout) + test_lb = self.conn.load_balancer.get_load_balancer(self.LB_ID) + self.assertEqual(self.LB_NAME, test_lb.name) + def test_listener_find(self): test_listener = self.conn.load_balancer.find_listener( self.LISTENER_NAME) diff --git a/openstack/tests/unit/load_balancer/test_load_balancer.py b/openstack/tests/unit/load_balancer/test_load_balancer.py index adab6706d..ef0c44f60 100644 --- a/openstack/tests/unit/load_balancer/test_load_balancer.py +++ b/openstack/tests/unit/load_balancer/test_load_balancer.py @@ -187,3 +187,16 @@ class TestLoadBalancerStats(base.TestCase): test_load_balancer.request_errors) self.assertEqual(EXAMPLE_STATS['total_connections'], test_load_balancer.total_connections) + + +class TestLoadBalancerFailover(base.TestCase): + + def test_basic(self): + test_load_balancer = load_balancer.LoadBalancerFailover() + self.assertEqual('/lbaas/loadbalancers/%(lb_id)s/failover', + test_load_balancer.base_path) + self.assertFalse(test_load_balancer.allow_create) + self.assertFalse(test_load_balancer.allow_fetch) + self.assertFalse(test_load_balancer.allow_delete) + self.assertFalse(test_load_balancer.allow_list) + self.assertTrue(test_load_balancer.allow_commit) diff --git a/openstack/tests/unit/load_balancer/test_proxy.py b/openstack/tests/unit/load_balancer/test_proxy.py index a7bd1f838..222645b27 100644 --- a/openstack/tests/unit/load_balancer/test_proxy.py +++ b/openstack/tests/unit/load_balancer/test_proxy.py @@ -97,6 +97,13 @@ class TestLoadBalancerProxy(test_proxy_base.TestProxyBase): self.verify_update(self.proxy.update_load_balancer, lb.LoadBalancer) + def test_load_balancer_failover(self): + self.verify_update(self.proxy.failover_load_balancer, + lb.LoadBalancerFailover, + value=[self.LB_ID], + expected_args=[], + expected_kwargs={'lb_id': self.LB_ID}) + def test_listeners(self): self.verify_list(self.proxy.listeners, listener.Listener) diff --git a/releasenotes/notes/add-octavia-lb-failover-9a34c9577d78ad34.yaml b/releasenotes/notes/add-octavia-lb-failover-9a34c9577d78ad34.yaml new file mode 100644 index 000000000..d87404ed9 --- /dev/null +++ b/releasenotes/notes/add-octavia-lb-failover-9a34c9577d78ad34.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Added Octavia (load_balancer) load balancer failover.