Move add_unit logic into Application
authorTim Van Steenburgh <tvansteenburgh@gmail.com>
Tue, 18 Oct 2016 13:37:26 +0000 (09:37 -0400)
committerTim Van Steenburgh <tvansteenburgh@gmail.com>
Tue, 18 Oct 2016 13:37:26 +0000 (09:37 -0400)
examples/relate.py
juju/application.py
juju/model.py

index 241d1a6..1952ffe 100644 (file)
@@ -68,6 +68,11 @@ async def run():
         lambda delta, old_unit, new_unit, model:
             print('Unit removed: {}'.format(old_unit.entity_id))
     ))
+    unit_a, unit_b = await ubuntu_app.add_units(count=2)
+    unit_a.on_change(asyncio.coroutine(
+        lambda delta, old_unit, new_unit, model:
+            print('Unit changed: {}'.format(new_unit.entity_id))
+    ))
     await model.deploy(
         'nrpe',
         service_name='nrpe',
index 53f584a..90a78ca 100644 (file)
@@ -1,3 +1,4 @@
+import asyncio
 import logging
 
 from . import model
@@ -44,7 +45,7 @@ class Application(model.ModelEntity):
         """
         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
@@ -56,7 +57,24 @@ class Application(model.ModelEntity):
             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):
index ab9f712..b168987 100644 (file)
@@ -1295,16 +1295,12 @@ class BundleHandler(object):
             # doesn't, so we're not bothering, either
             unit_name = self._units_by_app[application].pop()
             log.debug('Reusing unit %s for %s', unit_name, application)
-            return unit_name
-        log.debug('Adding unit of %s%s',
-                  application,
-                  (' to %s' % placement) if placement else '')
-        result = await self.app_facade.AddUnits(
-            application=application,
-            placement=placement,
-            num_units=1,
+            return self.model.units[unit_name]
+
+        return await self.model.applications[application].add_unit(
+            count=1,
+            to=placement,
         )
-        return result.units[0]
 
     async def expose(self, application):
         """