blob: fc10dae2fe07dc58ba8f03a0f181fc6e451b7a80 [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
27 def fetch_pending(self):
28 return ContentProvider.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
29
30 def sync_record(self, cp):
Scott Baker533d5432014-01-22 00:08:50 -080031 logger.info("sync'ing content provider %s" % str(cp))
Scott Bakercd96a8c2014-01-06 16:31:58 -080032 account_name = self.make_account_name(cp.name)
33 print "XXX", cp.name, account_name
34
35 if (not cp.serviceProvider) or (not cp.serviceProvider.service_provider_id):
36 return
37
38 spid = cp.serviceProvider.service_provider_id
39
Scott Baker533d5432014-01-22 00:08:50 -080040 cp_dict = {"account": account_name, "name": cp.name, "enabled": cp.enabled}
Scott Bakercd96a8c2014-01-06 16:31:58 -080041
Scott Baker88be16c2014-01-06 23:52:26 -080042 #print cp_dict
43
44 if not cp.content_provider_id:
Scott Baker533d5432014-01-22 00:08:50 -080045 cp_dict["service_provider_id"] = spid
Scott Bakercd96a8c2014-01-06 16:31:58 -080046 id = self.client.onev.Create("ContentProvider", cp_dict)
47 cp.content_provider_id = id
48 else:
Scott Baker88be16c2014-01-06 23:52:26 -080049 self.client.onev.Update("ContentProvider", cp.content_provider_id, cp_dict)
Scott Bakercd96a8c2014-01-06 16:31:58 -080050
51 cp.save()