5 from .client
import client
6 from .client
import connection
7 from .client
import watcher
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
),
103 self
.connection
.endpoint
,
105 self
.connection
.username
,
106 self
.connection
.password
,
107 self
.connection
.cacert
,
108 self
.connection
.macaroons
,
113 async def destroy_models(self
, *uuids
):
114 """Destroy one or more models.
116 :param str \*uuids: UUIDs of models to destroy
119 model_facade
= client
.ModelManagerFacade()
120 model_facade
.connect(self
.connection
)
123 'Destroying model%s %s',
124 '' if len(uuids
) == 1 else 's',
128 await model_facade
.DestroyModels([
129 client
.Entity(tag
.model(uuid
))
132 destroy_model
= destroy_models
134 def add_user(self
, username
, display_name
=None, acl
=None, models
=None):
135 """Add a user to this controller.
137 :param str username: Username
138 :param str display_name: Display name
139 :param str acl: Access control, e.g. 'read'
140 :param list models: Models to which the user is granted access
145 def change_user_password(self
, username
, password
):
146 """Change the password for a user in this controller.
148 :param str username: Username
149 :param str password: New password
154 def destroy(self
, destroy_all_models
=False):
155 """Destroy this controller.
157 :param bool destroy_all_models: Destroy all hosted models in the
163 def disable_user(self
, username
):
166 :param str username: Username
171 def enable_user(self
):
172 """Re-enable a previously disabled user.
178 """Forcibly terminate all machines and other associated resources for
184 async def get_cloud(self
):
186 Get the name of the cloud that this controller lives on.
188 cloud_facade
= client
.CloudFacade()
189 cloud_facade
.connect(self
.connection
)
191 result
= await cloud_facade
.Clouds()
192 cloud
= list(result
.clouds
.keys())[0] # only lives on one cloud
193 return tag
.untag('cloud-', cloud
)
195 def get_models(self
, all_
=False, username
=None):
196 """Return list of available models on this controller.
198 :param bool all_: List all models, regardless of user accessibilty
200 :param str username: User for which to list models (admin use only)
205 def get_payloads(self
, *patterns
):
206 """Return list of known payloads.
208 :param str \*patterns: Patterns to match against
210 Each pattern will be checked against the following info in Juju::
223 def get_users(self
, all_
=False):
224 """Return list of users that can connect to this controller.
226 :param bool all_: Include disabled users
232 """Log in to this controller.
237 def logout(self
, force
=False):
238 """Log out of this controller.
240 :param bool force: Don't fail even if user not previously logged in
246 def get_model(self
, name
):
247 """Get a model by name.
249 :param str name: Model name
254 def get_user(self
, username
):
255 """Get a user by name.
257 :param str username: Username