summaryrefslogtreecommitdiff
path: root/src/posts/simple-openstack-clients.hbs
blob: 882a6911560b5d84a4787196afd0278fef1cc61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!doctype html>
<html>
  <head>
    <title>Simple OpenStack access with os-client-config</title>
    <meta name="description" content="Easy consumption of OpenStack Clouds" />
  </head>
  <body>
    <p>Simple things should be simple, and nowhere is that more true than in
    constructing objects to interface with clouds. What you wnat to do is just
    interact with the cloud - what you often are required to do is generate
    sequences of object constructions because there are a bunch of things you
    <em>might</em> have wanted to do.</p>

    <p>In the spirit of making the simple things simple, we recently added a
    couple of simple helper factory functions to
    <a href='http://docs.openstack.org/developer/os-client-config/'>
      os-client-config</a>: One to create Client objects, and one to create
    mounted Session objects. Both functions fully support the breadth of
    config options afforded by os-client-config, including environment
    variables, config files and command line options. (Speaking of - if you
    haven't fallen in love with
    <a href='http://inaugust.com/posts/multi-cloud-with-python-openstackclient.html'>
      clouds.yaml</a> yet, now's the time)</p>

    <p>Examples are probably best</p>

    <h3>Client Library Client Objects</h3>

    <p>Make a nova client object that uses env vars for auth info:</p>

    <pre><code>
client = os_client_config.make_client('compute')
    </code></pre>

    <p>Make a glance client object for the cloud named 'mtvexx' in my
    clouds.yaml file:</p>

    <pre><code>
client = os_client_config.make_client('image', cloud='mtvxx')
    </code></pre>

    <p>Make a neutron client by passing in all of the values directly:</p>

    <pre><code>
client = os_client_config.make_client(
    'network',
    auth_url='https://example.com', username='my-user',
    password='awesome-password', project_name='my-project',
    region_name='the-best-region')
    </code></pre>

    <p>Make a barbican client from env vars with all the standard arguments
    passed on the comand line:</p>

    <pre><code>
import argparse
client = os_client_config.make_client(
    'key-manager', options=argparse.ArgumentParser())
    </code></pre>

    <p>That likely covers most of the common end-user use cases with using
    OpenStack Client Libraries. If you need more flexibility, you can always
    create an os-client-config CloudConfig object and call
    get_legacy_client, but I'm a big believer in one step instead of three
    if you can get away with it:</p>

    <pre><code>
config = os_client_config.OpenStackConfig()
cloud_config = config.get_one_cloud(cloud='vexxhost')
client = cloud_config.get_legacy_client('compute')
    </code></pre>

    <h3>Mounted Keystone Session</h3>

    <p>What if what you want to do is make some direct REST calls to an
    OpenStack service, but you want to be able to do env vars or argparse or
    clouds.yaml files to configure your authentication?</p>

    <p>Well - you're in luck!</p>

    <p>Make a Session object for nova that uses env vars for auth info:</p>

    <pre><code>
session = os_client_config.session_client('compute')
    </code></pre>

    <p>That will get you a keystoneauth Session object that has been "mounted" on the compute service. So you can do this:</p>

    <pre><code>
response = session.get('/servers')
server_list = response.json()['servers']
    </code></pre>

    <p>The same argument sequence used for <em>make_client</em> works for
    <em>session_client</em>.

    <p>Make a glance Session object for the cloud named 'mtvexx' in my
    clouds.yaml file:</p>

    <pre><code>
session = os_client_config.session_client('image', cloud='mtvxx')
    </code></pre>

    <p>Make a neutron Session object by passing in all of the values
    directly:</p>

    <pre><code>
session = os_client_config.session_client(
    'network',
    auth_url='https://example.com', username='my-user',
    password='awesome-password', project_name='my-project',
    region_name='the-best-region')
    </code></pre>

    <p>Make a barbican Session object from env vars with all the standard
    arguments passed on the comand line:</p>

    <pre><code>
import argparse
session = os_client_config.session_client(
    'key-manager', options=argparse.ArgumentParser())
    </code></pre>

    <p>Do it all without syntactic help:</p>

    <pre><code>
config = os_client_config.OpenStackConfig()
cloud_config = config.get_one_cloud(cloud='vexxhost')
session = cloud_config.get_session_client('compute')
    </code></pre>

</body>
</html>