Updated juju.controller.destroy_models(), prepend 'model-' to UUID and pass a single...
[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 async def connect_current(self):
14 """Connect to the current Juju controller.
15
16 """
17 self.connection = (
18 await connection.Connection.connect_current_controller())
19
20 async def disconnect(self):
21 """Shut down the watcher task and close websockets.
22
23 """
24 if self.connection and self.connection.is_open:
25 log.debug('Closing controller connection')
26 await self.connection.close()
27 self.connection = None
28
29 async def add_model(
30 self, name, cloud, credential, owner=None,
31 config=None, region=None):
32 """Add a model to this controller.
33
34 :param str name: Name of the model
35 :param dict config: Model configuration
36 :param str credential: e.g. '<cloud>:<credential>'
37 :param str owner: Owner username
38
39 """
40 model_facade = client.ModelManagerFacade()
41 model_facade.connect(self.connection)
42
43 log.debug('Creating model %s', name)
44
45 model_info = await model_facade.CreateModel(
46 cloud,
47 config,
48 credential,
49 name,
50 owner or self.connection.info['user-info']['identity'],
51 region,
52 )
53
54 model = Model()
55 await model.connect(
56 self.connection.endpoint,
57 model_info.uuid,
58 self.connection.username,
59 self.connection.password,
60 self.connection.cacert,
61 self.connection.macaroons,
62 )
63
64 return model
65
66 async def destroy_models(self, *args):
67
68 """Destroy a model to this controller.
69
70 :param str : <UUID> of the Model
71 param accepts string of <UUID> only OR `model-<UUID>`
72
73
74 """
75 model_facade = client.ModelManagerFacade()
76 model_facade.connect(self.connection)
77
78 #Generate list of args, pre-pend 'model-'
79 prependarg = list(args)
80 for index, item in enumerate(prependarg):
81 if not item.startswith('model-'):
82 prependarg[index]="model-%s" % item
83
84 #Create list of objects to pass to DestroyModels()
85 arglist = []
86 for arg in prependarg:
87 arglist.append(client.Entity(arg))
88 log.debug('Destroying Model %s', arg)
89
90 await model_facade.DestroyModels(arglist)
91
92 def add_user(self, username, display_name=None, acl=None, models=None):
93 """Add a user to this controller.
94
95 :param str username: Username
96 :param str display_name: Display name
97 :param str acl: Access control, e.g. 'read'
98 :param list models: Models to which the user is granted access
99
100 """
101 pass
102
103 def change_user_password(self, username, password):
104 """Change the password for a user in this controller.
105
106 :param str username: Username
107 :param str password: New password
108
109 """
110 pass
111
112 def destroy(self, destroy_all_models=False):
113 """Destroy this controller.
114
115 :param bool destroy_all_models: Destroy all hosted models in the
116 controller.
117
118 """
119 pass
120
121 def disable_user(self, username):
122 """Disable a user.
123
124 :param str username: Username
125
126 """
127 pass
128
129 def enable_user(self):
130 """Re-enable a previously disabled user.
131
132 """
133 pass
134
135 def kill(self):
136 """Forcibly terminate all machines and other associated resources for
137 this controller.
138
139 """
140 pass
141
142 def get_models(self, all_=False, username=None):
143 """Return list of available models on this controller.
144
145 :param bool all_: List all models, regardless of user accessibilty
146 (admin use only)
147 :param str username: User for which to list models (admin use only)
148
149 """
150 pass
151
152 def get_payloads(self, *patterns):
153 """Return list of known payloads.
154
155 :param str \*patterns: Patterns to match against
156
157 Each pattern will be checked against the following info in Juju::
158
159 - unit name
160 - machine id
161 - payload type
162 - payload class
163 - payload id
164 - payload tag
165 - payload status
166
167 """
168 pass
169
170 def get_users(self, all_=False):
171 """Return list of users that can connect to this controller.
172
173 :param bool all_: Include disabled users
174
175 """
176 pass
177
178 def login(self):
179 """Log in to this controller.
180
181 """
182 pass
183
184 def logout(self, force=False):
185 """Log out of this controller.
186
187 :param bool force: Don't fail even if user not previously logged in
188 with a password
189
190 """
191 pass
192
193 def get_model(self, name):
194 """Get a model by name.
195
196 :param str name: Model name
197
198 """
199 pass
200
201 def get_user(self, username):
202 """Get a user by name.
203
204 :param str username: Username
205
206 """
207 pass