blob: 1fe66cee9c92233f80444bf7fa30bb42da86edc0 [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 TrustDomain
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 SyncTrustDomain(NewOpenStackSyncStep):
26 provides=[TrustDomain]
27 requested_interval=0
28 observes=TrustDomain
29
30 def fetch_pending(self, deleted):
31 """ Figure out which TrustDomains are interesting to the OpenStack synchronizer. It's necessary to filter as
32 we're synchronizing a core model, and we only want to synchronize trust domains that will exist within
33 OpenStack.
34 """
35 objs = super(SyncTrustDomain, self).fetch_pending(deleted)
36 for obj in objs[:]:
37 # If the TrustDomain isn't part of the OpenStack service, then it's someone else's trust domain
38 if "OpenStackService" not in obj.owner.leaf_model.class_names:
39 objs.remove(obj)
40 return objs
41
42 def sync_record(self, trust_domain):
43 service = trust_domain.owner.leaf_model
44 conn = self.connect_openstack_admin(service)
45
46 os_domain = conn.identity.find_domain(trust_domain.name)
47 if (os_domain):
48 log.info("Trust Domain already exists in openstack", trust_domain=trust_domain)
49 else:
50 log.info("Creating Trust Domain", trust_domain=trust_domain)
51 os_domain = conn.identity.create_domain(name=trust_domain.name)
52
53 if os_domain.id != trust_domain.backend_handle:
54 trust_domain.backend_handle = os_domain.id
55 trust_domain.save(update_fields=["backend_handle"])
56
57 def delete_record(self, trust_domain):
58 service = trust_domain.owner.leaf_model
59 conn = self.connect_openstack_admin(service)
60
61 os_domain = conn.identity.find_domain(trust_domain.name)
62 if (not os_domain):
63 log.info("Trust Domain already does not exist in openstack", trust_domain=trust_domain)
64 else:
65 if os_domain.is_enabled:
66 log.info("Disabling Trust Domain", trust_domain=trust_domain, os_id=os_domain.id)
67 os_domain=conn.identity.update_domain(os_domain.id, enabled=False)
68 log.info("Deleting Trust Domain", trust_domain=trust_domain, os_id=os_domain.id)
69 conn.identity.delete_domain(os_domain.id)