4.9 KiB
Using os-client-config in an Application
Usage
The simplest and least useful thing you can do is:
-m os_client_config.config python
Which will print out whatever if finds for your config. If you want to use it from python, which is much more likely what you want to do, things like:
Get a named cloud.
import os_client_config
= os_client_config.OpenStackConfig().get_one_cloud(
cloud_config 'internap', region_name='ams01')
print(cloud_config.name, cloud_config.region, cloud_config.config)
Or, get all of the clouds.
import os_client_config
= os_client_config.OpenStackConfig().get_all_clouds()
cloud_config for cloud in cloud_config:
print(cloud.name, cloud.region, cloud.config)
argparse
If you're using os-client-config from a program that wants to process command line options, there is a registration function to register the arguments that both os-client-config and keystoneauth know how to deal with - as well as a consumption argument.
import argparse
import sys
import os_client_config
= os_client_config.OpenStackConfig()
cloud_config = argparse.ArgumentParser()
parser
cloud_config.register_argparse_arguments(parser, sys.argv)
= parser.parse_args()
options
= cloud_config.get_one_cloud(argparse=options) cloud
Constructing OpenStack SDK object
If what you want to do is get an OpenStack SDK Connection and you want it to do all the normal things related to clouds.yaml, OS_ environment variables, a helper function is provided. The following will get you a fully configured openstack.connection.Connection instance.
import os_client_config
= os_client_config.make_sdk() sdk
If you want to do the same thing but on a named cloud.
import os_client_config
= os_client_config.make_sdk(cloud='mtvexx') sdk
If you want to do the same thing but also support command line parsing.
import argparse
import os_client_config
= os_client_config.make_sdk(options=argparse.ArgumentParser()) sdk
It should be noted that OpenStack SDK has ways to construct itself that allow for additional flexibility. If the helper function here does not meet your needs, you should see the from_config method of openstack.connection.Connection
Constructing shade objects
If what you want to do is get a shade OpenStackCloud object, a helper function that honors clouds.yaml and OS_ environment variables is provided. The following will get you a fully configured OpenStackCloud instance.
import os_client_config
= os_client_config.make_shade() cloud
If you want to do the same thing but on a named cloud.
import os_client_config
= os_client_config.make_shade(cloud='mtvexx') cloud
If you want to do the same thing but also support command line parsing.
import argparse
import os_client_config
= os_client_config.make_shade(options=argparse.ArgumentParser()) cloud
Constructing REST API Clients
What if you want to make direct REST calls via a Session interface? You're in luck. A similar interface is available as with openstacksdk and shade. The main difference is that you need to specify which service you want to talk to and make_rest_client will return you a keystoneauth Session object that is mounted on the endpoint for the service you're looking for.
import os_client_config
= os_client_config.make_rest_client('compute', cloud='vexxhost')
session
= session.get('/servers')
response = response.json()['servers'] server_list
Constructing Legacy Client objects
If you want get an old-style Client object from a python-*client library, and you want it to do all the normal things related to clouds.yaml, OS_ environment variables, a helper function is also provided. The following will get you a fully configured novaclient instance.
import os_client_config
= os_client_config.make_client('compute') nova
If you want to do the same thing but on a named cloud.
import os_client_config
= os_client_config.make_client('compute', cloud='mtvexx') nova
If you want to do the same thing but also support command line parsing.
import argparse
import os_client_config
= os_client_config.make_client(
nova 'compute', options=argparse.ArgumentParser())
If you want to get fancier than that in your python, then the rest of the API is available to you. But often times, you just want to do the one thing.