Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | from django.db.models import F, Q |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 5 | from xos.config import Config |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 6 | from observer.syncstep import SyncStep |
| 7 | from core.models import Service |
| 8 | from hpc.models import ServiceProvider |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 9 | from util.logger import Logger, logging |
| 10 | |
| 11 | # hpclibrary will be in steps/.. |
| 12 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
S.Çağlar Onur | 0e59183 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 13 | sys.path.insert(0,parentdir) |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 14 | |
| 15 | from hpclib import HpcLibrary |
| 16 | |
| 17 | logger = Logger(level=logging.INFO) |
| 18 | |
| 19 | class SyncServiceProvider(SyncStep, HpcLibrary): |
| 20 | provides=[ServiceProvider] |
Scott Baker | efb5d94 | 2015-03-03 23:58:01 -0800 | [diff] [blame] | 21 | observes=ServiceProvider |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 22 | requested_interval=0 |
| 23 | |
| 24 | def __init__(self, **args): |
| 25 | SyncStep.__init__(self, **args) |
| 26 | HpcLibrary.__init__(self) |
| 27 | |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 28 | def fetch_pending(self, deleted): |
Scott Baker | a79222f | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 29 | #self.consistency_check() |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 30 | |
| 31 | return SyncStep.fetch_pending(self, deleted) |
| 32 | |
Scott Baker | a79222f | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 33 | def consistency_check(self): |
| 34 | # set to true if something changed |
| 35 | result=False |
| 36 | |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 37 | # 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 Baker | a79222f | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 44 | result = True |
| 45 | |
| 46 | return result |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 47 | |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 48 | 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 Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 51 | sp_dict = {"account": account_name, "name": sp.name, "enabled": sp.enabled} |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 52 | if not sp.service_provider_id: |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 53 | id = self.client.onev.Create("ServiceProvider", sp_dict) |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 54 | sp.service_provider_id = id |
| 55 | else: |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 56 | self.client.onev.Update("ServiceProvider", sp.service_provider_id, sp_dict) |
Scott Baker | f520845 | 2014-01-06 16:04:58 -0800 | [diff] [blame] | 57 | |
| 58 | sp.save() |
Sapan Bhatia | f49ed9c | 2014-07-23 10:25:21 -0400 | [diff] [blame] | 59 | |
| 60 | def delete_record(self, m): |
Scott Baker | 456c826 | 2014-09-23 22:53:51 -0700 | [diff] [blame] | 61 | if m.service_provider_id is not None: |
| 62 | self.client.onev.Delete("ServiceProvider", m.service_provider_id) |