Matteo Scandolo | f044103 | 2017-08-08 13:05: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 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 17 | import os |
| 18 | import base64 |
Scott Baker | 8b75e85 | 2016-08-16 15:04:59 -0700 | [diff] [blame] | 19 | from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 20 | from synchronizers.new_base.syncstep import * |
| 21 | from synchronizers.new_base.ansible_helper import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 22 | from xos.logger import observer_logger as logger |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 23 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 24 | |
| 25 | class SyncControllerSlices(OpenStackSyncStep): |
| 26 | provides=[Slice] |
| 27 | requested_interval=0 |
| 28 | observes=ControllerSlice |
| 29 | playbook='sync_controller_slices.yaml' |
| 30 | |
| 31 | def map_sync_inputs(self, controller_slice): |
| 32 | logger.info("sync'ing slice controller %s" % controller_slice) |
| 33 | |
| 34 | if not controller_slice.controller.admin_user: |
| 35 | logger.info("controller %r has no admin_user, skipping" % controller_slice.controller) |
| 36 | return |
| 37 | |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 38 | controller_users = ControllerUser.objects.filter(user_id=controller_slice.slice.creator.id, |
| 39 | controller_id=controller_slice.controller.id) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 40 | if not controller_users: |
| 41 | raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name)) |
| 42 | else: |
| 43 | controller_user = controller_users[0] |
Scott Baker | 04a37f5 | 2016-08-11 10:52:21 -0700 | [diff] [blame] | 44 | driver = self.driver.admin_driver(controller=controller_slice.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 45 | roles = [driver.get_admin_role().name] |
| 46 | |
| 47 | max_instances=int(controller_slice.slice.max_instances) |
| 48 | tenant_fields = {'endpoint':controller_slice.controller.auth_url, |
| 49 | 'endpoint_v3': controller_slice.controller.auth_url_v3, |
| 50 | 'domain': controller_slice.controller.domain, |
| 51 | 'admin_user': controller_slice.controller.admin_user, |
| 52 | 'admin_password': controller_slice.controller.admin_password, |
Zack Williams | 9cb1f3a | 2017-08-20 19:35:03 -0700 | [diff] [blame] | 53 | 'admin_project': 'admin', |
| 54 | 'project': controller_slice.slice.name, |
| 55 | 'project_description': controller_slice.slice.description, |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 56 | 'roles':roles, |
Zack Williams | 9cb1f3a | 2017-08-20 19:35:03 -0700 | [diff] [blame] | 57 | 'username':controller_user.user.email, |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 58 | 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name), |
| 59 | 'max_instances':max_instances} |
| 60 | |
| 61 | return tenant_fields |
| 62 | |
| 63 | def map_sync_outputs(self, controller_slice, res): |
Zack Williams | 2b1f7f8 | 2017-08-28 11:45:55 -0700 | [diff] [blame^] | 64 | tenant_id = res[0]['project']['id'] |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 65 | if (not controller_slice.tenant_id): |
| 66 | try: |
Scott Baker | 04a37f5 | 2016-08-11 10:52:21 -0700 | [diff] [blame] | 67 | driver = self.driver.admin_driver(controller=controller_slice.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 68 | driver.shell.nova.quotas.update(tenant_id=tenant_id, instances=int(controller_slice.slice.max_instances)) |
| 69 | except: |
| 70 | logger.log_exc('Could not update quota for %s'%controller_slice.slice.name) |
| 71 | raise Exception('Could not update quota for %s'%controller_slice.slice.name) |
| 72 | |
| 73 | controller_slice.tenant_id = tenant_id |
| 74 | controller_slice.backend_status = '1 - OK' |
| 75 | controller_slice.save() |
| 76 | |
| 77 | |
| 78 | def map_delete_inputs(self, controller_slice): |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 79 | controller_users = ControllerUser.objects.filter(user_id=controller_slice.slice.creator.id, |
| 80 | controller_id=controller_slice.controller.id) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 81 | if not controller_users: |
| 82 | raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name)) |
| 83 | else: |
| 84 | controller_user = controller_users[0] |
| 85 | |
| 86 | tenant_fields = {'endpoint':controller_slice.controller.auth_url, |
| 87 | 'admin_user': controller_slice.controller.admin_user, |
| 88 | 'admin_password': controller_slice.controller.admin_password, |
| 89 | 'admin_tenant': 'admin', |
| 90 | 'tenant': controller_slice.slice.name, |
| 91 | 'tenant_description': controller_slice.slice.description, |
| 92 | 'name':controller_user.user.email, |
| 93 | 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name), |
| 94 | 'delete': True} |
| 95 | return tenant_fields |