|
15 | 15 |
|
16 | 16 | """Image V2 Action Implementations""" |
17 | 17 |
|
| 18 | +import logging |
18 | 19 |
|
19 | 20 | from osc_lib.command import command |
| 21 | +from osc_lib import exceptions |
20 | 22 | from osc_lib import utils |
21 | 23 |
|
22 | 24 | from openstackclient.i18n import _ |
23 | 25 |
|
24 | 26 |
|
| 27 | +LOG = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
25 | 30 | def _format_object(md_object): |
26 | 31 | fields_to_show = ( |
27 | 32 | 'created_at', |
@@ -77,7 +82,7 @@ def take_action(self, parsed_args): |
77 | 82 |
|
78 | 83 | class ShowMetadefObjects(command.ShowOne): |
79 | 84 | _description = _( |
80 | | - "Describe a specific metadata definitions" "object inside a namespace" |
| 85 | + "Describe a specific metadata definitions object inside a namespace" |
81 | 86 | ) |
82 | 87 |
|
83 | 88 | def get_parser(self, prog_name): |
@@ -107,6 +112,55 @@ def take_action(self, parsed_args): |
107 | 112 | return fields, value |
108 | 113 |
|
109 | 114 |
|
| 115 | +class DeleteMetadefObject(command.Command): |
| 116 | + _description = _( |
| 117 | + "Delete a specific metadata definitions object inside a namespace" |
| 118 | + ) |
| 119 | + |
| 120 | + def get_parser(self, prog_name): |
| 121 | + parser = super().get_parser(prog_name) |
| 122 | + parser.add_argument( |
| 123 | + "namespace_name", |
| 124 | + metavar="<namespace_name>", |
| 125 | + help=_("Namespace (name) for the namespace"), |
| 126 | + ) |
| 127 | + parser.add_argument( |
| 128 | + "object_name", |
| 129 | + metavar="<object_name>", |
| 130 | + nargs="+", |
| 131 | + help=_("Name of an object."), |
| 132 | + ) |
| 133 | + return parser |
| 134 | + |
| 135 | + def take_action(self, parsed_args): |
| 136 | + image_client = self.app.client_manager.image |
| 137 | + |
| 138 | + namespace_name = parsed_args.namespace_name |
| 139 | + |
| 140 | + result = 0 |
| 141 | + for i in parsed_args.object_name: |
| 142 | + try: |
| 143 | + object = image_client.get_metadef_object(i, namespace_name) |
| 144 | + image_client.delete_metadef_object(object, namespace_name) |
| 145 | + except Exception as e: |
| 146 | + result += 1 |
| 147 | + LOG.error( |
| 148 | + _( |
| 149 | + "Failed to delete object with name or " |
| 150 | + "ID '%(object)s': %(e)s" |
| 151 | + ), |
| 152 | + {'object': i, 'e': e}, |
| 153 | + ) |
| 154 | + |
| 155 | + if result > 0: |
| 156 | + total = len(parsed_args.namespace_name) |
| 157 | + msg = _("%(result)s of %(total)s object failed to delete.") % { |
| 158 | + 'result': result, |
| 159 | + 'total': total, |
| 160 | + } |
| 161 | + raise exceptions.CommandError(msg) |
| 162 | + |
| 163 | + |
110 | 164 | class ListMetadefObjects(command.Lister): |
111 | 165 | _description = _("List metadef objects inside a specific namespace.") |
112 | 166 |
|
|
0 commit comments