Add usage show command
Should show basic usage by project id, if not specified then use the project id the user is authN'ing with. Change-Id: I0284a5efd84075b18e1a7117cc9f8f7fecf16274 Closes-Bug: #1400796
This commit is contained in:
parent
6e3c9a3d2d
commit
5761a0f0b7
@ -20,6 +20,8 @@ import logging
|
||||
import sys
|
||||
|
||||
from cliff import lister
|
||||
from cliff import show
|
||||
import six
|
||||
|
||||
from openstackclient.common import utils
|
||||
|
||||
@ -114,3 +116,75 @@ class ListUsage(lister.Lister):
|
||||
'total_local_gb_usage': lambda x: float("%.2f" % x),
|
||||
},
|
||||
) for s in usage_list))
|
||||
|
||||
|
||||
class ShowUsage(show.ShowOne):
|
||||
"""Show resource usage for a single project. """
|
||||
|
||||
log = logging.getLogger(__name__ + ".ShowUsage")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(ShowUsage, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
"--start",
|
||||
metavar="<start>",
|
||||
default=None,
|
||||
help="Usage range start date, ex 2012-01-20"
|
||||
" (default: 4 weeks ago)."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--end",
|
||||
metavar="<end>",
|
||||
default=None,
|
||||
help="Usage range end date, ex 2012-01-20 (default: tomorrow)."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--project",
|
||||
metavar="<project>",
|
||||
default=None,
|
||||
help="Name or ID of project to show usage for."
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug("take_action(%s)", parsed_args)
|
||||
|
||||
identity_client = self.app.client_manager.identity
|
||||
compute_client = self.app.client_manager.compute
|
||||
dateformat = "%Y-%m-%d"
|
||||
now = datetime.datetime.utcnow()
|
||||
|
||||
if parsed_args.start:
|
||||
start = datetime.datetime.strptime(parsed_args.start, dateformat)
|
||||
else:
|
||||
start = now - datetime.timedelta(weeks=4)
|
||||
|
||||
if parsed_args.end:
|
||||
end = datetime.datetime.strptime(parsed_args.end, dateformat)
|
||||
else:
|
||||
end = now + datetime.timedelta(days=1)
|
||||
|
||||
if parsed_args.project:
|
||||
project = utils.find_resource(
|
||||
identity_client.projects,
|
||||
parsed_args.project,
|
||||
).id
|
||||
else:
|
||||
# Get the project from the current auth
|
||||
project = self.app.client_manager.auth_ref.project_id
|
||||
|
||||
usage = compute_client.usage.get(project, start, end)
|
||||
|
||||
if parsed_args.formatter == 'table':
|
||||
sys.stdout.write("Usage from %s to %s on project %s: \n" % (
|
||||
start.strftime(dateformat),
|
||||
end.strftime(dateformat),
|
||||
project
|
||||
))
|
||||
|
||||
info = {}
|
||||
info['Servers'] = len(usage.server_usages)
|
||||
info['RAM MB-Hours'] = float("%.2f" % usage.total_memory_mb_usage)
|
||||
info['CPU Hours'] = float("%.2f" % usage.total_vcpus_usage)
|
||||
info['Disk GB-Hours'] = float("%.2f" % usage.total_local_gb_usage)
|
||||
return zip(*sorted(six.iteritems(info)))
|
||||
|
@ -131,6 +131,8 @@ openstack.compute.v2 =
|
||||
server_unrescue = openstackclient.compute.v2.server:UnrescueServer
|
||||
server_unset = openstackclient.compute.v2.server:UnsetServer
|
||||
|
||||
usage_show = openstackclient.compute.v2.usage:ShowUsage
|
||||
|
||||
openstack.identity.v2 =
|
||||
catalog_list = openstackclient.identity.v2_0.catalog:ListCatalog
|
||||
catalog_show = openstackclient.identity.v2_0.catalog:ShowCatalog
|
||||
|
Loading…
x
Reference in New Issue
Block a user