Skip to content

Commit fb837cc

Browse files
committed
Fix for unit.run_actions to support both old and new clients
Fixes #705
1 parent 1dd9cbf commit fb837cc

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

juju/action.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33

44
class Action(model.ModelEntity):
5+
6+
def __init__(self, entity_id, model, history_index=-1, connected=True):
7+
super().__init__(entity_id, model, history_index, connected)
8+
self.results = {}
9+
510
@property
611
def status(self):
712
return self.data['status']
813

914
async def wait(self):
10-
return await self.model.get_action_output(self.id)
15+
self.results = await self.model.get_action_output(self.id)
16+
return self

juju/model.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,19 @@ async def set_constraints(self, constraints):
22622262
constraints=constraints)
22632263

22642264
async def get_action_output(self, action_uuid, wait=None):
2265-
"""Get the results of an action by ID.
2265+
""" Get the results of an action by ID.
2266+
2267+
:param str action_uuid: Id of the action
2268+
:param int wait: Time in seconds to wait for action to complete.
2269+
:return dict: Output from action
2270+
:raises: :class:`JujuError` if invalid action_uuid
2271+
"""
2272+
action = await self._get_completed_action(action_uuid, wait=wait)
2273+
# ActionResult.output is None if the action produced no output
2274+
return {} if action.output is None else action.output
2275+
2276+
async def _get_completed_action(self, action_uuid, wait=None):
2277+
"""Get the completed internal _definitions.Action object.
22662278
22672279
:param str action_uuid: Id of the action
22682280
:param int wait: Time in seconds to wait for action to complete.
@@ -2288,13 +2300,8 @@ async def _wait_for_action_status():
22882300
await jasyncio.wait_for(
22892301
_wait_for_action_status(),
22902302
timeout=wait)
2291-
action_output = await action_facade.Actions(entities=entity)
2292-
# ActionResult.output is None if the action produced no output
2293-
if action_output.results[0].output is None:
2294-
output = {}
2295-
else:
2296-
output = action_output.results[0].output
2297-
return output
2303+
action_results = await action_facade.Actions(entities=entity)
2304+
return action_results.results[0]
22982305

22992306
async def get_action_status(self, uuid_or_prefix=None, name=None):
23002307
"""Get the status of all actions, filtered by ID, ID prefix, or name.

juju/unit.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,27 @@ async def run_action(self, action_name, **params):
227227
228228
"""
229229
action_facade = client.ActionFacade.from_connection(self.connection)
230-
231230
log.debug('Starting action `%s` on %s', action_name, self.name)
232231

233-
res = await action_facade.EnqueueOperation(actions=[client.Action(
232+
old_client = self.connection.is_using_old_client
233+
234+
op = action_facade.Enqueue if old_client else action_facade.EnqueueOperation
235+
res = await op(actions=[client.Action(
234236
name=action_name,
235237
parameters=params,
236238
receiver=self.tag,
237239
)])
238-
action = res.actions[0].result
239-
error = res.actions[0].error
240+
241+
_action = res.results[0] if old_client else res.actions[0]
242+
action = _action.action
243+
error = _action.error
244+
240245
if error and error.code == 'not found':
241246
raise ValueError('Action `%s` not found on %s' % (action_name,
242247
self.name))
243248
elif error:
244249
raise Exception('Unknown action error: %s' % error.serialize())
245-
action_id = action[len('action-'):]
250+
action_id = action.tag[len('action-'):]
246251
log.debug('Action started as %s', action_id)
247252
# we mustn't use wait_for_action because that blocks until the
248253
# action is complete, rather than just being in the model

0 commit comments

Comments
 (0)