Created
February 23, 2021 03:25
-
-
Save ViKingIX/1883a897fc93c9b467b8a476d9b0e20e to your computer and use it in GitHub Desktop.
List server and its volumes group by project with novaclient and cinderclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import json | |
from keystoneauth1.identity import v3 | |
from keystoneauth1 import session | |
import novaclient.client | |
import cinderclient.client | |
import config | |
def main(): | |
auth = v3.Password( | |
auth_url=os.environ['OS_AUTH_URL'], | |
username=os.environ['OS_USERNAME'], | |
password=os.environ['OS_PASSWORD'], | |
project_name=os.environ['OS_PROJECT_NAME'], | |
user_domain_name=os.environ['OS_USER_DOMAIN_NAME'], | |
project_domain_name=os.environ['OS_PROJECT_DOMAIN_NAME'] | |
) | |
sess = session.Session(auth=auth, verify=False) | |
nova = novaclient.client.Client('2.1', session=sess) | |
cinder = cinderclient.client.Client('2', session=sess) | |
project_server_volumes = [] | |
for prj in config.projects: | |
d = {'project': prj, 'servers': []} | |
servers = nova.servers.list(search_opts={'project_id': prj, 'all_tenants': True}) | |
for server in servers: | |
sv = server.to_dict() | |
volumes = sv['os-extended-volumes:volumes_attached'] | |
d['servers'].append({ | |
'id': sv['id'], | |
'name': sv['name'], | |
'volumes': [ | |
{ | |
'id': volume['id'], | |
'name': cinder.volumes.get(volume['id']).name | |
} for volume in volumes | |
] | |
}) | |
project_server_volumes.append(d) | |
print(json.dumps(project_server_volumes, indent=True)) | |
return | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment