another unclean commit to save progress

This commit is contained in:
tmetsch
2012-10-10 15:55:47 +02:00
parent 8711274c05
commit 0f777a3caf
5 changed files with 56 additions and 74 deletions

View File

@@ -27,7 +27,7 @@ import logging
from api.extensions import templates from api.extensions import templates
from nova_glue import vm from nova_glue import vm
from nova_glue import vol from nova_glue import storage
from occi.backend import KindBackend, ActionBackend from occi.backend import KindBackend, ActionBackend
from occi.extensions import infrastructure from occi.extensions import infrastructure
@@ -62,7 +62,7 @@ class ComputeBackend(KindBackend, ActionBackend):
# set some attributes # set some attributes
entity.attributes['occi.compute.hostname'] = instance['hostname'] entity.attributes['occi.compute.hostname'] = instance['hostname']
entity.attributes['occi.compute.architecture'] = \ entity.attributes['occi.compute.architecture'] = \
vol.get_image_architecture(uid, extras['nova_ctx']) storage.get_image_architecture(uid, extras['nova_ctx'])
entity.attributes['occi.compute.cores'] = str(instance['vcpus']) entity.attributes['occi.compute.cores'] = str(instance['vcpus'])
entity.attributes['occi.compute.speed'] = str(0.0) # N/A in instance entity.attributes['occi.compute.speed'] = str(0.0) # N/A in instance
value = str(float(instance['memory_mb']) / 1024) value = str(float(instance['memory_mb']) / 1024)
@@ -92,7 +92,7 @@ class ComputeBackend(KindBackend, ActionBackend):
# set up to date attributes # set up to date attributes
entity.attributes['occi.compute.hostname'] = instance['hostname'] entity.attributes['occi.compute.hostname'] = instance['hostname']
entity.attributes['occi.compute.architecture'] =\ entity.attributes['occi.compute.architecture'] =\
vol.get_image_architecture(uid, extras['nova_ctx']) storage.get_image_architecture(uid, extras['nova_ctx'])
entity.attributes['occi.compute.cores'] = str(instance['vcpus']) entity.attributes['occi.compute.cores'] = str(instance['vcpus'])
entity.attributes['occi.compute.speed'] = str(0.0) # N/A in instance entity.attributes['occi.compute.speed'] = str(0.0) # N/A in instance
value = str(float(instance['memory_mb']) / 1024) value = str(float(instance['memory_mb']) / 1024)
@@ -115,14 +115,12 @@ class ComputeBackend(KindBackend, ActionBackend):
old.attributes['occi.compute.state'] = 'inactive' old.attributes['occi.compute.state'] = 'inactive'
vm.confirm_resize_vm(uid, context) vm.confirm_resize_vm(uid, context)
# now update the mixin info # now update the mixin info
# TODO(tmetsch): remove old mixin!!!
old.mixins.append(mixin) old.mixins.append(mixin)
elif isinstance(mixin, templates.OsTemplate): elif isinstance(mixin, templates.OsTemplate):
image_href = mixin.os_id image_href = mixin.os_id
vm.rebuild_vm(uid, image_href, context) vm.rebuild_vm(uid, image_href, context)
old.attributes['occi.compute.state'] = 'inactive' old.attributes['occi.compute.state'] = 'inactive'
# now update the mixin info # now update the mixin info
# TODO(tmetsch): remove old mixin!!!
old.mixins.append(mixin) old.mixins.append(mixin)
else: else:
msg = 'Unrecognized mixin. %s' % str(mixin) msg = 'Unrecognized mixin. %s' % str(mixin)

View File

