|
| 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 | + |
| 14 | +"""Flavor action implementations""" |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +from osc_lib.command import command |
| 19 | +from osc_lib import exceptions |
| 20 | +from osc_lib import utils |
| 21 | + |
| 22 | +from openstackclient.i18n import _ |
| 23 | +from openstackclient.identity import common as identity_common |
| 24 | +from openstackclient.network import sdk_utils |
| 25 | + |
| 26 | + |
| 27 | +LOG = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +def _get_columns(item): |
| 31 | + column_map = { |
| 32 | + 'is_enabled': 'enabled', |
| 33 | + 'tenant_id': 'project_id', |
| 34 | + } |
| 35 | + |
| 36 | + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) |
| 37 | + |
| 38 | + |
| 39 | +def _get_attrs(client_manager, parsed_args): |
| 40 | + attrs = {} |
| 41 | + attrs['name'] = parsed_args.name |
| 42 | + attrs['service_type'] = parsed_args.service_type |
| 43 | + if parsed_args.description is not None: |
| 44 | + attrs['description'] = parsed_args.description |
| 45 | + if parsed_args.enable: |
| 46 | + attrs['enabled'] = True |
| 47 | + if parsed_args.disable: |
| 48 | + attrs['enabled'] = False |
| 49 | + if 'project' in parsed_args and parsed_args.project is not None: |
| 50 | + identity_client = client_manager.identity |
| 51 | + project_id = identity_common.find_project( |
| 52 | + identity_client, |
| 53 | + parsed_args.project, |
| 54 | + parsed_args.project_domain, |
| 55 | + ).id |
| 56 | + attrs['tenant_id'] = project_id |
| 57 | + |
| 58 | + return attrs |
| 59 | + |
| 60 | + |
| 61 | +# TODO(dasanind): Use the SDK resource mapped attribute names once the |
| 62 | +# OSC minimum requirements include SDK 1.0. |
| 63 | +class CreateNetworkFlavor(command.ShowOne): |
| 64 | + _description = _("Create new network flavor") |
| 65 | + |
| 66 | + def get_parser(self, prog_name): |
| 67 | + parser = super(CreateNetworkFlavor, self).get_parser(prog_name) |
| 68 | + parser.add_argument( |
| 69 | + 'name', |
| 70 | + metavar="<name>", |
| 71 | + help=_("Name for the flavor") |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + '--service-type', |
| 75 | + metavar="<service-type>", |
| 76 | + required=True, |
| 77 | + help=_('Service type to which the flavor applies to: e.g. VPN ' |
| 78 | + '(See openstack network service provider list for loaded ' |
| 79 | + 'examples.)') |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + '--description', |
| 83 | + help=_('Description for the flavor') |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + '--project', |
| 87 | + metavar="<project>", |
| 88 | + help=_("Owner's project (name or ID)") |
| 89 | + ) |
| 90 | + identity_common.add_project_domain_option_to_parser(parser) |
| 91 | + |
| 92 | + enable_group = parser.add_mutually_exclusive_group() |
| 93 | + enable_group.add_argument( |
| 94 | + '--enable', |
| 95 | + action='store_true', |
| 96 | + help=_("Enable the flavor (default)") |
| 97 | + ) |
| 98 | + enable_group.add_argument( |
| 99 | + '--disable', |
| 100 | + action='store_true', |
| 101 | + help=_("Disable the flavor") |
| 102 | + ) |
| 103 | + |
| 104 | + return parser |
| 105 | + |
| 106 | + def take_action(self, parsed_args): |
| 107 | + client = self.app.client_manager.network |
| 108 | + attrs = _get_attrs(self.app.client_manager, parsed_args) |
| 109 | + obj = client.create_flavor(**attrs) |
| 110 | + display_columns, columns = _get_columns(obj) |
| 111 | + data = utils.get_item_properties(obj, columns, formatters={}) |
| 112 | + |
| 113 | + return (display_columns, data) |
| 114 | + |
| 115 | + |
| 116 | +class DeleteNetworkFlavor(command.Command): |
| 117 | + _description = _("Delete network flavors") |
| 118 | + |
| 119 | + def get_parser(self, prog_name): |
| 120 | + parser = super(DeleteNetworkFlavor, self).get_parser(prog_name) |
| 121 | + |
| 122 | + parser.add_argument( |
| 123 | + 'flavor', |
| 124 | + metavar='<flavor>', |
| 125 | + nargs='+', |
| 126 | + help=_('Flavor(s) to delete (name or ID)') |
| 127 | + ) |
| 128 | + return parser |
| 129 | + |
| 130 | + def take_action(self, parsed_args): |
| 131 | + client = self.app.client_manager.network |
| 132 | + result = 0 |
| 133 | + |
| 134 | + for flavor in parsed_args.flavor: |
| 135 | + try: |
| 136 | + obj = client.find_flavor(flavor, ignore_missing=False) |
| 137 | + client.delete_flavor(obj) |
| 138 | + except Exception as e: |
| 139 | + result += 1 |
| 140 | + LOG.error(_("Failed to delete flavor with " |
| 141 | + "name or ID '%(flavor)s': %(e)s"), |
| 142 | + {"flavor": flavor, "e": e}) |
| 143 | + if result > 0: |
| 144 | + total = len(parsed_args.flavor) |
| 145 | + msg = (_("%(result)s of %(total)s flavors failed " |
| 146 | + "to delete.") % {"result": result, "total": total}) |
| 147 | + raise exceptions.CommandError(msg) |
| 148 | + |
| 149 | + |
| 150 | +class ListNetworkFlavor(command.Lister): |
| 151 | + _description = _("List network flavors") |
| 152 | + |
| 153 | + def take_action(self, parsed_args): |
| 154 | + client = self.app.client_manager.network |
| 155 | + |
| 156 | + columns = ( |
| 157 | + 'id', |
| 158 | + 'name', |
| 159 | + 'is_enabled', |
| 160 | + 'service_type', |
| 161 | + 'description' |
| 162 | + ) |
| 163 | + column_headers = ( |
| 164 | + 'ID', |
| 165 | + 'Name', |
| 166 | + 'Enabled', |
| 167 | + 'Service Type', |
| 168 | + 'Description' |
| 169 | + ) |
| 170 | + |
| 171 | + data = client.flavors() |
| 172 | + return (column_headers, |
| 173 | + (utils.get_item_properties( |
| 174 | + s, columns, |
| 175 | + ) for s in data)) |
| 176 | + |
| 177 | + |
| 178 | +# TODO(dasanind): Use only the SDK resource mapped attribute names once the |
| 179 | +# OSC minimum requirements include SDK 1.0. |
| 180 | +class SetNetworkFlavor(command.Command): |
| 181 | + _description = _("Set network flavor properties") |
| 182 | + |
| 183 | + def get_parser(self, prog_name): |
| 184 | + parser = super(SetNetworkFlavor, self).get_parser(prog_name) |
| 185 | + parser.add_argument( |
| 186 | + 'flavor', |
| 187 | + metavar="<flavor>", |
| 188 | + help=_("Flavor to update (name or ID)") |
| 189 | + ) |
| 190 | + parser.add_argument( |
| 191 | + '--description', |
| 192 | + help=_('Set network flavor description') |
| 193 | + ) |
| 194 | + enable_group = parser.add_mutually_exclusive_group() |
| 195 | + enable_group.add_argument( |
| 196 | + '--disable', |
| 197 | + action='store_true', |
| 198 | + help=_("Disable network flavor") |
| 199 | + ) |
| 200 | + enable_group.add_argument( |
| 201 | + '--enable', |
| 202 | + action='store_true', |
| 203 | + help=_("Enable network flavor") |
| 204 | + ) |
| 205 | + parser.add_argument( |
| 206 | + '--name', |
| 207 | + metavar="<name>", |
| 208 | + help=_('Set flavor name') |
| 209 | + ) |
| 210 | + |
| 211 | + return parser |
| 212 | + |
| 213 | + def take_action(self, parsed_args): |
| 214 | + client = self.app.client_manager.network |
| 215 | + obj = client.find_flavor( |
| 216 | + parsed_args.flavor, |
| 217 | + ignore_missing=False) |
| 218 | + attrs = {} |
| 219 | + if parsed_args.name is not None: |
| 220 | + attrs['name'] = parsed_args.name |
| 221 | + if parsed_args.description is not None: |
| 222 | + attrs['description'] = parsed_args.description |
| 223 | + if parsed_args.enable: |
| 224 | + attrs['enabled'] = True |
| 225 | + if parsed_args.disable: |
| 226 | + attrs['enabled'] = False |
| 227 | + client.update_flavor(obj, **attrs) |
| 228 | + |
| 229 | + |
| 230 | +class ShowNetworkFlavor(command.ShowOne): |
| 231 | + _description = _("Display network flavor details") |
| 232 | + |
| 233 | + def get_parser(self, prog_name): |
| 234 | + parser = super(ShowNetworkFlavor, self).get_parser(prog_name) |
| 235 | + parser.add_argument( |
| 236 | + 'flavor', |
| 237 | + metavar='<flavor>', |
| 238 | + help=_('Flavor to display (name or ID)') |
| 239 | + ) |
| 240 | + return parser |
| 241 | + |
| 242 | + def take_action(self, parsed_args): |
| 243 | + client = self.app.client_manager.network |
| 244 | + obj = client.find_flavor(parsed_args.flavor, ignore_missing=False) |
| 245 | + display_columns, columns = _get_columns(obj) |
| 246 | + data = utils.get_item_properties(obj, columns) |
| 247 | + return display_columns, data |
0 commit comments