Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | from django.db.models import F, Q |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame^] | 5 | from xos.config import Config |
Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 6 | from observer.syncstep import SyncStep |
| 7 | from core.models import Service |
| 8 | from hpc.models import ServiceProvider, ContentProvider |
| 9 | from util.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 SyncContentProvider(SyncStep, HpcLibrary): |
| 20 | provides=[ContentProvider] |
| 21 | requested_interval=0 |
| 22 | |
| 23 | def __init__(self, **args): |
| 24 | SyncStep.__init__(self, **args) |
| 25 | HpcLibrary.__init__(self) |
| 26 | |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 27 | def fetch_pending(self, deleted): |
Scott Baker | b74f004 | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 28 | #self.consistency_check() |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 29 | |
| 30 | return SyncStep.fetch_pending(self, deleted) |
| 31 | |
Scott Baker | b74f004 | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 32 | def consistency_check(self): |
| 33 | # set to true if something changed |
| 34 | result=False |
| 35 | |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 36 | # sanity check to make sure our PS objects have CMI objects behind them |
| 37 | all_cp_ids = [x["content_provider_id"] for x in self.client.onev.ListAll("ContentProvider")] |
| 38 | for cp in ContentProvider.objects.all(): |
| 39 | if (cp.content_provider_id is not None) and (cp.content_provider_id not in all_cp_ids): |
| 40 | logger.info("Content provider %s was not found on CMI" % cp.content_provider_id) |
| 41 | cp.content_provider_id=None |
| 42 | cp.save() |
Scott Baker | b74f004 | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 43 | result = True |
| 44 | |
| 45 | return result |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 46 | |
Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 47 | def sync_record(self, cp): |
Scott Baker | 533d543 | 2014-01-22 00:08:50 -0800 | [diff] [blame] | 48 | logger.info("sync'ing content provider %s" % str(cp)) |
Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 49 | account_name = self.make_account_name(cp.name) |
Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 50 | |
| 51 | if (not cp.serviceProvider) or (not cp.serviceProvider.service_provider_id): |
| 52 | return |
| 53 | |
| 54 | spid = cp.serviceProvider.service_provider_id |
| 55 | |
Scott Baker | 533d543 | 2014-01-22 00:08:50 -0800 | [diff] [blame] | 56 | cp_dict = {"account": account_name, "name": cp.name, "enabled": cp.enabled} |
Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 57 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 58 | #print cp_dict |
| 59 | |
| 60 | if not cp.content_provider_id: |
Scott Baker | 533d543 | 2014-01-22 00:08:50 -0800 | [diff] [blame] | 61 | cp_dict["service_provider_id"] = spid |
Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 62 | id = self.client.onev.Create("ContentProvider", cp_dict) |
| 63 | cp.content_provider_id = id |
| 64 | else: |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 65 | self.client.onev.Update("ContentProvider", cp.content_provider_id, cp_dict) |
Scott Baker | cd96a8c | 2014-01-06 16:31:58 -0800 | [diff] [blame] | 66 | |
| 67 | cp.save() |
Sapan Bhatia | c7de778 | 2014-07-23 10:13:10 -0400 | [diff] [blame] | 68 | |
Sapan Bhatia | b2b4484 | 2014-07-23 10:17:28 -0400 | [diff] [blame] | 69 | def delete_record(self, m): |
Scott Baker | 82c0bed | 2014-09-23 22:53:51 -0700 | [diff] [blame] | 70 | if m.content_provider_id is not None: |
| 71 | self.client.onev.Delete("ContentProvider", m.content_provider_id) |
Sapan Bhatia | c7de778 | 2014-07-23 10:13:10 -0400 | [diff] [blame] | 72 | |