Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | from django.db.models import F, Q |
| 5 | from planetstack.config import Config |
| 6 | from observer.syncstep import SyncStep |
| 7 | from core.models import Service |
| 8 | from hpc.models import ServiceProvider, ContentProvider, CDNPrefix |
| 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 SyncCDNPrefix(SyncStep, HpcLibrary): |
| 20 | provides=[CDNPrefix] |
| 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): |
| 28 | self.sanity_check() |
| 29 | |
| 30 | return SyncStep.fetch_pending(self, deleted) |
| 31 | |
| 32 | def sanity_check(self): |
| 33 | # sanity check to make sure our PS objects have CMI objects behind them |
| 34 | all_p_ids = [x["cdn_prefix_id"] for x in self.client.onev.ListAll("CDNPrefix")] |
| 35 | for p in CDNPrefix.objects.all(): |
| 36 | if (p.cdn_prefix_id is not None) and (p.cdn_prefix_id not in all_p_ids): |
| 37 | logger.info("CDN Prefix %s was not found on CMI" % p.cdn_prefix_id) |
| 38 | p.cdn_prefix_id=None |
| 39 | p.save() |
| 40 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 41 | def sync_record(self, cp): |
| 42 | logger.info("sync'ing cdn prefix %s" % str(cp)) |
| 43 | |
| 44 | if (not cp.contentProvider) or (not cp.contentProvider.content_provider_id): |
| 45 | return |
| 46 | |
| 47 | cpid = cp.contentProvider.content_provider_id |
| 48 | |
| 49 | cp_dict = {"service": "HyperCache", "enabled": cp.enabled, "content_provider_id": cpid, "cdn_prefix": cp.prefix} |
| 50 | |
Scott Baker | 9713d20 | 2014-08-26 14:38:25 -0700 | [diff] [blame] | 51 | if cp.defaultOriginServer and cp.defaultOriginServer.origin_server_id and cp.defaultOriginServer.url: |
| 52 | cp_dict["default_origin_server"] = cp.defaultOriginServer.url |
| 53 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 54 | #print cp_dict |
| 55 | |
| 56 | if not cp.cdn_prefix_id: |
| 57 | id = self.client.onev.Create("CDNPrefix", cp_dict) |
| 58 | cp.cdn_prefix_id = id |
| 59 | else: |
| 60 | self.client.onev.Update("CDNPrefix", cp.cdn_prefix_id, cp_dict) |
| 61 | |
| 62 | cp.save() |
Sapan Bhatia | 6ac3ecc | 2014-07-23 10:12:42 -0400 | [diff] [blame] | 63 | |
| 64 | def delete_record(self, m): |
| 65 | self.client.onev.Delete("CDNPrefix", m.cdn_prefix_id) |