Replace pass with NotImplementedError in method stubs
[osm/N2VC.git] / juju / controller.py
1 import asyncio
2 import logging
3
4 from . import tag
5 from . import utils
6 from .client import client
7 from .client import connection
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 cloud_name = cloud_name or await self.get_cloud()
80
81 if credential_name:
82 credential = tag.credential(
83 cloud_name,
84 tag.untag('user-', owner),
85 credential_name
86 )
87 else:
88 credential = None
89
90 log.debug('Creating model %s', model_name)
91
92 model_info = await model_facade.CreateModel(
93 tag.cloud(cloud_name),
94 config,
95 credential,
96 model_name,
97 owner,
98 region,
99 )
100
101 # Add our ssh key to the model, to work around
102 # https://bugs.launchpad.net/juju/+bug/1643076
103 try:
104 ssh_key = await utils.read_ssh_key(loop=self.loop)
105 await utils.execute_process(
106 'juju', 'add-ssh-key', '-m', model_name, ssh_key, log=log)
107 except Exception as e:
108 log.exception(
109 "Could not add ssh key to model. You will not be able "
110 "to ssh into machines in this model. "
111 "Manually running `juju add-ssh-key <key>` in the cli "
112 "may fix this problem.")
113
114 model = Model()
115 await model.connect(
116 self.connection.endpoint,
117 model_info.uuid,
118 self.connection.username,
119 self.connection.password,
120 self.connection.cacert,
121 self.connection.macaroons,
122 )
123
124 return model
125
126 async def destroy_models(self, *uuids):
127 """Destroy one or more models.
128
129 :param str \*uuids: UUIDs of models to destroy
130
131 """
132 model_facade = client.ModelManagerFacade()
133 model_facade.connect(self.connection)
134
135 log.debug(
136 'Destroying model%s %s',
137 '' if len(uuids) == 1 else 's',
138 ', '.join(uuids)
139 )
140
141 await model_facade.DestroyModels([
142 client.Entity(tag.model(uuid))
143 for uuid in uuids
144 ])
145 destroy_model = destroy_models
146
147 def add_user(self, username, display_name=None, acl=None, models=None):
148 """Add a user to this controller.
149
150 :param str username: Username
151 :param str display_name: Display name
152 :param str acl: Access control, e.g. 'read'
153 :param list models: Models to which the user is granted access
154
155 """
156 raise NotImplementedError()
157
158 def change_user_password(self, username, password):
159 """Change the password for a user in this controller.
160
161 :param str username: Username
162 :param str password: New password
163
164 """
165 raise NotImplementedError()
166
167 def destroy(self, destroy_all_models=False):
168 """Destroy this controller.
169
170 :param bool destroy_all_models: Destroy all hosted models in the
171 controller.
172
173 """
174 raise NotImplementedError()
175
176 def disable_user(self, username):
177 """Disable a user.
178
179 :param str username: Username
180
181 """
182 raise NotImplementedError()
183
184 def enable_user(self):
185 """Re-enable a previously disabled user.
186
187 """
188 raise NotImplementedError()
189
190 def kill(self):
191 """Forcibly terminate all machines and other associated resources for
192 this controller.
193
194 """
195 raise NotImplementedError()
196
197 async def get_cloud(self):
198 """
199 Get the name of the cloud that this controller lives on.
200 """
201 cloud_facade = client.CloudFacade()
202 cloud_facade.connect(self.connection)
203
204 result = await cloud_facade.Clouds()
205 cloud = list(result.clouds.keys())[0] # only lives on one cloud
206 return tag.untag('cloud-', cloud)
207
208 def get_models(self, all_=False, username=None):
209 """Return list of available models on this controller.
210
211 :param bool all_: List all models, regardless of user accessibilty
212 (admin use only)
213 :param str username: User for which to list models (admin use only)
214
215 """
216 raise NotImplementedError()
217
218 def get_payloads(self, *patterns):
219 """Return list of known payloads.
220
221 :param str \*patterns: Patterns to match against
222
223 Each pattern will be checked against the following info in Juju::
224
225 - unit name
226 - machine id
227 - payload type
228 - payload class
229 - payload id
230 - payload tag
231 - payload status
232
233 """
234 raise NotImplementedError()
235
236 def get_users(self, all_=False):
237 """Return list of users that can connect to this controller.
238
239 :param bool all_: Include disabled users
240
241 """
242 raise NotImplementedError()
243
244 def login(self):
245 """Log in to this controller.
246
247 """
248 raise NotImplementedError()
249
250 def logout(self, force=False):
251 """Log out of this controller.
252
253 :param bool force: Don't fail even if user not previously logged in
254 with a password
255
256 """
257 raise NotImplementedError()
258
259 def get_model(self, name):
260 """Get a model by name.
261
262 :param str name: Model name
263
264 """
265 raise NotImplementedError()
266
267 def get_user(self, username):
268 """Get a user by name.
269
270 :param str username: Username
271
272 """
273 raise NotImplementedError()