|
| 1 | +# Copyright (c) 2016, Intel Corporation. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 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 | + |
| 25 | + |
| 26 | +LOG = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def _get_columns(item): |
| 30 | + columns = list(item.keys()) |
| 31 | + if 'tenant_id' in columns: |
| 32 | + columns.remove('tenant_id') |
| 33 | + columns.append('project_id') |
| 34 | + return tuple(sorted(columns)) |
| 35 | + |
| 36 | + |
| 37 | +def _get_attrs(client_manager, parsed_args): |
| 38 | + attrs = {} |
| 39 | + if parsed_args.name is not None: |
| 40 | + attrs['name'] = str(parsed_args.name) |
| 41 | + if parsed_args.description is not None: |
| 42 | + attrs['description'] = parsed_args.description |
| 43 | + if parsed_args.share: |
| 44 | + attrs['shared'] = True |
| 45 | + if parsed_args.no_share: |
| 46 | + attrs['shared'] = False |
| 47 | + if parsed_args.project is not None: |
| 48 | + identity_client = client_manager.identity |
| 49 | + project_id = identity_common.find_project( |
| 50 | + identity_client, |
| 51 | + parsed_args.project, |
| 52 | + parsed_args.project_domain, |
| 53 | + ).id |
| 54 | + attrs['tenant_id'] = project_id |
| 55 | + |
| 56 | + return attrs |
| 57 | + |
| 58 | + |
| 59 | +class CreateNetworkQosPolicy(command.ShowOne): |
| 60 | + """Create a QoS policy""" |
| 61 | + |
| 62 | + def get_parser(self, prog_name): |
| 63 | + parser = super(CreateNetworkQosPolicy, self).get_parser(prog_name) |
| 64 | + parser.add_argument( |
| 65 | + 'name', |
| 66 | + metavar='<name>', |
| 67 | + help=_("Name of QoS policy to create") |
| 68 | + ) |
| 69 | + parser.add_argument( |
| 70 | + '--description', |
| 71 | + metavar='<description>', |
| 72 | + help=_("Description of the QoS policy") |
| 73 | + ) |
| 74 | + share_group = parser.add_mutually_exclusive_group() |
| 75 | + share_group.add_argument( |
| 76 | + '--share', |
| 77 | + action='store_true', |
| 78 | + default=None, |
| 79 | + help=_("Make the QoS policy accessible by other projects") |
| 80 | + ) |
| 81 | + share_group.add_argument( |
| 82 | + '--no-share', |
| 83 | + action='store_true', |
| 84 | + help=_("Make the QoS policy not accessible by other projects " |
| 85 | + "(default)") |
| 86 | + ) |
| 87 | + parser.add_argument( |
| 88 | + '--project', |
| 89 | + metavar='<project>', |
| 90 | + help=_("Owner's project (name or ID)") |
| 91 | + ) |
| 92 | + identity_common.add_project_domain_option_to_parser(parser) |
| 93 | + return parser |
| 94 | + |
| 95 | + def take_action(self, parsed_args): |
| 96 | + client = self.app.client_manager.network |
| 97 | + attrs = _get_attrs(self.app.client_manager, parsed_args) |
| 98 | + obj = client.create_qos_policy(**attrs) |
| 99 | + columns = _get_columns(obj) |
| 100 | + data = utils.get_item_properties(obj, columns, formatters={}) |
| 101 | + return columns, data |
| 102 | + |
| 103 | + |
| 104 | +class DeleteNetworkQosPolicy(command.Command): |
| 105 | + """Delete Qos Policy(s)""" |
| 106 | + |
| 107 | + def get_parser(self, prog_name): |
| 108 | + parser = super(DeleteNetworkQosPolicy, self).get_parser(prog_name) |
| 109 | + parser.add_argument( |
| 110 | + 'policy', |
| 111 | + metavar="<qos-policy>", |
| 112 | + nargs="+", |
| 113 | + help=_("QoS policy(s) to delete (name or ID)") |
| 114 | + ) |
| 115 | + return parser |
| 116 | + |
| 117 | + def take_action(self, parsed_args): |
| 118 | + client = self.app.client_manager.network |
| 119 | + result = 0 |
| 120 | + |
| 121 | + for policy in parsed_args.policy: |
| 122 | + try: |
| 123 | + obj = client.find_qos_policy(policy, ignore_missing=False) |
| 124 | + client.delete_qos_policy(obj) |
| 125 | + except Exception as e: |
| 126 | + result += 1 |
| 127 | + LOG.error(_("Failed to delete QoS policy " |
| 128 | + "name or ID '%(qos_policy)s': %(e)s"), |
| 129 | + {'qos_policy': policy, 'e': e}) |
| 130 | + |
| 131 | + if result > 0: |
| 132 | + total = len(parsed_args.policy) |
| 133 | + msg = (_("%(result)s of %(total)s QoS policies failed " |
| 134 | + "to delete.") % {'result': result, 'total': total}) |
| 135 | + raise exceptions.CommandError(msg) |
| 136 | + |
| 137 | + |
| 138 | +class ListNetworkQosPolicy(command.Lister): |
| 139 | + """List QoS policies""" |
| 140 | + |
| 141 | + def take_action(self, parsed_args): |
| 142 | + client = self.app.client_manager.network |
| 143 | + columns = ( |
| 144 | + 'id', |
| 145 | + 'name', |
| 146 | + 'shared', |
| 147 | + 'tenant_id', |
| 148 | + ) |
| 149 | + column_headers = ( |
| 150 | + 'ID', |
| 151 | + 'Name', |
| 152 | + 'Shared', |
| 153 | + 'Project', |
| 154 | + ) |
| 155 | + data = client.qos_policies() |
| 156 | + |
| 157 | + return (column_headers, |
| 158 | + (utils.get_item_properties( |
| 159 | + s, columns, formatters={}, |
| 160 | + ) for s in data)) |
| 161 | + |
| 162 | + |
| 163 | +class SetNetworkQosPolicy(command.Command): |
| 164 | + """Set QoS policy properties""" |
| 165 | + |
| 166 | + def get_parser(self, prog_name): |
| 167 | + parser = super(SetNetworkQosPolicy, self).get_parser(prog_name) |
| 168 | + parser.add_argument( |
| 169 | + 'policy', |
| 170 | + metavar="<qos-policy>", |
| 171 | + help=_("QoS policy to modify (name or ID)") |
| 172 | + ) |
| 173 | + parser.add_argument( |
| 174 | + '--name', |
| 175 | + metavar="<name>", |
| 176 | + help=_('Set QoS policy name') |
| 177 | + ) |
| 178 | + parser.add_argument( |
| 179 | + '--description', |
| 180 | + metavar='<description>', |
| 181 | + help=_("Description of the QoS policy") |
| 182 | + ) |
| 183 | + enable_group = parser.add_mutually_exclusive_group() |
| 184 | + enable_group.add_argument( |
| 185 | + '--share', |
| 186 | + action='store_true', |
| 187 | + help=_('Make the QoS policy accessible by other projects'), |
| 188 | + ) |
| 189 | + enable_group.add_argument( |
| 190 | + '--no-share', |
| 191 | + action='store_true', |
| 192 | + help=_('Make the QoS policy not accessible by other projects'), |
| 193 | + ) |
| 194 | + return parser |
| 195 | + |
| 196 | + def take_action(self, parsed_args): |
| 197 | + client = self.app.client_manager.network |
| 198 | + obj = client.find_qos_policy( |
| 199 | + parsed_args.policy, |
| 200 | + ignore_missing=False) |
| 201 | + attrs = {} |
| 202 | + if parsed_args.name is not None: |
| 203 | + attrs['name'] = parsed_args.name |
| 204 | + if parsed_args.share: |
| 205 | + attrs['shared'] = True |
| 206 | + if parsed_args.no_share: |
| 207 | + attrs['shared'] = False |
| 208 | + if parsed_args.description is not None: |
| 209 | + attrs['description'] = parsed_args.description |
| 210 | + client.update_qos_policy(obj, **attrs) |
| 211 | + |
| 212 | + |
| 213 | +class ShowNetworkQosPolicy(command.ShowOne): |
| 214 | + """Display QoS policy details""" |
| 215 | + |
| 216 | + def get_parser(self, prog_name): |
| 217 | + parser = super(ShowNetworkQosPolicy, self).get_parser(prog_name) |
| 218 | + parser.add_argument( |
| 219 | + 'policy', |
| 220 | + metavar="<qos-policy>", |
| 221 | + help=_("QoS policy to display (name or ID)") |
| 222 | + ) |
| 223 | + return parser |
| 224 | + |
| 225 | + def take_action(self, parsed_args): |
| 226 | + client = self.app.client_manager.network |
| 227 | + obj = client.find_qos_policy(parsed_args.policy, |
| 228 | + ignore_missing=False) |
| 229 | + columns = _get_columns(obj) |
| 230 | + data = utils.get_item_properties(obj, columns) |
| 231 | + return columns, data |
0 commit comments