blob: 3781e1f6216ada7780ed8c854344f8eddda27191 [file] [log] [blame]
Scott Bakerc2fddaa2019-01-30 15:45:03 -08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Zack Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
16
Scott Bakerc2fddaa2019-01-30 15:45:03 -080017from xossynchronizer.ansible_helper import run_template
Zack Williams5c2ea232019-01-30 15:23:01 -070018
19from .syncstep import SyncStep
20
Scott Bakerc2fddaa2019-01-30 15:45:03 -080021
22class AnsibleSyncStep(SyncStep):
23 def sync_record(self, o):
24 self.log.debug("In default sync record", **o.tologdict())
25
26 tenant_fields = self.map_sync_inputs(o)
27 if tenant_fields == SyncStep.SYNC_WITHOUT_RUNNING:
28 return
29
30 main_obj = self.observes_classes[0]
31
32 path = "".join(main_obj.__name__).lower()
33 res = run_template(self.playbook, tenant_fields, path=path, object=o)
34
35 if hasattr(self, "map_sync_outputs"):
36 self.map_sync_outputs(o, res)
37
38 self.log.debug("Finished default sync record", **o.tologdict())
39
40 def delete_record(self, o):
41 self.log.debug("In default delete record", **o.tologdict())
42
43 # If there is no map_delete_inputs, then assume deleting a record is a no-op.
44 if not hasattr(self, "map_delete_inputs"):
45 return
46
47 tenant_fields = self.map_delete_inputs(o)
48
49 main_obj = self.observes_classes[0]
50
51 path = "".join(main_obj.__name__).lower()
52
53 tenant_fields["delete"] = True
54 res = run_template(self.playbook, tenant_fields, path=path)
55
56 if hasattr(self, "map_delete_outputs"):
57 self.map_delete_outputs(o, res)
58 else:
59 # "rc" is often only returned when something bad happens, so assume that no "rc" implies a successful rc
60 # of 0.
61 if res[0].get("rc", 0) != 0:
62 raise Exception("Nonzero rc from Ansible during delete_record")
63
Zack Williams5c2ea232019-01-30 15:23:01 -070064 self.log.debug("Finished default delete record", **o.tologdict())