Repository URL to install this package:
|
Version:
6.0.24 ▾
|
python3-workloadmgrclient
/
usr
/
lib
/
python3
/
dist-packages
/
workloadmgrclient
/
v1
/
allowed_quota.py
|
|---|
import six
from cliff import show, lister
from osc_lib import exceptions
from osc_lib import utils as osc_utils
from workloadmgrclient import utils
from workloadmgrclient.v1.workload import WorkloadCommand
from workloadmgrclient.v1.validators import validate_uuid
class AllowedQuotaCommand(WorkloadCommand):
resource = "allowed_quotas"
class ListAllowedQuota(AllowedQuotaCommand, lister.Lister):
"""List all the allowed quotas of current project."""
@staticmethod
def _add_arguments(parser):
parser.add_argument(
"project_id", metavar="<project_id>",
help="Project id of allowed_quota."
)
def take_action(self, parsed_args):
validate_uuid(parsed_args.project_id)
client = self.get_client()
res = []
quota_type_objs = client.list(parsed_args.project_id) or []
if quota_type_objs:
for quota_type in quota_type_objs:
res.append(
{"id": quota_type.id,
"quota_type_id": quota_type.quota_type_id,
"display_name": quota_type.quota_type_name,
"allowed_value": quota_type.allowed_value}
)
headers = ["ID", "Name", "Allowed Value"]
columns = ["id", "display_name", "allowed_value"]
return (
headers,
(osc_utils.get_dict_properties(s, columns) for s in res),
)
class ShowAllowedQuota(AllowedQuotaCommand, show.ShowOne):
"""Show details about a allowed_quota for current project."""
@staticmethod
def _add_arguments(parser):
parser.add_argument(
"allowed_quota_id",
metavar="<allowed_quota_id>",
help="ID of the allowed_quota.",
)
def take_action(self, parsed_args):
"""Show details about a allowed_quota_id."""
validate_uuid(parsed_args.allowed_quota_id)
client = self.get_client()
allowed_quota_obj = utils.find_resource(
client, parsed_args.allowed_quota_id
)
if not allowed_quota_obj._info:
raise exceptions.CommandError(
"No project allowed quota is present for provided id"
)
info = {}
info.update(allowed_quota_obj._info)
return zip(*sorted(six.iteritems(info)))
class CreateAllowedQuota(AllowedQuotaCommand, show.ShowOne):
"""Create a allowed_quota."""
@staticmethod
def _add_arguments(parser):
parser.add_argument(
"--quota-type-id",
metavar="quota_type_id",
required=True,
help="ID of the quota_type.",
)
parser.add_argument(
"--allowed-value",
metavar="allowed_value",
required=True,
help="Allowed (numeric) value for the quota.",
)
parser.add_argument(
"--high-watermark",
metavar="high_watermark",
required=True,
help="High watermark (numeric) value for the quota for warning.",
)
parser.add_argument(
"--project-id",
metavar="project_id",
required=True,
help="project_id to set a quota for",
)
def take_action(self, parsed_args):
[validate_uuid(id_val) for id_val in \
[parsed_args.quota_type_id, parsed_args.project_id]]
client = self.get_client()
res = client.create(
parsed_args.quota_type_id,
parsed_args.project_id,
parsed_args.allowed_value,
parsed_args.high_watermark,
)
info = {}
if res._info and isinstance(res._info, list):
info.update(res._info[0])
return zip(*sorted(six.iteritems(info)))
else:
raise Exception("Allowed quota creation operation failed, please verify parameters entered.")
class ModifyAllowedQuota(AllowedQuotaCommand, show.ShowOne):
"""Modify a allowed_quota."""
@staticmethod
def _add_arguments(parser):
parser.add_argument(
"allowed_quota_id",
metavar="<allowed_quota_id>",
help="ID of the allowed_quota.",
)
parser.add_argument(
"--allowed-value",
metavar="allowed_value",
help="Allowed (numeric) value for the quota.",
)
parser.add_argument(
"--high-watermark",
metavar="high_watermark",
help="High watermark (numeric) value for the quota for warning.",
)
parser.add_argument(
"--project-id", metavar="project_id",
help="project_id to set a quota for"
)
def take_action(self, parsed_args):
[validate_uuid(id_val) for id_val in \
[parsed_args.allowed_quota_id, parsed_args.project_id] if id_val is not None]
client = self.get_client()
allowed_quota_obj = utils.find_resource(
client, parsed_args.allowed_quota_id
)
res = allowed_quota_obj.update(
parsed_args.allowed_quota_id,
parsed_args.project_id,
parsed_args.allowed_value,
parsed_args.high_watermark,
)
if res.get('allowed_quotas', None) and isinstance(res['allowed_quotas'], list):
return zip(*sorted(six.iteritems(res['allowed_quotas'][0])))
else:
raise Exception("Allowed quota modification operation failed, please verify parameters entered.")
class DeleteAllowedQuota(AllowedQuotaCommand):
"""Remove a allowed_quota."""
@staticmethod
def _add_arguments(parser):
parser.add_argument(
"allowed_quota_id",
metavar="<allowed_quota_id>",
help="ID of the allowed_quota to delete.",
)
def take_action(self, parsed_args):
validate_uuid(parsed_args.allowed_quota_id)
client = self.get_client()
allowed_quota = utils.find_resource(
client, parsed_args.allowed_quota_id
)
allowed_quota.delete(parsed_args.allowed_quota_id)
return