def get_resource(self, key, extras): """ Retrieve a single resource. """ context = extras['nova_ctx'] iden = key[key.rfind('/') + 1:] vms = vm.get_vms(context) vm_res_ids = [item['uuid'] for item in vms] stors = storage.get_storage_volumes(context) stor_res_ids = [item['id'] for item in stors] if (key, context.user_id) in self.cache: # I have seen it - need to update or delete if gone in OS! # I have already seen it cached_item = self.cache[(key, context.user_id)] if not iden in vm_res_ids and cached_item.kind == \ infrastructure.COMPUTE: # it was delete in OS -> remove links, cache + KeyError! # can delete it because it was my item! for link in cached_item.links: self.cache.pop((link.identifier, repr(extras))) self.cache.pop((key, repr(extras))) raise KeyError if not iden in stor_res_ids and cached_item.kind == \ infrastructure.STORAGE: # it was delete in OS -> remove from cache + KeyError! # can delete it because it was my item! self.cache.pop((key, repr(extras))) raise KeyError elif iden in vm_res_ids: # it also exists in OS -> update it (take links, mixins # from cached one) result = self._update_occi_compute(cached_item, extras) elif iden in stor_res_ids: # it also exists in OS -> update it! result = self._update_occi_storage(cached_item, extras) else: # return cached item (links) return cached_item elif (key, None) in self.cache: # return shared entities from cache! return self.cache[(key, None)] else: # construct it. if iden in vm_res_ids: # create new & add to cache! result = self._construct_occi_compute(iden, extras)[0] elif iden in stor_res_ids: result = self._construct_occi_storage(iden, extras)[0] else: # doesn't exist! raise KeyError if result.identifier != key: raise AttributeError('Key/identifier mismatch! Requested: ' + key + ' Got: ' + result.identifier) return result
def get_resources(self, extras): """ Retrieve a set of resources. """ # TODO: add security rules! context = extras['nova_ctx'] result = [] # VMs vms = vm.get_vms(context) for instance in vms: occi_compute = self._construct_occi_compute(instance, extras) result.append(occi_compute) result.extend(occi_compute.links) # Volumes stors = storage.get_storage_volumes(context) for stor in stors: occi_storage = self._construct_occi_storage(stor, extras) result.append(occi_storage) result.extend(occi_storage.links) # Networks, XXX not sure if I need to return this result.extend(self.nets.values()) return result
def get_resources(self, extras): """ Retrieve a set of resources. """ # TODO: add security rules! context = extras['nova_ctx'] result = [] vms = vm.get_vms(context) vm_res_ids = [item['uuid'] for item in vms] stors = storage.get_storage_volumes(context) stor_res_ids = [item['id'] for item in stors] for item in self.cache.values(): if item.extras is not None and item.extras['user_id'] != \ context.user_id: # filter out items not belonging to this user! continue item_id = item.identifier[item.identifier.rfind('/') + 1:] if item.extras is None: # add to result set result.append(item) elif item_id in vm_res_ids and item.kind == \ infrastructure.COMPUTE: # check & update (take links, mixins from cache) # add compute and it's links to result self._update_occi_compute(item, extras) result.append(item) result.extend(item.links) elif item_id in stor_res_ids and item.kind == \ infrastructure.STORAGE: # check & update (take links, mixins from cache) # add compute and it's links to result self._update_occi_storage(item, extras) result.append(item) elif item_id not in vm_res_ids and item.kind == \ infrastructure.COMPUTE: # remove item and it's links from cache! for link in item.links: self.cache.pop((link.identifier, item.extras['user_id'])) self.cache.pop((item.identifier, item.extras['user_id'])) elif item_id not in stor_res_ids and item.kind == \ infrastructure.STORAGE: # remove item self.cache.pop((item.identifier, item.extras['user_id'])) for item in vms: if (infrastructure.COMPUTE.location + item['uuid'], context.user_id) in self.cache: continue else: # construct (with links and mixins and add to cache! # add compute and it's linke to result ent_list = self._construct_occi_compute(item['uuid'], extras) result.extend(ent_list) for item in stors: if (infrastructure.STORAGE.location + item['id'], context.user_id) in self.cache: continue else: # construct (with links and mixins and add to cache! # add compute and it's linke to result ent_list = self._construct_occi_storage(item['id'], extras) result.extend(ent_list) return result
def _construct_occi_compute(self, identifier, extras): """ Construct a OCCI compute instance. First item in result list is entity self! Adds it to the cache too! """ result = [] context = extras['nova_ctx'] instance = vm.get_vm(identifier, context) # 1. get identifier iden = infrastructure.COMPUTE.location + identifier entity = core_model.Resource(iden, infrastructure.COMPUTE, [os_addon.OS_VM]) result.append(entity) # 2. os and res templates flavor_id = int(instance['instance_type_id']) res_tmp = self.get_category('/' + str(flavor_id) + '/', extras) if res_tmp: entity.mixins.append(res_tmp) image_id = instance['image_ref'] image_tmp = self.get_category('/' + image_id + '/', extras) if image_tmp: entity.mixins.append(image_tmp) # 3. network links & get links from cache! net_links = net.get_network_details(identifier, context) for item in net_links['public']: link = self._construct_network_link(item, entity, self.pub_net, extras) result.append(link) for item in net_links['admin']: link = self._construct_network_link(item, entity, self.adm_net, extras) result.append(link) # 4. storage links from the storage (and cache them) stors = storage.get_storage_volumes(context) for item in stors: if item['status'] == 'in-use' and item['instance_uuid'] \ == identifier: stor = core_model.Resource( infrastructure.STORAGE.location + item['id'], infrastructure.STORAGE, []) stor.attributes['occi.core.id'] = item['id'] link = core_model.Link(infrastructure.STORAGELINK.location + str(uuid.uuid4()), infrastructure.STORAGELINK, [], entity, stor) link.attributes = { 'occi.storagelink.deviceid': item['mountpoint'], 'occi.storagelink.mountpoint': item['mountpoint'], 'occi.storagelink.state': 'active'} link.extras = self.get_extras(extras) entity.links.append(link) result.append(link) self.cache[(link.identifier, context.user_id)] = link # core.id and cache it! entity.attributes['occi.core.id'] = identifier entity.extras = self.get_extras(extras) self.cache[(entity.identifier, context.user_id)] = entity return result