blob: 39c15de76737b5ca6d0e541c1af85260b8638673 [file] [log] [blame]
Scott Bakercd96a8c2014-01-06 16:31:58 -08001import os
2import sys
3import base64
4from django.db.models import F, Q
Scott Baker86e132c2015-02-11 21:38:09 -08005from xos.config import Config
Scott Bakercd96a8c2014-01-06 16:31:58 -08006from observer.syncstep import SyncStep
7from core.models import Service
8from hpc.models import ServiceProvider, ContentProvider
9from util.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 requested_interval=0
22
23 def __init__(self, **args):
24 SyncStep.__init__(self, **args)
25 HpcLibrary.__init__(self)
26
Scott Baker897eadc2014-09-23 17:23:02 -070027 def fetch_pending(self, deleted):
Scott Bakerb74f0042014-09-30 11:31:56 -070028 #self.consistency_check()
Scott Baker897eadc2014-09-23 17:23:02 -070029
30 return SyncStep.fetch_pending(self, deleted)
31
Scott Bakerb74f0042014-09-30 11:31:56 -070032 def consistency_check(self):
33 # set to true if something changed
34 result=False
35
Scott Baker897eadc2014-09-23 17:23:02 -070036 # sanity check to make sure our PS objects have CMI objects behind them
37 all_cp_ids = [x["content_provider_id"] for x in self.client.onev.ListAll("ContentProvider")]
38 for cp in ContentProvider.objects.all():
39 if (cp.content_provider_id is not None) and (cp.content_provider_id not in all_cp_ids):
40 logger.info("Content provider %s was not found on CMI" % cp.content_provider_id)
41 cp.content_provider_id=None
42 cp.save()
Scott Bakerb74f0042014-09-30 11:31:56 -070043 result = True
44
45 return result
Scott Baker897eadc2014-09-23 17:23:02 -070046
Scott Bakercd96a8c2014-01-06 16:31:58 -080047 def sync_record(self, cp):
Scott Baker533d5432014-01-22 00:08:50 -080048 logger.info("sync'ing content provider %s" % str(cp))
Scott Bakercd96a8c2014-01-06 16:31:58 -080049 account_name = self.make_account_name(cp.name)
Scott Bakercd96a8c2014-01-06 16:31:58 -080050
51 if (not cp.serviceProvider) or (not cp.serviceProvider.service_provider_id):
52 return
53
54 spid = cp.serviceProvider.service_provider_id
55
Scott Baker533d5432014-01-22 00:08:50 -080056 cp_dict = {"account": account_name, "name": cp.name, "enabled": cp.enabled}
Scott Bakercd96a8c2014-01-06 16:31:58 -080057
Scott Baker88be16c2014-01-06 23:52:26 -080058 #print cp_dict
59
60 if not cp.content_provider_id:
Scott Baker533d5432014-01-22 00:08:50 -080061 cp_dict["service_provider_id"] = spid
Scott Bakercd96a8c2014-01-06 16:31:58 -080062 id = self.client.onev.Create("ContentProvider", cp_dict)
63 cp.content_provider_id = id
64 else:
Scott Baker88be16c2014-01-06 23:52:26 -080065 self.client.onev.Update("ContentProvider", cp.content_provider_id, cp_dict)
Scott Bakercd96a8c2014-01-06 16:31:58 -080066
67 cp.save()
Sapan Bhatiac7de7782014-07-23 10:13:10 -040068
Sapan Bhatiab2b44842014-07-23 10:17:28 -040069 def delete_record(self, m):
Scott Baker82c0bed2014-09-23 22:53:51 -070070 if m.content_provider_id is not None:
71 self.client.onev.Delete("ContentProvider", m.content_provider_id)
Sapan Bhatiac7de7782014-07-23 10:13:10 -040072