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 |
| 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 SyncServiceProvider(SyncStep, HpcLibrary): |
| 36 | provides=[ServiceProvider] |
| 37 | observes=ServiceProvider |
| 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.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_sp_ids = [x["service_provider_id"] for x in self.client.onev.ListAll("ServiceProvider")] |
| 60 | for sp in ServiceProvider.objects.all(): |
| 61 | if (sp.service_provider_id is not None) and (sp.service_provider_id not in all_sp_ids): |
| 62 | logger.info("Service provider %s was not found on CMI" % sp.service_provider_id) |
| 63 | sp.service_provider_id=None |
| 64 | sp.save() |
| 65 | result = True |
| 66 | |
| 67 | return result |
| 68 | |
| 69 | def sync_record(self, sp): |
| 70 | logger.info("sync'ing service provider %s" % str(sp),extra=sp.tologdict()) |
| 71 | account_name = self.make_account_name(sp.name) |
| 72 | sp_dict = {"account": account_name, "name": sp.name, "enabled": sp.enabled} |
| 73 | if not sp.service_provider_id: |
| 74 | id = self.client.onev.Create("ServiceProvider", sp_dict) |
| 75 | sp.service_provider_id = id |
| 76 | else: |
| 77 | self.client.onev.Update("ServiceProvider", sp.service_provider_id, sp_dict) |
| 78 | |
| 79 | sp.save() |
| 80 | |
| 81 | def delete_record(self, m): |
| 82 | if m.service_provider_id is not None: |
| 83 | self.client.onev.Delete("ServiceProvider", m.service_provider_id) |