blob: 9d7d7eabf0f0c6e6853bdeef67d663334b127952 [file] [log] [blame]
Scott Bakerf5208452014-01-06 16:04:58 -08001import os
2import sys
3import base64
4from django.db.models import F, Q
Scott Baker76a840e2015-02-11 21:38:09 -08005from xos.config import Config
Scott Bakerf5208452014-01-06 16:04:58 -08006from 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__),"..")
S.Çağlar Onur0e591832015-02-24 17:28:09 -050013sys.path.insert(0,parentdir)
Scott Bakerf5208452014-01-06 16:04:58 -080014
15from hpclib import HpcLibrary
16
17logger = Logger(level=logging.INFO)
18
19class SyncServiceProvider(SyncStep, HpcLibrary):
20 provides=[ServiceProvider]
Scott Bakerefb5d942015-03-03 23:58:01 -080021 observes=ServiceProvider
Scott Bakerf5208452014-01-06 16:04:58 -080022 requested_interval=0
23
24 def __init__(self, **args):
25 SyncStep.__init__(self, **args)
26 HpcLibrary.__init__(self)
27
Scott Baker217c8ed2014-09-23 17:23:02 -070028 def fetch_pending(self, deleted):
Scott Bakera79222f2014-09-30 11:31:56 -070029 #self.consistency_check()
Scott Baker217c8ed2014-09-23 17:23:02 -070030
31 return SyncStep.fetch_pending(self, deleted)
32
Scott Bakera79222f2014-09-30 11:31:56 -070033 def consistency_check(self):
34 # set to true if something changed
35 result=False
36
Scott Baker217c8ed2014-09-23 17:23:02 -070037 # sanity check to make sure our PS objects have CMI objects behind them
38 all_sp_ids = [x["service_provider_id"] for x in self.client.onev.ListAll("ServiceProvider")]
39 for sp in ServiceProvider.objects.all():
40 if (sp.service_provider_id is not None) and (sp.service_provider_id not in all_sp_ids):
41 logger.info("Service provider %s was not found on CMI" % sp.service_provider_id)
42 sp.service_provider_id=None
43 sp.save()
Scott Bakera79222f2014-09-30 11:31:56 -070044 result = True
45
46 return result
Scott Baker217c8ed2014-09-23 17:23:02 -070047
Scott Bakerf5208452014-01-06 16:04:58 -080048 def sync_record(self, sp):
49 logger.info("sync'ing service provider %s" % str(sp))
50 account_name = self.make_account_name(sp.name)
Scott Baker88be16c2014-01-06 23:52:26 -080051 sp_dict = {"account": account_name, "name": sp.name, "enabled": sp.enabled}
Scott Bakerf5208452014-01-06 16:04:58 -080052 if not sp.service_provider_id:
Scott Baker88be16c2014-01-06 23:52:26 -080053 id = self.client.onev.Create("ServiceProvider", sp_dict)
Scott Bakerf5208452014-01-06 16:04:58 -080054 sp.service_provider_id = id
55 else:
Scott Baker88be16c2014-01-06 23:52:26 -080056 self.client.onev.Update("ServiceProvider", sp.service_provider_id, sp_dict)
Scott Bakerf5208452014-01-06 16:04:58 -080057
58 sp.save()
Sapan Bhatiaf49ed9c2014-07-23 10:25:21 -040059
60 def delete_record(self, m):
Scott Baker456c8262014-09-23 22:53:51 -070061 if m.service_provider_id is not None:
62 self.client.onev.Delete("ServiceProvider", m.service_provider_id)