| 1 |
================ |
| 2 |
Using Python API |
| 3 |
================ |
| 4 |
|
| 5 |
Python bindings to the OpenStack Monasca API |
| 6 |
============================================ |
| 7 |
|
| 8 |
This is a client for the OpenStack Monasca API. It includes a Python |
| 9 |
API (the :mod:`monascaclient` module) and a command-line script |
| 10 |
(installed as :program:`monasca`). |
| 11 |
|
| 12 |
Python API |
| 13 |
========== |
| 14 |
|
| 15 |
To use python-monascaclient in a project, you need to create a client instance |
| 16 |
first. There are couple ways of doing this properly. |
| 17 |
|
| 18 |
With session |
| 19 |
------------ |
| 20 |
|
| 21 |
A pseudo-code would be similar to this:: |
| 22 |
|
| 23 |
from keystoneauth1 import identity |
| 24 |
from keystoneauth1 import session |
| 25 |
from monascaclient import client |
| 26 |
|
| 27 |
auth = identity.Password( |
| 28 |
auth_url='http://my.keystone.com/identity', |
| 29 |
username='mini-mon', |
| 30 |
password='password', |
| 31 |
project_name='mini-mon', |
| 32 |
user_domain_id='default', |
| 33 |
project_domain_id='default' |
| 34 |
) |
| 35 |
sess = session.Session(auth=auth) |
| 36 |
|
| 37 |
endpoint = 'http://monasca:8070/v2.0' |
| 38 |
api_version = '2_0' |
| 39 |
|
| 40 |
c = client.Client( |
| 41 |
api_version=api_version, |
| 42 |
endpoint=endpoint, |
| 43 |
session=sess |
| 44 |
) |
| 45 |
|
| 46 |
c.alarms.list() |
| 47 |
|
| 48 |
For more information on keystoneauth API, see `Using Sessions`_. We also |
| 49 |
suggest taking closer look at `Keystone Auth Plugins`_. Each of the plugin |
| 50 |
can be used to properly instantiate new session and pass it into the client. |
| 51 |
|
| 52 |
.. note:: This is recommended way to setup a client. |
| 53 |
Other cases, described below, create sessions internally. |
| 54 |
|
| 55 |
|
| 56 |
Without session |
| 57 |
--------------- |
| 58 |
|
| 59 |
If you do not want to use a session or simply prefer client to instantiate |
| 60 |
one on its own, there are two supported ways |
| 61 |
|
| 62 |
With token |
| 63 |
~~~~~~~~~~ |
| 64 |
|
| 65 |
A pseudo-code would be similar to this:: |
| 66 |
|
| 67 |
from monascaclient import client |
| 68 |
|
| 69 |
c = client.Client( |
| 70 |
api_version='2_0', |
| 71 |
endpoint='http://monasca:8070/v2.0', |
| 72 |
token='3bcc3d3a03f44e3d8377f9247b0ad155', |
| 73 |
project_name='mini-mon', |
| 74 |
auth_url='http://my.keystone.com/identity' |
| 75 |
) |
| 76 |
|
| 77 |
c.alarms.list() |
| 78 |
|
| 79 |
|
| 80 |
With username & password |
| 81 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 82 |
|
| 83 |
A pseudo-code would be similar to this:: |
| 84 |
|
| 85 |
from monascaclient import client |
| 86 |
|
| 87 |
c = client.Client( |
| 88 |
api_version='2_0', |
| 89 |
endpoint='http://monasca:8070/v2.0', |
| 90 |
username='mini-mon', |
| 91 |
password='password', |
| 92 |
project_name='mini-mon', |
| 93 |
auth_url='http://my.keystone.com/identity' |
| 94 |
) |
| 95 |
|
| 96 |
c.alarms.list() |
| 97 |
|
| 98 |
Examples |
| 99 |
======== |
| 100 |
|
| 101 |
* `Monasca Agent Example`_ - with session |
| 102 |
* `Monasca UI Example`_ - with token |
| 103 |
|
| 104 |
.. _Monasca Agent Example: https://github.com/openstack/monasca-agent/blob/master/monasca_agent/forwarder/api/monasca_api.py |
| 105 |
.. _Monasca UI Example: https://github.com/openstack/monasca-ui/blob/master/monitoring/api/client.py |
| 106 |
.. _Using Sessions: https://docs.openstack.org/keystoneauth/latest/using-sessions.html |
| 107 |
.. _Keystone Auth Plugins: https://docs.openstack.org/keystoneauth/latest/authentication-plugins.html |