Monty Taylor 535f2f48ff
Merge shade and os-client-config into the tree
This sucks in the git history for both projects, then moves their files
in place. It should not introduce any behavior changes to any of the
existing openstacksdk code, nor to openstack.config and openstack.cloud
- other than the name change.

TODO(shade) comments have been left indicating places where further
integration work should be done.

It should not be assumed that these are the final places for either to
live. This is just about getting them in-tree so we can work with them.

The enforcer code for reasons surpassing understanding does not work
with python setup.py build_sphinx but it does work with sphinx-build
(what?) For now turn it off. We can turn it back on once the build
sphinx job is migrated to the new PTI.

Change-Id: I9523e4e281285360c61e9e0456a8e07b7ac1243c
2017-11-15 09:03:23 -06:00

3.6 KiB

Using openstack.config in an Application

Usage

The simplest and least useful thing you can do is:

python -m openstack.config.loader

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

cloud_config = openstack.config.OpenStackConfig().get_one_cloud(
    'internap', region_name='ams01')
print(cloud_config.name, cloud_config.region, cloud_config.config)

Or, get all of the clouds.

import openstack.config

cloud_config = openstack.config.OpenStackConfig().get_all_clouds()
for cloud in cloud_config:
    print(cloud.name, cloud.region, cloud.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

cloud_config = openstack.config.OpenStackConfig()
parser = argparse.ArgumentParser()
cloud_config.register_argparse_arguments(parser, sys.argv)

options = parser.parse_args()

cloud = cloud_config.get_one_cloud(argparse=options)

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

conn = openstack.config.make_connection()

If you want to do the same thing but on a named cloud.

import openstack.config

conn = openstack.config.make_connection(cloud='mtvexx')

If you want to do the same thing but also support command line parsing.

import argparse

import openstack.config

conn = openstack.config.make_connection(options=argparse.ArgumentParser())

Constructing cloud 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

cloud = openstack.config.make_cloud()

If you want to do the same thing but on a named cloud.

import openstack.config

cloud = openstack.config.make_cloud(cloud='mtvexx')

If you want to do the same thing but also support command line parsing.

import argparse

import openstack.config

cloud = openstack.config.make_cloud(options=argparse.ArgumentParser())

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

session = openstack.config.make_rest_client('compute', cloud='vexxhost')

response = session.get('/servers')
server_list = response.json()['servers']