Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame^] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | from django.db.models import F, Q |
| 5 | from xos.config import Config |
| 6 | from synchronizers.base.syncstep import SyncStep |
| 7 | from core.models import Service |
| 8 | from services.hpc.models import ServiceProvider, ContentProvider |
| 9 | from xos.logger import Logger, logging |
| 10 | |
| 11 | # hpclibrary will be in steps/.. |
| 12 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 13 | sys.path.insert(0,parentdir) |
| 14 | |
| 15 | from hpclib import HpcLibrary |
| 16 | |
| 17 | logger = Logger(level=logging.INFO) |
| 18 | |
| 19 | class SyncContentProvider(SyncStep, HpcLibrary): |
| 20 | provides=[ContentProvider] |
| 21 | observes=ContentProvider |
| 22 | requested_interval=0 |
| 23 | |
| 24 | def __init__(self, **args): |
| 25 | SyncStep.__init__(self, **args) |
| 26 | HpcLibrary.__init__(self) |
| 27 | |
| 28 | def filter_hpc_service(self, objs): |
| 29 | hpcService = self.get_hpc_service() |
| 30 | |
| 31 | return [x for x in objs if x.serviceProvider.hpcService == hpcService] |
| 32 | |
| 33 | def fetch_pending(self, deleted): |
| 34 | #self.consistency_check() |
| 35 | |
| 36 | return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted)) |
| 37 | |
| 38 | def consistency_check(self): |
| 39 | # set to true if something changed |
| 40 | result=False |
| 41 | |
| 42 | # sanity check to make sure our PS objects have CMI objects behind them |
| 43 | all_cp_ids = [x["content_provider_id"] for x in self.client.onev.ListAll("ContentProvider")] |
| 44 | for cp in ContentProvider.objects.all(): |
| 45 | if (cp.content_provider_id is not None) and (cp.content_provider_id not in all_cp_ids): |
| 46 | logger.info("Content provider %s was not found on CMI" % cp.content_provider_id) |
| 47 | cp.content_provider_id=None |
| 48 | cp.save() |
| 49 | result = True |
| 50 | |
| 51 | return result |
| 52 | |
| 53 | def sync_record(self, cp): |
| 54 | logger.info("sync'ing content provider %s" % str(cp), extra=cp.tologdict()) |
| 55 | account_name = self.make_account_name(cp.name) |
| 56 | |
| 57 | if (not cp.serviceProvider) or (not cp.serviceProvider.service_provider_id): |
| 58 | raise Exception("ContentProvider %s is linked to a serviceProvider with no id" % str(cp)) |
| 59 | |
| 60 | spid = cp.serviceProvider.service_provider_id |
| 61 | |
| 62 | cp_dict = {"account": account_name, "name": cp.name, "enabled": cp.enabled} |
| 63 | |
| 64 | #print cp_dict |
| 65 | |
| 66 | if not cp.content_provider_id: |
| 67 | cp_dict["service_provider_id"] = spid |
| 68 | id = self.client.onev.Create("ContentProvider", cp_dict) |
| 69 | cp.content_provider_id = id |
| 70 | else: |
| 71 | self.client.onev.Update("ContentProvider", cp.content_provider_id, cp_dict) |
| 72 | |
| 73 | cp.save() |
| 74 | |
| 75 | def delete_record(self, m): |
| 76 | if m.content_provider_id is not None: |
| 77 | self.client.onev.Delete("ContentProvider", m.content_provider_id) |
| 78 | |