
The name CloudConfig has always sucked. While working on the next patch, it occurred to me that what a CloudConfig represents is the config for a given region of a cloud. We even reference "Cloud Region" as a unit of work when giving conference talks. Take the opportunity while we're doing this sdk/occ merge to rename it. Obviously we can provide naming compat shim in OCC itself. Leave in *some* naming compat shims to get us past the 0.10 release so that OSC continues to have the happies. Change-Id: Ia0bbc20eb28a3a36e69adba3a4b45323e4aa284e
3.6 KiB
Using openstack.config in an Application
Usage
The simplest and least useful thing you can do is:
-m openstack.config.loader 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 openstack.config
= openstack.config.OpenStackConfig().get_one(
cloud_region 'internap', region_name='ams01')
print(cloud_region.name, cloud_region.region, cloud_region.config)
Or, get all of the clouds.
import openstack.config
= openstack.config.OpenStackConfig().get_all()
cloud_regions for cloud_region in cloud_regions:
print(cloud_region.name, cloud_region.region, cloud_region.config)
argparse
If you're using openstack.config from a program that wants to process command line options, there is a registration function to register the arguments that both openstack.config and keystoneauth know how to deal with - as well as a consumption argument.
import argparse
import sys
import openstack.config
= openstack.config.OpenStackConfig()
config = argparse.ArgumentParser()
parser
config.register_argparse_arguments(parser, sys.argv)
= parser.parse_args()
options
= config.get_one(argparse=options) cloud_region
Constructing a Connection object
If what you want to do is get an openstack.connection.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 openstacksdk instance.
import openstack.config
= openstack.config.make_connection() conn
If you want to do the same thing but on a named cloud.
import openstack.config
= openstack.config.make_connection(cloud='mtvexx') conn
If you want to do the same thing but also support command line parsing.
import argparse
import openstack.config
= openstack.config.make_connection(options=argparse.ArgumentParser()) conn
Constructing OpenStackCloud objects
If what you want to do is get an opentack.cloud.openstackcloud.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 openstack.config
= openstack.config.make_cloud() cloud
If you want to do the same thing but on a named cloud.
import openstack.config
= openstack.config.make_cloud(cloud='mtvexx') cloud
If you want to do the same thing but also support command line parsing.
import argparse
import openstack.config
= openstack.config.make_cloud(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 openstack.config
= openstack.config.make_rest_client('compute', cloud='vexxhost')
session
= session.get('/servers')
response = response.json()['servers'] server_list