CORD-3195 update ExampleService to use new OpenStack modeling
Change-Id: I4982623294ed3ae19bcc39ffc25ae288b55ad94e
diff --git a/xos/synchronizer/model_policies/model_policy_exampleserviceinstance.py b/xos/synchronizer/model_policies/model_policy_exampleserviceinstance.py
index 8244204..5e20122 100644
--- a/xos/synchronizer/model_policies/model_policy_exampleserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_exampleserviceinstance.py
@@ -14,8 +14,56 @@
# limitations under the License.
-from synchronizers.new_base.modelaccessor import *
-from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy
+from synchronizers.new_base.modelaccessor import OpenStackService, Service
+from synchronizers.new_base.policy import Policy
+from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import LeastLoadedNodeScheduler
-class ExampleServiceInstancePolicy(TenantWithContainerPolicy):
+class ExampleServiceInstancePolicy(Policy):
model_name = "ExampleServiceInstance"
+
+ def handle_create(self, service_instance):
+ return self.handle_update(service_instance)
+
+ def handle_update(self, service_instance):
+ if not service_instance.compute_instance:
+ # TODO: Break dependency
+ compute_service = OpenStackService.objects.first()
+ compute_service_instance_class = Service.objects.get(id=compute_service.id).get_service_instance_class()
+
+ exampleservice = service_instance.owner.leaf_model
+
+ # TODO: What if there is the wrong number of slices?
+ slice = exampleservice.slices.first()
+
+ # TODO: What if there is no default image?
+ image = slice.default_image
+
+ # TODO: What if there is no default flavor?
+ flavor = slice.default_flavor
+
+ if slice.default_node:
+ node = slice.default_node
+ else:
+ scheduler = LeastLoadedNodeScheduler
+ # TODO(smbaker): Labeling and constraints
+ (node, parent) = scheduler(slice).pick()
+
+ name="exampleserviceinstance-%s" % service_instance.id
+ compute_service_instance = compute_service_instance_class(slice=slice,
+ owner=compute_service,
+ image=image,
+ flavor=flavor,
+ name=name,
+ node=node)
+ compute_service_instance.save()
+
+ service_instance.compute_instance = compute_service_instance
+ service_instance.save(update_fields=["compute_instance"])
+
+ def handle_delete(self, service_instance):
+ if service_instance.compute_instance:
+ service_instance.compute_instance.delete()
+ service_instance.compute_instance = None
+ # TODO: I'm not sure we can save things that are being deleted...
+ service_instance.save(update_fields=["compute_instance"])
+