Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 3 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.slice import * |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 7 | from core.models.controllerusers import ControllerUsers |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 8 | from util.logger import Logger, logging |
| 9 | |
| 10 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 11 | |
| 12 | class SyncSliceMemberships(OpenStackSyncStep): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 13 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 14 | provides=[SlicePrivilege] |
| 15 | |
Sapan Bhatia | b83643c | 2014-07-23 09:59:32 -0400 | [diff] [blame] | 16 | def fetch_pending(self, deleted): |
| 17 | # Deleting site memberships is not supported yet |
| 18 | if (deleted): |
| 19 | return [] |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 20 | return SlicePrivilege.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 21 | |
Tony Mack | 37146d2 | 2013-10-21 11:02:51 -0400 | [diff] [blame] | 22 | def sync_record(self, slice_memb): |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 23 | # sync slice memberships at all slice controllers |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 24 | logger.info("syncing slice privilege: %s %s" % (slice_memb.slice.name, slice_memb.user.email)) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 25 | slice_controllers = ControllerSlices.objects.filter(slice=slice_memb.slice) |
| 26 | for slice_controller in slice_controllers: |
| 27 | if not slice_controller.tenant_id: |
Tony Mack | ac2c617 | 2014-04-10 12:25:22 -0400 | [diff] [blame] | 28 | continue |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 29 | controller_users = ControllerUsers.objects.filter(controller=slice_controller.controller, |
Tony Mack | e7045ca | 2014-04-15 12:14:09 -0400 | [diff] [blame] | 30 | user=slice_memb.user) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 31 | if controller_users: |
| 32 | kuser_id = controller_users[0].kuser_id |
| 33 | driver = self.driver.admin_driver(controller=slice_controller.controller.name) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 34 | driver.add_user_role(kuser_id, |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 35 | slice_controller.tenant_id, |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 36 | slice_memb.role.role) |