blob: cebe1869264592b7cfa60c38d321841376b0fcbb [file] [log] [blame]
Scott Bakercd96a8c2014-01-06 16:31:58 -08001import os
2import sys
3import base64
4from django.db.models import F, Q
5from planetstack.config import Config
6from 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):
28 self.sanity_check()
29
30 return SyncStep.fetch_pending(self, deleted)
31
32 def sanity_check(self):
33 # sanity check to make sure our PS objects have CMI objects behind them
34 all_cp_ids = [x["content_provider_id"] for x in self.client.onev.ListAll("ContentProvider")]
35 for cp in ContentProvider.objects.all():
36 if (cp.content_provider_id is not None) and (cp.content_provider_id not in all_cp_ids):
37 logger.info("Content provider %s was not found on CMI" % cp.content_provider_id)
38 cp.content_provider_id=None
39 cp.save()
40
Scott Bakercd96a8c2014-01-06 16:31:58 -080041 def sync_record(self, cp):
Scott Baker533d5432014-01-22 00:08:50 -080042 logger.info("sync'ing content provider %s" % str(cp))
Scott Bakercd96a8c2014-01-06 16:31:58 -080043 account_name = self.make_account_name(cp.name)
44 print "XXX", cp.name, account_name
45
46 if (not cp.serviceProvider) or (not cp.serviceProvider.service_provider_id):
47 return
48
49 spid = cp.serviceProvider.service_provider_id
50
Scott Baker533d5432014-01-22 00:08:50 -080051 cp_dict = {"account": account_name, "name": cp.name, "enabled": cp.enabled}
Scott Bakercd96a8c2014-01-06 16:31:58 -080052
Scott Baker88be16c2014-01-06 23:52:26 -080053 #print cp_dict
54
55 if not cp.content_provider_id:
Scott Baker533d5432014-01-22 00:08:50 -080056 cp_dict["service_provider_id"] = spid
Scott Bakercd96a8c2014-01-06 16:31:58 -080057 id = self.client.onev.Create("ContentProvider", cp_dict)
58 cp.content_provider_id = id
59 else:
Scott Baker88be16c2014-01-06 23:52:26 -080060 self.client.onev.Update("ContentProvider", cp.content_provider_id, cp_dict)
Scott Bakercd96a8c2014-01-06 16:31:58 -080061
62 cp.save()
Sapan Bhatiac7de7782014-07-23 10:13:10 -040063
Sapan Bhatiab2b44842014-07-23 10:17:28 -040064 def delete_record(self, m):
Scott Baker82c0bed2014-09-23 22:53:51 -070065 if m.content_provider_id is not None:
66 self.client.onev.Delete("ContentProvider", m.content_provider_id)
Sapan Bhatiac7de7782014-07-23 10:13:10 -040067