blob: ad8a253cc5a23be152a8c296bd52e4520561cb1a [file] [log] [blame]
Scott Baker62c7eaf2018-05-22 15:59:26 -07001
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 Bakerc808c672019-02-04 11:38:20 -080017from xossynchronizer.modelaccessor import Slice
Scott Baker62c7eaf2018-05-22 15:59:26 -070018from newopenstacksyncstep import NewOpenStackSyncStep
19
20from xosconfig import Config
21from multistructlog import create_logger
22
23log = create_logger(Config().get('logging'))
24
25class SyncSlice(NewOpenStackSyncStep):
26 provides=[Slice]
27 requested_interval=0
28 observes=Slice
29
30 def fetch_pending(self, deleted):
31 """ Figure out which Principals are interesting to the OpenStack synchronizer. It's necessary to filter as we're
32 synchronizing a core model, and we only want to synchronize trust domains that will exist within
33 OpenStack.
34 """
35 objs = super(SyncSlice, self).fetch_pending(deleted)
36 for obj in objs[:]:
37 # If the Slice isn't in a TrustDomain, then the OpenStack synchronizer can't do anything with it
38 if not obj.trust_domain:
39 objs.remove(obj)
40 continue
41
42 # If the TrustDomain isn't part of the OpenStack service, then it's someone else's trust domain
43 if "OpenStackService" not in obj.trust_domain.owner.leaf_model.class_names:
44 objs.remove(obj)
45 return objs
46
47 def sync_record(self, slice):
48 service = slice.trust_domain.owner.leaf_model
49 conn = self.connect_openstack_admin(service)
50
51 os_domain = conn.identity.find_domain(slice.trust_domain.name)
52
53 os_slice = conn.identity.find_project(slice.name, domain_id=os_domain.id)
54 if os_slice:
55 log.info("Slice already exists in openstack", slice=slice)
56 else:
57 log.info("Creating Slice", slice=slice)
58 os_slice = conn.identity.create_project(name=slice.name, domain_id=os_domain.id)
59
60 if os_slice.id != slice.backend_handle:
61 slice.backend_handle = os_slice.id
62 slice.save(update_fields=["backend_handle"])
63
64 def delete_record(self, slice):
65 service = slice.trust_domain.owner.leaf_model
66 conn = self.connect_openstack_admin(service)
67
68 os_domain = conn.identity.find_domain(slice.trust_domain.name)
69
70 os_slice = conn.identity.find_project(slice.name, domain_id=os_domain.id)
71 if (not os_slice):
72 log.info("Slice already does not exist in openstack", slice=slice)
73 else:
74 log.info("Deleting Slice", slice=slice, os_id=os_slice.id)
75 conn.identity.delete_project(os_slice.id)