3 from snfOCCI.registry import snfRegistry
4 from snfOCCI.compute import ComputeBackend
5 from snfOCCI.config import SERVER_CONFIG
7 from kamaki.clients.compute import ComputeClient
8 from kamaki.clients.cyclades import CycladesClient
9 from kamaki.config import Config
11 from occi.core_model import Mixin
12 from occi.backend import MixinBackend
13 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
14 from occi.wsgi import Application
16 from wsgiref.simple_server import make_server
17 from wsgiref.validate import validator
22 class MyAPP(Application):
24 An OCCI WSGI application.
27 def refresh_images(self, snf, client):
29 images = snf.list_images()
31 IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])}
32 IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES)
33 self.register_backend(IMAGE, MixinBackend())
35 def refresh_flavors(self, snf, client):
37 flavors = snf.list_flavors()
38 for flavor in flavors:
39 details = snf.get_flavor_details(flavor['id'])
40 FLAVOR_ATTRIBUTES = {'occi.core.id': flavor['id'],
41 'occi.compute.cores': details['cpu'],
42 'occi.compute.memory': details['ram'],
43 'occi.storage.size': details['disk'],
45 FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
46 self.register_backend(FLAVOR, MixinBackend())
49 def __call__(self, environ, response):
52 conf.set('token',environ['HTTP_AUTH_TOKEN'])
53 compClient = ComputeClient(conf)
54 cyclClient = CycladesClient(conf)
56 #Up-to-date flavors and images
57 self.refresh_images(compClient, cyclClient)
58 self.refresh_flavors(compClient, cyclClient)
60 # token will be represented in self.extras
61 return self._call_occi(environ, response, security = None, token = environ['HTTP_AUTH_TOKEN'], snf = compClient, client = cyclClient)
67 APP = MyAPP(registry = snfRegistry())
68 COMPUTE_BACKEND = ComputeBackend()
70 APP.register_backend(COMPUTE, COMPUTE_BACKEND)
71 APP.register_backend(START, COMPUTE_BACKEND)
72 APP.register_backend(STOP, COMPUTE_BACKEND)
73 APP.register_backend(RESTART, COMPUTE_BACKEND)
74 APP.register_backend(SUSPEND, COMPUTE_BACKEND)
75 APP.register_backend(RESOURCE_TEMPLATE, MixinBackend())
76 APP.register_backend(OS_TEMPLATE, MixinBackend())
78 VALIDATOR_APP = validator(APP)
79 HTTPD = make_server('', SERVER_CONFIG['port'], VALIDATOR_APP)