Matteo Scandolo | 35113f7 | 2017-08-08 13:05:25 -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 | 037ad24 | 2018-06-28 09:38:16 -0700 | [diff] [blame^] | 17 | from synchronizers.new_base.modelaccessor import OpenStackService, Service |
| 18 | from synchronizers.new_base.policy import Policy |
| 19 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import LeastLoadedNodeScheduler |
Scott Baker | 5552b40 | 2017-06-07 15:48:53 -0700 | [diff] [blame] | 20 | |
Scott Baker | 037ad24 | 2018-06-28 09:38:16 -0700 | [diff] [blame^] | 21 | class ExampleServiceInstancePolicy(Policy): |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 22 | model_name = "ExampleServiceInstance" |
Scott Baker | 037ad24 | 2018-06-28 09:38:16 -0700 | [diff] [blame^] | 23 | |
| 24 | def handle_create(self, service_instance): |
| 25 | return self.handle_update(service_instance) |
| 26 | |
| 27 | def handle_update(self, service_instance): |
| 28 | if not service_instance.compute_instance: |
| 29 | # TODO: Break dependency |
| 30 | compute_service = OpenStackService.objects.first() |
| 31 | compute_service_instance_class = Service.objects.get(id=compute_service.id).get_service_instance_class() |
| 32 | |
| 33 | exampleservice = service_instance.owner.leaf_model |
| 34 | |
| 35 | # TODO: What if there is the wrong number of slices? |
| 36 | slice = exampleservice.slices.first() |
| 37 | |
| 38 | # TODO: What if there is no default image? |
| 39 | image = slice.default_image |
| 40 | |
| 41 | # TODO: What if there is no default flavor? |
| 42 | flavor = slice.default_flavor |
| 43 | |
| 44 | if slice.default_node: |
| 45 | node = slice.default_node |
| 46 | else: |
| 47 | scheduler = LeastLoadedNodeScheduler |
| 48 | # TODO(smbaker): Labeling and constraints |
| 49 | (node, parent) = scheduler(slice).pick() |
| 50 | |
| 51 | name="exampleserviceinstance-%s" % service_instance.id |
| 52 | compute_service_instance = compute_service_instance_class(slice=slice, |
| 53 | owner=compute_service, |
| 54 | image=image, |
| 55 | flavor=flavor, |
| 56 | name=name, |
| 57 | node=node) |
| 58 | compute_service_instance.save() |
| 59 | |
| 60 | service_instance.compute_instance = compute_service_instance |
| 61 | service_instance.save(update_fields=["compute_instance"]) |
| 62 | |
| 63 | def handle_delete(self, service_instance): |
| 64 | if service_instance.compute_instance: |
| 65 | service_instance.compute_instance.delete() |
| 66 | service_instance.compute_instance = None |
| 67 | # TODO: I'm not sure we can save things that are being deleted... |
| 68 | service_instance.save(update_fields=["compute_instance"]) |
| 69 | |