Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | from django.db.models import Q, F |
Srikanth Vavilapalli | 2c41ab3 | 2016-11-17 03:14:30 +0000 | [diff] [blame] | 4 | from core.models import ModelLink, CoarseTenant |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 5 | from services.exampleservice.models import ExampleService, ExampleTenant |
| 6 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 7 | |
| 8 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 9 | sys.path.insert(0, parentdir) |
| 10 | |
| 11 | class SyncExampleTenant(SyncInstanceUsingAnsible): |
| 12 | |
| 13 | provides = [ExampleTenant] |
| 14 | |
| 15 | observes = ExampleTenant |
| 16 | |
| 17 | requested_interval = 0 |
| 18 | |
| 19 | template_name = "exampletenant_playbook.yaml" |
| 20 | |
| 21 | service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key" |
| 22 | |
Srikanth Vavilapalli | 2c41ab3 | 2016-11-17 03:14:30 +0000 | [diff] [blame] | 23 | watches = [ModelLink(CoarseTenant,via='coarsetenant')] |
| 24 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 25 | def __init__(self, *args, **kwargs): |
| 26 | super(SyncExampleTenant, self).__init__(*args, **kwargs) |
| 27 | |
| 28 | def fetch_pending(self, deleted): |
| 29 | |
| 30 | if (not deleted): |
| 31 | objs = ExampleTenant.get_tenant_objects().filter( |
| 32 | Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False)) |
| 33 | else: |
| 34 | # If this is a deletion we get all of the deleted tenants.. |
| 35 | objs = ExampleTenant.get_deleted_tenant_objects() |
| 36 | |
| 37 | return objs |
| 38 | |
| 39 | def get_exampleservice(self, o): |
| 40 | if not o.provider_service: |
| 41 | return None |
| 42 | |
| 43 | exampleservice = ExampleService.get_service_objects().filter(id=o.provider_service.id) |
| 44 | |
| 45 | if not exampleservice: |
| 46 | return None |
| 47 | |
| 48 | return exampleservice[0] |
| 49 | |
| 50 | # Gets the attributes that are used by the Ansible template but are not |
| 51 | # part of the set of default attributes. |
| 52 | def get_extra_attributes(self, o): |
| 53 | fields = {} |
| 54 | fields['tenant_message'] = o.tenant_message |
| 55 | exampleservice = self.get_exampleservice(o) |
| 56 | fields['service_message'] = exampleservice.service_message |
| 57 | return fields |
| 58 | |
Scott Baker | 9b1a3eb | 2017-01-23 14:46:27 -0800 | [diff] [blame^] | 59 | def delete_record(self, port): |
| 60 | # Nothing needs to be done to delete an exampleservice; it goes away |
| 61 | # when the instance holding the exampleservice is deleted. |
| 62 | pass |
| 63 | |