Added HTTPError handling
authorJohn Giannelos <johngiannelos@gmail.com>
Mon, 30 Apr 2012 13:00:37 +0000 (16:00 +0300)
committerJohn Giannelos <johngiannelos@gmail.com>
Mon, 30 Apr 2012 13:00:37 +0000 (16:00 +0300)
snfOCCI/compute.py

index 279498f..883bac6 100644 (file)
@@ -6,6 +6,7 @@ from kamaki.config  import Config
 
 from occi.backend import ActionBackend, KindBackend
 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART
+from occi.exceptions import HTTPError
 
 
 #Compute Backend for snf-occi-server
@@ -16,7 +17,7 @@ class MyBackend(KindBackend, ActionBackend):
     attributes. Support for links and mixins would need to added.
     '''
 
-    # Update and Replace compute instances not supported by Cyclades
+    # Updating and Replacing compute instances not supported by Cyclades
 
     def update(self, old, new, extras):
         raise AttributeError("This action is currently no applicable.")
@@ -31,6 +32,8 @@ class ComputeBackend(MyBackend):
     '''
 
     def create(self, entity, extras):
+
+        #Creating new compute instance
     
         for mixin in entity.mixins:
             if mixin.related[0].term == 'os_tpl':
@@ -43,9 +46,6 @@ class ComputeBackend(MyBackend):
         entity.attributes['occi.compute.state'] = 'inactive'
         entity.actions = [START]
 
-        #Registry identifier is the uuid key occi.handler assigns
-        #attribute 'occi.core.id' will be the snf-server id
-
         conf = Config()
         conf.set('token',extras['token'])
         snf = ComputeClient(conf)
@@ -60,7 +60,7 @@ class ComputeBackend(MyBackend):
 
     def retrieve(self, entity, extras):
         
-        # triggering cyclades to retrieve up to date information
+        #Triggering cyclades to retrieve up to date information
 
         conf = Config()
         conf.set('token',extras['token'])
@@ -78,18 +78,21 @@ class ComputeBackend(MyBackend):
                        }
         
         entity.attributes['occi.compute.state'] = status_dict[vm_state]
+                
+        if vm_state == 'ERROR':
+            raise HTTPError(500, 'ERROR building the compute instance')
 
-        if entity.attributes['occi.compute.state'] == 'inactive':
-            entity.actions = [START]
-        if entity.attributes['occi.compute.state'] == 'active': 
-            entity.actions = [STOP, SUSPEND, RESTART]
-        if entity.attributes['occi.compute.state'] == 'suspended':
-            entity.actions = [START]
+        else:
+            if entity.attributes['occi.compute.state'] == 'inactive':
+                entity.actions = [START]
+            if entity.attributes['occi.compute.state'] == 'active': 
+                entity.actions = [STOP, SUSPEND, RESTART]
+            if entity.attributes['occi.compute.state'] == 'suspended':
+                entity.actions = [START]
 
 
     def delete(self, entity, extras):
 
-        # delete vm with vm_id = entity.attributes['occi.core.id']
         conf = Config()
         conf.set('token',extras['token'])
         snf = ComputeClient(conf)
@@ -103,25 +106,31 @@ class ComputeBackend(MyBackend):
         conf = Config()
         conf.set('token',extras['token'])
         client = CycladesClient(conf)
+        snf = ComputeClient(conf)
 
         vm_id = int(entity.attributes['occi.core.id'])
+        vm_info = snf.get_server_details(vm_id)
+        vm_state = vm_info['status']
 
-        if action not in entity.actions:
-            raise AttributeError("This action is currently no applicable.")
-
-        elif action == START:
-            print "Starting VM"
-            client.start_server(vm_id)
 
+        if vm_state == 'ERROR':
+            raise HTTPError(500, 'ERROR building the compute instance')
 
-        elif action == STOP:
-            print "Stopping VM"
-            client.shutdown_server(vm_id)
+        else:
+            if action not in entity.actions:
+                raise AttributeError("This action is currently no applicable.")
             
-        elif action == RESTART:
-            print "Restarting VM"
-            snf.reboot_server(vm_id)
-
+            elif action == START:
+                print "Starting VM"
+                client.start_server(vm_id)
+                
+            elif action == STOP:
+                print "Stopping VM"
+                client.shutdown_server(vm_id)
+    
+            elif action == RESTART:
+                print "Restarting VM"
+                snf.reboot_server(vm_id)
 
-        elif action == SUSPEND:
-            raise AttributeError("This actions is currently no applicable")
+            elif action == SUSPEND:
+                raise AttributeError("This actions is currently no applicable")