Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | from django.db.models import F, Q |
| 5 | from xos.config import Config |
| 6 | from synchronizers.base.syncstep import SyncStep |
| 7 | from core.models import Service |
| 8 | from services.hpc.models import ServiceProvider |
| 9 | from xos.logger import Logger, logging |
| 10 | |
| 11 | # hpclibrary will be in steps/.. |
| 12 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 13 | sys.path.insert(0,parentdir) |
| 14 | |
| 15 | from hpclib import HpcLibrary |
| 16 | |
| 17 | logger = Logger(level=logging.INFO) |
| 18 | |
| 19 | class 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) |