1 from snfOCCI.config import SERVER_CONFIG
3 from occi.backend import ActionBackend, KindBackend
4 from occi.extensions.infrastructure import START, STOP, SUSPEND, RESTART
5 from occi.exceptions import HTTPError
8 #Compute Backend for snf-occi-server
10 class MyBackend(KindBackend, ActionBackend):
12 # Updating and Replacing compute instances not supported by Cyclades
14 def update(self, old, new, extras):
15 raise HTTPError(501, "Update is currently no applicable")
17 def replace(self, old, new, extras):
18 raise HTTPError(501, "Replace is currently no applicable")
21 class ComputeBackend(MyBackend):
23 Backend for Cyclades/Openstack compute instances
26 def create(self, entity, extras):
28 #Creating new compute instance
34 for mixin in entity.mixins:
35 if mixin.related[0].term == 'os_tpl':
36 image_id = mixin.attributes['occi.core.id']
37 if mixin.related[0].term == 'resource_tpl':
39 flavor_id = mixin.attributes['occi.core.id']
41 vm_name = entity.attributes['occi.core.title']
42 info = snf.create_server(vm_name, flavor_id, image_id)
44 entity.actions = [START]
45 entity.attributes['occi.compute.state'] = 'inactive'
46 entity.attributes['occi.core.id'] = str(info['id'])
47 entity.attributes['occi.compute.architecture'] = SERVER_CONFIG['compute_arch']
48 entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
49 entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
50 entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']}
52 except (UnboundLocalError, KeyError) as e:
53 raise HTTPError(406, 'Missing details about compute instance')
56 def retrieve(self, entity, extras):
58 #Triggering cyclades to retrieve up to date information
62 vm_id = int(entity.attributes['occi.core.id'])
63 vm_info = snf.get_server_details(vm_id)
64 vm_state = vm_info['status']
66 status_dict = {'ACTIVE' : 'active',
67 'STOPPED' : 'inactive',
70 'DELETED' : 'inactive',
73 entity.attributes['occi.compute.state'] = status_dict[vm_state]
75 if vm_state == 'ERROR':
76 raise HTTPError(500, 'ERROR building the compute instance')
79 if entity.attributes['occi.compute.state'] == 'inactive':
80 entity.actions = [START]
81 if entity.attributes['occi.compute.state'] == 'active':
82 entity.actions = [STOP, SUSPEND, RESTART]
85 def delete(self, entity, extras):
87 #Deleting compute instance
90 vm_id = int(entity.attributes['occi.core.id'])
91 snf.delete_server(vm_id)
94 def action(self, entity, action, extras):
96 #Triggering action to compute instances
98 client = extras['client']
101 vm_id = int(entity.attributes['occi.core.id'])
102 vm_info = snf.get_server_details(vm_id)
103 vm_state = vm_info['status']
106 if vm_state == 'ERROR':
107 raise HTTPError(500, 'ERROR building the compute instance')
110 if action not in entity.actions:
111 raise AttributeError("This action is currently no applicable.")
113 elif action == START:
115 client.start_server(vm_id)
119 client.shutdown_server(vm_id)
121 elif action == RESTART:
122 print "Restarting VM"
123 snf.reboot_server(vm_id)
125 elif action == SUSPEND:
126 raise HTTPError(501, "This actions is currently no applicable")