6331c12b7c63563b4b9d67f35eb4978a7355112b
[osm/N2VC.git] / juju / controller.py
1 import asyncio
2 import logging
3
4 from . import tag
5 from .client import client
6 from .client import connection
7 from .client import watcher
8 from .model import Model
9
10 log = logging.getLogger(__name__)
11
12
13 class Controller(object):
14 def __init__(self, loop=None):
15 """Instantiate a new Controller.
16
17 One of the connect_* methods will need to be called before this
18 object can be used for anything interesting.
19
20 :param loop: an asyncio event loop
21
22 """
23 self.loop = loop or asyncio.get_event_loop()
24 self.connection = None
25
26 async def connect(
27 self, endpoint, username, password, cacert=None, macaroons=None):
28 """Connect to an arbitrary Juju controller.
29
30 """
31 self.connection = await connection.Connection.connect(
32 endpoint, None, username, password, cacert, macaroons)
33
34 async def connect_current(self):
35 """Connect to the current Juju controller.
36
37 """
38 self.connection = (
39 await connection.Connection.connect_current_controller())
40
41 async def connect_controller(self, controller_name):
42 """Connect to a Juju controller by name.
43
44 """
45 self.connection = (
46 await connection.Connection.connect_controller(controller_name))
47
48 async def disconnect(self):
49 """Shut down the watcher task and close websockets.
50
51 """
52 if self.connection and self.connection.is_open:
53 log.debug('Closing controller connection')
54 await self.connection.close()
55 self.connection = None
56
57 async def add_model(
58 self, model_name, cloud_name=None, credential_name=None,
59 owner=None, config=None, region=None):
60 """Add a model to this controller.
61
62 :param str model_name: Name to give the new model.
63 :param str cloud_name: Name of the cloud in which to create the
64 model, e.g. 'aws'. Defaults to same cloud as controller.
65 :param str credential_name: Name of the credential to use when
66 creating the model. Defaults to current credential. If you
67 pass a credential_name, you must also pass a cloud_name,
68 even if it's the default cloud.
69 :param str owner: Username that will own the model. Defaults to
70 the current user.
71 :param dict config: Model configuration.
72 :param str region: Region in which to create the model.
73
74 """
75 model_facade = client.ModelManagerFacade()
76 model_facade.connect(self.connection)
77
78 owner = owner or self.connection.info['user-info']['identity']
79
80 # XXX: We should be able to accept a credential_name without
81 # a cloud_name, and just get the cloud_name from the controller.
82 if credential_name and cloud_name:
83 credential = tag.credential(
84 cloud_name,
85 tag.untag('user-', owner),
86 credential_name
87 )
88 else:
89 credential = None
90
91
92 log.debug('Creating model %s', model_name)
93
94 model_info = await model_facade.CreateModel(
95 tag.cloud(cloud_name),
96 config,
97 credential,
98 model_name,
99 owner,
100 region,
101 )
102
103 model = Model()
104 await model.connect(
105 self.connection.endpoint,
106 model_info.uuid,
107 self.connection.username,
108 self.connection.password,
109 self.connection.cacert,
110 self.connection.macaroons,
111 )
112
113 return model
114
115 async def destroy_models(self, *uuids):
116 """Destroy one or more models.
117
118 :param str \*uuids: UUIDs of models to destroy
119
120 """
121 model_facade = client.ModelManagerFacade()
122 model_facade.connect(self.connection)
123
124 log.debug(
125 'Destroying model%s %s',
126 '' if len(uuids) == 1 else 's',
127 ', '.join(uuids)
128 )
129
130 await model_facade.DestroyModels([
131 client.Entity(tag.model(uuid))
132 for uuid in uuids
133 ])
134 destroy_model = destroy_models
135
136 def add_user(self, username, display_name=None, acl=None, models=None):
137 """Add a user to this controller.
138
139 :param str username: Username
140 :param str display_name: Display name
141 :param str acl: Access control, e.g. 'read'
142 :param list models: Models to which the user is granted access
143
144 """
145 pass
146
147 def change_user_password(self, username, password):
148 """Change the password for a user in this controller.
149
150 :param str username: Username
151 :param str password: New password
152
153 """
154 pass
155
156 def destroy(self, destroy_all_models=False):
157 """Destroy this controller.
158
159 :param bool destroy_all_models: Destroy all hosted models in the
160 controller.
161
162 """
163 pass
164
165 def disable_user(self, username):
166 """Disable a user.
167
168 :param str username: Username
169
170 """
171 pass
172
173 def enable_user(self):
174 """Re-enable a previously disabled user.
175
176 """
177 pass
178
179 def kill(self):
180 """Forcibly terminate all machines and other associated resources for
181 this controller.
182
183 """
184 pass
185
186 def get_models(self, all_=False, username=None):
187 """Return list of available models on this controller.
188
189 :param bool all_: List all models, regardless of user accessibilty
190 (admin use only)
191 :param str username: User for which to list models (admin use only)
192
193 """
194 pass
195
196 def get_payloads(self, *patterns):
197 """Return list of known payloads.
198
199 :param str \*patterns: Patterns to match against
200
201 Each pattern will be checked against the following info in Juju::
202
203 - unit name
204 - machine id
205 - payload type
206 - payload class
207 - payload id
208 - payload tag
209 - payload status
210
211 """
212 pass
213
214 def get_users(self, all_=False):
215 """Return list of users that can connect to this controller.
216
217 :param bool all_: Include disabled users
218
219 """
220 pass
221
222 def login(self):
223 """Log in to this controller.
224
225 """
226 pass
227
228 def logout(self, force=False):
229 """Log out of this controller.
230
231 :param bool force: Don't fail even if user not previously logged in
232 with a password
233
234 """
235 pass
236
237 def get_model(self, name):
238 """Get a model by name.
239
240 :param str name: Model name
241
242 """
243 pass
244
245 def get_user(self, username):
246 """Get a user by name.
247
248 :param str username: Username
249
250 """
251 pass