| 1 |
# Copyright (c) 2016 Catalyst IT Ltd. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import collections |
| 16 |
import json |
| 17 |
import logging |
| 18 |
import requests |
| 19 |
from urllib.parse import urljoin |
| 20 |
|
| 21 |
from django.conf import settings |
| 22 |
from django.utils.translation import gettext_lazy as _ |
| 23 |
|
| 24 |
from horizon import exceptions |
| 25 |
from horizon.utils import functions as utils |
| 26 |
from horizon.utils import memoized |
| 27 |
|
| 28 |
from openstack_dashboard.api import base |
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
USER = collections.namedtuple('User', |
| 32 |
['id', 'name', 'email', |
| 33 |
'roles', 'cohort', 'status']) |
| 34 |
TOKEN = collections.namedtuple('Token', |
| 35 |
['action']) |
| 36 |
|
| 37 |
TASK = collections.namedtuple('Task', |
| 38 |
['id', 'task_type', 'valid', |
| 39 |
'request_by', 'request_project', |
| 40 |
'created_on', 'approved_on', 'page', |
| 41 |
'completed_on', 'actions', 'status']) |
| 42 |
|
| 43 |
NOTIFICATION = collections.namedtuple('Notification', |
| 44 |
['uuid', 'notes', 'error', 'created_on', |
| 45 |
'acknowledged', 'task']) |
| 46 |
|
| 47 |
QUOTA_SIZE = collections.namedtuple('QuotaSize', |
| 48 |
['id', 'name', 'cinder', |
| 49 |
'nova', 'neutron']) |
| 50 |
|
| 51 |
REGION_QUOTA = collections.namedtuple('RegionQuota', |
| 52 |
['id', 'region', |
| 53 |
'quota_size', 'preapproved_quotas']) |
| 54 |
|
| 55 |
REGION_QUOTA_VALUE = collections.namedtuple('RegionQuotaValue', |
| 56 |
['id', 'name', |
| 57 |
'service', 'current_quota', |
| 58 |
'current_usage', 'percent', |
| 59 |
'size_blob', 'important']) |
| 60 |
|
| 61 |
SIZE_QUOTA_VALUE = collections.namedtuple('SizeQuotaValue', |
| 62 |
['id', 'name', 'service', |
| 63 |
'value', 'current_quota', |
| 64 |
'current_usage', 'percent']) |
| 65 |
|
| 66 |
QUOTA_TASK = collections.namedtuple( |
| 67 |
'QuotaTask', |
| 68 |
['id', 'regions', 'size', 'user', 'created', 'valid', 'status']) |
| 69 |
|
| 70 |
|
| 71 |
# NOTE(amelia): A list of quota names that we consider to be the most |
| 72 |
# relevant to customers to be shown initially on the update page. |
| 73 |
# These can be overriden in the local_settings file: |
| 74 |
# IMPORTANT_QUOTAS = {<service>: [<quota_name>], } |
| 75 |
IMPORTANT_QUOTAS = { |
| 76 |
'nova': [ |
| 77 |
'instances', 'cores', 'ram', |
| 78 |
], |
| 79 |
'cinder': [ |
| 80 |
'volumes', 'snapshots', 'gigabytes', |
| 81 |
], |
| 82 |
'neutron': [ |
| 83 |
'network', 'floatingip', 'router', 'security_group', |
| 84 |
], |
| 85 |
'octavia': [ |
| 86 |
'load_balancer', |
| 87 |
], |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
# NOTE(adriant): Quotas that should be hidden by default. |
| 92 |
# Can be overriden in the local_settings file by setting: |
| 93 |
# HIDDEN_QUOTAS = {<service>: [<quota_name>], } |
| 94 |
# or disabled entirely with: HIDDEN_QUOTAS = {} |
| 95 |
HIDDEN_QUOTAS = { |
| 96 |
# these values have long since been deprecated from Nova |
| 97 |
'nova': [ |
| 98 |
'security_groups', 'security_group_rules', |
| 99 |
'floating_ips', 'fixed_ips', |
| 100 |
], |
| 101 |
# these by default have no limit |
| 102 |
'cinder': [ |
| 103 |
'per_volume_gigabytes', 'volumes_lvmdriver-1', |
| 104 |
'gigabytes_lvmdriver-1', 'snapshots_lvmdriver-1', |
| 105 |
|
| 106 |
], |
| 107 |
'neutron': [ |
| 108 |
'subnetpool', |
| 109 |
], |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
ROLE_TRANSLATIONS = { |
| 114 |
'project_admin': _('Project Administrator'), |
| 115 |
'project_mod': _('Project Moderator'), |
| 116 |
'_member_': _('Project Member'), |
| 117 |
'Member': _('Project Member'), |
| 118 |
'heat_stack_owner': _('Heat Stack Owner'), |
| 119 |
'project_readonly': _('Project Read-only'), |
| 120 |
'compute_start_stop': _('Compute Start/Stop'), |
| 121 |
'object_storage': _('Object Storage') |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
def get_role_text(name): |
| 126 |
# Gets the role text for a given role. |
| 127 |
# If it doesn't exist will simply return the role name. |
| 128 |
role_translations = getattr(settings, 'ROLE_TRANSLATIONS', None) |
| 129 |
if role_translations is None: |
| 130 |
role_translations = ROLE_TRANSLATIONS |
| 131 |
if name in role_translations: |
| 132 |
return role_translations[name].format() |
| 133 |
return name |
| 134 |
|
| 135 |
|
| 136 |
SERVICE_TRANSLATIONS = { |
| 137 |
'cinder': _('Volume'), |
| 138 |
'neutron': _('Networking'), |
| 139 |
'nova': _('Compute'), |
| 140 |
'octavia': _('Load Balancer'), |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
def get_service_type(name): |
| 145 |
# Takes service names and returns a 'nice' name of where they |
| 146 |
# are from |
| 147 |
service_translations = getattr(settings, 'SERVICE_TRANSLATIONS', None) |
| 148 |
if service_translations is None: |
| 149 |
service_translations = SERVICE_TRANSLATIONS |
| 150 |
if name in service_translations: |
| 151 |
return service_translations[name].format() |
| 152 |
return name |
| 153 |
|
| 154 |
|
| 155 |
class AdjutantApiError(BaseException): |
| 156 |
pass |
| 157 |
|
| 158 |
|
| 159 |
def _get_endpoint_url(request): |
| 160 |
# If the request is made by an anonymous user, this endpoint request fails. |
| 161 |
# Thus, we must hardcode this in Horizon. |
| 162 |
if getattr(request.user, "service_catalog", None): |
| 163 |
try: |
| 164 |
url = base.url_for(request, service_type='admin-logic') |
| 165 |
except exceptions.ServiceCatalogException: |
| 166 |
url = base.url_for(request, service_type='registration') |
| 167 |
else: |
| 168 |
try: |
| 169 |
url = getattr(settings, 'OPENSTACK_ADJUTANT_URL') |
| 170 |
except AttributeError: |
| 171 |
url = getattr(settings, 'OPENSTACK_REGISTRATION_URL') |
| 172 |
|
| 173 |
# Ensure ends in slash |
| 174 |
if not url.endswith('/'): |
| 175 |
url += '/' |
| 176 |
|
| 177 |
return url |
| 178 |
|
| 179 |
|
| 180 |
def _request(request, method, url, headers, **kwargs): |
| 181 |
try: |
| 182 |
endpoint_url = _get_endpoint_url(request) |
| 183 |
url = urljoin(endpoint_url, url) |
| 184 |
session = requests.Session() |
| 185 |
data = kwargs.pop("data", None) |
| 186 |
return session.request(method, url, headers=headers, |
| 187 |
data=data, **kwargs) |
| 188 |
except Exception as e: |
| 189 |
LOG.error(e) |
| 190 |
raise |
| 191 |
|
| 192 |
|
| 193 |
def head(request, url, **kwargs): |
| 194 |
return _request(request, 'HEAD', url, **kwargs) |
| 195 |
|
| 196 |
|
| 197 |
def get(request, url, **kwargs): |
| 198 |
return _request(request, 'GET', url, **kwargs) |
| 199 |
|
| 200 |
|
| 201 |
def post(request, url, **kwargs): |
| 202 |
return _request(request, 'POST', url, **kwargs) |
| 203 |
|
| 204 |
|
| 205 |
def put(request, url, **kwargs): |
| 206 |
return _request(request, 'PUT', url, **kwargs) |
| 207 |
|
| 208 |
|
| 209 |
def patch(request, url, **kwargs): |
| 210 |
return _request(request, 'PATCH', url, **kwargs) |
| 211 |
|
| 212 |
|
| 213 |
def delete(request, url, **kwargs): |
| 214 |
return _request(request, 'DELETE', url, **kwargs) |
| 215 |
|
| 216 |
|
| 217 |
def user_invite(request, user): |
| 218 |
headers = {'Content-Type': 'application/json', |
| 219 |
'X-Auth-Token': request.user.token.id} |
| 220 |
user['project_id'] = request.user.tenant_id |
| 221 |
return post(request, 'openstack/users', |
| 222 |
headers=headers, data=json.dumps(user)) |
| 223 |
|
| 224 |
|
| 225 |
def user_list(request): |
| 226 |
users = [] |
| 227 |
try: |
| 228 |
headers = {'Content-Type': 'application/json', |
| 229 |
'X-Auth-Token': request.user.token.id} |
| 230 |
resp = json.loads(get(request, 'openstack/users', |
| 231 |
headers=headers).content) |
| 232 |
|
| 233 |
for user in resp['users']: |
| 234 |
users.append( |
| 235 |
USER( |
| 236 |
id=user['id'], |
| 237 |
name=user['name'], |
| 238 |
email=user['email'], |
| 239 |
roles=user['roles'], |
| 240 |
status=user['status'], |
| 241 |
cohort=user['cohort'] |
| 242 |
) |
| 243 |
) |
| 244 |
except Exception as e: |
| 245 |
LOG.error(e) |
| 246 |
raise |
| 247 |
return users |
| 248 |
|
| 249 |
|
| 250 |
def user_get(request, user_id): |
| 251 |
try: |
| 252 |
headers = {'X-Auth-Token': request.user.token.id} |
| 253 |
resp = get(request, 'openstack/users/%s' % user_id, |
| 254 |
headers=headers).content |
| 255 |
return json.loads(resp) |
| 256 |
except Exception as e: |
| 257 |
LOG.error(e) |
| 258 |
raise |
| 259 |
|
| 260 |
|
| 261 |
def user_roles_update(request, user): |
| 262 |
try: |
| 263 |
headers = {'Content-Type': 'application/json', |
| 264 |
'X-Auth-Token': request.user.token.id} |
| 265 |
user['project_id'] = request.user.tenant_id |
| 266 |
user['roles'] = user.roles |
| 267 |
return put(request, 'openstack/users/%s/roles' % user['id'], |
| 268 |
headers=headers, |
| 269 |
data=json.dumps(user)) |
| 270 |
except Exception as e: |
| 271 |
LOG.error(e) |
| 272 |
raise |
| 273 |
|
| 274 |
|
| 275 |
def user_roles_add(request, user_id, roles): |
| 276 |
try: |
| 277 |
headers = {'Content-Type': 'application/json', |
| 278 |
'X-Auth-Token': request.user.token.id} |
| 279 |
params = {} |
| 280 |
params['project_id'] = request.user.tenant_id |
| 281 |
params['roles'] = roles |
| 282 |
return put(request, 'openstack/users/%s/roles' % user_id, |
| 283 |
headers=headers, |
| 284 |
data=json.dumps(params)) |
| 285 |
except Exception as e: |
| 286 |
LOG.error(e) |
| 287 |
raise |
| 288 |
|
| 289 |
|
| 290 |
def user_roles_remove(request, user_id, roles): |
| 291 |
try: |
| 292 |
headers = {'Content-Type': 'application/json', |
| 293 |
'X-Auth-Token': request.user.token.id} |
| 294 |
params = {} |
| 295 |
params['project_id'] = request.user.tenant_id |
| 296 |
params['roles'] = roles |
| 297 |
return delete(request, 'openstack/users/%s/roles' % user_id, |
| 298 |
headers=headers, |
| 299 |
data=json.dumps(params)) |
| 300 |
except Exception as e: |
| 301 |
LOG.error(e) |
| 302 |
raise |
| 303 |
|
| 304 |
|
| 305 |
def user_revoke(request, user_id): |
| 306 |
try: |
| 307 |
headers = {'Content-Type': 'application/json', |
| 308 |
'X-Auth-Token': request.user.token.id} |
| 309 |
data = dict() |
| 310 |
return delete(request, 'openstack/users/%s' % user_id, |
| 311 |
headers=headers, |
| 312 |
data=json.dumps(data)) |
| 313 |
except Exception as e: |
| 314 |
LOG.error(e) |
| 315 |
raise |
| 316 |
|
| 317 |
|
| 318 |
def user_invitation_resend(request, user_id): |
| 319 |
headers = {'Content-Type': 'application/json', |
| 320 |
'X-Auth-Token': request.user.token.id} |
| 321 |
# For non-active users, the task id is the same as their userid |
| 322 |
# For active users, re-sending an invitation doesn't make sense. |
| 323 |
data = { |
| 324 |
"task": user_id |
| 325 |
} |
| 326 |
return post(request, 'tokens', |
| 327 |
headers=headers, |
| 328 |
data=json.dumps(data)) |
| 329 |
|
| 330 |
|
| 331 |
def valid_roles_get(request): |
| 332 |
headers = {'Content-Type': 'application/json', |
| 333 |
'X-Auth-Token': request.user.token.id} |
| 334 |
role_data = get(request, 'openstack/roles', headers=headers) |
| 335 |
return role_data.json() |
| 336 |
|
| 337 |
|
| 338 |
def valid_role_names_get(request): |
| 339 |
roles_data = valid_roles_get(request) |
| 340 |
role_names = [r['name'] for r in roles_data['roles']] |
| 341 |
return role_names |
| 342 |
|
| 343 |
|
| 344 |
def token_get(request, token, data): |
| 345 |
headers = {'Content-Type': 'application/json'} |
| 346 |
return get(request, 'tokens/%s' % token, |
| 347 |
data=json.dumps(data), headers=headers) |
| 348 |
|
| 349 |
|
| 350 |
def token_submit(request, token, data): |
| 351 |
headers = {"Content-Type": "application/json"} |
| 352 |
return post(request, 'tokens/%s' % token, |
| 353 |
data=json.dumps(data), headers=headers) |
| 354 |
|
| 355 |
|
| 356 |
def token_reissue(request, task_id): |
| 357 |
headers = {'Content-Type': 'application/json', |
| 358 |
'X-Auth-Token': request.user.token.id} |
| 359 |
data = {'task': task_id} |
| 360 |
return post(request, 'tokens/', |
| 361 |
data=json.dumps(data), headers=headers) |
| 362 |
|
| 363 |
|
| 364 |
def email_update(request, email): |
| 365 |
headers = {'Content-Type': 'application/json', |
| 366 |
'X-Auth-Token': request.user.token.id} |
| 367 |
data = { |
| 368 |
'new_email': email |
| 369 |
} |
| 370 |
return post(request, 'openstack/users/email-update', |
| 371 |
data=json.dumps(data), headers=headers) |
| 372 |
|
| 373 |
|
| 374 |
def forgotpassword_submit(request, data): |
| 375 |
headers = {"Content-Type": "application/json"} |
| 376 |
try: |
| 377 |
return post(request, 'openstack/users/password-reset', |
| 378 |
data=json.dumps(data), |
| 379 |
headers=headers) |
| 380 |
except Exception as e: |
| 381 |
LOG.error(e) |
| 382 |
raise |
| 383 |
|
| 384 |
|
| 385 |
def signup_submit(request, data): |
| 386 |
headers = {"Content-Type": "application/json"} |
| 387 |
try: |
| 388 |
return post(request, 'openstack/sign-up', |
| 389 |
data=json.dumps(data), |
| 390 |
headers=headers) |
| 391 |
except Exception as e: |
| 392 |
LOG.error(e) |
| 393 |
raise |
| 394 |
|
| 395 |
|
| 396 |
def notification_list(request, filters={}, page=1): |
| 397 |
notifs_per_page = utils.get_page_size(request) |
| 398 |
headers = {"Content-Type": "application/json", |
| 399 |
'X-Auth-Token': request.user.token.id} |
| 400 |
|
| 401 |
response = get(request, 'notifications', headers=headers, |
| 402 |
params={'filters': json.dumps(filters), 'page': page, |
| 403 |
'notifications_per_page': notifs_per_page}) |
| 404 |
if not response.status_code == 200: |
| 405 |
if response.json() == {'error': 'Empty page'}: |
| 406 |
raise AdjutantApiError("Empty Page") |
| 407 |
raise BaseException |
| 408 |
|
| 409 |
notificationlist = [] |
| 410 |
for notification in response.json()['notifications']: |
| 411 |
notificationlist.append(notification_obj_get( |
| 412 |
request, notification=notification)) |
| 413 |
has_more = response.json()['has_more'] |
| 414 |
has_prev = response.json()['has_prev'] |
| 415 |
return notificationlist, has_prev, has_more |
| 416 |
|
| 417 |
|
| 418 |
def notification_get(request, uuid): |
| 419 |
headers = {"Content-Type": "application/json", |
| 420 |
'X-Auth-Token': request.user.token.id} |
| 421 |
|
| 422 |
response = get(request, 'notifications/%s/' % uuid, headers=headers) |
| 423 |
return response |
| 424 |
|
| 425 |
|
| 426 |
def notification_obj_get(request, notification_id=None, notification=None): |
| 427 |
if not notification: |
| 428 |
notification = notification_get(request, notification_id).json() |
| 429 |
|
| 430 |
if notification['error']: |
| 431 |
notes = notification['notes'].get('errors') |
| 432 |
else: |
| 433 |
notes = notification['notes'].get('notes') |
| 434 |
|
| 435 |
if not notes: |
| 436 |
notes = notification['notes'] |
| 437 |
if isinstance(notes, list) and len(notes) == 1: |
| 438 |
notes = notes[0] |
| 439 |
|
| 440 |
if not isinstance(notes, str): |
| 441 |
notes = json.dumps(notes) |
| 442 |
|
| 443 |
return NOTIFICATION(uuid=notification['uuid'], |
| 444 |
task=notification['task'], |
| 445 |
error=notification['error'], |
| 446 |
created_on=notification['created_on'], |
| 447 |
acknowledged=notification['acknowledged'], |
| 448 |
notes=notes) |
| 449 |
|
| 450 |
|
| 451 |
def notifications_acknowlege(request, notification_id=None): |
| 452 |
headers = {"Content-Type": "application/json", |
| 453 |
'X-Auth-Token': request.user.token.id} |
| 454 |
# Takes either a single notification id or a list of them |
| 455 |
# and acknowleges all of them |
| 456 |
if isinstance(notification_id, list): |
| 457 |
data = {'notifications': notification_id} |
| 458 |
return post(request, 'notifications', data=json.dumps(data), |
| 459 |
headers=headers) |
| 460 |
else: |
| 461 |
url = "notifications/%s/" % notification_id |
| 462 |
return post(request, url, data=json.dumps({'acknowledged': True}), |
| 463 |
headers=headers) |
| 464 |
|
| 465 |
|
| 466 |
def task_list(request, filters={}, page=1): |
| 467 |
tasks_per_page = utils.get_page_size(request) |
| 468 |
tasklist = [] |
| 469 |
prev = more = False |
| 470 |
try: |
| 471 |
headers = {"Content-Type": "application/json", |
| 472 |
'X-Auth-Token': request.user.token.id} |
| 473 |
params = { |
| 474 |
"filters": json.dumps(filters), |
| 475 |
"page": page, |
| 476 |
"tasks_per_page": tasks_per_page |
| 477 |
} |
| 478 |
resp = get(request, "tasks", params=params, data=json.dumps({}), |
| 479 |
headers=headers).json() |
| 480 |
prev = resp['has_prev'] |
| 481 |
more = resp['has_more'] |
| 482 |
for task in resp['tasks']: |
| 483 |
tasklist.append(task_obj_get(request, task=task, page=page)) |
| 484 |
return tasklist, prev, more |
| 485 |
except Exception as e: |
| 486 |
LOG.error(e) |
| 487 |
raise |
| 488 |
|
| 489 |
|
| 490 |
def task_get(request, task_id): |
| 491 |
# Get a single task |
| 492 |
headers = {"Content-Type": "application/json", |
| 493 |
'X-Auth-Token': request.user.token.id} |
| 494 |
|
| 495 |
return get(request, "tasks/%s" % task_id, |
| 496 |
headers=headers) |
| 497 |
|
| 498 |
|
| 499 |
def task_obj_get(request, task_id=None, task=None, page=0): |
| 500 |
if not task: |
| 501 |
task = task_get(request, task_id).json() |
| 502 |
|
| 503 |
status = "Awaiting Approval" |
| 504 |
if task['cancelled']: |
| 505 |
status = "Cancelled" |
| 506 |
elif task['completed_on']: |
| 507 |
status = "Completed" |
| 508 |
elif task['approved_on']: |
| 509 |
status = "Approved; Incomplete" |
| 510 |
|
| 511 |
valid = False not in [action['valid'] for |
| 512 |
action in task['actions']] |
| 513 |
return TASK( |
| 514 |
id=task['uuid'], |
| 515 |
task_type=task['task_type'], |
| 516 |
valid=valid, |
| 517 |
request_by=task['keystone_user'].get('username', '-'), |
| 518 |
request_project=task['keystone_user'].get('project_name', '-'), |
| 519 |
status=status, |
| 520 |
created_on=task['created_on'], |
| 521 |
approved_on=task['approved_on'], |
| 522 |
completed_on=task['completed_on'], |
| 523 |
actions=task['actions'], |
| 524 |
page=page |
| 525 |
) |
| 526 |
|
| 527 |
|
| 528 |
def task_cancel(request, task_id): |
| 529 |
headers = {"Content-Type": "application/json", |
| 530 |
'X-Auth-Token': request.user.token.id} |
| 531 |
|
| 532 |
return delete(request, "tasks/%s" % task_id, |
| 533 |
headers=headers) |
| 534 |
|
| 535 |
|
| 536 |
def task_approve(request, task_id): |
| 537 |
headers = {"Content-Type": "application/json", |
| 538 |
'X-Auth-Token': request.user.token.id} |
| 539 |
|
| 540 |
return post(request, "tasks/%s" % task_id, |
| 541 |
data=json.dumps({"approved": True}), headers=headers) |
| 542 |
|
| 543 |
|
| 544 |
def task_update(request, task_id, new_data): |
| 545 |
headers = {"Content-Type": "application/json", |
| 546 |
'X-Auth-Token': request.user.token.id} |
| 547 |
|
| 548 |
return put(request, "tasks/%s" % task_id, |
| 549 |
data=new_data, headers=headers) |
| 550 |
|
| 551 |
|
| 552 |
def task_revalidate(request, task_id): |
| 553 |
task = task_get(request, task_id=task_id).json() |
| 554 |
|
| 555 |
data = {} |
| 556 |
for action_data in [action['data'] for action in task['actions']]: |
| 557 |
data.update(action_data) |
| 558 |
|
| 559 |
return task_update(request, task_id, json.dumps(data)) |
| 560 |
|
| 561 |
|
| 562 |
# Quota management functions |
| 563 |
def _is_quota_hidden(service, resource): |
| 564 |
hidden_quotas = getattr(settings, 'HIDDEN_QUOTAS', None) |
| 565 |
if hidden_quotas is None: |
| 566 |
hidden_quotas = HIDDEN_QUOTAS |
| 567 |
return service in hidden_quotas and resource in hidden_quotas[service] |
| 568 |
|
| 569 |
|
| 570 |
def _is_quota_important(service, resource): |
| 571 |
important_quotas = getattr(settings, 'IMPORTANT_QUOTAS', None) |
| 572 |
if important_quotas is None: |
| 573 |
important_quotas = IMPORTANT_QUOTAS |
| 574 |
return ( |
| 575 |
service in important_quotas and resource in important_quotas[service]) |
| 576 |
|
| 577 |
|
| 578 |
@memoized.memoized_method |
| 579 |
def _get_quota_information(request, regions=None, include_usage=True): |
| 580 |
headers = {'Content-Type': 'application/json', |
| 581 |
'X-Auth-Token': request.user.token.id} |
| 582 |
params = {'include_usage': include_usage} |
| 583 |
if regions: |
| 584 |
params['regions'] = regions |
| 585 |
try: |
| 586 |
return get(request, 'openstack/quotas/', |
| 587 |
params=params, headers=headers).json() |
| 588 |
except Exception as e: |
| 589 |
LOG.error(e) |
| 590 |
raise |
| 591 |
|
| 592 |
|
| 593 |
def quota_sizes_get(request, region=None): |
| 594 |
# Gets the list of quota sizes, and a json blob defining what they |
| 595 |
# have for each of the services |
| 596 |
# Region param is useless here, but nedded for memoized decorator to work |
| 597 |
quota_sizes_dict = {} |
| 598 |
|
| 599 |
resp = _get_quota_information(request, regions=region, include_usage=False) |
| 600 |
|
| 601 |
for size_name, size in resp['quota_sizes'].items(): |
| 602 |
quota_sizes_dict[size_name] = QUOTA_SIZE( |
| 603 |
id=size_name, |
| 604 |
name=size_name, |
| 605 |
cinder=json.dumps(size['cinder'], indent=1), |
| 606 |
nova=json.dumps(size['nova'], indent=1), |
| 607 |
neutron=json.dumps(size['neutron'], indent=1), |
| 608 |
) |
| 609 |
|
| 610 |
quota_sizes = [] |
| 611 |
for size in resp['quota_size_order']: |
| 612 |
quota_sizes.append(quota_sizes_dict[size]) |
| 613 |
|
| 614 |
return quota_sizes |
| 615 |
|
| 616 |
|
| 617 |
def size_details_get(request, size, region=None): |
| 618 |
"""Gets the current details of the size as well as the current region's |
| 619 |
quota |
| 620 |
""" |
| 621 |
quota_details = [] |
| 622 |
|
| 623 |
if not region: |
| 624 |
region = request.user.services_region |
| 625 |
resp = _get_quota_information(request, regions=region) |
| 626 |
|
| 627 |
data = resp['quota_sizes'][size] |
| 628 |
try: |
| 629 |
region = resp['regions'][0] |
| 630 |
except IndexError: |
| 631 |
region = {} |
| 632 |
current_quota = region.get('current_quota', {}) |
| 633 |
current_usage = region.get('current_usage', {}) |
| 634 |
for service, values in data.items(): |
| 635 |
for resource, value in values.items(): |
| 636 |
if _is_quota_hidden(service, resource): |
| 637 |
continue |
| 638 |
|
| 639 |
quota = current_quota.get(service, {}).get(resource, '-') |
| 640 |
usage = current_usage.get(service, {}).get(resource, '-') |
| 641 |
percent = ( |
| 642 |
'-' |
| 643 |
if not value or usage == '-' |
| 644 |
else (usage / value) |
| 645 |
) |
| 646 |
|
| 647 |
quota_details.append( |
| 648 |
SIZE_QUOTA_VALUE( |
| 649 |
id=resource, |
| 650 |
name=resource, |
| 651 |
service=service, |
| 652 |
value=value, |
| 653 |
current_quota=quota, |
| 654 |
current_usage=usage, |
| 655 |
percent=percent |
| 656 |
) |
| 657 |
) |
| 658 |
return quota_details |
| 659 |
|
| 660 |
|
| 661 |
def quota_details_get(request, region): |
| 662 |
quota_details = [] |
| 663 |
|
| 664 |
resp = _get_quota_information(request, regions=region) |
| 665 |
|
| 666 |
try: |
| 667 |
region = resp['regions'][0] |
| 668 |
except IndexError: |
| 669 |
region = {} |
| 670 |
data = region.get('current_quota', {}) |
| 671 |
|
| 672 |
for service, values in data.items(): |
| 673 |
for name, value in values.items(): |
| 674 |
if _is_quota_hidden(service, name): |
| 675 |
continue |
| 676 |
|
| 677 |
try: |
| 678 |
if value < 0: |
| 679 |
value = 'No Limit' |
| 680 |
except (TypeError): |
| 681 |
pass |
| 682 |
usage = resp['regions'][0]['current_usage'][service].get(name) |
| 683 |
try: |
| 684 |
percent = float(usage) / value |
| 685 |
except (TypeError, ZeroDivisionError): |
| 686 |
percent = '-' |
| 687 |
|
| 688 |
size_blob = {} |
| 689 |
for size_name, size_data in resp['quota_sizes'].items(): |
| 690 |
size_blob[size_name] = size_data[service].get(name, '-') |
| 691 |
|
| 692 |
if name != 'id': |
| 693 |
quota_details.append( |
| 694 |
REGION_QUOTA_VALUE( |
| 695 |
id=name, |
| 696 |
name=name, |
| 697 |
service=service, |
| 698 |
current_quota=value, |
| 699 |
current_usage=usage, |
| 700 |
percent=percent, |
| 701 |
size_blob=size_blob, |
| 702 |
important=_is_quota_important(service, name) |
| 703 |
) |
| 704 |
) |
| 705 |
return quota_details |
| 706 |
|
| 707 |
|
| 708 |
def region_quotas_get(request, region=None): |
| 709 |
quota_details = [] |
| 710 |
|
| 711 |
resp = _get_quota_information(request, regions=region, include_usage=False) |
| 712 |
|
| 713 |
data = resp['regions'] |
| 714 |
for region_values in data: |
| 715 |
quota_details.append( |
| 716 |
REGION_QUOTA( |
| 717 |
id=region_values['region'], |
| 718 |
region=region_values['region'], |
| 719 |
quota_size=region_values['current_quota_size'], |
| 720 |
preapproved_quotas=', '.join(region_values[ |
| 721 |
'quota_change_options']) |
| 722 |
) |
| 723 |
) |
| 724 |
return quota_details |
| 725 |
|
| 726 |
|
| 727 |
def quota_tasks_get(request, region=None): |
| 728 |
# Region param only used to help with memoized decorator |
| 729 |
quota_tasks = [] |
| 730 |
|
| 731 |
resp = _get_quota_information(request, regions=region, include_usage=False) |
| 732 |
|
| 733 |
for task in resp['active_quota_tasks']: |
| 734 |
quota_tasks.append( |
| 735 |
QUOTA_TASK( |
| 736 |
id=task['id'], |
| 737 |
regions=', '.join(task['regions']), |
| 738 |
size=task['size'], |
| 739 |
user=task['request_user'], |
| 740 |
created=task['task_created'].split("T")[0], |
| 741 |
valid=task['valid'], |
| 742 |
status=task['status'], |
| 743 |
) |
| 744 |
) |
| 745 |
return quota_tasks |
| 746 |
|
| 747 |
|
| 748 |
def update_quotas(request, size, regions=[]): |
| 749 |
headers = {'Content-Type': 'application/json', |
| 750 |
'X-Auth-Token': request.user.token.id} |
| 751 |
data = { |
| 752 |
'size': size, |
| 753 |
} |
| 754 |
if regions: |
| 755 |
data['regions'] = regions |
| 756 |
|
| 757 |
return post(request, 'openstack/quotas/', |
| 758 |
data=json.dumps(data), |
| 759 |
headers=headers) |