diff --git a/doc/source/cli/command-objects/image-metadef.rst b/doc/source/cli/command-objects/image-metadef.rst index 9fbe75f385..ed36d95f75 100644 --- a/doc/source/cli/command-objects/image-metadef.rst +++ b/doc/source/cli/command-objects/image-metadef.rst @@ -37,6 +37,9 @@ Image v2 .. autoprogram-cliff:: openstack.image.v2 :command: image metadef object update +.. autoprogram-cliff:: openstack.image.v2 + :command: image metadef object property show + .. autoprogram-cliff:: openstack.image.v2 :command: image metadef property create diff --git a/doc/source/cli/data/glance.csv b/doc/source/cli/data/glance.csv index c0a2f96025..8852e2d00f 100644 --- a/doc/source/cli/data/glance.csv +++ b/doc/source/cli/data/glance.csv @@ -36,7 +36,7 @@ md-object-create,image metadef object create,Create a new metadata definitions o md-object-show,image metadef object show,Describe a specific metadata definitions object inside a namespace. md-object-list,image metadef object list,List metadata definitions objects inside a specific namespace. md-object-delete,image metadef object delete,Delete a specific metadata definitions object inside a namespace. -md-object-property-show,,Describe a specific metadata definitions property inside an object. +md-object-property-show,image metadef object property show,Describe a specific metadata definitions property inside an object. md-object-update,image metadef object update,Update metadata definitions object inside a namespace. md-property-create,image metadef property create,Create a new metadata definitions property inside a namespace. md-property-delete,image metadef property delete,Delete a specific metadata definitions property inside a namespace. diff --git a/openstackclient/image/v2/metadef_objects.py b/openstackclient/image/v2/metadef_objects.py index 614f5c0c41..2dc33968ad 100644 --- a/openstackclient/image/v2/metadef_objects.py +++ b/openstackclient/image/v2/metadef_objects.py @@ -223,3 +223,47 @@ class SetMetadefObject(command.Command): image_client.update_metadef_object( object, parsed_args.namespace, **kwargs ) + + +class ShowMetadefObjectProperty(command.ShowOne): + _description = _( + "Describe a specific metadata definitions property inside an object." + ) + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument( + "namespace", + metavar="", + help=_("Namespace (name) for the namespace"), + ) + parser.add_argument( + "object", + metavar="", + help=_("Name of an object."), + ) + parser.add_argument( + "property", + help=_("Name of the property."), + ) + return parser + + def take_action(self, parsed_args): + image_client = self.app.client_manager.image + + namespace_name = parsed_args.namespace + object_name = parsed_args.object + + obj = image_client.get_metadef_object(object_name, namespace_name) + try: + prop = obj['properties'][parsed_args.property] + prop['name'] = parsed_args.property + + except KeyError: + msg = _('Property %s not found in object %s.') % ( + parsed_args.property, + parsed_args.object, + ) + raise exceptions.CommandError(msg) + + return zip(*sorted(prop.items())) diff --git a/openstackclient/tests/unit/image/v2/test_metadef_objects.py b/openstackclient/tests/unit/image/v2/test_metadef_objects.py index 5533d555b9..323d8ea151 100644 --- a/openstackclient/tests/unit/image/v2/test_metadef_objects.py +++ b/openstackclient/tests/unit/image/v2/test_metadef_objects.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +from osc_lib import exceptions + from openstackclient.image.v2 import metadef_objects from openstackclient.tests.unit.image.v2 import fakes @@ -198,3 +200,63 @@ class TestMetadefObjectSet(fakes.TestImagev2): result = self.cmd.take_action(parsed_args) self.assertIsNone(result) + + +class TestMetadefObjectPropertyShow(fakes.TestImagev2): + _metadef_namespace = fakes.create_one_metadef_namespace() + _metadef_objects = fakes.create_one_metadef_object() + md_property = _metadef_objects['properties']['quota:cpu_quota'] + md_property['name'] = 'quota:cpu_quota' + + expected_columns = ( + 'description', + 'name', + 'title', + 'type', + ) + expected_data = ( + md_property['description'], + md_property['name'], + md_property['title'], + md_property['type'], + ) + + def setUp(self): + super().setUp() + + self.image_client.get_metadef_object.return_value = ( + self._metadef_objects + ) + self.cmd = metadef_objects.ShowMetadefObjectProperty(self.app, None) + + def test_object_property_show(self): + arglist = [ + self._metadef_namespace.namespace, + self._metadef_objects.name, + 'quota:cpu_quota', + ] + verifylist = [] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(self.expected_columns, columns) + self.assertEqual(self.expected_data, data) + + def test_neg_object_property_show(self): + arglist = [ + self._metadef_namespace.namespace, + self._metadef_objects.name, + 'prop1', + ] + verifylist = [] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args, + ) + self.assertIn( + 'Property %s not found in object %s.' + % (parsed_args.property, parsed_args.object), + str(exc), + ) diff --git a/releasenotes/notes/add-image-metadef-object-property-show-4ab2c957451ea230.yaml b/releasenotes/notes/add-image-metadef-object-property-show-4ab2c957451ea230.yaml new file mode 100644 index 0000000000..32b259c129 --- /dev/null +++ b/releasenotes/notes/add-image-metadef-object-property-show-4ab2c957451ea230.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``image metadef object property show`` command which + shows a particular property inside metadef object. diff --git a/setup.cfg b/setup.cfg index 1ea56efb23..c8ff01ee52 100644 --- a/setup.cfg +++ b/setup.cfg @@ -404,7 +404,7 @@ openstack.image.v2 = image_metadef_object_list = openstackclient.image.v2.metadef_objects:ListMetadefObjects image_metadef_object_delete = openstackclient.image.v2.metadef_objects:DeleteMetadefObject image_metadef_object_update = openstackclient.image.v2.metadef_objects:SetMetadefObject - + image_metadef_object_property_show = openstackclient.image.v2.metadef_objects:ShowMetadefObjectProperty image_metadef_property_create = openstackclient.image.v2.metadef_properties:CreateMetadefProperty image_metadef_property_delete = openstackclient.image.v2.metadef_properties:DeleteMetadefProperty