67409ba879c628de63c39be2a53707b07a9f1fb6
[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, credential_name,
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'.
65 :param str credential_name: Name of the credential to use when
66 creating the model.
67 :param str owner: Username that will own the model. Defaults to
68 the current user.
69 :param dict config: Model configuration.
70 :param str region: Region in which to create the model.
71
72 """
73 # XXX: We can probably obviate the cloud_name param by getting it
74 # from the controller itself.
75
76 model_facade = client.ModelManagerFacade()
77 model_facade.connect(self.connection)
78
79 owner = owner or self.connection.info['user-info']['identity']
80
81 log.debug('Creating model %s', model_name)
82
83 model_info = await model_facade.CreateModel(
84 tag.cloud(cloud_name),
85 config,
86 tag.credential(
87 cloud_name,
88 tag.untag('user-', owner),
89 credential_name
90 ),
91 model_name,
92 owner,
93 region,
94 )
95
96 model = Model()
97 await model.connect(
98 self.connection.endpoint,
99 model_info.uuid,
100 self.connection.username,
101 self.connection.password,
102 self.connection.cacert,
103 self.connection.macaroons,
104 )
105
106 return model
107
108 async def destroy_models(self, *uuids):
109 """Destroy one or more models.
110
111 :param str \*uuids: UUIDs of models to destroy
112
113 """
114 model_facade = client.ModelManagerFacade()
115 model_facade.connect(self.connection)
116
117 log.debug(
118 'Destroying model%s %s',
119 '' if len(uuids) == 1 else 's',
120 ', '.join(uuids)
121 )
122
123 await model_facade.DestroyModels([
124 client.Entity(tag.model(uuid))
125 for uuid in uuids
126 ])
127 destroy_model = destroy_models
128
129 def add_user(self, username, display_name=None, acl=None, models=None):
130 """Add a user to this controller.
131
132 :param str username: Username
133 :param str display_name: Display name
134 :param str acl: Access control, e.g. 'read'
135 :param list models: Models to which the user is granted access
136
137 """
138 pass
139
140 def change_user_password(self, username, password):
141 """Change the password for a user in this controller.
142
143 :param str username: Username
144 :param str password: New password
145
146 """
147 pass
148
149 def destroy(self, destroy_all_models=False):
150 """Destroy this controller.
151
152 :param bool destroy_all_models: Destroy all hosted models in the
153 controller.
154
155 """
156 pass
157
158 def disable_user(self, username):
159 """Disable a user.
160
161 :param str username: Username
162
163 """
164 pass
165
166 def enable_user(self):
167 """Re-enable a previously disabled user.
168
169 """
170 pass
171
172 def kill(self):
173 """Forcibly terminate all machines and other associated resources for
174 this controller.
175
176 """
177 pass
178
179 def get_models(self, all_=False, username=None):
180 """Return list of available models on this controller.
181
182 :param bool all_: List all models, regardless of user accessibilty
183 (admin use only)
184 :param str username: User for which to list models (admin use only)
185
186 """
187 pass
188
189 def get_payloads(self, *patterns):
190 """Return list of known payloads.
191
192 :param str \*patterns: Patterns to match against
193
194 Each pattern will be checked against the following info in Juju::
195
196 - unit name
197 - machine id
198 - payload type
199 - payload class
200 - payload id
201 - payload tag
202 - payload status
203
204 """
205 pass
206
207 def get_users(self, all_=False):
208 """Return list of users that can connect to this controller.
209
210 :param bool all_: Include disabled users
211
212 """
213 pass
214
215 def login(self):
216 """Log in to this controller.
217
218 """
219 pass
220
221 def logout(self, force=False):
222 """Log out of this controller.
223
224 :param bool force: Don't fail even if user not previously logged in
225 with a password
226
227 """
228 pass
229
230 def get_model(self, name):
231 """Get a model by name.
232
233 :param str name: Model name
234
235 """
236 pass
237
238 def get_user(self, username):
239 """Get a user by name.
240
241 :param str username: Username
242
243 """
244 pass