Python OpenStack SDK list usage for all projects

764 views Asked by At

I'm looking for the python OpenStack SDK version of 'openstack usage list --start <start_time> '--end <end_time> -f ' which return each projects usage for a given period of time. I've found conn.get_compute_usage(), which returns for a user?

1

There are 1 answers

0
Rockcat On

You have get_compute_usage(projectName) which will give a detailed info for a project. It admits arguments for start and end time.

import openstack
from pprint import pprint
from datetime import datetime

os_connect = openstack.connect(
        auth_url=AUTH_URL,
        project_name=PROJECT_NAME,
        username=USERNAME,
        password=PASSWORD,
        region_name=REGION_NAME,
        user_domain_name=USER_DOMAIN_NAME,
        project_domain_name=PROJECT_DOMAIN_ID,
        app_version='1.0')

for p in os_connect.list_projects() :
   usage=os_connect.get_compute_usage(p['name'], start=datetime.strptime("2021-06-01","%Y-%m-%d"), end=datetime.strptime("2021-07-01","%Y-%m-%d"))
   #pprint(usage) #Probably too detailed
   print("Project:", p['name'])
   print("vcpus_usage", usage["total_vcpus_usage"])
   print("total_hours", usage["total_hours"])