Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 1 | # 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 | |
| 15 | from xossynchronizer.ansible_helper import run_template |
| 16 | from syncstep import SyncStep |
| 17 | |
| 18 | class AnsibleSyncStep(SyncStep): |
| 19 | def sync_record(self, o): |
| 20 | self.log.debug("In default sync record", **o.tologdict()) |
| 21 | |
| 22 | tenant_fields = self.map_sync_inputs(o) |
| 23 | if tenant_fields == SyncStep.SYNC_WITHOUT_RUNNING: |
| 24 | return |
| 25 | |
| 26 | main_obj = self.observes_classes[0] |
| 27 | |
| 28 | path = "".join(main_obj.__name__).lower() |
| 29 | res = run_template(self.playbook, tenant_fields, path=path, object=o) |
| 30 | |
| 31 | if hasattr(self, "map_sync_outputs"): |
| 32 | self.map_sync_outputs(o, res) |
| 33 | |
| 34 | self.log.debug("Finished default sync record", **o.tologdict()) |
| 35 | |
| 36 | def delete_record(self, o): |
| 37 | self.log.debug("In default delete record", **o.tologdict()) |
| 38 | |
| 39 | # If there is no map_delete_inputs, then assume deleting a record is a no-op. |
| 40 | if not hasattr(self, "map_delete_inputs"): |
| 41 | return |
| 42 | |
| 43 | tenant_fields = self.map_delete_inputs(o) |
| 44 | |
| 45 | main_obj = self.observes_classes[0] |
| 46 | |
| 47 | path = "".join(main_obj.__name__).lower() |
| 48 | |
| 49 | tenant_fields["delete"] = True |
| 50 | res = run_template(self.playbook, tenant_fields, path=path) |
| 51 | |
| 52 | if hasattr(self, "map_delete_outputs"): |
| 53 | self.map_delete_outputs(o, res) |
| 54 | else: |
| 55 | # "rc" is often only returned when something bad happens, so assume that no "rc" implies a successful rc |
| 56 | # of 0. |
| 57 | if res[0].get("rc", 0) != 0: |
| 58 | raise Exception("Nonzero rc from Ansible during delete_record") |
| 59 | |
| 60 | self.log.debug("Finished default delete record", **o.tologdict()) |