'Uploads the specified volume to image service.'

@wsgi.response(http_client.ACCEPTED)
@wsgi.action('os-volume_upload_image')
def _volume_upload_image(self, req, id, body):

    context = req.environ['cinder.context']
    params = body['os-volume_upload_image']
    req_version = req.api_version_request
    if (not params.get('image_name')):
        msg = _('No    image_name    was    specified    in    request.')
        raise webob.exc.HTTPBadRequest(explanation=msg)
    force = params.get('force', 'False')
    try:
        force = strutils.bool_from_string(force, strict=True)
    except ValueError as error:
        err_msg = encodeutils.exception_to_unicode(error)
        msg = (_("Invalid    value    for    'force':    '%s'") % err_msg)
        raise webob.exc.HTTPBadRequest(explanation=msg)
    volume = self.volume_api.get(context, id)
    authorize(context, 'upload_image')
    disk_format = params.get('disk_format', 'raw')
    if (not image_utils.validate_disk_format(disk_format)):
        msg = (_("Invalid    disk-format    '%(disk_format)s'    is    specified.    Allowed    disk-formats    are    %(allowed_disk_formats)s.") % {'disk_format': disk_format, 'allowed_disk_formats': ',    '.join(image_utils.VALID_DISK_FORMATS)})
        raise webob.exc.HTTPBadRequest(explanation=msg)
    if (disk_format == 'parallels'):
        disk_format = 'ploop'
    image_metadata = {'container_format': params.get('container_format', 'bare'), 'disk_format': disk_format, 'name': params['image_name']}
    if volume.encryption_key_id:
        encryption_key_id = self._key_manager.store(context, self._key_manager.get(context, volume.encryption_key_id))
        image_metadata['cinder_encryption_key_id'] = encryption_key_id
    if (req_version >= api_version_request.APIVersionRequest('3.1')):
        image_metadata['visibility'] = params.get('visibility', 'private')
        image_metadata['protected'] = params.get('protected', 'False')
        if (image_metadata['visibility'] == 'public'):
            authorize(context, 'upload_public')
        if (CONF.glance_api_version != 2):
            image_metadata['is_public'] = (image_metadata['visibility'] == 'public')
            image_metadata.pop('visibility', None)
        image_metadata['protected'] = utils.get_bool_param('protected', image_metadata)
    try:
        response = self.volume_api.copy_volume_to_image(context, volume, image_metadata, force)
    except exception.InvalidVolume as error:
        raise webob.exc.HTTPBadRequest(explanation=error.msg)
    except ValueError as error:
        raise webob.exc.HTTPBadRequest(explanation=six.text_type(error))
    except messaging.RemoteError as error:
        msg = ('%(err_type)s:    %(err_msg)s' % {'err_type': error.exc_type, 'err_msg': error.value})
        raise webob.exc.HTTPBadRequest(explanation=msg)
    except Exception as error:
        raise webob.exc.HTTPBadRequest(explanation=six.text_type(error))
    return {'os-volume_upload_image': response}
