Move add_unit logic into Application
[osm/N2VC.git] / juju / application.py
1 import asyncio
2 import logging
3
4 from . import model
5 from .client import client
6
7 log = logging.getLogger(__name__)
8
9
10 class Application(model.ModelEntity):
11 @property
12 def _unit_match_pattern(self):
13 return r'^{}.*$'.format(self.entity_id)
14
15 def on_unit_add(self, callable_):
16 """Add a "unit added" observer to this entity, which will be called
17 whenever a unit is added to this application.
18
19 """
20 self.model.add_observer(
21 callable_, 'unit', 'add', self._unit_match_pattern)
22
23 def on_unit_remove(self, callable_):
24 """Add a "unit removed" observer to this entity, which will be called
25 whenever a unit is removed from this application.
26
27 """
28 self.model.add_observer(
29 callable_, 'unit', 'remove', self._unit_match_pattern)
30
31 @property
32 def units(self):
33 return [
34 unit for unit in self.model.units.values()
35 if unit.application == self.name
36 ]
37
38 def add_relation(self, local_relation, remote_relation):
39 """Add a relation to another service.
40
41 :param str local_relation: Name of relation on this service
42 :param str remote_relation: Name of relation on the other service in
43 the form '<service>[:<relation_name>]'
44
45 """
46 pass
47
48 async def add_unit(self, count=1, to=None):
49 """Add one or more units to this service.
50
51 :param int count: Number of units to add
52 :param str to: Placement directive, e.g.::
53 '23' - machine 23
54 'lxc:7' - new lxc container on machine 7
55 '24/lxc/3' - lxc container 3 or machine 24
56
57 If None, a new machine is provisioned.
58
59 """
60 app_facade = client.ApplicationFacade()
61 app_facade.connect(self.connection)
62
63 log.debug(
64 'Adding %s unit%s to %s',
65 count, '' if count == 1 else 's', self.name)
66
67 result = await app_facade.AddUnits(
68 application=self.name,
69 placement=to,
70 num_units=count,
71 )
72
73 return await asyncio.gather(*[
74 asyncio.ensure_future(self.model._wait_for_new('unit', unit_id))
75 for unit_id in result.units
76 ])
77
78 add_units = add_unit
79
80 def allocate(self, budget, value):
81 """Allocate budget to this service.
82
83 :param str budget: Name of budget
84 :param int value: Budget limit
85
86 """
87 pass
88
89 def attach(self, resource_name, file_path):
90 """Upload a file as a resource for this service.
91
92 :param str resource: Name of the resource
93 :param str file_path: Path to the file to upload
94
95 """
96 pass
97
98 def collect_metrics(self):
99 """Collect metrics on this service.
100
101 """
102 pass
103
104 def destroy_relation(self, local_relation, remote_relation):
105 """Remove a relation to another service.
106
107 :param str local_relation: Name of relation on this service
108 :param str remote_relation: Name of relation on the other service in
109 the form '<service>[:<relation_name>]'
110
111 """
112 pass
113 remove_relation = destroy_relation
114
115 async def destroy(self):
116 """Remove this service from the model.
117
118 """
119 app_facade = client.ApplicationFacade()
120 app_facade.connect(self.connection)
121
122 log.debug(
123 'Destroying %s', self.name)
124
125 return await app_facade.Destroy(self.name)
126 remove = destroy
127
128 async def expose(self):
129 """Make this service publicly available over the network.
130
131 """
132 app_facade = client.ApplicationFacade()
133 app_facade.connect(self.connection)
134
135 log.debug(
136 'Exposing %s', self.name)
137
138 return await app_facade.Expose(self.name)
139
140 def get_config(self):
141 """Return the configuration settings for this service.
142
143 """
144 pass
145
146 def get_constraints(self):
147 """Return the machine constraints for this service.
148
149 """
150 pass
151
152 def get_actions(self, schema=False):
153 """Get actions defined for this service.
154
155 :param bool schema: Return the full action schema
156
157 """
158 pass
159
160 def get_resources(self, details=False):
161 """Return resources for this service.
162
163 :param bool details: Include detailed info about resources used by each
164 unit
165
166 """
167 pass
168
169 def run(self, command, timeout=None):
170 """Run command on all units for this service.
171
172 :param str command: The command to run
173 :param int timeout: Time to wait before command is considered failed
174
175 """
176 pass
177
178 def set_config(self, to_default=False, **config):
179 """Set configuration options for this service.
180
181 :param bool to_default: Set service options to default values
182 :param \*\*config: Config key/values
183
184 """
185 pass
186
187 def set_constraints(self, constraints):
188 """Set machine constraints for this service.
189
190 :param :class:`juju.Constraints` constraints: Machine constraints
191
192 """
193 pass
194
195 def set_meter_status(self, status, info=None):
196 """Set the meter status on this status.
197
198 :param str status: Meter status, e.g. 'RED', 'AMBER'
199 :param str info: Extra info message
200
201 """
202 pass
203
204 def set_plan(self, plan_name):
205 """Set the plan for this service, effective immediately.
206
207 :param str plan_name: Name of plan
208
209 """
210 pass
211
212 def unexpose(self):
213 """Remove public availability over the network for this service.
214
215 """
216 pass
217
218 def update_allocation(self, allocation):
219 """Update existing allocation for this service.
220
221 :param int allocation: The allocation to set
222
223 """
224 pass
225
226 def upgrade_charm(
227 self, channel=None, force_series=False, force_units=False,
228 path=None, resources=None, revision=-1, switch=None):
229 """Upgrade the charm for this service.
230
231 :param str channel: Channel to use when getting the charm from the
232 charm store, e.g. 'development'
233 :param bool force_series: Upgrade even if series of deployed service
234 is not supported by the new charm
235 :param bool force_units: Upgrade all units immediately, even if in
236 error state
237 :param str path: Uprade to a charm located at path
238 :param dict resources: Dictionary of resource name/filepath pairs
239 :param int revision: Explicit upgrade revision
240 :param str switch: Crossgrade charm url
241
242 """
243 pass