Replace pass with NotImplementedError in method stubs
[osm/N2VC.git] / juju / application.py
1 import asyncio
2 import logging
3
4 from . import model
5 from .client import client
6 from .placement import parse as parse_placement
7
8 log = logging.getLogger(__name__)
9
10
11 class Application(model.ModelEntity):
12 @property
13 def _unit_match_pattern(self):
14 return r'^{}.*$'.format(self.entity_id)
15
16 def on_unit_add(self, callable_):
17 """Add a "unit added" observer to this entity, which will be called
18 whenever a unit is added to this application.
19
20 """
21 self.model.add_observer(
22 callable_, 'unit', 'add', self._unit_match_pattern)
23
24 def on_unit_remove(self, callable_):
25 """Add a "unit removed" observer to this entity, which will be called
26 whenever a unit is removed from this application.
27
28 """
29 self.model.add_observer(
30 callable_, 'unit', 'remove', self._unit_match_pattern)
31
32 @property
33 def units(self):
34 return [
35 unit for unit in self.model.units.values()
36 if unit.application == self.name
37 ]
38
39 @property
40 def status(self):
41 """Get the application status, as set by the charm's leader.
42
43 """
44 return self.safe_data['status']['current']
45
46 @property
47 def status_message(self):
48 """Get the application status message, as set by the charm's leader.
49
50 """
51 return self.safe_data['status']['message']
52
53 @property
54 def tag(self):
55 return 'application-%s' % self.name
56
57 async def add_relation(self, local_relation, remote_relation):
58 """Add a relation to another application.
59
60 :param str local_relation: Name of relation on this application
61 :param str remote_relation: Name of relation on the other
62 application in the form '<application>[:<relation_name>]'
63
64 """
65 if ':' not in local_relation:
66 local_relation = '{}:{}'.format(self.name, local_relation)
67
68 return await self.model.add_relation(local_relation, remote_relation)
69
70 async def add_unit(self, count=1, to=None):
71 """Add one or more units to this application.
72
73 :param int count: Number of units to add
74 :param str to: Placement directive, e.g.::
75 '23' - machine 23
76 'lxc:7' - new lxc container on machine 7
77 '24/lxc/3' - lxc container 3 or machine 24
78
79 If None, a new machine is provisioned.
80
81 """
82 app_facade = client.ApplicationFacade()
83 app_facade.connect(self.connection)
84
85 log.debug(
86 'Adding %s unit%s to %s',
87 count, '' if count == 1 else 's', self.name)
88
89 result = await app_facade.AddUnits(
90 application=self.name,
91 placement=parse_placement(to) if to else None,
92 num_units=count,
93 )
94
95 return await asyncio.gather(*[
96 asyncio.ensure_future(self.model._wait_for_new('unit', unit_id))
97 for unit_id in result.units
98 ])
99
100 add_units = add_unit
101
102 def allocate(self, budget, value):
103 """Allocate budget to this application.
104
105 :param str budget: Name of budget
106 :param int value: Budget limit
107
108 """
109 raise NotImplementedError()
110
111 def attach(self, resource_name, file_path):
112 """Upload a file as a resource for this application.
113
114 :param str resource: Name of the resource
115 :param str file_path: Path to the file to upload
116
117 """
118 raise NotImplementedError()
119
120 def collect_metrics(self):
121 """Collect metrics on this application.
122
123 """
124 raise NotImplementedError()
125
126 async def destroy_relation(self, local_relation, remote_relation):
127 """Remove a relation to another application.
128
129 :param str local_relation: Name of relation on this application
130 :param str remote_relation: Name of relation on the other
131 application in the form '<application>[:<relation_name>]'
132
133 """
134 if ':' not in local_relation:
135 local_relation = '{}:{}'.format(self.name, local_relation)
136
137 app_facade = client.ApplicationFacade()
138 app_facade.connect(self.connection)
139
140 log.debug(
141 'Destroying relation %s <-> %s', local_relation, remote_relation)
142
143 return await app_facade.DestroyRelation([
144 local_relation, remote_relation])
145 remove_relation = destroy_relation
146
147 async def destroy_unit(self, *unit_names):
148 """Destroy units by name.
149
150 """
151 return await self.model.destroy_units(*unit_names)
152 destroy_units = destroy_unit
153
154 async def destroy(self):
155 """Remove this application from the model.
156
157 """
158 app_facade = client.ApplicationFacade()
159 app_facade.connect(self.connection)
160
161 log.debug(
162 'Destroying %s', self.name)
163
164 return await app_facade.Destroy(self.name)
165 remove = destroy
166
167 async def expose(self):
168 """Make this application publicly available over the network.
169
170 """
171 app_facade = client.ApplicationFacade()
172 app_facade.connect(self.connection)
173
174 log.debug(
175 'Exposing %s', self.name)
176
177 return await app_facade.Expose(self.name)
178
179 async def get_config(self):
180 """Return the configuration settings dict for this application.
181
182 """
183 app_facade = client.ApplicationFacade()
184 app_facade.connect(self.connection)
185
186 log.debug(
187 'Getting config for %s', self.name)
188
189 return (await app_facade.Get(self.name)).config
190
191 async def get_constraints(self):
192 """Return the machine constraints dict for this application.
193
194 """
195 app_facade = client.ApplicationFacade()
196 app_facade.connect(self.connection)
197
198 log.debug(
199 'Getting constraints for %s', self.name)
200
201 result = (await app_facade.Get(self.name)).constraints
202 return vars(result) if result else result
203
204 def get_actions(self, schema=False):
205 """Get actions defined for this application.
206
207 :param bool schema: Return the full action schema
208
209 """
210 raise NotImplementedError()
211
212 def get_resources(self, details=False):
213 """Return resources for this application.
214
215 :param bool details: Include detailed info about resources used by each
216 unit
217
218 """
219 raise NotImplementedError()
220
221 async def run(self, command, timeout=None):
222 """Run command on all units for this application.
223
224 :param str command: The command to run
225 :param int timeout: Time to wait before command is considered failed
226
227 """
228 action = client.ActionFacade()
229 action.connect(self.connection)
230
231 log.debug(
232 'Running `%s` on all units of %s', command, self.name)
233
234 # TODO this should return a list of Actions
235 return await action.Run(
236 [self.name],
237 command,
238 [],
239 timeout,
240 [],
241 )
242
243 async def set_annotations(self, annotations):
244 """Set annotations on this application.
245
246 :param annotations map[string]string: the annotations as key/value
247 pairs.
248
249 """
250 log.debug('Updating annotations on application %s', self.name)
251
252 self.ann_facade = client.AnnotationsFacade()
253 self.ann_facade.connect(self.connection)
254
255 ann = client.EntityAnnotations(
256 entity=self.name,
257 annotations=annotations,
258 )
259 return await self.ann_facade.Set([ann])
260
261 async def set_config(self, config, to_default=False):
262 """Set configuration options for this application.
263
264 :param config: Dict of configuration to set
265 :param bool to_default: Set application options to default values
266
267 """
268 app_facade = client.ApplicationFacade()
269 app_facade.connect(self.connection)
270
271 log.debug(
272 'Setting config for %s: %s', self.name, config)
273
274 return await app_facade.Set(self.name, config)
275
276 async def set_constraints(self, constraints):
277 """Set machine constraints for this application.
278
279 :param dict constraints: Dict of machine constraints
280
281 """
282 app_facade = client.ApplicationFacade()
283 app_facade.connect(self.connection)
284
285 log.debug(
286 'Setting constraints for %s: %s', self.name, constraints)
287
288 return await app_facade.SetConstraints(self.name, constraints)
289
290 def set_meter_status(self, status, info=None):
291 """Set the meter status on this status.
292
293 :param str status: Meter status, e.g. 'RED', 'AMBER'
294 :param str info: Extra info message
295
296 """
297 raise NotImplementedError()
298
299 def set_plan(self, plan_name):
300 """Set the plan for this application, effective immediately.
301
302 :param str plan_name: Name of plan
303
304 """
305 raise NotImplementedError()
306
307 async def unexpose(self):
308 """Remove public availability over the network for this application.
309
310 """
311 app_facade = client.ApplicationFacade()
312 app_facade.connect(self.connection)
313
314 log.debug(
315 'Unexposing %s', self.name)
316
317 return await app_facade.Unexpose(self.name)
318
319 def update_allocation(self, allocation):
320 """Update existing allocation for this application.
321
322 :param int allocation: The allocation to set
323
324 """
325 raise NotImplementedError()
326
327 def upgrade_charm(
328 self, channel=None, force_series=False, force_units=False,
329 path=None, resources=None, revision=-1, switch=None):
330 """Upgrade the charm for this application.
331
332 :param str channel: Channel to use when getting the charm from the
333 charm store, e.g. 'development'
334 :param bool force_series: Upgrade even if series of deployed
335 application is not supported by the new charm
336 :param bool force_units: Upgrade all units immediately, even if in
337 error state
338 :param str path: Uprade to a charm located at path
339 :param dict resources: Dictionary of resource name/filepath pairs
340 :param int revision: Explicit upgrade revision
341 :param str switch: Crossgrade charm url
342
343 """
344 raise NotImplementedError()
345
346 async def get_metrics(self):
347 """Get metrics for this application's units.
348
349 :return: Dictionary of unit_name:metrics
350
351 """
352 return await self.model.get_metrics(self.tag)