diff --git a/octaviaclient/api/constants.py b/octaviaclient/api/constants.py index 8330cc8..32b4ac5 100644 --- a/octaviaclient/api/constants.py +++ b/octaviaclient/api/constants.py @@ -21,6 +21,7 @@ BASE_LOADBALANCER_FAILOVER_URL = BASE_SINGLE_LB_URL + '/failover' BASE_LISTENER_URL = BASE_LBAAS_ENDPOINT + '/listeners' BASE_SINGLE_LISTENER_URL = BASE_LISTENER_URL + '/{uuid}' +BASE_LISTENER_STATS_URL = BASE_SINGLE_LISTENER_URL + '/stats' BASE_POOL_URL = BASE_LBAAS_ENDPOINT + '/pools' BASE_SINGLE_POOL_URL = BASE_POOL_URL + '/{pool_id}' diff --git a/octaviaclient/api/v2/octavia.py b/octaviaclient/api/v2/octavia.py index 35da865..b02a2ea 100644 --- a/octaviaclient/api/v2/octavia.py +++ b/octaviaclient/api/v2/octavia.py @@ -221,6 +221,19 @@ class OctaviaAPI(api.BaseAPI): return response + def listener_stats_show(self, listener_id, **kwargs): + """Shows the current statistics for a listener + + :param string listener_id: + ID of the listener + :return: + A dict of the specified listener's statistics + """ + url = const.BASE_LISTENER_STATS_URL.format(uuid=listener_id) + response = self.list(url, **kwargs) + + return response + def pool_list(self, **kwargs): """List all pools diff --git a/octaviaclient/osc/v2/listener.py b/octaviaclient/osc/v2/listener.py index 296899c..c48a8a5 100644 --- a/octaviaclient/osc/v2/listener.py +++ b/octaviaclient/osc/v2/listener.py @@ -312,3 +312,32 @@ class SetListener(command.Command): self.app.client_manager.load_balancer.listener_set( listener_id, json=body) + + +class ShowListenerStats(command.ShowOne): + """Shows the current statistics for a listener.""" + + def get_parser(self, prog_name): + parser = super(ShowListenerStats, self).get_parser(prog_name) + + parser.add_argument( + 'listener', + metavar='', + help='Name or UUID of the listener' + ) + + return parser + + def take_action(self, parsed_args): + rows = const.LOAD_BALANCER_STATS_ROWS + attrs = v2_utils.get_listener_attrs(self.app.client_manager, + parsed_args) + + listener_id = attrs.pop('listener_id') + + data = self.app.client_manager.load_balancer.listener_stats_show( + listener_id=listener_id, + ) + + return (rows, (utils.get_dict_properties( + data['stats'], rows, formatters={}))) diff --git a/octaviaclient/tests/unit/api/test_octavia.py b/octaviaclient/tests/unit/api/test_octavia.py index 8c4f9ab..6342119 100644 --- a/octaviaclient/tests/unit/api/test_octavia.py +++ b/octaviaclient/tests/unit/api/test_octavia.py @@ -328,6 +328,16 @@ class TestLoadBalancer(TestOctaviaClient): self.api.listener_delete, FAKE_LI) + def test_stats_show_listener(self): + self.requests_mock.register_uri( + 'GET', + FAKE_LBAAS_URL + 'listeners/' + FAKE_LI + '/stats', + json=SINGLE_LB_STATS_RESP, + status_code=200, + ) + ret = self.api.listener_stats_show(FAKE_LI) + self.assertEqual(SINGLE_LB_STATS_RESP, ret) + def test_list_pool_no_options(self): self.requests_mock.register_uri( 'GET', diff --git a/octaviaclient/tests/unit/osc/v2/test_listener.py b/octaviaclient/tests/unit/osc/v2/test_listener.py index 337d65f..12a4b42 100644 --- a/octaviaclient/tests/unit/osc/v2/test_listener.py +++ b/octaviaclient/tests/unit/osc/v2/test_listener.py @@ -247,3 +247,26 @@ class TestListenerSet(TestListener): 'default_tls_container_ref': self._li.default_tls_container_ref }}) + + +class TestListenerStatsShow(TestListener): + + def setUp(self): + super(TestListenerStatsShow, self).setUp() + listener_stats_info = {'stats': {'bytes_in': '0'}} + self.api_mock.listener_stats_show.return_value = { + 'stats': listener_stats_info['stats']} + lb_client = self.app.client_manager + lb_client.load_balancer = self.api_mock + self.cmd = listener.ShowListenerStats(self.app, None) + + def test_listener_stats_show(self): + arglist = [self._li.id] + verifylist = [ + ('listener', self._li.id), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + self.api_mock.listener_stats_show.assert_called_with( + listener_id=self._li.id) diff --git a/setup.cfg b/setup.cfg index aac6d02..9c7cdb8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -39,6 +39,7 @@ openstack.load_balancer.v2 = loadbalancer_listener_show = octaviaclient.osc.v2.listener:ShowListener loadbalancer_listener_delete = octaviaclient.osc.v2.listener:DeleteListener loadbalancer_listener_set = octaviaclient.osc.v2.listener:SetListener + loadbalancer_listener_stats_show = octaviaclient.osc.v2.listener:ShowListenerStats loadbalancer_pool_create = octaviaclient.osc.v2.pool:CreatePool loadbalancer_pool_list = octaviaclient.osc.v2.pool:ListPool loadbalancer_pool_show = octaviaclient.osc.v2.pool:ShowPool