+import asyncio
import logging
from . import model
class Application(model.ModelEntity):
+ @property
+ def _unit_match_pattern(self):
+ return r'^{}.*$'.format(self.entity_id)
+
+ def on_unit_add(self, callable_):
+ """Add a "unit added" observer to this entity, which will be called
+ whenever a unit is added to this application.
+
+ """
+ self.model.add_observer(
+ callable_, 'unit', 'add', self._unit_match_pattern)
+
+ def on_unit_remove(self, callable_):
+ """Add a "unit removed" observer to this entity, which will be called
+ whenever a unit is removed from this application.
+
+ """
+ self.model.add_observer(
+ callable_, 'unit', 'remove', self._unit_match_pattern)
+
@property
def units(self):
return [
"""
pass
- def add_unit(self, count=1, to=None):
+ async def add_unit(self, count=1, to=None):
"""Add one or more units to this service.
:param int count: Number of units to add
If None, a new machine is provisioned.
"""
- pass
+ app_facade = client.ApplicationFacade()
+ app_facade.connect(self.connection)
+
+ log.debug(
+ 'Adding %s unit%s to %s',
+ count, '' if count == 1 else 's', self.name)
+
+ result = await app_facade.AddUnits(
+ application=self.name,
+ placement=to,
+ num_units=count,
+ )
+
+ return await asyncio.gather(*[
+ asyncio.ensure_future(self.model._wait_for_new('unit', unit_id))
+ for unit_id in result.units
+ ])
+
add_units = add_unit
def allocate(self, budget, value):
return await app_facade.Destroy(self.name)
remove = destroy
- def expose(self):
+ async def expose(self):
"""Make this service publicly available over the network.
"""
- pass
+ app_facade = client.ApplicationFacade()
+ app_facade.connect(self.connection)
+
+ log.debug(
+ 'Exposing %s', self.name)
+
+ return await app_facade.Expose(self.name)
def get_config(self):
"""Return the configuration settings for this service.