CORD-2918 delete tags with dangling instances
Change-Id: I8fc0601ad380f9105d54c0c3d98613d2b9d7f3e1
diff --git a/xos/synchronizer/model_policies/model_policy_vsgserviceinstance.py b/xos/synchronizer/model_policies/model_policy_vsgserviceinstance.py
index 6800e81..48f0527 100644
--- a/xos/synchronizer/model_policies/model_policy_vsgserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_vsgserviceinstance.py
@@ -17,6 +17,7 @@
from synchronizers.new_base.modelaccessor import VSGServiceInstance, AddressManagerServiceInstance, VSGService, Tag, Flavor, Instance, Port, NetworkParameterType, NetworkParameter, ServiceInstance, model_accessor
from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler
from synchronizers.new_base.exceptions import *
+from xosapi.orm import ORMGenericObjectNotFoundException
class VSGServiceInstancePolicy(TenantWithContainerPolicy):
model_name = "VSGServiceInstance"
@@ -80,8 +81,12 @@
def find_instance_for_s_tag(self, s_tag):
tags = Tag.objects.filter(name="s_tag", value=s_tag)
- if tags:
- return tags[0].content_object
+ for tag in tags:
+ try:
+ return tag.content_object
+ except ORMGenericObjectNotFoundException:
+ self.logger.warning("Dangling Instance reference for s-tag %s. Deleting Tag object." % s_tag)
+ tag.delete()
return None