c9959970e4b655ea9d3bd31bcb8c7f36773da573
[snf-occi] / snfOCCI / snf-occi-server.py
1 #!/usr/bin/env python
2
3 from snfOCCI.registry import snfRegistry
4 from snfOCCI.compute import ComputeBackend
5
6 from kamaki.clients.compute import ComputeClient
7 from kamaki.clients.cyclades import CycladesClient
8 from kamaki.config  import Config
9
10 from occi.core_model import Mixin
11 from occi.backend import MixinBackend
12 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
13 from occi.wsgi import Application
14
15 from wsgiref.simple_server import make_server
16 from wsgiref.validate import validator
17
18
19
20
21 class MyAPP(Application):
22     '''
23     An OCCI WSGI application.
24     '''
25
26     def __call__(self, environ, response):
27
28
29         #TODO up-to-date compute instances                
30
31
32         # token will be represented in self.extras
33         return self._call_occi(environ, response, security = None, token = environ['HTTP_AUTH_TOKEN'])
34
35
36 if __name__ == '__main__':
37
38     APP = MyAPP(registry = snfRegistry())
39
40     COMPUTE_BACKEND = ComputeBackend()
41
42     APP.register_backend(COMPUTE, COMPUTE_BACKEND)
43     APP.register_backend(START, COMPUTE_BACKEND)
44     APP.register_backend(STOP, COMPUTE_BACKEND)
45     APP.register_backend(RESTART, COMPUTE_BACKEND)
46     APP.register_backend(SUSPEND, COMPUTE_BACKEND)
47     APP.register_backend(RESOURCE_TEMPLATE, MixinBackend())
48     APP.register_backend(OS_TEMPLATE, MixinBackend())
49     
50     snf = ComputeClient(Config())
51
52     images = snf.list_images()
53     for image in images:
54         IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])}
55         IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES)
56         APP.register_backend(IMAGE, MixinBackend())
57
58     flavors = snf.list_flavors()
59     for flavor in flavors:
60         FLAVOR_ATTRIBUTES = {'occi.core.id': flavor['id'],
61                              'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'],
62                              'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'],
63                              'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'],
64                              }
65         FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
66         APP.register_backend(FLAVOR, MixinBackend())
67
68  
69     VALIDATOR_APP = validator(APP)
70     HTTPD = make_server('', 8888, VALIDATOR_APP)
71     HTTPD.serve_forever()
72