@@ -26,7 +26,7 @@ import uuid
from occi import backend from occi import backend
from occi import exceptions from occi import exceptions
from occi.extensions import infrastructure from occi.extensions import infrastructure
from nova_glue import vol, vm from nova_glue import storage, vm
class StorageBackend(backend.KindBackend, backend.ActionBackend): class StorageBackend(backend.KindBackend, backend.ActionBackend):
@@ -42,12 +42,12 @@ class StorageBackend(backend.KindBackend, backend.ActionBackend):
if 'occi.storage.size' not in entity.attributes: if 'occi.storage.size' not in entity.attributes:
raise AttributeError('size attribute not found!') raise AttributeError('size attribute not found!')
new_volume = vol.create_storage(entity.attributes['occi.storage' \ new_volume = storage.create_storage(entity.attributes['occi.storage' \
'.size'], context) '.size'], context)
vol_id = new_volume['id'] vol_id = new_volume['id']
# Work around problem that instance is lazy-loaded... # Work around problem that instance is lazy-loaded...
new_volume = vol.get_storage(vol_id, context) new_volume = storage.get_storage(vol_id, context)
if new_volume['status'] == 'error': if new_volume['status'] == 'error':
raise exceptions.HTTPError(500, 'There was an error creating the ' raise exceptions.HTTPError(500, 'There was an error creating the '
@@ -67,7 +67,7 @@ class StorageBackend(backend.KindBackend, backend.ActionBackend):
""" """
v_id = entity.attributes['occi.core.id'] v_id = entity.attributes['occi.core.id']
volume = vol.get_storage(v_id, extras['nova_ctx']) volume = storage.get_storage(v_id, extras['nova_ctx'])
entity.attributes['occi.storage.size'] = str(float(volume['size'])) entity.attributes['occi.storage.size'] = str(float(volume['size']))
@@ -115,7 +115,7 @@ class StorageBackend(backend.KindBackend, backend.ActionBackend):
context = extras['nova_ctx'] context = extras['nova_ctx']
volume_id = entity.attributes['occi.core.id'] volume_id = entity.attributes['occi.core.id']
vol.delete_storage_instance(volume_id, context) storage.delete_storage_instance(volume_id, context)
def action(self, entity, action, attributes, extras): def action(self, entity, action, attributes, extras):
""" """
@@ -160,7 +160,7 @@ class StorageBackend(backend.KindBackend, backend.ActionBackend):
# occi.core.title, occi.core.summary # occi.core.title, occi.core.summary
name = 'snapshot name' name = 'snapshot name'
description = 'snapshot description' description = 'snapshot description'
vol.snapshot_storage_instance(volume_id, name, description, storage.snapshot_storage_instance(volume_id, name, description,
extras['nova_ctx']) extras['nova_ctx'])
elif action == infrastructure.RESIZE: elif action == infrastructure.RESIZE:

View File

@@ -23,10 +23,12 @@ OCCI registry
#R0201:method could be func.E1002:old style obj #R0201:method could be func.E1002:old style obj
#pylint: disable=R0201,E1002 #pylint: disable=R0201,E1002
from occi import registry as occi_registry, core_model
from occi import registry as occi_registry
from occi import core_model
from occi.extensions import infrastructure from occi.extensions import infrastructure
from nova_glue import vm from nova_glue import vm, storage
class OCCIRegistry(occi_registry.NonePersistentRegistry): class OCCIRegistry(occi_registry.NonePersistentRegistry):
""" """
@@ -59,12 +61,14 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
iden = key[key.rfind('/') + 1:] iden = key[key.rfind('/') + 1:]
vm_desc = vm.get_vm(iden, context) vm_desc = vm.get_vm(iden, context)
if (key, context.user_id) in self.cache:
entity = self._update_occi_compute(key, extras)
else:
entity = self._construct_occi_compute(iden, [vm_desc], extras) entity = self._construct_occi_compute(iden, [vm_desc], extras)
else: else:
# shared resources - look in cache! # shared resources - look in cache!
entity = self.cache[(key, None)] entity = self.cache[(key, None)]
print 'Returning: ', entity
return entity return entity
def get_resource_keys(self, extras): def get_resource_keys(self, extras):
@@ -81,13 +85,19 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
Retrieve a set of resources. Retrieve a set of resources.
""" """
context = extras['nova_ctx'] context = extras['nova_ctx']
result = [] result = [self.pub_net, self.adm_net]
vms = vm.get_vms(context) vms = vm.get_vms(context)
res_ids = [item['uuid'] for item in vms] res_ids = [item['uuid'] for item in vms]
for item in res_ids: for item in res_ids:
# TODO: look if cached and if so take that! if (infrastructure.COMPUTE.location + item,
context.user_id) in self.cache:
entity = self._update_occi_compute(infrastructure.COMPUTE
.location + item, extras)
result.append(entity)
result.extend(entity.links)
else:
entity = self._construct_occi_compute(item, vms, extras) entity = self._construct_occi_compute(item, vms, extras)
result.append(entity) result.append(entity)
result.extend(entity.links) result.extend(entity.links)
@@ -96,26 +106,40 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
# Not part of parent # Not part of parent
def _update_occi_compute(self, identifier, extras):
context = extras['nova_ctx']
return self.cache[identifier, context.user_id]
def _construct_occi_compute(self, identifier, vms, extras): def _construct_occi_compute(self, identifier, vms, extras):
""" """
Construct a OCCI compute instance. Construct a OCCI compute instance.
Adds it to the cache too! Adds it to the cache too!
""" """
context = extras['nova_ctx']
# 1. get identifier # 1. get identifier
iden = infrastructure.COMPUTE.location + identifier iden = infrastructure.COMPUTE.location + identifier
entity = core_model.Resource(iden, infrastructure.COMPUTE, [])
# 2. os and res templates # 2. os and res templates
flavor_name = vm.get_vm(identifier, context)['instance_type'].name
res_tmp = self.get_category('/' + flavor_name + '/', extras)
entity.mixins.append(res_tmp)
os_id = vm.get_vm(identifier, context)['image_ref']
image_name = storage.get_image(os_id, context)['name']
os_tmp = self.get_category('/' + image_name + '/', extras)
entity.mixins.append(os_tmp)
# 3. network links & get links from cache! # 3. network links & get links from cache!
# 4. storage links & get links from cache! # 4. storage links & get links from cache!
# core.id and cache it! # core.id and cache it!
entity = core_model.Resource(iden, infrastructure.COMPUTE, [])
entity.attributes['occi.core.id'] = identifier entity.attributes['occi.core.id'] = identifier
entity.extras = extras entity.extras = extras
self.cache[(entity.identifier, repr(extras))] = entity self.cache[(entity.identifier, context.user_id)] = entity
return entity return entity
@@ -124,9 +148,9 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
Add a public and an admin network interface. Add a public and an admin network interface.
""" """
# TODO: read from openstack! # TODO: read from openstack!
pub_net = core_model.Resource('/network/public', self.pub_net = core_model.Resource('/network/public',
infrastructure.NETWORK, [infrastructure.IPNETWORK]) infrastructure.NETWORK, [infrastructure.IPNETWORK])
pub_net.attributes = {'occi.network.vlan': 'external', self.pub_net.attributes = {'occi.network.vlan': 'external',
'occi.network.label': 'default', 'occi.network.label': 'default',
'occi.network.state': 'active', 'occi.network.state': 'active',
'occi.networkinterface.address': '192' 'occi.networkinterface.address': '192'
@@ -135,14 +159,14 @@ class OCCIRegistry(occi_registry.NonePersistentRegistry):
'occi.networkinterface.gateway': '192.168' 'occi.networkinterface.gateway': '192.168'
'.0.1', '.0.1',
'occi.networkinterface.allocation': 'dynamic'} 'occi.networkinterface.allocation': 'dynamic'}
adm_net = core_model.Resource('/network/admin', self.adm_net = core_model.Resource('/network/admin',
infrastructure.NETWORK, [infrastructure.IPNETWORK]) infrastructure.NETWORK, [infrastructure.IPNETWORK])
adm_net.attributes = {'occi.network.vlan': 'admin', self.adm_net.attributes = {'occi.network.vlan': 'admin',
'occi.network.label': 'default', 'occi.network.label': 'default',
'occi.network.state': 'active', 'occi.network.state': 'active',
'occi.networkinterface.address': '10.0.0.0/24', 'occi.networkinterface.address': '10.0.0.0/24',
'occi.networkinterface.gateway': '10.0.0' 'occi.networkinterface.gateway': '10.0.0'
'.1', '.1',
'occi.networkinterface.allocation': 'dynamic'} 'occi.networkinterface.allocation': 'dynamic'}
self.cache[(adm_net.identifier, None)] = adm_net self.cache[(self.adm_net.identifier, None)] = self.adm_net
self.cache[(pub_net.identifier, None)] = pub_net self.cache[(self.pub_net.identifier, None)] = self.pub_net

View File

@@ -137,8 +137,6 @@ class OCCIApplication(occi_wsgi.Application, wsgi.Application):
self._refresh_resource_mixins(extras) self._refresh_resource_mixins(extras)
# register/refresh the openstack security groups as Mixins # register/refresh the openstack security groups as Mixins
self._refresh_security_mixins(extras) self._refresh_security_mixins(extras)
# register/refresh the openstack floating IP pools as Mixins
self._refresh_floating_ippools(extras)
return self._call_occi(environ, response, nova_ctx=extras['nova_ctx'], return self._call_occi(environ, response, nova_ctx=extras['nova_ctx'],
registry=self.registry) registry=self.registry)
@@ -233,46 +231,3 @@ class OCCIApplication(occi_wsgi.Application, wsgi.Application):
self.registry.get_backend(sec_mix, extras) self.registry.get_backend(sec_mix, extras)
except AttributeError: except AttributeError:
self.register_backend(sec_mix, MIXIN_BACKEND) self.register_backend(sec_mix, MIXIN_BACKEND)
def _refresh_floating_ippools(self, extras):
"""
Gets the list of floating ip pools and registers them as mixins.
"""
network_api = api.API()
pools = network_api.get_floating_ip_pools(extras['nova_ctx'])
for pool in pools:
pool_mixin = core_model.Mixin(
term=pool['name'],
scheme='http://schemas.openstack.org/instance/network/pool/floating#',
related=[],
attributes=None,
title="This is a floating IP pool",
location='/network/pool/floating/')
try:
self.registry.get_backend(pool_mixin, extras)
except AttributeError:
self.register_backend(pool_mixin, MIXIN_BACKEND)
def get_net_info(net_attrs):
"""
Gets basic information about the default network.
"""
ctx = context.get_admin_context()
network_api = api.API()
networks = network_api.get_all(ctx)
if len(networks) > 1:
msg = ('There is more that one network.'
'Using the first network: %s') % networks[0]['id']
LOG.warn(msg)
net_attrs['occi.network.address'] = networks[0]['cidr']
net_attrs['occi.network.label'] = 'public'
net_attrs['occi.network.state'] = 'up'
net_attrs['occi.network.gateway'] = str(networks[0]['gateway'])
net_attrs['occi.network.allocation'] = 'dhcp'
return net_attrs

View File

@@ -96,6 +96,11 @@ def snapshot_storage_instance(uid, name, description, context):
instance = get_storage(uid, context) instance = get_storage(uid, context)
VOLUME_API.create_snapshot(context, instance, name, description) VOLUME_API.create_snapshot(context, instance, name, description)
def get_image(uid, context):
"""
Return details on an image.
"""
return IMAGE_API.show(context, uid)
def get_image_architecture(uid, context): def get_image_architecture(uid, context):
""" """