summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVijendar Komalla <vijendar.komalla@RACKSPACE.COM>2017-01-25 14:33:14 -0600
committerVijendar Komalla <vijendar.komalla@RACKSPACE.COM>2017-01-27 13:11:07 -0600
commit7cb91456538a1a72033a5c6df55aae99ec51c8fe (patch)
tree0d74e2d64f495e7f4434be165b334919f76aa1d0
parent8fee8c50cb40253560e713419e4520a345cae5ee (diff)
Adding stats-list command to magnum client2.4.0
Notes
Notes (review): Code-Review+2: Adrian Otto <adrian.otto@rackspace.com> Code-Review+2: Ton Ngo <ton@us.ibm.com> Workflow+1: Ton Ngo <ton@us.ibm.com> Code-Review+2: Jaycen Grant <jaycen.v.grant@intel.com> Verified+2: Jenkins Submitted-by: Jenkins Submitted-at: Fri, 27 Jan 2017 19:54:57 +0000 Reviewed-on: https://review.openstack.org/425918 Project: openstack/python-magnumclient Branch: refs/heads/master
-rw-r--r--magnumclient/tests/v1/test_stats.py91
-rw-r--r--magnumclient/tests/v1/test_stats_shell.py53
-rw-r--r--magnumclient/v1/client.py2
-rw-r--r--magnumclient/v1/shell.py2
-rw-r--r--magnumclient/v1/stats.py32
-rw-r--r--magnumclient/v1/stats_shell.py28
6 files changed, 208 insertions, 0 deletions
diff --git a/magnumclient/tests/v1/test_stats.py b/magnumclient/tests/v1/test_stats.py
new file mode 100644
index 0000000..d145540
--- /dev/null
+++ b/magnumclient/tests/v1/test_stats.py
@@ -0,0 +1,91 @@
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import testtools
14
15from magnumclient.tests import utils
16from magnumclient.v1 import stats
17
18
19CLUSTER1 = {'id': 123,
20 'uuid': '66666666-7777-8888-9999-000000000001',
21 'name': 'cluster1',
22 'cluster_template_id': 'e74c40e0-d825-11e2-a28f-0800200c9a61',
23 'stack_id': '5d12f6fd-a196-4bf0-ae4c-1f639a523a51',
24 'api_address': '172.17.2.1',
25 'node_addresses': ['172.17.2.3'],
26 'node_count': 2,
27 'master_count': 1,
28 'project_id': 'abc'
29 }
30CLUSTER2 = {'id': 124,
31 'uuid': '66666666-7777-8888-9999-000000000002',
32 'name': 'cluster2',
33 'cluster_template_id': 'e74c40e0-d825-11e2-a28f-0800200c9a62',
34 'stack_id': '5d12f6fd-a196-4bf0-ae4c-1f639a523a52',
35 'api_address': '172.17.2.2',
36 'node_addresses': ['172.17.2.4'],
37 'node_count': 2,
38 'master_count': 1,
39 'project_id': 'bcd'
40 }
41
42
43nc = 'node_count'
44mc = 'master_count'
45C1 = CLUSTER1
46C2 = CLUSTER2
47fake_responses = {
48 '/v1/stats':
49 {
50 'GET': (
51 {},
52 {'clusters': 2, 'nodes': C1[nc] + C1[mc] + C2[nc] + C2[mc]},
53 )
54 },
55 '/v1/stats?project_id=%s' % C2['project_id']:
56 {
57 'GET': (
58 {},
59 {'clusters': 1, 'nodes': C2[nc] + C2[mc]},
60 )
61 },
62}
63
64
65class StatsManagerTest(testtools.TestCase):
66
67 def setUp(self):
68 super(StatsManagerTest, self).setUp()
69 self.api = utils.FakeAPI(fake_responses)
70 self.mgr = stats.StatsManager(self.api)
71
72 def test_stats(self):
73 stats = self.mgr.list()
74 expect = [
75 ('GET', '/v1/stats', {}, None),
76 ]
77 self.assertEqual(expect, self.api.calls)
78 expected_stats = {'clusters': 2,
79 'nodes': C1[nc] + C1[mc] + C2[nc] + C2[mc]}
80 self.assertEqual(expected_stats, stats._info)
81
82 def test_stats_with_project_id(self):
83 expect = [
84 ('GET', '/v1/stats?project_id=%s' % CLUSTER2['project_id'], {},
85 None),
86 ]
87 stats = self.mgr.list(project_id=CLUSTER2['project_id'])
88 self.assertEqual(expect, self.api.calls)
89 expected_stats = {'clusters': 1,
90 'nodes': C2[nc] + C2[mc]}
91 self.assertEqual(expected_stats, stats._info)
diff --git a/magnumclient/tests/v1/test_stats_shell.py b/magnumclient/tests/v1/test_stats_shell.py
new file mode 100644
index 0000000..1d6ccc7
--- /dev/null
+++ b/magnumclient/tests/v1/test_stats_shell.py
@@ -0,0 +1,53 @@
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import mock
14
15from magnumclient.tests.v1 import shell_test_base
16from magnumclient.v1.stats import Stats
17
18
19class FakeStats(Stats):
20 def __init__(self, manager=None, info={}, **kwargs):
21 Stats.__init__(self, manager=manager, info=info)
22 self.clusters = kwargs.get('clusters', 0)
23 self.nodes = kwargs.get('nodes', 0)
24
25
26class ShellTest(shell_test_base.TestCommandLineArgument):
27
28 def _get_expected_args_list(self, project_id=None):
29 expected_args = {}
30 expected_args['project_id'] = project_id
31 return expected_args
32
33 @mock.patch(
34 'magnumclient.v1.stats.StatsManager.list')
35 def test_stats_get_success(self, mock_list):
36 self._test_arg_success('stats-list')
37 expected_args = self._get_expected_args_list()
38 mock_list.assert_called_once_with(**expected_args)
39
40 @mock.patch(
41 'magnumclient.v1.stats.StatsManager.list')
42 def test_stats_get_success_with_arg(self, mock_list):
43 self._test_arg_success('stats-list '
44 '--project-id 111 ')
45 expected_args = self._get_expected_args_list('111')
46 mock_list.assert_called_once_with(**expected_args)
47
48 @mock.patch(
49 'magnumclient.v1.stats.StatsManager.list')
50 def test_stats_get_failure(self, mock_list):
51 self._test_arg_failure('stats-list --wrong',
52 self._unrecognized_arg_error)
53 mock_list.assert_not_called()
diff --git a/magnumclient/v1/client.py b/magnumclient/v1/client.py
index 60288e1..15e9f5a 100644
--- a/magnumclient/v1/client.py
+++ b/magnumclient/v1/client.py
@@ -25,6 +25,7 @@ from magnumclient.v1 import certificates
25from magnumclient.v1 import cluster_templates 25from magnumclient.v1 import cluster_templates
26from magnumclient.v1 import clusters 26from magnumclient.v1 import clusters
27from magnumclient.v1 import mservices 27from magnumclient.v1 import mservices
28from magnumclient.v1 import stats
28 29
29profiler = importutils.try_import("osprofiler.profiler") 30profiler = importutils.try_import("osprofiler.profiler")
30 31
@@ -211,3 +212,4 @@ class Client(object):
211 # will fail to check the request signature and will skip 212 # will fail to check the request signature and will skip
212 # initialization of osprofiler on the server side. 213 # initialization of osprofiler on the server side.
213 profiler.init(profile) 214 profiler.init(profile)
215 self.stats = stats.StatsManager(self.http_client)
diff --git a/magnumclient/v1/shell.py b/magnumclient/v1/shell.py
index 2380f76..a095d40 100644
--- a/magnumclient/v1/shell.py
+++ b/magnumclient/v1/shell.py
@@ -19,6 +19,7 @@ from magnumclient.v1 import certificates_shell
19from magnumclient.v1 import cluster_templates_shell 19from magnumclient.v1 import cluster_templates_shell
20from magnumclient.v1 import clusters_shell 20from magnumclient.v1 import clusters_shell
21from magnumclient.v1 import mservices_shell 21from magnumclient.v1 import mservices_shell
22from magnumclient.v1 import stats_shell
22 23
23COMMAND_MODULES = [ 24COMMAND_MODULES = [
24 baymodels_shell, 25 baymodels_shell,
@@ -27,4 +28,5 @@ COMMAND_MODULES = [
27 clusters_shell, 28 clusters_shell,
28 cluster_templates_shell, 29 cluster_templates_shell,
29 mservices_shell, 30 mservices_shell,
31 stats_shell,
30] 32]
diff --git a/magnumclient/v1/stats.py b/magnumclient/v1/stats.py
new file mode 100644
index 0000000..7db5511
--- /dev/null
+++ b/magnumclient/v1/stats.py
@@ -0,0 +1,32 @@
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13from magnumclient.common import base
14
15
16class Stats(base.Resource):
17 def __repr__(self):
18 return "<Stats %s>" % self._info
19
20
21class StatsManager(base.Manager):
22 resource_class = Stats
23
24 @staticmethod
25 def _path(id=None):
26 return '/v1/stats?project_id=%s' % id if id else '/v1/stats'
27
28 def list(self, project_id=None):
29 try:
30 return self._list(self._path(project_id))[0]
31 except IndexError:
32 return None
diff --git a/magnumclient/v1/stats_shell.py b/magnumclient/v1/stats_shell.py
new file mode 100644
index 0000000..a5f25d0
--- /dev/null
+++ b/magnumclient/v1/stats_shell.py
@@ -0,0 +1,28 @@
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13from magnumclient.common import cliutils as utils
14from magnumclient.i18n import _
15
16
17@utils.arg('--project-id',
18 required=False,
19 metavar='<project-id>',
20 help=_('Project ID'))
21def do_stats_list(cs, args):
22 """Show stats for the given project_id"""
23 opts = {
24 'project_id': args.project_id
25 }
26
27 stats = cs.stats.list(**opts)
28 utils.print_dict(stats._info)