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