blob: ca0ba6a7746c8cca0438b0055c2cf1b6a315bd6a [file] [log] [blame]
Scott Bakerf5208452014-01-06 16:04: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
Scott Bakerf5208452014-01-06 16:04:58 -08009from 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 SyncServiceProvider(SyncStep, HpcLibrary):
20 provides=[ServiceProvider]
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_sp_ids = [x["service_provider_id"] for x in self.client.onev.ListAll("ServiceProvider")]
38 for sp in ServiceProvider.objects.all():
39 if (sp.service_provider_id is not None) and (sp.service_provider_id not in all_sp_ids):
40 logger.info("Service provider %s was not found on CMI" % sp.service_provider_id)
41 sp.service_provider_id=None
42 sp.save()
Scott Bakerb74f0042014-09-30 11:31:56 -070043 result = True
44
45 return result
Scott Baker897eadc2014-09-23 17:23:02 -070046
Scott Bakerf5208452014-01-06 16:04:58 -080047 def sync_record(self, sp):
48 logger.info("sync'ing service provider %s" % str(sp))
49 account_name = self.make_account_name(sp.name)
Scott Baker88be16c2014-01-06 23:52:26 -080050 sp_dict = {"account": account_name, "name": sp.name, "enabled": sp.enabled}
Scott Bakerf5208452014-01-06 16:04:58 -080051 if not sp.service_provider_id:
Scott Baker88be16c2014-01-06 23:52:26 -080052 id = self.client.onev.Create("ServiceProvider", sp_dict)
Scott Bakerf5208452014-01-06 16:04:58 -080053 sp.service_provider_id = id
54 else:
Scott Baker88be16c2014-01-06 23:52:26 -080055 self.client.onev.Update("ServiceProvider", sp.service_provider_id, sp_dict)
Scott Bakerf5208452014-01-06 16:04:58 -080056
57 sp.save()
Sapan Bhatiaf49ed9c2014-07-23 10:25:21 -040058
59 def delete_record(self, m):
Scott Baker82c0bed2014-09-23 22:53:51 -070060 if m.service_provider_id is not None:
61 self.client.onev.Delete("ServiceProvider", m.service_provider_id)