| 1 |
# Copyright 2012 OpenStack Foundation. |
| 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 abc |
| 18 |
import argparse |
| 19 |
|
| 20 |
from cliff import lister |
| 21 |
from cliff import show |
| 22 |
from oslo_serialization import jsonutils |
| 23 |
|
| 24 |
from neutronclient._i18n import _ |
| 25 |
from neutronclient.common import exceptions |
| 26 |
from neutronclient.common import utils |
| 27 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 28 |
|
| 29 |
|
| 30 |
def get_tenant_id(args, client): |
| 31 |
return (args.pos_tenant_id or args.tenant_id or |
| 32 |
client.get_quotas_tenant()['tenant']['tenant_id']) |
| 33 |
|
| 34 |
|
| 35 |
class DeleteQuota(neutronV20.NeutronCommand): |
| 36 |
"""Delete defined quotas of a given tenant.""" |
| 37 |
|
| 38 |
resource = 'quota' |
| 39 |
|
| 40 |
def get_parser(self, prog_name): |
| 41 |
parser = super(DeleteQuota, self).get_parser(prog_name) |
| 42 |
parser.add_argument( |
| 43 |
'--tenant-id', metavar='tenant-id', |
| 44 |
help=_('The owner tenant ID.')) |
| 45 |
parser.add_argument( |
| 46 |
'--tenant_id', |
| 47 |
help=argparse.SUPPRESS) |
| 48 |
parser.add_argument( |
| 49 |
'pos_tenant_id', |
| 50 |
help=argparse.SUPPRESS, nargs='?') |
| 51 |
return parser |
| 52 |
|
| 53 |
def take_action(self, parsed_args): |
| 54 |
neutron_client = self.get_client() |
| 55 |
tenant_id = get_tenant_id(parsed_args, neutron_client) |
| 56 |
obj_deleter = getattr(neutron_client, |
| 57 |
"delete_%s" % self.resource) |
| 58 |
obj_deleter(tenant_id) |
| 59 |
print((_('Deleted %(resource)s: %(tenant_id)s') |
| 60 |
% {'tenant_id': tenant_id, |
| 61 |
'resource': self.resource}), |
| 62 |
file=self.app.stdout) |
| 63 |
return |
| 64 |
|
| 65 |
|
| 66 |
class ListQuota(neutronV20.NeutronCommand, lister.Lister): |
| 67 |
"""List quotas of all tenants who have non-default quota values.""" |
| 68 |
|
| 69 |
resource = 'quota' |
| 70 |
|
| 71 |
def get_parser(self, prog_name): |
| 72 |
parser = super(ListQuota, self).get_parser(prog_name) |
| 73 |
return parser |
| 74 |
|
| 75 |
def take_action(self, parsed_args): |
| 76 |
neutron_client = self.get_client() |
| 77 |
search_opts = {} |
| 78 |
self.log.debug('search options: %s', search_opts) |
| 79 |
obj_lister = getattr(neutron_client, |
| 80 |
"list_%ss" % self.resource) |
| 81 |
data = obj_lister(**search_opts) |
| 82 |
info = [] |
| 83 |
collection = self.resource + "s" |
| 84 |
if collection in data: |
| 85 |
info = data[collection] |
| 86 |
_columns = len(info) > 0 and sorted(info[0].keys()) or [] |
| 87 |
return (_columns, (utils.get_item_properties(s, _columns) |
| 88 |
for s in info)) |
| 89 |
|
| 90 |
|
| 91 |
class ShowQuotaBase(neutronV20.NeutronCommand, show.ShowOne): |
| 92 |
"""Base class to show quotas of a given tenant.""" |
| 93 |
|
| 94 |
resource = "quota" |
| 95 |
|
| 96 |
@abc.abstractmethod |
| 97 |
def retrieve_data(self, tenant_id, neutron_client): |
| 98 |
"""Retrieve data using neutron client for the given tenant.""" |
| 99 |
|
| 100 |
def get_parser(self, prog_name): |
| 101 |
parser = super(ShowQuotaBase, self).get_parser(prog_name) |
| 102 |
parser.add_argument( |
| 103 |
'--tenant-id', metavar='tenant-id', |
| 104 |
help=_('The owner tenant ID.')) |
| 105 |
parser.add_argument( |
| 106 |
'--tenant_id', |
| 107 |
help=argparse.SUPPRESS) |
| 108 |
# allow people to do neutron quota-show <tenant-id>. |
| 109 |
# we use a different name for this because the default will |
| 110 |
# override whatever is in the named arg otherwise. |
| 111 |
parser.add_argument( |
| 112 |
'pos_tenant_id', |
| 113 |
help=argparse.SUPPRESS, nargs='?') |
| 114 |
return parser |
| 115 |
|
| 116 |
def take_action(self, parsed_args): |
| 117 |
neutron_client = self.get_client() |
| 118 |
tenant_id = get_tenant_id(parsed_args, neutron_client) |
| 119 |
data = self.retrieve_data(tenant_id, neutron_client) |
| 120 |
if self.resource in data: |
| 121 |
return zip(*sorted(data[self.resource].items())) |
| 122 |
return |
| 123 |
|
| 124 |
|
| 125 |
class ShowQuota(ShowQuotaBase): |
| 126 |
"""Show quotas for a given tenant.""" |
| 127 |
|
| 128 |
def retrieve_data(self, tenant_id, neutron_client): |
| 129 |
return neutron_client.show_quota(tenant_id) |
| 130 |
|
| 131 |
|
| 132 |
class ShowQuotaDefault(ShowQuotaBase): |
| 133 |
"""Show default quotas for a given tenant.""" |
| 134 |
|
| 135 |
def retrieve_data(self, tenant_id, neutron_client): |
| 136 |
return neutron_client.show_quota_default(tenant_id) |
| 137 |
|
| 138 |
|
| 139 |
class UpdateQuota(neutronV20.NeutronCommand, show.ShowOne): |
| 140 |
"""Update a given tenant's quotas.""" |
| 141 |
|
| 142 |
resource = 'quota' |
| 143 |
|
| 144 |
def get_parser(self, prog_name): |
| 145 |
parser = super(UpdateQuota, self).get_parser(prog_name) |
| 146 |
parser.add_argument( |
| 147 |
'--tenant-id', metavar='tenant-id', |
| 148 |
help=_('The owner tenant ID.')) |
| 149 |
parser.add_argument( |
| 150 |
'--tenant_id', |
| 151 |
help=argparse.SUPPRESS) |
| 152 |
parser.add_argument( |
| 153 |
'--network', metavar='networks', |
| 154 |
help=_('The limit of networks.')) |
| 155 |
parser.add_argument( |
| 156 |
'--subnet', metavar='subnets', |
| 157 |
help=_('The limit of subnets.')) |
| 158 |
parser.add_argument( |
| 159 |
'--port', metavar='ports', |
| 160 |
help=_('The limit of ports.')) |
| 161 |
parser.add_argument( |
| 162 |
'--router', metavar='routers', |
| 163 |
help=_('The limit of routers.')) |
| 164 |
parser.add_argument( |
| 165 |
'--floatingip', metavar='floatingips', |
| 166 |
help=_('The limit of floating IPs.')) |
| 167 |
parser.add_argument( |
| 168 |
'--security-group', metavar='security_groups', |
| 169 |
help=_('The limit of security groups.')) |
| 170 |
parser.add_argument( |
| 171 |
'--security-group-rule', metavar='security_group_rules', |
| 172 |
help=_('The limit of security groups rules.')) |
| 173 |
parser.add_argument( |
| 174 |
'--vip', metavar='vips', |
| 175 |
help=_('The limit of vips.')) |
| 176 |
parser.add_argument( |
| 177 |
'--pool', metavar='pools', |
| 178 |
help=_('The limit of pools.')) |
| 179 |
parser.add_argument( |
| 180 |
'--member', metavar='members', |
| 181 |
help=_('The limit of pool members.')) |
| 182 |
parser.add_argument( |
| 183 |
'--health-monitor', metavar='health_monitors', |
| 184 |
dest='healthmonitor', |
| 185 |
help=_('The limit of health monitors.')) |
| 186 |
parser.add_argument( |
| 187 |
'--loadbalancer', metavar='loadbalancers', |
| 188 |
help=_('The limit of load balancers.')) |
| 189 |
parser.add_argument( |
| 190 |
'--listener', metavar='listeners', |
| 191 |
help=_('The limit of listeners.')) |
| 192 |
parser.add_argument( |
| 193 |
'--rbac-policy', metavar='rbac_policies', |
| 194 |
help=_('The limit of RBAC policies.')) |
| 195 |
parser.add_argument( |
| 196 |
'pos_tenant_id', |
| 197 |
help=argparse.SUPPRESS, nargs='?') |
| 198 |
|
| 199 |
return parser |
| 200 |
|
| 201 |
def _validate_int(self, name, value): |
| 202 |
try: |
| 203 |
return_value = int(value) |
| 204 |
except Exception: |
| 205 |
message = (_('Quota limit for %(name)s must be an integer') % |
| 206 |
{'name': name}) |
| 207 |
raise exceptions.CommandError(message=message) |
| 208 |
return return_value |
| 209 |
|
| 210 |
def args2body(self, parsed_args): |
| 211 |
quota = {} |
| 212 |
for resource in ('network', 'subnet', 'port', 'router', 'floatingip', |
| 213 |
'security_group', 'security_group_rule', |
| 214 |
'vip', 'pool', 'member', 'healthmonitor', |
| 215 |
'loadbalancer', 'listener', 'rbac_policy'): |
| 216 |
if getattr(parsed_args, resource): |
| 217 |
quota[resource] = self._validate_int( |
| 218 |
resource, |
| 219 |
getattr(parsed_args, resource)) |
| 220 |
if not quota: |
| 221 |
raise exceptions.CommandError( |
| 222 |
message=_('Must specify a valid resource with new quota ' |
| 223 |
'value')) |
| 224 |
return {self.resource: quota} |
| 225 |
|
| 226 |
def take_action(self, parsed_args): |
| 227 |
neutron_client = self.get_client() |
| 228 |
_extra_values = neutronV20.parse_args_to_dict(self.values_specs) |
| 229 |
neutronV20._merge_args(self, parsed_args, _extra_values, |
| 230 |
self.values_specs) |
| 231 |
body = self.args2body(parsed_args) |
| 232 |
if self.resource in body: |
| 233 |
body[self.resource].update(_extra_values) |
| 234 |
else: |
| 235 |
body[self.resource] = _extra_values |
| 236 |
obj_updator = getattr(neutron_client, |
| 237 |
"update_%s" % self.resource) |
| 238 |
tenant_id = get_tenant_id(parsed_args, neutron_client) |
| 239 |
data = obj_updator(tenant_id, body) |
| 240 |
if self.resource in data: |
| 241 |
for k, v in data[self.resource].items(): |
| 242 |
if isinstance(v, list): |
| 243 |
value = "" |
| 244 |
for _item in v: |
| 245 |
if value: |
| 246 |
value += "\n" |
| 247 |
if isinstance(_item, dict): |
| 248 |
value += jsonutils.dumps(_item) |
| 249 |
else: |
| 250 |
value += str(_item) |
| 251 |
data[self.resource][k] = value |
| 252 |
elif v is None: |
| 253 |
data[self.resource][k] = '' |
| 254 |
return zip(*sorted(data[self.resource].items())) |
| 255 |
else: |
| 256 |
return |