From 04eee1fda9fe592e9055ac23af71f07603c8ceff Mon Sep 17 00:00:00 2001 From: Adam Israel Date: Mon, 29 Apr 2019 14:59:45 -0400 Subject: [PATCH] Fix bug 680 This patch adds two new methods, Subscribe and Unsubscribe, allowing the caller to subscribe to callback messages for an already deployed charm. Change-Id: I1e34b488914feb488cf80b157fd664ca37037e76 Signed-off-by: Adam Israel --- Makefile | 1 + n2vc/vnf.py | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index abc2c39..fab6991 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ clean: find . -name *.pyc -delete rm -rf .tox rm -rf tests/charms/builds/* + lxc list test- --format=json|jq '.[]["name"]'| xargs lxc delete --force .tox: tox -r --notest test: lint diff --git a/n2vc/vnf.py b/n2vc/vnf.py index 9f8360b..a68e657 100644 --- a/n2vc/vnf.py +++ b/n2vc/vnf.py @@ -443,11 +443,10 @@ class N2VC: # Register this application with the model-level event monitor # ################################################################ if callback: - self.monitors[model_name].AddApplication( + self.log.debug("JujuApi: Registering callback for {}".format( application_name, - callback, - *callback_args - ) + )) + await self.Subscribe(model_name, application_name, callback, *callback_args) ######################################################## # Check for specific machine placement (native charms) # @@ -801,7 +800,7 @@ class N2VC: app = await self.get_application(model, application_name) if app: # Remove this application from event monitoring - self.monitors[model_name].RemoveApplication(application_name) + await self.Unsubscribe(model_name, application_name) # self.notify_callback(model_name, application_name, "removing", callback, *callback_args) self.log.debug( @@ -906,6 +905,33 @@ class N2VC: return True return False + async def Subscribe(self, ns_name, application_name, callback, *callback_args): + """Subscribe to callbacks for an application. + + :param ns_name str: The name of the Network Service + :param application_name str: The name of the application + :param callback obj: The callback method + :param callback_args list: The list of arguments to append to calls to + the callback method + """ + self.monitors[ns_name].AddApplication( + application_name, + callback, + *callback_args + ) + + async def Unsubscribe(self, ns_name, application_name): + """Unsubscribe to callbacks for an application. + + Unsubscribes the caller from notifications from a deployed application. + + :param ns_name str: The name of the Network Service + :param application_name str: The name of the application + """ + self.monitors[ns_name].RemoveApplication( + application_name, + ) + # Non-public methods async def add_relation(self, model_name, relation1, relation2): """ -- 2.17.1