Skip to content

Commit 1e0880e

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add volume summary command"
2 parents f9b025e + 77266bd commit 1e0880e

File tree

6 files changed

+215
-1
lines changed

6 files changed

+215
-1
lines changed

doc/source/cli/command-objects/volume.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,8 @@ Unset volume properties
388388
.. describe:: <volume>
389389
390390
Volume to modify (name or ID)
391+
392+
Block Storage v3
393+
394+
.. autoprogram-cliff:: openstack.volume.v3
395+
:command: volume summary

doc/source/cli/data/cinder.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ snapshot-rename,snapshot set --name,Renames a snapshot.
120120
snapshot-reset-state,snapshot set --state,Explicitly updates the snapshot state.
121121
snapshot-show,snapshot show,Shows snapshot details.
122122
snapshot-unmanage,volume snapshot delete --remote,Stop managing a snapshot.
123-
summary,,Get volumes summary. (Supported by API versions 3.12 - 3.latest)
123+
summary,volume summary,Get volumes summary. (Supported by API versions 3.12 - 3.latest)
124124
thaw-host,volume host set --enable,Thaw and enable the specified cinder-volume host.
125125
transfer-accept,volume transfer accept,Accepts a volume transfer.
126126
transfer-create,volume transfer create,Creates a volume transfer.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
15+
import copy
16+
17+
from cinderclient import api_versions
18+
from osc_lib.cli import format_columns
19+
from osc_lib import exceptions
20+
21+
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
22+
from openstackclient.volume.v3 import volume
23+
24+
25+
class TestVolumeSummary(volume_fakes.TestVolume):
26+
27+
columns = [
28+
'Total Count',
29+
'Total Size',
30+
]
31+
32+
def setUp(self):
33+
super().setUp()
34+
35+
self.volumes_mock = self.app.client_manager.volume.volumes
36+
self.volumes_mock.reset_mock()
37+
self.mock_vol_1 = volume_fakes.create_one_volume()
38+
self.mock_vol_2 = volume_fakes.create_one_volume()
39+
self.return_dict = {
40+
'volume-summary': {
41+
'total_count': 2,
42+
'total_size': self.mock_vol_1.size + self.mock_vol_2.size}}
43+
self.volumes_mock.summary.return_value = self.return_dict
44+
45+
# Get the command object to test
46+
self.cmd = volume.VolumeSummary(self.app, None)
47+
48+
def test_volume_summary(self):
49+
self.app.client_manager.volume.api_version = \
50+
api_versions.APIVersion('3.12')
51+
arglist = [
52+
'--all-projects',
53+
]
54+
verifylist = [
55+
('all_projects', True),
56+
]
57+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
58+
59+
columns, data = self.cmd.take_action(parsed_args)
60+
61+
self.volumes_mock.summary.assert_called_once_with(
62+
all_tenants=True,
63+
)
64+
65+
self.assertEqual(self.columns, columns)
66+
67+
datalist = (
68+
2,
69+
self.mock_vol_1.size + self.mock_vol_2.size)
70+
self.assertCountEqual(datalist, tuple(data))
71+
72+
def test_volume_summary_pre_312(self):
73+
arglist = [
74+
'--all-projects',
75+
]
76+
verifylist = [
77+
('all_projects', True),
78+
]
79+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
80+
81+
exc = self.assertRaises(
82+
exceptions.CommandError,
83+
self.cmd.take_action,
84+
parsed_args)
85+
self.assertIn(
86+
'--os-volume-api-version 3.12 or greater is required',
87+
str(exc))
88+
89+
def test_volume_summary_with_metadata(self):
90+
self.app.client_manager.volume.api_version = \
91+
api_versions.APIVersion('3.36')
92+
93+
combine_meta = {**self.mock_vol_1.metadata, **self.mock_vol_2.metadata}
94+
meta_dict = copy.deepcopy(self.return_dict)
95+
meta_dict['volume-summary']['metadata'] = combine_meta
96+
self.volumes_mock.summary.return_value = meta_dict
97+
98+
new_cols = copy.deepcopy(self.columns)
99+
new_cols.extend(['Metadata'])
100+
101+
arglist = [
102+
'--all-projects',
103+
]
104+
verifylist = [
105+
('all_projects', True),
106+
]
107+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
108+
109+
columns, data = self.cmd.take_action(parsed_args)
110+
111+
self.volumes_mock.summary.assert_called_once_with(
112+
all_tenants=True,
113+
)
114+
115+
self.assertEqual(new_cols, columns)
116+
117+
datalist = (
118+
2,
119+
self.mock_vol_1.size + self.mock_vol_2.size,
120+
format_columns.DictColumn(combine_meta))
121+
self.assertCountEqual(datalist, tuple(data))
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
15+
"""Volume V3 Volume action implementations"""
16+
17+
import logging
18+
19+
from cinderclient import api_versions
20+
from osc_lib.cli import format_columns
21+
from osc_lib.command import command
22+
from osc_lib import exceptions
23+
from osc_lib import utils
24+
25+
from openstackclient.i18n import _
26+
27+
28+
LOG = logging.getLogger(__name__)
29+
30+
31+
class VolumeSummary(command.ShowOne):
32+
_description = _("Show a summary of all volumes in this deployment.")
33+
34+
def get_parser(self, prog_name):
35+
parser = super().get_parser(prog_name)
36+
parser.add_argument(
37+
'--all-projects',
38+
action='store_true',
39+
default=False,
40+
help=_('Include all projects (admin only)'),
41+
)
42+
return parser
43+
44+
def take_action(self, parsed_args):
45+
46+
volume_client = self.app.client_manager.volume
47+
48+
if volume_client.api_version < api_versions.APIVersion('3.12'):
49+
msg = _(
50+
"--os-volume-api-version 3.12 or greater is required to "
51+
"support the 'volume summary' command"
52+
)
53+
raise exceptions.CommandError(msg)
54+
55+
columns = [
56+
'total_count',
57+
'total_size',
58+
]
59+
column_headers = [
60+
'Total Count',
61+
'Total Size',
62+
]
63+
if volume_client.api_version.matches('3.36'):
64+
columns.append('metadata')
65+
column_headers.append('Metadata')
66+
67+
# set value of 'all_tenants' when using project option
68+
all_projects = parsed_args.all_projects
69+
70+
vol_summary = volume_client.volumes.summary(
71+
all_tenants=all_projects,
72+
)
73+
74+
return (
75+
column_headers,
76+
utils.get_dict_properties(
77+
vol_summary['volume-summary'],
78+
columns,
79+
formatters={'metadata': format_columns.DictColumn},
80+
),
81+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Added ``volume summary`` command to show the total size,
5+
total count and metadata of volumes.

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,3 +819,5 @@ openstack.volume.v3 =
819819
volume_transfer_request_delete = openstackclient.volume.v2.volume_transfer_request:DeleteTransferRequest
820820
volume_transfer_request_list = openstackclient.volume.v2.volume_transfer_request:ListTransferRequest
821821
volume_transfer_request_show = openstackclient.volume.v2.volume_transfer_request:ShowTransferRequest
822+
823+
volume_summary = openstackclient.volume.v3.volume:VolumeSummary

0 commit comments

Comments
 (0)