Commit 55ab2427 authored by John Giannelos's avatar John Giannelos
Browse files

Optimized kamaki calls/Fixed exception handling

parent b7fca2d2
......@@ -12,10 +12,6 @@ from occi.exceptions import HTTPError
#Compute Backend for snf-occi-server
class MyBackend(KindBackend, ActionBackend):
'''
An very simple abstract backend which handles update and replace for
attributes. Support for links and mixins would need to added.
'''
# Updating and Replacing compute instances not supported by Cyclades
......@@ -34,37 +30,39 @@ class ComputeBackend(MyBackend):
def create(self, entity, extras):
#Creating new compute instance
for mixin in entity.mixins:
if mixin.related[0].term == 'os_tpl':
image = mixin
image_id = mixin.attributes['occi.core.id']
if mixin.related[0].term == 'resource_tpl':
flavor = mixin
flavor_id = mixin.attributes['occi.core.id']
entity.attributes['occi.compute.state'] = 'inactive'
entity.actions = [START]
conf = Config()
conf.set('token',extras['token'])
snf = ComputeClient(conf)
vm_name = entity.attributes['occi.core.title']
info = snf.create_server(vm_name, flavor_id, image_id)
entity.attributes['occi.core.id'] = str(info['id'])
entity.attributes['occi.compute.architecture'] = SERVER_CONFIG['compute_arch']
entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']}
try:
snf = extras['snf']
for mixin in entity.mixins:
if mixin.related[0].term == 'os_tpl':
image = mixin
image_id = mixin.attributes['occi.core.id']
if mixin.related[0].term == 'resource_tpl':
flavor = mixin
flavor_id = mixin.attributes['occi.core.id']
vm_name = entity.attributes['occi.core.title']
info = snf.create_server(vm_name, flavor_id, image_id)
entity.actions = [START]
entity.attributes['occi.compute.state'] = 'inactive'
entity.attributes['occi.core.id'] = str(info['id'])
entity.attributes['occi.compute.architecture'] = SERVER_CONFIG['compute_arch']
entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']}
except (UnboundLocalError, KeyError) as e:
raise HTTPError(406, 'Missing details about compute instance')
def retrieve(self, entity, extras):
#Triggering cyclades to retrieve up to date information
conf = Config()
conf.set('token',extras['token'])
snf = ComputeClient(conf)
snf = extras['snf']
vm_id = int(entity.attributes['occi.core.id'])
vm_info = snf.get_server_details(vm_id)
......@@ -87,18 +85,13 @@ class ComputeBackend(MyBackend):
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):
#Deleting compute instance
conf = Config()
conf.set('token',extras['token'])
snf = ComputeClient(conf)
snf = extras['snf']
vm_id = int(entity.attributes['occi.core.id'])
snf.delete_server(vm_id)
......@@ -107,10 +100,8 @@ class ComputeBackend(MyBackend):
#Triggering action to compute instances
conf = Config()
conf.set('token',extras['token'])
client = CycladesClient(conf)
snf = ComputeClient(conf)
client = extras['client']
snf = extras['snf']
vm_id = int(entity.attributes['occi.core.id'])
vm_info = snf.get_server_details(vm_id)
......@@ -137,4 +128,4 @@ class ComputeBackend(MyBackend):
snf.reboot_server(vm_id)
elif action == SUSPEND:
raise AttributeError("This actions is currently no applicable")
raise HTTPError(501, "This actions is currently no applicable")
......@@ -24,18 +24,16 @@ class MyAPP(Application):
An OCCI WSGI application.
'''
def refresh_images(self):
def refresh_images(self, snf, client):
snf = ComputeClient(Config())
images = snf.list_images()
for image in images:
IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])}
IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES)
self.register_backend(IMAGE, MixinBackend())
def refresh_flavors(self):
def refresh_flavors(self, snf, client):
snf = ComputeClient(Config())
flavors = snf.list_flavors()
for flavor in flavors:
details = snf.get_flavor_details(flavor['id'])
......@@ -50,13 +48,17 @@ class MyAPP(Application):
def __call__(self, environ, response):
#Up-to-date flavors and images
conf = Config()
conf.set('token',environ['HTTP_AUTH_TOKEN'])
compClient = ComputeClient(conf)
cyclClient = CycladesClient(conf)
self.refresh_images()
self.refresh_flavors()
#Up-to-date flavors and images
self.refresh_images(compClient, cyclClient)
self.refresh_flavors(compClient, cyclClient)
# token will be represented in self.extras
return self._call_occi(environ, response, security = None, token = environ['HTTP_AUTH_TOKEN'])
return self._call_occi(environ, response, security = None, token = environ['HTTP_AUTH_TOKEN'], snf = compClient, client = cyclClient)
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment