|
| 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 | +"""Metering Label 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 | +LOG = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def _get_columns(item): |
| 30 | + column_map = { |
| 31 | + 'is_shared': 'shared', |
| 32 | + 'tenant_id': 'project_id', |
| 33 | + } |
| 34 | + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) |
| 35 | + |
| 36 | + |
| 37 | +def _get_attrs(client_manager, parsed_args): |
| 38 | + attrs = {} |
| 39 | + |
| 40 | + if parsed_args.description is not None: |
| 41 | + attrs['description'] = parsed_args.description |
| 42 | + if parsed_args.project is not None and 'project' in parsed_args: |
| 43 | + identity_client = client_manager.identity |
| 44 | + project_id = identity_common.find_project( |
| 45 | + identity_client, |
| 46 | + parsed_args.project, |
| 47 | + parsed_args.project_domain, |
| 48 | + ).id |
| 49 | + attrs['tenant_id'] = project_id |
| 50 | + if parsed_args.share: |
| 51 | + attrs['shared'] = True |
| 52 | + if parsed_args.no_share: |
| 53 | + attrs['shared'] = False |
| 54 | + if parsed_args.name is not None: |
| 55 | + attrs['name'] = parsed_args.name |
| 56 | + |
| 57 | + return attrs |
| 58 | + |
| 59 | + |
| 60 | +# TODO(ankur-gupta-f): Use the SDK resource mapped attribute names once the |
| 61 | +# OSC minimum requirements include SDK 1.0. |
| 62 | +class CreateMeter(command.ShowOne): |
| 63 | + _description = _("Create network meter") |
| 64 | + |
| 65 | + def get_parser(self, prog_name): |
| 66 | + parser = super(CreateMeter, self).get_parser(prog_name) |
| 67 | + |
| 68 | + parser.add_argument( |
| 69 | + '--description', |
| 70 | + metavar='<description>', |
| 71 | + help=_("Create description for meter") |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + '--project', |
| 75 | + metavar='<project>', |
| 76 | + help=_("Owner's project (name or ID)") |
| 77 | + ) |
| 78 | + |
| 79 | + identity_common.add_project_domain_option_to_parser(parser) |
| 80 | + share_group = parser.add_mutually_exclusive_group() |
| 81 | + share_group.add_argument( |
| 82 | + '--share', |
| 83 | + action='store_true', |
| 84 | + default=None, |
| 85 | + help=_("Share meter between projects") |
| 86 | + ) |
| 87 | + share_group.add_argument( |
| 88 | + '--no-share', |
| 89 | + action='store_true', |
| 90 | + help=_("Do not share meter between projects") |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + 'name', |
| 94 | + metavar='<name>', |
| 95 | + help=_('Name of meter'), |
| 96 | + ) |
| 97 | + |
| 98 | + return parser |
| 99 | + |
| 100 | + def take_action(self, parsed_args): |
| 101 | + client = self.app.client_manager.network |
| 102 | + attrs = _get_attrs(self.app.client_manager, parsed_args) |
| 103 | + obj = client.create_metering_label(**attrs) |
| 104 | + display_columns, columns = _get_columns(obj) |
| 105 | + data = utils.get_item_properties(obj, columns, formatters={}) |
| 106 | + |
| 107 | + return (display_columns, data) |
| 108 | + |
| 109 | + |
| 110 | +# TODO(ankur-gupta-f): Use the SDK resource mapped attribute names once the |
| 111 | +# OSC minimum requirements include SDK 1.0. |
| 112 | +class DeleteMeter(command.Command): |
| 113 | + _description = _("Delete network meter") |
| 114 | + |
| 115 | + def get_parser(self, prog_name): |
| 116 | + parser = super(DeleteMeter, self).get_parser(prog_name) |
| 117 | + |
| 118 | + parser.add_argument( |
| 119 | + 'meter', |
| 120 | + metavar='<meter>', |
| 121 | + nargs='+', |
| 122 | + help=_('Meter to delete (name or ID)') |
| 123 | + ) |
| 124 | + return parser |
| 125 | + |
| 126 | + def take_action(self, parsed_args): |
| 127 | + client = self.app.client_manager.network |
| 128 | + result = 0 |
| 129 | + |
| 130 | + for meter in parsed_args.meter: |
| 131 | + try: |
| 132 | + obj = client.find_metering_label(meter, ignore_missing=False) |
| 133 | + client.delete_metering_label(obj) |
| 134 | + except Exception as e: |
| 135 | + result += 1 |
| 136 | + LOG.error(_("Failed to delete meter with " |
| 137 | + "ID '%(meter)s': %(e)s"), |
| 138 | + {"meter": meter, "e": e}) |
| 139 | + if result > 0: |
| 140 | + total = len(parsed_args.meter) |
| 141 | + msg = (_("%(result)s of %(total)s meters failed " |
| 142 | + "to delete.") % {"result": result, "total": total}) |
| 143 | + raise exceptions.CommandError(msg) |
| 144 | + |
| 145 | + |
| 146 | +class ListMeter(command.Lister): |
| 147 | + _description = _("List network meters") |
| 148 | + |
| 149 | + def take_action(self, parsed_args): |
| 150 | + client = self.app.client_manager.network |
| 151 | + |
| 152 | + columns = ( |
| 153 | + 'id', |
| 154 | + 'name', |
| 155 | + 'description', |
| 156 | + 'shared', |
| 157 | + ) |
| 158 | + column_headers = ( |
| 159 | + 'ID', |
| 160 | + 'Name', |
| 161 | + 'Description', |
| 162 | + 'Shared', |
| 163 | + ) |
| 164 | + |
| 165 | + data = client.metering_labels() |
| 166 | + return (column_headers, |
| 167 | + (utils.get_item_properties( |
| 168 | + s, columns, |
| 169 | + ) for s in data)) |
| 170 | + |
| 171 | + |
| 172 | +class ShowMeter(command.ShowOne): |
| 173 | + _description = _("Show network meter") |
| 174 | + |
| 175 | + def get_parser(self, prog_name): |
| 176 | + parser = super(ShowMeter, self).get_parser(prog_name) |
| 177 | + parser.add_argument( |
| 178 | + 'meter', |
| 179 | + metavar='<meter>', |
| 180 | + help=_('Meter to display (name or ID)') |
| 181 | + ) |
| 182 | + return parser |
| 183 | + |
| 184 | + def take_action(self, parsed_args): |
| 185 | + client = self.app.client_manager.network |
| 186 | + obj = client.find_metering_label(parsed_args.meter, |
| 187 | + ignore_missing=False) |
| 188 | + display_columns, columns = _get_columns(obj) |
| 189 | + data = utils.get_item_properties(obj, columns) |
| 190 | + return display_columns, data |
0 commit comments