Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
| 4 | from planetstack.config import Config |
Tony Mack | 8f30ebe | 2015-01-06 15:08:20 -0500 | [diff] [blame] | 5 | from openstack_observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 6 | from core.models.site import * |
| 7 | from observer.ansible import * |
| 8 | |
Tony Mack | 8f30ebe | 2015-01-06 15:08:20 -0500 | [diff] [blame] | 9 | class SyncControllerSites(OpenStackSyncStep): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 10 | requested_interval=0 |
| 11 | provides=[ControllerSite] |
| 12 | |
| 13 | def fetch_pending(self, deleted=False): |
| 14 | pending = super(OpenStackSyncStep, self).fetch_pending(deleted) |
| 15 | return pending.filter(controller__isnull=False) |
| 16 | |
| 17 | def sync_record(self, controller_site): |
| 18 | |
| 19 | template = os_template_env.get_template('sync_controller_sites.yaml') |
| 20 | tenant_fields = {'endpoint':controller_site.controller.auth_url, |
| 21 | 'admin_user': controller_site.controller.admin_user, |
| 22 | 'admin_password': controller_site.controller.admin_password, |
| 23 | 'admin_tenant': controller_site.controller.admin_tenant, |
Tony Mack | 8f30ebe | 2015-01-06 15:08:20 -0500 | [diff] [blame] | 24 | 'ansible_tag': '%s@%s'%(controller_site.site.login_base,controller_site.controller.name), # name of ansible playbook |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 25 | 'tenant': controller_site.site.login_base, |
| 26 | 'tenant_description': controller_site.site.name} |
| 27 | |
| 28 | rendered = template.render(tenant_fields) |
| 29 | res = run_template('sync_controller_sites.yaml', tenant_fields, path='controller_sites') |
| 30 | |
| 31 | if (len(res)==1): |
| 32 | controller_site.tenant_id = res[0]['id'] |
| 33 | controller_site.save() |
| 34 | elif (len(res)): |
| 35 | raise Exception('Could not assign roles for user %s'%tenant_fields['tenant']) |
| 36 | else: |
| 37 | raise Exception('Could not create or update user %s'%tenant_fields['tenant']) |
| 38 | |
| 39 | def delete_record(self, controller_site): |
| 40 | if controller_site.tenant_id: |
| 41 | driver = self.driver.admin_driver(controller=controller_site.controller) |
| 42 | driver.delete_tenant(controller_site.tenant_id) |
| 43 | |
| 44 | """ |
| 45 | Ansible does not support tenant deletion yet |
| 46 | |
| 47 | import pdb |
| 48 | pdb.set_trace() |
| 49 | template = os_template_env.get_template('delete_controller_sites.yaml') |
| 50 | tenant_fields = {'endpoint':controller_site.controller.auth_url, |
| 51 | 'admin_user': controller_site.controller.admin_user, |
| 52 | 'admin_password': controller_site.controller.admin_password, |
| 53 | 'admin_tenant': 'admin', |
| 54 | 'ansible_tag': 'controller_sites/%s@%s'%(controller_site.controller_site.site.login_base,controller_site.controller_site.deployment.name), # name of ansible playbook |
| 55 | 'tenant': controller_site.controller_site.site.login_base, |
| 56 | 'delete': True} |
| 57 | |
| 58 | rendered = template.render(tenant_fields) |
| 59 | res = run_template('sync_controller_sites.yaml', tenant_fields) |
| 60 | |
| 61 | if (len(res)!=1): |
| 62 | raise Exception('Could not assign roles for user %s'%tenant_fields['tenant']) |
| 63 | """ |