Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -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 | |
| 17 | from synchronizers.new_base.modelaccessor import TrustDomain, Principal |
| 18 | from newopenstacksyncstep import NewOpenStackSyncStep |
| 19 | |
| 20 | from xosconfig import Config |
| 21 | from multistructlog import create_logger |
| 22 | |
| 23 | log = create_logger(Config().get('logging')) |
| 24 | |
| 25 | class SyncPrincipal(NewOpenStackSyncStep): |
| 26 | provides=[Principal] |
| 27 | requested_interval=0 |
| 28 | observes=Principal |
| 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(SyncPrincipal, self).fetch_pending(deleted) |
| 36 | for obj in objs[:]: |
| 37 | # If the Principal 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, principal): |
| 48 | service = principal.trust_domain.owner.leaf_model |
| 49 | conn = self.connect_openstack_admin(service) |
| 50 | |
| 51 | os_domain = conn.identity.find_domain(principal.trust_domain.name) |
| 52 | |
| 53 | os_user = conn.identity.find_user(principal.name, domain_id=os_domain.id) |
| 54 | if (os_user): |
| 55 | log.info("Principal already exists in openstack", principal=principal) |
| 56 | else: |
| 57 | log.info("Creating Principal", principal=principal) |
| 58 | os_user = conn.identity.create_user(name=principal.name, domain_id=os_domain.id) |
| 59 | |
| 60 | if os_user.id != principal.backend_handle: |
| 61 | principal.backend_handle = os_user.id |
| 62 | principal.save(update_fields=["backend_handle"]) |
| 63 | |
| 64 | def delete_record(self, principal): |
| 65 | service = principal.trust_domain.owner.leaf_model |
| 66 | conn = self.connect_openstack_admin(service) |
| 67 | |
| 68 | os_domain = conn.identity.find_domain(principal.trust_domain.name) |
| 69 | |
| 70 | os_user = conn.identity.find_user(principal.name, domain_id=os_domain.id) |
| 71 | if (not os_user): |
| 72 | log.info("Principal already does not exist in openstack", principal=principal) |
| 73 | else: |
| 74 | log.info("Deleting Principal", principal=principal, os_id=os_domain.id) |
| 75 | conn.identity.delete_user(os_user.id) |