Skip to content

Commit 0ff3b7b

Browse files
committed
Add attach_storage
1 parent 137a687 commit 0ff3b7b

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

juju/tag.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def user(username):
4343
def application(app_name):
4444
return _prefix('application-', app_name)
4545

46+
def storage(app_name):
47+
return _prefix('storage-', app_name)
4648

4749
def unit(unit_name):
4850
return _prefix('unit-', unit_name.replace('/', '-'))

juju/unit.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pyrfc3339
44

5-
from juju.errors import JujuAPIError
5+
from juju.errors import JujuAPIError, JujuError
66

77
from . import model, tag
88
from .annotationhelper import _get_annotations, _set_annotations
@@ -139,13 +139,26 @@ async def add_storage(self, storage_name, pool=None, count=1, size=1024):
139139
constraints = client.StorageConstraints(pool=pool, count=count, size=size)
140140

141141
storage_facade = client.StorageFacade.from_connection(self.connection)
142-
res = await storage_facade.AddToUnit(storages=[client.StorageAddParams(
142+
return await storage_facade.AddToUnit(storages=[client.StorageAddParams(
143143
name=storage_name,
144144
unit=tag.unit(self.name),
145145
storage=constraints,
146146
)])
147-
import pdb;pdb.set_trace()
148-
return res
147+
148+
async def attach_storage(self, storage_ids=[]):
149+
"""Attaches existing storage to this unit.
150+
151+
:param [str] storage_ids: existing storage ids to attach to the unit
152+
:return:
153+
"""
154+
if not storage_ids:
155+
raise JujuError("Expected a storage ID to be attached to unit {}".format(self.name))
156+
157+
storage_facade = client.StorageFacade.from_connection(self.connection)
158+
return await storage_facade.Attach(ids=[client.StorageAttachmentId(
159+
storage_tag=tag.storage(s_id),
160+
unit_tag=self.tag,
161+
) for s_id in storage_ids])
149162

150163
async def run(self, command, timeout=None):
151164
"""Run command on this unit.

0 commit comments

Comments
 (0)