6 from .client
import client
7 from .client
import connection
8 from .model
import Model
10 log
= logging
.getLogger(__name__
)
13 class Controller(object):
14 def __init__(self
, loop
=None):
15 """Instantiate a new Controller.
17 One of the connect_* methods will need to be called before this
18 object can be used for anything interesting.
20 :param loop: an asyncio event loop
23 self
.loop
= loop
or asyncio
.get_event_loop()
24 self
.connection
= None
27 self
, endpoint
, username
, password
, cacert
=None, macaroons
=None):
28 """Connect to an arbitrary Juju controller.
31 self
.connection
= await connection
.Connection
.connect(
32 endpoint
, None, username
, password
, cacert
, macaroons
)
34 async def connect_current(self
):
35 """Connect to the current Juju controller.
39 await connection
.Connection
.connect_current_controller())
41 async def connect_controller(self
, controller_name
):
42 """Connect to a Juju controller by name.
46 await connection
.Connection
.connect_controller(controller_name
))
48 async def disconnect(self
):
49 """Shut down the watcher task and close websockets.
52 if self
.connection
and self
.connection
.is_open
:
53 log
.debug('Closing controller connection')
54 await self
.connection
.close()
55 self
.connection
= None
58 self
, model_name
, cloud_name
=None, credential_name
=None,
59 owner
=None, config
=None, region
=None):
60 """Add a model to this controller.
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
71 :param dict config: Model configuration.
72 :param str region: Region in which to create the model.
75 model_facade
= client
.ModelManagerFacade()
76 model_facade
.connect(self
.connection
)
78 owner
= owner
or self
.connection
.info
['user-info']['identity']
79 cloud_name
= cloud_name
or await self
.get_cloud()
82 credential
= tag
.credential(
84 tag
.untag('user-', owner
),
90 log
.debug('Creating model %s', model_name
)
92 model_info
= await model_facade
.CreateModel(
93 tag
.cloud(cloud_name
),
101 # Add our ssh key to the model, to work around
102 # https://bugs.launchpad.net/juju/+bug/1643076
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
,
110 "Could not add ssh key to model. You will not be able "
111 "to ssh into machines in this model. "
112 "Manually running `juju add-ssh-key <key>` in the cli "
113 "may fix this problem.")
117 self
.connection
.endpoint
,
119 self
.connection
.username
,
120 self
.connection
.password
,
121 self
.connection
.cacert
,
122 self
.connection
.macaroons
,
128 async def destroy_models(self
, *uuids
):
129 """Destroy one or more models.
131 :param str \*uuids: UUIDs of models to destroy
134 model_facade
= client
.ModelManagerFacade()
135 model_facade
.connect(self
.connection
)
138 'Destroying model%s %s',
139 '' if len(uuids
) == 1 else 's',
143 await model_facade
.DestroyModels([
144 client
.Entity(tag
.model(uuid
))
147 destroy_model
= destroy_models
149 def add_user(self
, username
, display_name
=None, acl
=None, models
=None):
150 """Add a user to this controller.
152 :param str username: Username
153 :param str display_name: Display name
154 :param str acl: Access control, e.g. 'read'
155 :param list models: Models to which the user is granted access
158 raise NotImplementedError()
160 def change_user_password(self
, username
, password
):
161 """Change the password for a user in this controller.
163 :param str username: Username
164 :param str password: New password
167 raise NotImplementedError()
169 def destroy(self
, destroy_all_models
=False):
170 """Destroy this controller.
172 :param bool destroy_all_models: Destroy all hosted models in the
176 raise NotImplementedError()
178 def disable_user(self
, username
):
181 :param str username: Username
184 raise NotImplementedError()
186 def enable_user(self
):
187 """Re-enable a previously disabled user.
190 raise NotImplementedError()
193 """Forcibly terminate all machines and other associated resources for
197 raise NotImplementedError()
199 async def get_cloud(self
):
201 Get the name of the cloud that this controller lives on.
203 cloud_facade
= client
.CloudFacade()
204 cloud_facade
.connect(self
.connection
)
206 result
= await cloud_facade
.Clouds()
207 cloud
= list(result
.clouds
.keys())[0] # only lives on one cloud
208 return tag
.untag('cloud-', cloud
)
210 def get_models(self
, all_
=False, username
=None):
211 """Return list of available models on this controller.
213 :param bool all_: List all models, regardless of user accessibilty
215 :param str username: User for which to list models (admin use only)
218 raise NotImplementedError()
220 def get_payloads(self
, *patterns
):
221 """Return list of known payloads.
223 :param str \*patterns: Patterns to match against
225 Each pattern will be checked against the following info in Juju::
236 raise NotImplementedError()
238 def get_users(self
, all_
=False):
239 """Return list of users that can connect to this controller.
241 :param bool all_: Include disabled users
244 raise NotImplementedError()
247 """Log in to this controller.
250 raise NotImplementedError()
252 def logout(self
, force
=False):
253 """Log out of this controller.
255 :param bool force: Don't fail even if user not previously logged in
259 raise NotImplementedError()
261 def get_model(self
, name
):
262 """Get a model by name.
264 :param str name: Model name
267 raise NotImplementedError()
269 def get_user(self
, username
):
270 """Get a user by name.
272 :param str username: Username
275 raise NotImplementedError()