SEBA-405 Cleanup synchronizer imports of model_accessor to globals;
Move mock modelaccessor to /tmp;
Easier mock modelaccessor configuration
Change-Id: I67a17b9a72ea69f61d92206f1b520a11c2f18d80
diff --git a/lib/xos-synchronizer/xossynchronizer/steps/SyncInstanceUsingAnsible.py b/lib/xos-synchronizer/xossynchronizer/steps/SyncInstanceUsingAnsible.py
index 6ed656c..1bc54ce 100644
--- a/lib/xos-synchronizer/xossynchronizer/steps/SyncInstanceUsingAnsible.py
+++ b/lib/xos-synchronizer/xossynchronizer/steps/SyncInstanceUsingAnsible.py
@@ -23,7 +23,6 @@
from xossynchronizer.steps.syncstep import SyncStep, DeferredException
from xossynchronizer.ansible_helper import run_template_ssh
-from xossynchronizer.modelaccessor import *
class SyncInstanceUsingAnsible(SyncStep):
diff --git a/lib/xos-synchronizer/xossynchronizer/steps/ansiblesyncstep.py b/lib/xos-synchronizer/xossynchronizer/steps/ansiblesyncstep.py
new file mode 100644
index 0000000..116f8c2
--- /dev/null
+++ b/lib/xos-synchronizer/xossynchronizer/steps/ansiblesyncstep.py
@@ -0,0 +1,60 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from xossynchronizer.ansible_helper import run_template
+from syncstep import SyncStep
+
+class AnsibleSyncStep(SyncStep):
+ def sync_record(self, o):
+ self.log.debug("In default sync record", **o.tologdict())
+
+ tenant_fields = self.map_sync_inputs(o)
+ if tenant_fields == SyncStep.SYNC_WITHOUT_RUNNING:
+ return
+
+ main_obj = self.observes_classes[0]
+
+ path = "".join(main_obj.__name__).lower()
+ res = run_template(self.playbook, tenant_fields, path=path, object=o)
+
+ if hasattr(self, "map_sync_outputs"):
+ self.map_sync_outputs(o, res)
+
+ self.log.debug("Finished default sync record", **o.tologdict())
+
+ def delete_record(self, o):
+ self.log.debug("In default delete record", **o.tologdict())
+
+ # If there is no map_delete_inputs, then assume deleting a record is a no-op.
+ if not hasattr(self, "map_delete_inputs"):
+ return
+
+ tenant_fields = self.map_delete_inputs(o)
+
+ main_obj = self.observes_classes[0]
+
+ path = "".join(main_obj.__name__).lower()
+
+ tenant_fields["delete"] = True
+ res = run_template(self.playbook, tenant_fields, path=path)
+
+ if hasattr(self, "map_delete_outputs"):
+ self.map_delete_outputs(o, res)
+ else:
+ # "rc" is often only returned when something bad happens, so assume that no "rc" implies a successful rc
+ # of 0.
+ if res[0].get("rc", 0) != 0:
+ raise Exception("Nonzero rc from Ansible during delete_record")
+
+ self.log.debug("Finished default delete record", **o.tologdict())
\ No newline at end of file
diff --git a/lib/xos-synchronizer/xossynchronizer/steps/sync_object.py b/lib/xos-synchronizer/xossynchronizer/steps/sync_object.py
deleted file mode 100644
index 1fb5894..0000000
--- a/lib/xos-synchronizer/xossynchronizer/steps/sync_object.py
+++ /dev/null
@@ -1,25 +0,0 @@
-# Copyright 2017-present Open Networking Foundation
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-
-from synchronizers.new_base.syncstep import *
-
-
-class SyncObject(SyncStep):
- provides = [] # Caller fills this in
- requested_interval = 0
- observes = [] # Caller fills this in
-
- def sync_record(self, r):
- raise DeferredException("Waiting for Service dependency: %r" % r)
diff --git a/lib/xos-synchronizer/xossynchronizer/steps/syncstep.py b/lib/xos-synchronizer/xossynchronizer/steps/syncstep.py
index 2f31e3e..7644822 100644
--- a/lib/xos-synchronizer/xossynchronizer/steps/syncstep.py
+++ b/lib/xos-synchronizer/xossynchronizer/steps/syncstep.py
@@ -13,19 +13,6 @@
# limitations under the License.
-import os
-import base64
-
-from xosconfig import Config
-from xossynchronizer.modelaccessor import *
-from xossynchronizer.ansible_helper import run_template
-
-# from tests.steps.mock_modelaccessor import model_accessor
-
-import json
-import time
-import pdb
-
from xosconfig import Config
from functools import reduce
@@ -86,13 +73,16 @@
def __init__(self, **args):
"""Initialize a sync step
Keyword arguments:
- name -- Name of the step
- provides -- XOS models sync'd by this step
+ model_accessor: class used to access models
+ driver: used by openstack synchronizer (DEPRECATED)
+ error_map: used by openstack synchronizer (DEPRECATED)
"""
- dependencies = []
+ self.model_accessor = args.get("model_accessor")
self.driver = args.get("driver")
self.error_map = args.get("error_map")
+ assert self.model_accessor is not None
+
try:
self.soft_deadline = int(self.get_prop("soft_deadline_seconds"))
except BaseException:
@@ -103,56 +93,40 @@
return
+ @property
+ def observes_classes(self):
+ """ Return a list of classes that this syncstep observes. The "observes" class member can be either a list of
+ items or a single item. Those items may be either classes or names of classes. This function always returns
+ a list of classes.
+ """
+ if not self.observes:
+ return []
+ if isinstance(self.observes, list):
+ observes = self.observes
+ else:
+ observes = [self.observes]
+ result = []
+ for class_or_name in observes:
+ if isinstance(class_or_name, str):
+ result.append(self.model_accessor.get_model_class(class_or_name))
+ else:
+ result.append(class_or_name)
+ return result
+
+
def fetch_pending(self, deletion=False):
# This is the most common implementation of fetch_pending
# Steps should override it if they have their own logic
# for figuring out what objects are outstanding.
- return model_accessor.fetch_pending(self.observes, deletion)
+ return self.model_accessor.fetch_pending(self.observes_classes, deletion)
+
def sync_record(self, o):
- self.log.debug("In default sync record", **o.tologdict())
+ self.log.debug("In abstract sync record", **o.tologdict())
+ # This method should be overridden by the service
- tenant_fields = self.map_sync_inputs(o)
- if tenant_fields == SyncStep.SYNC_WITHOUT_RUNNING:
- return
-
- main_objs = self.observes
- if isinstance(main_objs, list):
- main_objs = main_objs[0]
-
- path = "".join(main_objs.__name__).lower()
- res = run_template(self.playbook, tenant_fields, path=path, object=o)
-
- if hasattr(self, "map_sync_outputs"):
- self.map_sync_outputs(o, res)
-
- self.log.debug("Finished default sync record", **o.tologdict())
def delete_record(self, o):
- self.log.debug("In default delete record", **o.tologdict())
-
- # If there is no map_delete_inputs, then assume deleting a record is a no-op.
- if not hasattr(self, "map_delete_inputs"):
- return
-
- tenant_fields = self.map_delete_inputs(o)
-
- main_objs = self.observes
- if isinstance(main_objs, list):
- main_objs = main_objs[0]
-
- path = "".join(main_objs.__name__).lower()
-
- tenant_fields["delete"] = True
- res = run_template(self.playbook, tenant_fields, path=path)
-
- if hasattr(self, "map_delete_outputs"):
- self.map_delete_outputs(o, res)
- else:
- # "rc" is often only returned when something bad happens, so assume that no "rc" implies a successful rc
- # of 0.
- if res[0].get("rc", 0) != 0:
- raise Exception("Nonzero rc from Ansible during delete_record")
-
- self.log.debug("Finished default delete record", **o.tologdict())
+ self.log.debug("In abstract delete record", **o.tologdict())
+ # This method should be overridden by the service