| 1 |
# Copyright (c) 2016 Catalyst IT Ltd. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import logging |
| 16 |
|
| 17 |
from osc_lib.command import command |
| 18 |
from osc_lib.i18n import _ |
| 19 |
|
| 20 |
|
| 21 |
LOG = logging.getLogger(__name__) |
| 22 |
|
| 23 |
|
| 24 |
class QuotaShow(command.Lister): |
| 25 |
"""Displays current quota information. |
| 26 |
|
| 27 |
If not given a region it will print basic details of the state of |
| 28 |
the quotas. If given a region it will print all details for it. |
| 29 |
""" |
| 30 |
|
| 31 |
def get_parser(self, prog_name): |
| 32 |
parser = super(QuotaShow, self).get_parser(prog_name) |
| 33 |
|
| 34 |
parser.add_argument( |
| 35 |
'--region', metavar='<region>', required=False, |
| 36 |
help=_("Single region to display details on.")) |
| 37 |
|
| 38 |
return parser |
| 39 |
|
| 40 |
def take_action(self, parsed_args): |
| 41 |
client = self.app.client_manager.admin_logic |
| 42 |
|
| 43 |
if not parsed_args.region: |
| 44 |
quota_data = client.quota.get() |
| 45 |
|
| 46 |
headers = [ |
| 47 |
'Region', 'Current Size', 'Preapproved Sizes'] |
| 48 |
|
| 49 |
rows = [] |
| 50 |
for region in quota_data['regions']: |
| 51 |
rows.append([ |
| 52 |
region['region'], |
| 53 |
region['current_quota_size'], |
| 54 |
", ".join(region['quota_change_options']), |
| 55 |
]) |
| 56 |
return headers, rows |
| 57 |
else: |
| 58 |
quota_data = client.quota.get(regions=parsed_args.region) |
| 59 |
headers = ['Service', 'Resource', 'Current Quota', 'Current Usage'] |
| 60 |
region = quota_data['regions'][0] |
| 61 |
|
| 62 |
rows = [] |
| 63 |
for service, service_detail in region['current_usage'].items(): |
| 64 |
for resource, value in service_detail.items(): |
| 65 |
current_quota = region['current_quota'][service].get( |
| 66 |
resource) |
| 67 |
rows.append([service, resource, current_quota, value]) |
| 68 |
return headers, rows |
| 69 |
|
| 70 |
|
| 71 |
class QuotaSizes(command.Lister): |
| 72 |
"""Displays possible quota sizes.""" |
| 73 |
|
| 74 |
def take_action(self, parsed_args): |
| 75 |
client = self.app.client_manager.admin_logic |
| 76 |
|
| 77 |
quota_data = client.quota.get() |
| 78 |
|
| 79 |
headers = [ |
| 80 |
'Size Name', 'Service', 'Resource', 'Value'] |
| 81 |
|
| 82 |
rows = [] |
| 83 |
for size, size_details in quota_data['quota_sizes'].items(): |
| 84 |
for service, service_details in size_details.items(): |
| 85 |
for resource, value in service_details.items(): |
| 86 |
rows.append([size, service, resource, value]) |
| 87 |
|
| 88 |
return headers, rows |
| 89 |
|
| 90 |
|
| 91 |
class QuotaTasks(command.Lister): |
| 92 |
"""Displays quota tasks.""" |
| 93 |
|
| 94 |
def take_action(self, parsed_args): |
| 95 |
client = self.app.client_manager.admin_logic |
| 96 |
|
| 97 |
quota_data = client.quota.get() |
| 98 |
|
| 99 |
headers = [ |
| 100 |
'ID', 'Regions', 'Proposed Size', 'Requested By', 'Created On', |
| 101 |
'valid', 'status', |
| 102 |
] |
| 103 |
|
| 104 |
rows = [] |
| 105 |
for task in quota_data['active_quota_tasks']: |
| 106 |
rows.append([ |
| 107 |
task['id'], |
| 108 |
", ".join(task['regions']), |
| 109 |
task['size'], |
| 110 |
task['request_user'], |
| 111 |
task['task_created'], |
| 112 |
task['valid'], |
| 113 |
task['status'], |
| 114 |
]) |
| 115 |
|
| 116 |
return headers, rows |
| 117 |
|
| 118 |
|
| 119 |
class QuotaUpdate(command.Command): |
| 120 |
"""Submits a quota update task.""" |
| 121 |
|
| 122 |
def get_parser(self, prog_name): |
| 123 |
parser = super(QuotaUpdate, self).get_parser(prog_name) |
| 124 |
|
| 125 |
parser.add_argument( |
| 126 |
'size', metavar='<size>', |
| 127 |
help=_("The size to update to.")) |
| 128 |
parser.add_argument( |
| 129 |
'--regions', metavar='<regions>', nargs='+', required=False, |
| 130 |
help=_("Regions to update the quota on.")) |
| 131 |
return parser |
| 132 |
|
| 133 |
def take_action(self, parsed_args): |
| 134 |
client = self.app.client_manager.admin_logic |
| 135 |
if parsed_args.regions: |
| 136 |
status = client.quota.update(parsed_args.size, parsed_args.regions) |
| 137 |
else: |
| 138 |
# NOTE(amelia): regions aren't set the API will update all of them |
| 139 |
status = client.quota.update(parsed_args.size) |
| 140 |
status_code = status.status_code |
| 141 |
|
| 142 |
if status_code == 200: |
| 143 |
if parsed_args.regions: |
| 144 |
print("Regions: %s quota updated to size %s." |
| 145 |
% (parsed_args.regions, parsed_args.size)) |
| 146 |
else: |
| 147 |
print("All regions' quota updated to size %s." |
| 148 |
% (parsed_args.size)) |
| 149 |
elif status_code == 202: |
| 150 |
print("The task has been created however requires admin approval" |
| 151 |
" before executing.") |