|
| 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 | + |
| 13 | +import logging |
| 14 | + |
| 15 | +from osc_lib.command import command |
| 16 | +from osc_lib import exceptions |
| 17 | +from osc_lib import utils |
| 18 | + |
| 19 | +from openstackclient.i18n import _ |
| 20 | + |
| 21 | +LOG = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def _get_columns(item): |
| 25 | + column_map = {} |
| 26 | + hidden_columns = ['location'] |
| 27 | + return utils.get_osc_show_columns_for_sdk_resource( |
| 28 | + item, column_map, hidden_columns |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +class CreateMetadefResourceTypeAssociation(command.ShowOne): |
| 33 | + _description = _("Create metadef resource type association") |
| 34 | + |
| 35 | + def get_parser(self, prog_name): |
| 36 | + parser = super().get_parser(prog_name) |
| 37 | + parser.add_argument( |
| 38 | + "namespace", |
| 39 | + metavar="<namespace>", |
| 40 | + help=_( |
| 41 | + "The name of the namespace you want to create the " |
| 42 | + "resource type association in" |
| 43 | + ), |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "name", |
| 47 | + metavar="<name>", |
| 48 | + help=_("A name of the new resource type"), |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "--properties-target", |
| 52 | + metavar="<properties_target>", |
| 53 | + help=_( |
| 54 | + "Some resource types allow more than one " |
| 55 | + "key/value pair per instance." |
| 56 | + ), |
| 57 | + ) |
| 58 | + return parser |
| 59 | + |
| 60 | + def take_action(self, parsed_args): |
| 61 | + image_client = self.app.client_manager.image |
| 62 | + kwargs = {} |
| 63 | + |
| 64 | + kwargs['namespace'] = parsed_args.namespace |
| 65 | + kwargs['name'] = parsed_args.name |
| 66 | + |
| 67 | + if parsed_args.properties_target: |
| 68 | + kwargs['properties_target'] = parsed_args.properties_target |
| 69 | + |
| 70 | + obj = image_client.create_metadef_resource_type_association( |
| 71 | + parsed_args.namespace, **kwargs |
| 72 | + ) |
| 73 | + |
| 74 | + display_columns, columns = _get_columns(obj) |
| 75 | + data = utils.get_item_properties(obj, columns, formatters={}) |
| 76 | + |
| 77 | + return (display_columns, data) |
| 78 | + |
| 79 | + |
| 80 | +class DeleteMetadefResourceTypeAssociation(command.Command): |
| 81 | + _description = _("Delete metadef resource type association") |
| 82 | + |
| 83 | + def get_parser(self, prog_name): |
| 84 | + parser = super().get_parser(prog_name) |
| 85 | + parser.add_argument( |
| 86 | + "metadef_namespace", |
| 87 | + metavar="<metadef_namespace>", |
| 88 | + help=_("The name of the namespace whose details you want to see"), |
| 89 | + ) |
| 90 | + parser.add_argument( |
| 91 | + "name", |
| 92 | + metavar="<name>", |
| 93 | + nargs="+", |
| 94 | + help=_( |
| 95 | + "The name of the resource type(s) (repeat option to delete" |
| 96 | + "multiple metadef resource type associations)" |
| 97 | + ), |
| 98 | + ) |
| 99 | + parser.add_argument( |
| 100 | + "--force", |
| 101 | + dest='force', |
| 102 | + action='store_true', |
| 103 | + default=False, |
| 104 | + help=_( |
| 105 | + "Force delete the resource type association if the" |
| 106 | + "namespace is protected" |
| 107 | + ), |
| 108 | + ) |
| 109 | + return parser |
| 110 | + |
| 111 | + def take_action(self, parsed_args): |
| 112 | + image_client = self.app.client_manager.image |
| 113 | + |
| 114 | + result = 0 |
| 115 | + for resource_type in parsed_args.name: |
| 116 | + try: |
| 117 | + metadef_namespace = image_client.get_metadef_namespace( |
| 118 | + parsed_args.metadef_namespace |
| 119 | + ) |
| 120 | + |
| 121 | + kwargs = {} |
| 122 | + is_initially_protected = ( |
| 123 | + True if metadef_namespace.is_protected else False |
| 124 | + ) |
| 125 | + if is_initially_protected and parsed_args.force: |
| 126 | + kwargs['is_protected'] = False |
| 127 | + |
| 128 | + image_client.update_metadef_namespace( |
| 129 | + metadef_namespace.namespace, **kwargs |
| 130 | + ) |
| 131 | + |
| 132 | + try: |
| 133 | + image_client.delete_metadef_resource_type_association( |
| 134 | + resource_type, metadef_namespace, ignore_missing=False |
| 135 | + ) |
| 136 | + finally: |
| 137 | + if is_initially_protected: |
| 138 | + kwargs['is_protected'] = True |
| 139 | + image_client.update_metadef_namespace( |
| 140 | + metadef_namespace.namespace, **kwargs |
| 141 | + ) |
| 142 | + |
| 143 | + except Exception as e: |
| 144 | + result += 1 |
| 145 | + LOG.error( |
| 146 | + _( |
| 147 | + "Failed to delete resource type with name or " |
| 148 | + "ID '%(resource_type)s': %(e)s" |
| 149 | + ), |
| 150 | + {'resource_type': resource_type, 'e': e}, |
| 151 | + ) |
| 152 | + |
| 153 | + if result > 0: |
| 154 | + total = len(parsed_args.metadef_namespace) |
| 155 | + msg = _( |
| 156 | + "%(result)s of %(total)s resource type failed to delete." |
| 157 | + ) % {'result': result, 'total': total} |
| 158 | + raise exceptions.CommandError(msg) |
| 159 | + |
| 160 | + |
| 161 | +class ListMetadefResourceTypeAssociations(command.Lister): |
| 162 | + _description = _("List metadef resource type associations") |
| 163 | + |
| 164 | + def get_parser(self, prog_name): |
| 165 | + parser = super().get_parser(prog_name) |
| 166 | + parser.add_argument( |
| 167 | + "metadef_namespace", |
| 168 | + metavar="<metadef_namespace>", |
| 169 | + help=_("The name of the namespace whose details you want to see"), |
| 170 | + ) |
| 171 | + return parser |
| 172 | + |
| 173 | + def take_action(self, parsed_args): |
| 174 | + image_client = self.app.client_manager.image |
| 175 | + data = image_client.metadef_resource_type_associations( |
| 176 | + parsed_args.metadef_namespace, |
| 177 | + ) |
| 178 | + columns = ['Name'] |
| 179 | + column_headers = columns |
| 180 | + return ( |
| 181 | + column_headers, |
| 182 | + ( |
| 183 | + utils.get_item_properties( |
| 184 | + s, |
| 185 | + columns, |
| 186 | + ) |
| 187 | + for s in data |
| 188 | + ), |
| 189 | + ) |
0 commit comments