Matteo Scandolo | ede125b | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 17 | import os |
| 18 | import sys |
| 19 | import base64 |
| 20 | from django.db.models import F, Q |
| 21 | from xos.config import Config |
| 22 | from synchronizers.base.syncstep import SyncStep |
| 23 | from core.models import Service |
| 24 | from services.hpc.models import ServiceProvider, ContentProvider |
| 25 | from xos.logger import Logger, logging |
| 26 | |
| 27 | # hpclibrary will be in steps/.. |
| 28 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 29 | sys.path.insert(0,parentdir) |
| 30 | |
| 31 | from hpclib import HpcLibrary |
| 32 | |
| 33 | logger = Logger(level=logging.INFO) |
| 34 | |
| 35 | class SyncContentProvider(SyncStep, HpcLibrary): |
| 36 | provides=[ContentProvider] |
| 37 | observes=ContentProvider |
| 38 | requested_interval=0 |
| 39 | |
| 40 | def __init__(self, **args): |
| 41 | SyncStep.__init__(self, **args) |
| 42 | HpcLibrary.__init__(self) |
| 43 | |
| 44 | def filter_hpc_service(self, objs): |
| 45 | hpcService = self.get_hpc_service() |
| 46 | |
| 47 | return [x for x in objs if x.serviceProvider.hpcService == hpcService] |
| 48 | |
| 49 | def fetch_pending(self, deleted): |
| 50 | #self.consistency_check() |
| 51 | |
| 52 | return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted)) |
| 53 | |
| 54 | def consistency_check(self): |
| 55 | # set to true if something changed |
| 56 | result=False |
| 57 | |
| 58 | # sanity check to make sure our PS objects have CMI objects behind them |
| 59 | all_cp_ids = [x["content_provider_id"] for x in self.client.onev.ListAll("ContentProvider")] |
| 60 | for cp in ContentProvider.objects.all(): |
| 61 | if (cp.content_provider_id is not None) and (cp.content_provider_id not in all_cp_ids): |
| 62 | logger.info("Content provider %s was not found on CMI" % cp.content_provider_id) |
| 63 | cp.content_provider_id=None |
| 64 | cp.save() |
| 65 | result = True |
| 66 | |
| 67 | return result |
| 68 | |
| 69 | def sync_record(self, cp): |
| 70 | logger.info("sync'ing content provider %s" % str(cp), extra=cp.tologdict()) |
| 71 | account_name = self.make_account_name(cp.name) |
| 72 | |
| 73 | if (not cp.serviceProvider) or (not cp.serviceProvider.service_provider_id): |
| 74 | raise Exception("ContentProvider %s is linked to a serviceProvider with no id" % str(cp)) |
| 75 | |
| 76 | spid = cp.serviceProvider.service_provider_id |
| 77 | |
| 78 | cp_dict = {"account": account_name, "name": cp.name, "enabled": cp.enabled} |
| 79 | |
| 80 | #print cp_dict |
| 81 | |
| 82 | if not cp.content_provider_id: |
| 83 | cp_dict["service_provider_id"] = spid |
| 84 | id = self.client.onev.Create("ContentProvider", cp_dict) |
| 85 | cp.content_provider_id = id |
| 86 | else: |
| 87 | self.client.onev.Update("ContentProvider", cp.content_provider_id, cp_dict) |
| 88 | |
| 89 | cp.save() |
| 90 | |
| 91 | def delete_record(self, m): |
| 92 | if m.content_provider_id is not None: |
| 93 | self.client.onev.Delete("ContentProvider", m.content_provider_id) |
| 94 | |