Revision 4adfebfe snf-occi-server.py

b/snf-occi-server.py
1 1
#!/usr/bin/env python
2 2

  
3 3
from kamaki.clients.compute import ComputeClient
4
from kamaki.clients.cyclades import CycladesClient
4 5
from kamaki.config  import Config
5 6

  
6
from occi.core_model import Mixin, Resource, Link, Entity
7
from occi.core_model import Mixin
7 8
from occi.backend import ActionBackend, KindBackend, MixinBackend
8 9
from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
9 10

  
......
90 91
        if entity.attributes['occi.compute.state'] == 'suspended':
91 92
            entity.actions = [START]
92 93

  
93
        
94

  
95 94

  
96 95
    def delete(self, entity, extras):
97 96
        # delete vm with vm_id = entity.attributes['occi.core.id']
......
101 100

  
102 101

  
103 102
    def action(self, entity, action, extras):
103
        
104
        client = CycladesClient(Config())
105
        vm_id = int(entity.attributes['occi.core.id'])
106

  
104 107
        if action not in entity.actions:
105 108
            raise AttributeError("This action is currently no applicable.")
109

  
106 110
        elif action == START:
107
            entity.attributes['occi.compute.state'] = 'active'
108
            entity.actions = [STOP, SUSPEND, RESTART]
109
            # read attributes from action and do something with it :-)
110
            print('Starting virtual machine with id' + entity.identifier)
111

  
112
            print "Starting VM"
113
            client.start_server(vm_id)
114

  
115

  
111 116
        elif action == STOP:
112
            entity.attributes['occi.compute.state'] = 'inactive'
113
            entity.actions = [START]
114
            # read attributes from action and do something with it :-)
115
            print('Stopping virtual machine with id' + entity.identifier)
117

  
118
            print "Stopping VM"
119
            client.shutdown_server(vm_id)
120
            
116 121
        elif action == RESTART:
117
            entity.actions = [STOP, SUSPEND, RESTART]
118
            entity.attributes['occi.compute.state'] = 'active'
119
            # read attributes from action and do something with it :-)
120
            print('Restarting virtual machine with id' + entity.identifier)
122
            snf.reboot_server(vm_id)
123

  
124

  
121 125
        elif action == SUSPEND:
126

  
127
            #TODO VM suspending
128

  
122 129
            entity.attributes['occi.compute.state'] = 'suspended'
123 130
            entity.actions = [START]
124
            # read attributes from action and do something with it :-)
125
            print('Suspending virtual machine with id' + entity.identifier)
126 131

  
127 132

  
128 133
class MyAPP(Application):
......
131 136
    '''
132 137

  
133 138
    def __call__(self, environ, response):
134
        sec_obj = {'username': 'password'}
135 139

  
136
        
137
        #Refresh registry entries with current Cyclades state
138
        snf = ComputeClient(Config())
139

  
140
        '''
141
        images = snf.list_images()
142
        for image in images:
143
            IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])}
144
            IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES)
145
            self.register_backend(IMAGE, MixinBackend())
146

  
147
        flavors = snf.list_flavors()
148
        for flavor in flavors:
149
            FLAVOR_ATTRIBUTES = {'occi.core.id': flavor['id'],
150
                                 'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'],
151
                                 'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'],
152
                                 'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'],
153
                                 }
154
            FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
155
            self.register_backend(FLAVOR, MixinBackend())
156
            '''       
157

  
158
        #TODO show only current VM instances
159
        
160
        return self._call_occi(environ, response, security=sec_obj, foo=None)
140
        return self._call_occi(environ, response)
161 141

  
162 142
if __name__ == '__main__':
163 143

  

Also available in: Unified diff