blob: 3e30ed36b59715802755e8c16f1c1ce28157442b [file] [log] [blame]
Scott Baker25467ff2016-08-04 09:50:22 -07001import os
2import sys
3import base64
4from django.db.models import F, Q
5from xos.config import Config
6from synchronizers.base.syncstep import SyncStep
7from core.models import Service
8from services.hpc.models import ServiceProvider, ContentProvider
9from xos.logger import Logger, logging
10
11# hpclibrary will be in steps/..
12parentdir = os.path.join(os.path.dirname(__file__),"..")
13sys.path.insert(0,parentdir)
14
15from hpclib import HpcLibrary
16
17logger = Logger(level=logging.INFO)
18
19class 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