blob: af6d6858c60912e6882392dffae986b0460a5a0b [file] [log] [blame]
Scott Baker25467ff2016-08-04 09:50:22 -07001import os
2import sys
3import base64
4from django.db.models import F, Q
5from xos.config import Config
6from synchronizers.base.syncstep import SyncStep
7from core.models import Service
8from services.hpc.models import ServiceProvider
9from xos.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 observes=ServiceProvider
22 requested_interval=0
23
24 def __init__(self, **args):
25 SyncStep.__init__(self, **args)
26 HpcLibrary.__init__(self)
27
28 def filter_hpc_service(self, objs):
29 hpcService = self.get_hpc_service()
30
31 return [x for x in objs if x.hpcService == hpcService]
32
33 def fetch_pending(self, deleted):
34 #self.consistency_check()
35
36 return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted))
37
38 def consistency_check(self):
39 # set to true if something changed
40 result=False
41
42 # sanity check to make sure our PS objects have CMI objects behind them
43 all_sp_ids = [x["service_provider_id"] for x in self.client.onev.ListAll("ServiceProvider")]
44 for sp in ServiceProvider.objects.all():
45 if (sp.service_provider_id is not None) and (sp.service_provider_id not in all_sp_ids):
46 logger.info("Service provider %s was not found on CMI" % sp.service_provider_id)
47 sp.service_provider_id=None
48 sp.save()
49 result = True
50
51 return result
52
53 def sync_record(self, sp):
54 logger.info("sync'ing service provider %s" % str(sp),extra=sp.tologdict())
55 account_name = self.make_account_name(sp.name)
56 sp_dict = {"account": account_name, "name": sp.name, "enabled": sp.enabled}
57 if not sp.service_provider_id:
58 id = self.client.onev.Create("ServiceProvider", sp_dict)
59 sp.service_provider_id = id
60 else:
61 self.client.onev.Update("ServiceProvider", sp.service_provider_id, sp_dict)
62
63 sp.save()
64
65 def delete_record(self, m):
66 if m.service_provider_id is not None:
67 self.client.onev.Delete("ServiceProvider", m.service_provider_id)