blob: 1d8632159caf30a9dddedb94c606cef3146b23f6 [file] [log] [blame]
Adam Israeldcdf82b2017-08-15 15:26:43 -04001Controllers
2===========
3A Juju controller provides websocket endpoints for itself and each of its
4models. In order to do anything useful, the juju lib must connect to one of
5these endpoints.
6
7Connecting to the controller endpoint is useful if you want to programmatically
8create a new model. If the model you want to use already exists, you can
Adam Israelb0943662018-08-02 15:32:00 -04009connect directly to it (see py:doc:`model`).
Adam Israeldcdf82b2017-08-15 15:26:43 -040010
Adam Israelb0943662018-08-02 15:32:00 -040011For API docs, see py:class:`juju.controller.Controller`.
Adam Israeldcdf82b2017-08-15 15:26:43 -040012
13
14Connecting to the Current Controller
15------------------------------------
16Connect to the currently active Juju controller (the one returned by
17`juju switch`). This only works if you have the Juju CLI client installed.
18
19.. code:: python
20
21 from juju.controller import Controller
22
23 controller = Controller()
Adam Israelb0943662018-08-02 15:32:00 -040024 await controller.connect()
Adam Israeldcdf82b2017-08-15 15:26:43 -040025
26
27Connecting to a Named Controller
28--------------------------------
29Connect to a controller by name.
30
31.. code:: python
32
33 from juju.controller import Controller
34
35 controller = Controller()
Adam Israelb0943662018-08-02 15:32:00 -040036 await controller.connect('mycontroller')
Adam Israeldcdf82b2017-08-15 15:26:43 -040037
38
Adam Israelb0943662018-08-02 15:32:00 -040039Connecting with Authentication
40------------------------------
41You can control what user you are connecting with by specifying either a
42username/password pair, or a macaroon or bakery client that can provide
43a macaroon.
44
45
46.. code:: python
47
48 controller = Controller()
49 await controller.connect(username='admin',
50 password='f53f08cfc32a2e257fe5393271d89d62')
51
52 # or with a macaroon
53 await controller.connect(macaroons=[
54 {
55 "Name": "macaroon-218d87053ad19626bcd5a0eef0bc9ba8bd4fbd80a968f52a5fd430b2aa8660df",
56 "Value": "W3siY2F2ZWF0cyI6 ... jBkZiJ9XQ==",
57 "Domain": "10.130.48.27",
58 "Path": "/auth",
59 "Secure": false,
60 "HostOnly": true,
61 "Expires": "2018-03-07T22:07:23Z",
62 },
63 ])
64
65 # or with a bakery client
66 from macaroonbakery.httpbakery import Client
67 from http.cookiejar import FileCookieJar
68
69 bakery_client=Client()
70 bakery_client.cookies = FileCookieJar('cookies.txt')
71 controller = Controller()
72 await controller.connect(bakery_client=bakery_client)
73
74
75
76Connecting with an Explicit Endpoint
77------------------------------------
Adam Israeldcdf82b2017-08-15 15:26:43 -040078The most flexible, but also most verbose, way to connect is using the API
Adam Israelb0943662018-08-02 15:32:00 -040079endpoint url and credentials directly. This method does NOT require the
80Juju CLI client to be installed.
Adam Israeldcdf82b2017-08-15 15:26:43 -040081
82.. code:: python
83
Adam Israeldcdf82b2017-08-15 15:26:43 -040084 controller = Controller()
Adam Israeldcdf82b2017-08-15 15:26:43 -040085 await controller.connect(
Adam Israelb0943662018-08-02 15:32:00 -040086 endpoint='10.0.4.171:17070',
87 username='admin',
88 password='f53f08cfc32a2e257fe5393271d89d62',
89 cacert=None, # Left out for brevity, but if you have a cert string you
90 # should pass it in. You can get the cert from the output
91 # of The `juju show-controller` command.
Adam Israeldcdf82b2017-08-15 15:26:43 -040092 )