Fixed compute instances attributes: actions, states
[snf-occi] / snf-occi-server.py
index 04b388e..cdf0776 100755 (executable)
@@ -3,7 +3,7 @@
 from kamaki.clients.compute import ComputeClient
 from kamaki.config  import Config
 
 from kamaki.clients.compute import ComputeClient
 from kamaki.config  import Config
 
-from occi.core_model import Mixin
+from occi.core_model import Mixin, Resource, Link, Entity
 from occi.backend import ActionBackend, KindBackend, MixinBackend
 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
 
 from occi.backend import ActionBackend, KindBackend, MixinBackend
 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
 
@@ -72,18 +72,22 @@ class ComputeBackend(MyBackend):
             raise AttributeError("This action is currently no applicable.")
         elif action == START:
             entity.attributes['occi.compute.state'] = 'active'
             raise AttributeError("This action is currently no applicable.")
         elif action == START:
             entity.attributes['occi.compute.state'] = 'active'
+            entity.actions = [STOP, SUSPEND, RESTART]
             # read attributes from action and do something with it :-)
             print('Starting virtual machine with id' + entity.identifier)
         elif action == STOP:
             entity.attributes['occi.compute.state'] = 'inactive'
             # read attributes from action and do something with it :-)
             print('Starting virtual machine with id' + entity.identifier)
         elif action == STOP:
             entity.attributes['occi.compute.state'] = 'inactive'
+            entity.actions = [START]
             # read attributes from action and do something with it :-)
             print('Stopping virtual machine with id' + entity.identifier)
         elif action == RESTART:
             # read attributes from action and do something with it :-)
             print('Stopping virtual machine with id' + entity.identifier)
         elif action == RESTART:
+            entity.actions = [STOP, SUSPEND, RESTART]
             entity.attributes['occi.compute.state'] = 'active'
             # read attributes from action and do something with it :-)
             print('Restarting virtual machine with id' + entity.identifier)
         elif action == SUSPEND:
             entity.attributes['occi.compute.state'] = 'suspended'
             entity.attributes['occi.compute.state'] = 'active'
             # read attributes from action and do something with it :-)
             print('Restarting virtual machine with id' + entity.identifier)
         elif action == SUSPEND:
             entity.attributes['occi.compute.state'] = 'suspended'
+            entity.actions = [START]
             # read attributes from action and do something with it :-)
             print('Suspending virtual machine with id' + entity.identifier)
 
             # read attributes from action and do something with it :-)
             print('Suspending virtual machine with id' + entity.identifier)
 
@@ -95,6 +99,25 @@ class MyAPP(Application):
 
     def __call__(self, environ, response):
         sec_obj = {'username': 'password'}
 
     def __call__(self, environ, response):
         sec_obj = {'username': 'password'}
+
+        snf = ComputeClient(Config())
+
+        images = snf.list_images()
+        for image in images:
+            IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE])
+            self.register_backend(IMAGE, MixinBackend())
+
+        flavors = snf.list_flavors()
+        for flavor in flavors:
+            FLAVOR_ATTRIBUTES = {'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'],
+                                 'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'],
+                                 'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'],
+                                 }
+            FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
+            self.register_backend(FLAVOR, MixinBackend())
+        
+        
+        
         return self._call_occi(environ, response, security=sec_obj, foo=None)
 
 if __name__ == '__main__':
         return self._call_occi(environ, response, security=sec_obj, foo=None)
 
 if __name__ == '__main__':
@@ -111,24 +134,8 @@ if __name__ == '__main__':
     APP.register_backend(RESOURCE_TEMPLATE, MixinBackend())
     APP.register_backend(OS_TEMPLATE, MixinBackend())
     
     APP.register_backend(RESOURCE_TEMPLATE, MixinBackend())
     APP.register_backend(OS_TEMPLATE, MixinBackend())
     
-
-    snf = ComputeClient(Config())
-    images = snf.list_images()
-    for image in images:
-        IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE])
-        APP.register_backend(IMAGE, MixinBackend())
-
-    flavors = snf.list_flavors()
-    for flavor in flavors:
-        FLAVOR_ATTRIBUTES = {'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'],
-                             'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'],
-                             'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'],
-                             }
-        FLAVOR =Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
-        APP.register_backend(FLAVOR, MixinBackend())
-        
     VALIDATOR_APP = validator(APP)
     VALIDATOR_APP = validator(APP)
-
     HTTPD = make_server('', 8888, VALIDATOR_APP)
     HTTPD.serve_forever()
 
     HTTPD = make_server('', 8888, VALIDATOR_APP)
     HTTPD.serve_forever()