| 1 |
# Copyright 2015 Huawei Technologies India Pvt Ltd, Inc. |
| 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 |
|
| 17 |
import os |
| 18 |
|
| 19 |
from neutronclient._i18n import _ |
| 20 |
from neutronclient.neutron import v2_0 as neutronv20 |
| 21 |
|
| 22 |
|
| 23 |
def get_qos_policy_id(client, policy_id_or_name): |
| 24 |
_policy_id = neutronv20.find_resourceid_by_name_or_id( |
| 25 |
client, 'policy', policy_id_or_name, cmd_resource='qos_policy') |
| 26 |
return _policy_id |
| 27 |
|
| 28 |
|
| 29 |
class CreateQosPolicyMixin(object): |
| 30 |
def add_arguments_qos_policy(self, parser): |
| 31 |
qos_policy_args = parser.add_mutually_exclusive_group() |
| 32 |
qos_policy_args.add_argument( |
| 33 |
'--qos-policy', |
| 34 |
help=_('ID or name of the QoS policy that should' |
| 35 |
'be attached to the resource.')) |
| 36 |
return qos_policy_args |
| 37 |
|
| 38 |
def args2body_qos_policy(self, parsed_args, resource): |
| 39 |
if parsed_args.qos_policy: |
| 40 |
_policy_id = get_qos_policy_id(self.get_client(), |
| 41 |
parsed_args.qos_policy) |
| 42 |
resource['qos_policy_id'] = _policy_id |
| 43 |
|
| 44 |
|
| 45 |
class UpdateQosPolicyMixin(CreateQosPolicyMixin): |
| 46 |
def add_arguments_qos_policy(self, parser): |
| 47 |
qos_policy_args = (super(UpdateQosPolicyMixin, self). |
| 48 |
add_arguments_qos_policy(parser)) |
| 49 |
qos_policy_args.add_argument( |
| 50 |
'--no-qos-policy', |
| 51 |
action='store_true', |
| 52 |
help=_('Detach QoS policy from the resource.')) |
| 53 |
return qos_policy_args |
| 54 |
|
| 55 |
def args2body_qos_policy(self, parsed_args, resource): |
| 56 |
super(UpdateQosPolicyMixin, self).args2body_qos_policy(parsed_args, |
| 57 |
resource) |
| 58 |
if parsed_args.no_qos_policy: |
| 59 |
resource['qos_policy_id'] = None |
| 60 |
|
| 61 |
|
| 62 |
class ListQoSPolicy(neutronv20.ListCommand): |
| 63 |
"""List QoS policies that belong to a given tenant connection.""" |
| 64 |
|
| 65 |
resource = 'policy' |
| 66 |
shadow_resource = 'qos_policy' |
| 67 |
list_columns = ['id', 'name'] |
| 68 |
pagination_support = True |
| 69 |
sorting_support = True |
| 70 |
|
| 71 |
|
| 72 |
class ShowQoSPolicy(neutronv20.ShowCommand): |
| 73 |
"""Show information of a given qos policy.""" |
| 74 |
|
| 75 |
resource = 'policy' |
| 76 |
shadow_resource = 'qos_policy' |
| 77 |
|
| 78 |
def format_output_data(self, data): |
| 79 |
rules = [] |
| 80 |
for rule in data['policy'].get('rules', []): |
| 81 |
rules.append("%s (type: %s)" % (rule['id'], rule['type'])) |
| 82 |
data['policy']['rules'] = os.linesep.join(rules) |
| 83 |
|
| 84 |
super(ShowQoSPolicy, self).format_output_data(data) |
| 85 |
|
| 86 |
|
| 87 |
class CreateQoSPolicy(neutronv20.CreateCommand): |
| 88 |
"""Create a qos policy.""" |
| 89 |
|
| 90 |
resource = 'policy' |
| 91 |
shadow_resource = 'qos_policy' |
| 92 |
|
| 93 |
def add_known_arguments(self, parser): |
| 94 |
parser.add_argument( |
| 95 |
'name', metavar='NAME', |
| 96 |
help=_('Name of the QoS policy to be created.')) |
| 97 |
parser.add_argument( |
| 98 |
'--description', |
| 99 |
help=_('Description of the QoS policy to be created.')) |
| 100 |
parser.add_argument( |
| 101 |
'--shared', |
| 102 |
action='store_true', |
| 103 |
help=_('Accessible by other tenants. ' |
| 104 |
'Set shared to True (default is False).')) |
| 105 |
|
| 106 |
def args2body(self, parsed_args): |
| 107 |
body = {'name': parsed_args.name} |
| 108 |
if parsed_args.description: |
| 109 |
body['description'] = parsed_args.description |
| 110 |
if parsed_args.shared: |
| 111 |
body['shared'] = parsed_args.shared |
| 112 |
if parsed_args.tenant_id: |
| 113 |
body['tenant_id'] = parsed_args.tenant_id |
| 114 |
return {self.resource: body} |
| 115 |
|
| 116 |
|
| 117 |
class UpdateQoSPolicy(neutronv20.UpdateCommand): |
| 118 |
"""Update a given qos policy.""" |
| 119 |
|
| 120 |
resource = 'policy' |
| 121 |
shadow_resource = 'qos_policy' |
| 122 |
|
| 123 |
def add_known_arguments(self, parser): |
| 124 |
parser.add_argument( |
| 125 |
'--name', |
| 126 |
help=_('Name of the QoS policy.')) |
| 127 |
parser.add_argument( |
| 128 |
'--description', |
| 129 |
help=_('Description of the QoS policy.')) |
| 130 |
shared_group = parser.add_mutually_exclusive_group() |
| 131 |
shared_group.add_argument( |
| 132 |
'--shared', |
| 133 |
action='store_true', |
| 134 |
help=_('Accessible by other tenants. ' |
| 135 |
'Set shared to True (default is False).')) |
| 136 |
shared_group.add_argument( |
| 137 |
'--no-shared', |
| 138 |
action='store_true', |
| 139 |
help=_('Not accessible by other tenants. ' |
| 140 |
'Set shared to False.')) |
| 141 |
|
| 142 |
def args2body(self, parsed_args): |
| 143 |
body = {} |
| 144 |
if parsed_args.name: |
| 145 |
body['name'] = parsed_args.name |
| 146 |
if parsed_args.description: |
| 147 |
body['description'] = parsed_args.description |
| 148 |
if parsed_args.shared: |
| 149 |
body['shared'] = True |
| 150 |
if parsed_args.no_shared: |
| 151 |
body['shared'] = False |
| 152 |
|
| 153 |
return {self.resource: body} |
| 154 |
|
| 155 |
|
| 156 |
class DeleteQoSPolicy(neutronv20.DeleteCommand): |
| 157 |
"""Delete a given qos policy.""" |
| 158 |
|
| 159 |
resource = 'policy' |
| 160 |
shadow_resource = 'qos_policy' |