import os
import sys
import base64

from django.db.models import F, Q
from xos.config import Config
from observer.syncstep import SyncStep
from core.models import Service
from hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer
from util.logger import Logger, logging

# hpclibrary will be in steps/..
parentdir = os.path.join(os.path.dirname(__file__),"..")
sys.path.insert(0,parentdir)

from hpclib import HpcLibrary

logger = Logger(level=logging.INFO)

class SyncOriginServer(SyncStep, HpcLibrary):
    provides=[OriginServer]
    requested_interval=0

    def __init__(self, **args):
        SyncStep.__init__(self, **args)
        HpcLibrary.__init__(self)

    def fetch_pending(self, deleted):
        #self.consistency_check()

        return SyncStep.fetch_pending(self, deleted)

    def consistency_check(self):
        # set to true if something changed
        result=False

        # sanity check to make sure our PS objects have CMI objects behind them
        all_ors_ids = [x["origin_server_id"] for x in self.client.onev.ListAll("OriginServer")]
        for ors in OriginServer.objects.all():
            if (ors.origin_server_id is not None) and (ors.origin_server_id not in all_ors_ids):
                # we have an origin server ID, but it doesn't exist in the CMI
                # something went wrong
                # start over
                logger.info("origin server %s was not found on CMI" % ors.origin_server_id)
                ors.origin_server_id=None
                ors.save()
                result = True

        return result

    def sync_record(self, ors):
        logger.info("sync'ing origin server %s" % str(ors))

        if (not ors.contentProvider) or (not ors.contentProvider.content_provider_id):
            return

        cpid = ors.contentProvider.content_provider_id

        # validation requires URL start with http://
        url = ors.url
        if not url.startswith("http://"):
            url = "http://" + url

        ors_dict = {"authenticated_content": ors.authenticated, "zone_redirects": ors.redirects, "content_provider_id": cpid, "url": url, "service_type": "HyperCache", "caching_type": "Optimistic", "description": ors.description}

        #print os_dict

        if not ors.origin_server_id:
            id = self.client.onev.Create("OriginServer", ors_dict)
            ors.origin_server_id = id
        else:
            self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict)

        # ... something breaks (analytics) if the URL starts with http://, so we
        # change it in cob after we added it via onev.
        url = url[7:]
        self.client.cob.UpdateContent(ors.origin_server_id, {"url": url})

        ors.silent = True
        ors.save()

    def delete_record(self, m):
        if m.origin_server_id is not None:
            self.client.onev.Delete("OriginServer", m.origin_server_id)

[((14, 0, 14, 28), 'sys.path.insert', 'sys.path.insert', ({(14, 16, 14, 17): '(0)', (14, 18, 14, 27): 'parentdir'}, {}), '(0, parentdir)', False, 'import sys\n'), ((18, 9, 18, 35), 'util.logger.Logger', 'Logger', (), '', False, 'from util.logger import Logger, logging\n'), ((13, 25, 13, 50), 'os.path.dirname', 'os.path.dirname', ({(13, 41, 13, 49): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((25, 8, 25, 39), 'observer.syncstep.SyncStep.__init__', 'SyncStep.__init__', ({(25, 26, 25, 30): 'self'}, {}), '(self, **args)', False, 'from observer.syncstep import SyncStep\n'), ((26, 8, 26, 33), 'hpclib.HpcLibrary.__init__', 'HpcLibrary.__init__', ({(26, 28, 26, 32): 'self'}, {}), '(self)', False, 'from hpclib import HpcLibrary\n'), ((31, 15, 31, 52), 'observer.syncstep.SyncStep.fetch_pending', 'SyncStep.fetch_pending', ({(31, 38, 31, 42): 'self', (31, 44, 31, 51): 'deleted'}, {}), '(self, deleted)', False, 'from observer.syncstep import SyncStep\n'), ((39, 19, 39, 45), 'hpc.models.OriginServer.objects.all', 'OriginServer.objects.all', ({}, {}), '()', False, 'from hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer\n')]