def get_volume_mapping_by_uuid(self, context, volume_mapping_uuid): query = model_query(models.VolumeMapping) query = self._add_tenant_filters(context, query) query = query.filter_by(uuid=volume_mapping_uuid) try: return query.one() except NoResultFound: raise exception.VolumeMappingNotFound(volume_mapping_uuid)
def destroy_volume_mapping(self, context, volume_mapping_uuid): session = get_session() with session.begin(): query = model_query(models.VolumeMapping, session=session) query = add_identity_filter(query, volume_mapping_uuid) count = query.delete() if count != 1: raise exception.VolumeMappingNotFound(volume_mapping_uuid)
def get_volume_mapping_by_uuid(self, context, volume_mapping_uuid): try: res = self.client.read('/volume_mappings/' + volume_mapping_uuid) volume_mapping = translate_etcd_result(res, 'volume_mapping') filtered_vms = self._filter_resources( [volume_mapping], self._add_tenant_filters(context, {})) if filtered_vms: return filtered_vms[0] else: raise exception.VolumeMappingNotFound( volume_mapping=volume_mapping_uuid) except etcd.EtcdKeyNotFound: raise exception.VolumeMappingNotFound( volume_mapping=volume_mapping_uuid) except Exception as e: LOG.error('Error occurred while retrieving volume mapping: %s', six.text_type(e)) raise
def _do_update_volume_mapping(self, volume_mapping_uuid, values): session = get_session() with session.begin(): query = model_query(models.VolumeMapping, session=session) query = add_identity_filter(query, volume_mapping_uuid) try: ref = query.with_lockmode('update').one() except NoResultFound: raise exception.VolumeMappingNotFound(volume_mapping_uuid) ref.update(values) return ref
def update_volume_mapping(self, context, volume_mapping_uuid, values): if 'uuid' in values: msg = _('Cannot overwrite UUID for an existing VolumeMapping.') raise exception.InvalidParameterValue(err=msg) try: target_uuid = self.get_volume_mapping_by_uuid( context, volume_mapping_uuid).uuid target = self.client.read('/volume_mapping/' + target_uuid) target_value = json.loads(target.value) target_value.update(values) target.value = json.dump_as_bytes(target_value) self.client.update(target) except etcd.EtcdKeyNotFound: raise exception.VolumeMappingNotFound( volume_mapping=volume_mapping_uuid) except Exception as e: LOG.error('Error occurred while updating volume mappping: %s', six.text_type(e)) raise return translate_etcd_result(target, 'volume_mapping')