Esempio n. 1
0
class LocalVolumeController(object):
    """Disk controller for OS API"""
    def __init__(self):
        self.local_volume_api = LocalAPI()

    def create(self, req, body):
        context = req.environ['nova.context']

        vol = body['volume']

        vol_type = vol.get('volume_type', None)
        if vol_type:
            try:
                vol_type = volume_types.get_volume_type_by_name(context,
                    vol_type)
            except exception.NotFound:
                return faults.Fault(exc.HTTPNotFound())

        metadata = vol.get('metadata', None)

        new_volume = self.local_volume_api.create_local(context,
            instance_id=vol['instance_id'],
            snapshot_id=vol.get('snapshot_id'),
            device=vol['device'],
            size=self._get_size(vol.get('size')),
            description=vol.get('display_description'),
            volume_type=vol_type,
            metadata=metadata)

        new_volume = self.local_volume_api.get(context, new_volume['id'])

        retval = _translate_volume_detail_view(new_volume)

        return {'volume': retval}

    def delete(self, req, id):
        """Delete a volume."""
        volume_id = id
        context = req.environ['nova.context']

        LOG.audit(_("Delete volume with id: %s"), volume_id, context=context)

        try:
            self.local_volume_api.delete(context, volume_id)
        except exception.NotFound:
            return faults.Fault(exc.HTTPNotFound())
        return webob.Response(status_int=202)

    def index(self, req):
        """Returns a summary list of volumes."""
        return self._items(req, entity_maker=_translate_volume_summary_view)

    def _items(self, req, entity_maker):
        """Returns a list of volumes, transformed through entity_maker."""
        context = req.environ['nova.context']

        volumes = self.local_volume_api.get_all(context)
        limited_list = common.limited(volumes, req)
        res = [entity_maker(vol) for vol in limited_list]
        return {'volumes': res}

    def _get_size(self, parameter):
        if not parameter:
            return parameter

        modifiers = {
            '':  1,
            'b': 1,
            'K': 1024,
            'M': 1024 * 1024,
            'G': 1024 * 1024 * 1024
        }
        match = re.search('^(?P<size>[0-9]+)(?P<modifier>[A-Za-z]?)$', parameter)
        if not match:
            raise RuntimeError('Invalid parameter: %s' % parameter)
        size = int(match.group('size'))
        modifier = match.group('modifier')
        if modifier not in modifiers:
            raise RuntimeError('Only %s modifiers supported' % modifiers.keys())

        return size * modifiers[modifier]

    def update(self, req, id, body):
        context = req.environ.get('nova.context')
        vol = body['volume']
        volume_id = id
        new_size = self._get_size(vol['size'])
        LOG.audit(_("Resizing volume with id: %s to size %s"), volume_id, vol['size'])
        try:
            self.local_volume_api.resize(context, volume_id, new_size)
        except exception.NotFound:
            return faults.Fault(exc.HTTPNotFound())
        return webob.Response(status_int=202)