Add more connect_ methods on Controller
[osm/N2VC.git] / juju / controller.py
1 import asyncio
2 import logging
3
4 from .client import client
5 from .client import connection
6 from .client import watcher
7 from .model import Model
8
9 log = logging.getLogger(__name__)
10
11
12 class Controller(object):
13 def __init__(self, loop=None):
14 """Instantiate a new Controller.
15
16 One of the connect_* methods will need to be called before this
17 object can be used for anything interesting.
18
19 :param loop: an asyncio event loop
20
21 """
22 self.loop = loop or asyncio.get_event_loop()
23 self.connection = None
24
25 async def connect(
26 self, endpoint, username, password, cacert=None, macaroons=None):
27 """Connect to an arbitrary Juju controller.
28
29 """
30 self.connection = await connection.Connection.connect(
31 endpoint, None, username, password, cacert, macaroons)
32
33 async def connect_current(self):
34 """Connect to the current Juju controller.
35
36 """
37 self.connection = (
38 await connection.Connection.connect_current_controller())
39
40 async def connect_controller(self, controller_name):
41 """Connect to a Juju controller by name.
42
43 """
44 self.connection = (
45 await connection.Connection.connect_controller(controller_name))
46
47 async def disconnect(self):
48 """Shut down the watcher task and close websockets.
49
50 """
51 if self.connection and self.connection.is_open:
52 log.debug('Closing controller connection')
53 await self.connection.close()
54 self.connection = None
55
56 async def add_model(
57 self, name, cloud, credential, owner=None,
58 config=None, region=None):
59 """Add a model to this controller.
60
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
65
66 """
67 model_facade = client.ModelManagerFacade()
68 model_facade.connect(self.connection)
69
70 log.debug('Creating model %s', name)
71
72 model_info = await model_facade.CreateModel(
73 cloud,
74 config,
75 credential,
76 name,
77 owner or self.connection.info['user-info']['identity'],
78 region,
79 )
80
81 model = Model()
82 await model.connect(
83 self.connection.endpoint,
84 model_info.uuid,
85 self.connection.username,
86 self.connection.password,
87 self.connection.cacert,
88 self.connection.macaroons,
89 )
90
91 return model
92
93 async def destroy_models(self, *args):
94
95 """Destroy a model to this controller.
96
97 :param str : <UUID> of the Model
98 param accepts string of <UUID> only OR `model-<UUID>`
99
100
101 """
102 model_facade = client.ModelManagerFacade()
103 model_facade.connect(self.connection)
104
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
110
111 # Create list of objects to pass to DestroyModels()
112 arglist = []
113 for arg in prependarg:
114 arglist.append(client.Entity(arg))
115 log.debug('Destroying Model %s', arg)
116
117 await model_facade.DestroyModels(arglist)
118
119 def add_user(self, username, display_name=None, acl=None, models=None):
120 """Add a user to this controller.
121
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
126
127 """
128 pass
129
130 def change_user_password(self, username, password):
131 """Change the password for a user in this controller.
132
133 :param str username: Username
134 :param str password: New password
135
136 """
137 pass
138
139 def destroy(self, destroy_all_models=False):
140 """Destroy this controller.
141
142 :param bool destroy_all_models: Destroy all hosted models in the
143 controller.
144
145 """
146 pass
147
148 def disable_user(self, username):
149 """Disable a user.
150
151 :param str username: Username
152
153 """
154 pass
155
156 def enable_user(self):
157 """Re-enable a previously disabled user.
158
159 """
160 pass
161
162 def kill(self):
163 """Forcibly terminate all machines and other associated resources for
164 this controller.
165
166 """
167 pass
168
169 def get_models(self, all_=False, username=None):
170 """Return list of available models on this controller.
171
172 :param bool all_: List all models, regardless of user accessibilty
173 (admin use only)
174 :param str username: User for which to list models (admin use only)
175
176 """
177 pass
178
179 def get_payloads(self, *patterns):
180 """Return list of known payloads.
181
182 :param str \*patterns: Patterns to match against
183
184 Each pattern will be checked against the following info in Juju::
185
186 - unit name
187 - machine id
188 - payload type
189 - payload class
190 - payload id
191 - payload tag
192 - payload status
193
194 """
195 pass
196
197 def get_users(self, all_=False):
198 """Return list of users that can connect to this controller.
199
200 :param bool all_: Include disabled users
201
202 """
203 pass
204
205 def login(self):
206 """Log in to this controller.
207
208 """
209 pass
210
211 def logout(self, force=False):
212 """Log out of this controller.
213
214 :param bool force: Don't fail even if user not previously logged in
215 with a password
216
217 """
218 pass
219
220 def get_model(self, name):
221 """Get a model by name.
222
223 :param str name: Model name
224
225 """
226 pass
227
228 def get_user(self, username):
229 """Get a user by name.
230
231 :param str username: Username
232
233 """
234 pass