Skip to content

Commit 18a6199

Browse files
author
Mridula Joshi
committed
Adding image stores info command
This is an equivalent for ``glance stores-info``. Depends-on: https://review.opendev.org/c/openstack/openstacksdk/+/883493 Change-Id: I15d2c2c523ace1cfb32045328ecee8ce8beea63f
1 parent 7dc1276 commit 18a6199

5 files changed

Lines changed: 124 additions & 0 deletions

File tree

openstackclient/image/v2/image.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,3 +1836,48 @@ def take_action(self, parsed_args):
18361836

18371837
info = _format_image(image)
18381838
return zip(*sorted(info.items()))
1839+
1840+
1841+
class StoresInfo(command.Lister):
1842+
_description = _(
1843+
"Get available backends (only valid with Multi-Backend support)"
1844+
)
1845+
1846+
def get_parser(self, prog_name):
1847+
parser = super().get_parser(prog_name)
1848+
parser.add_argument(
1849+
"--detail",
1850+
action='store_true',
1851+
default=None,
1852+
help=_(
1853+
'Shows details of stores (admin only) '
1854+
'(requires --os-image-api-version 2.15 or later)'
1855+
),
1856+
)
1857+
return parser
1858+
1859+
def take_action(self, parsed_args):
1860+
image_client = self.app.client_manager.image
1861+
try:
1862+
columns = ("id", "description", "is_default")
1863+
column_headers = ("ID", "Description", "Default")
1864+
if parsed_args.detail:
1865+
columns += ("properties",)
1866+
column_headers += ("Properties",)
1867+
1868+
data = list(image_client.stores(details=parsed_args.detail))
1869+
except sdk_exceptions.ResourceNotFound:
1870+
msg = _('Multi Backend support not enabled')
1871+
raise exceptions.CommandError(msg)
1872+
else:
1873+
return (
1874+
column_headers,
1875+
(
1876+
utils.get_item_properties(
1877+
store,
1878+
columns,
1879+
formatters=_formatters,
1880+
)
1881+
for store in data
1882+
),
1883+
)

openstackclient/tests/unit/image/v2/fakes.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self, **kwargs):
5757
self.get_task = mock.Mock()
5858

5959
self.get_import_info = mock.Mock()
60+
self.stores = mock.Mock()
6061

6162
self.auth_token = kwargs['token']
6263
self.management_url = kwargs['endpoint']
@@ -171,6 +172,38 @@ def create_one_import_info(attrs=None):
171172
return _service_info.Import(**import_info)
172173

173174

175+
def create_one_stores_info(attrs=None):
176+
"""Create a fake stores info.
177+
178+
:param attrs: A dictionary with all attributes of stores
179+
:type attrs: dict
180+
:return: A fake Store object list.
181+
:rtype: `openstack.image.v2.service_info.Store`
182+
"""
183+
attrs = attrs or {}
184+
185+
stores_info = {
186+
"stores": [
187+
{
188+
"id": "reliable",
189+
"description": "More expensive store with data redundancy",
190+
},
191+
{
192+
"id": "fast",
193+
"description": "Provides quick access to your image data",
194+
"default": True,
195+
},
196+
{
197+
"id": "cheap",
198+
"description": "Less expensive store for seldom-used images",
199+
},
200+
]
201+
}
202+
stores_info.update(attrs)
203+
204+
return _service_info.Store(**stores_info)
205+
206+
174207
def create_one_task(attrs=None):
175208
"""Create a fake task.
176209

openstackclient/tests/unit/image/v2/test_image.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,3 +2101,43 @@ def test_get_data_from_stdin__interactive(self):
21012101
test_fd = _image.get_data_from_stdin()
21022102

21032103
self.assertIsNone(test_fd)
2104+
2105+
2106+
class TestStoresInfo(TestImage):
2107+
stores_info = image_fakes.create_one_stores_info()
2108+
2109+
def setUp(self):
2110+
super().setUp()
2111+
2112+
self.client.stores.return_value = self.stores_info
2113+
2114+
self.cmd = _image.StoresInfo(self.app, None)
2115+
2116+
def test_stores_info(self):
2117+
arglist = []
2118+
parsed_args = self.check_parser(self.cmd, arglist, [])
2119+
self.cmd.take_action(parsed_args)
2120+
2121+
self.client.stores.assert_called()
2122+
2123+
def test_stores_info_with_detail(self):
2124+
arglist = ['--detail']
2125+
parsed_args = self.check_parser(self.cmd, arglist, [])
2126+
self.cmd.take_action(parsed_args)
2127+
2128+
self.client.stores.assert_called_with(details=True)
2129+
2130+
def test_stores_info_neg(self):
2131+
arglist = []
2132+
parsed_args = self.check_parser(self.cmd, arglist, [])
2133+
self.client.stores.side_effect = sdk_exceptions.ResourceNotFound()
2134+
2135+
exc = self.assertRaises(
2136+
exceptions.CommandError,
2137+
self.cmd.take_action,
2138+
parsed_args,
2139+
)
2140+
self.assertIn(
2141+
"Multi Backend support not enabled",
2142+
str(exc),
2143+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
features:
2+
- |
3+
Add ``image stores info`` command, allowing users to know available backends.

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ openstack.image.v2 =
388388
image_task_show = openstackclient.image.v2.task:ShowTask
389389
image_task_list = openstackclient.image.v2.task:ListTask
390390
image_import_info = openstackclient.image.v2.info:ImportInfo
391+
image_import = openstackclient.image.v2.image:ImportImage
392+
image_stores_list = openstackclient.image.v2.image:StoresInfo
393+
391394

392395
image_metadef_namespace_create = openstackclient.image.v2.metadef_namespaces:CreateMetadefNameSpace
393396
image_metadef_namespace_delete = openstackclient.image.v2.metadef_namespaces:DeleteMetadefNameSpace

0 commit comments

Comments
 (0)