4 from .client
import client
5 from .client
import connection
6 from .client
import watcher
7 from .model
import Model
9 log
= logging
.getLogger(__name__
)
12 class Controller(object):
13 def __init__(self
, loop
=None):
14 """Instantiate a new Controller.
16 One of the connect_* methods will need to be called before this
17 object can be used for anything interesting.
19 :param loop: an asyncio event loop
22 self
.loop
= loop
or asyncio
.get_event_loop()
23 self
.connection
= None
26 self
, endpoint
, username
, password
, cacert
=None, macaroons
=None):
27 """Connect to an arbitrary Juju controller.
30 self
.connection
= await connection
.Connection
.connect(
31 endpoint
, None, username
, password
, cacert
, macaroons
)
33 async def connect_current(self
):
34 """Connect to the current Juju controller.
38 await connection
.Connection
.connect_current_controller())
40 async def connect_controller(self
, controller_name
):
41 """Connect to a Juju controller by name.
45 await connection
.Connection
.connect_controller(controller_name
))
47 async def disconnect(self
):
48 """Shut down the watcher task and close websockets.
51 if self
.connection
and self
.connection
.is_open
:
52 log
.debug('Closing controller connection')
53 await self
.connection
.close()
54 self
.connection
= None
57 self
, name
, cloud
, credential
, owner
=None,
58 config
=None, region
=None):
59 """Add a model to this controller.
61 :param str name: Name of the model
62 :param dict config: Model configuration
63 :param str credential: e.g. '<cloud>:<credential>'
64 :param str owner: Owner username
67 model_facade
= client
.ModelManagerFacade()
68 model_facade
.connect(self
.connection
)
70 log
.debug('Creating model %s', name
)
72 model_info
= await model_facade
.CreateModel(
77 owner
or self
.connection
.info
['user-info']['identity'],
83 self
.connection
.endpoint
,
85 self
.connection
.username
,
86 self
.connection
.password
,
87 self
.connection
.cacert
,
88 self
.connection
.macaroons
,
93 async def destroy_models(self
, *args
):
95 """Destroy a model to this controller.
97 :param str : <UUID> of the Model
98 param accepts string of <UUID> only OR `model-<UUID>`
102 model_facade
= client
.ModelManagerFacade()
103 model_facade
.connect(self
.connection
)
105 # Generate list of args, pre-pend 'model-'
106 prependarg
= list(args
)
107 for index
, item
in enumerate(prependarg
):
108 if not item
.startswith('model-'):
109 prependarg
[index
] = "model-%s" % item
111 # Create list of objects to pass to DestroyModels()
113 for arg
in prependarg
:
114 arglist
.append(client
.Entity(arg
))
115 log
.debug('Destroying Model %s', arg
)
117 await model_facade
.DestroyModels(arglist
)
119 def add_user(self
, username
, display_name
=None, acl
=None, models
=None):
120 """Add a user to this controller.
122 :param str username: Username
123 :param str display_name: Display name
124 :param str acl: Access control, e.g. 'read'
125 :param list models: Models to which the user is granted access
130 def change_user_password(self
, username
, password
):
131 """Change the password for a user in this controller.
133 :param str username: Username
134 :param str password: New password
139 def destroy(self
, destroy_all_models
=False):
140 """Destroy this controller.
142 :param bool destroy_all_models: Destroy all hosted models in the
148 def disable_user(self
, username
):
151 :param str username: Username
156 def enable_user(self
):
157 """Re-enable a previously disabled user.
163 """Forcibly terminate all machines and other associated resources for
169 def get_models(self
, all_
=False, username
=None):
170 """Return list of available models on this controller.
172 :param bool all_: List all models, regardless of user accessibilty
174 :param str username: User for which to list models (admin use only)
179 def get_payloads(self
, *patterns
):
180 """Return list of known payloads.
182 :param str \*patterns: Patterns to match against
184 Each pattern will be checked against the following info in Juju::
197 def get_users(self
, all_
=False):
198 """Return list of users that can connect to this controller.
200 :param bool all_: Include disabled users
206 """Log in to this controller.
211 def logout(self
, force
=False):
212 """Log out of this controller.
214 :param bool force: Don't fail even if user not previously logged in
220 def get_model(self
, name
):
221 """Get a model by name.
223 :param str name: Model name
228 def get_user(self
, username
):
229 """Get a user by name.
231 :param str username: Username