'Rebuild an instance with the given attributes.'

@wsgi.response(202)
@extensions.expected_errors((400, 403, 404, 409))
@wsgi.action('rebuild')
@validation.schema(schema_server_rebuild_v20, '2.0', '2.0')
@validation.schema(schema_server_rebuild, '2.1', '2.18')
@validation.schema(schema_server_rebuild_v219, '2.19')
def _action_rebuild(self, req, id, body):

    rebuild_dict = body['rebuild']
    image_href = rebuild_dict['imageRef']
    password = self._get_server_admin_password(rebuild_dict)
    context = req.environ['nova.context']
    instance = self._get_server(context, req, id)
    context.can((server_policies.SERVERS % 'rebuild'), target={'user_id': instance.user_id, 'project_id': instance.project_id})
    attr_map = {'name': 'display_name', 'description': 'display_description', 'metadata': 'metadata'}
    kwargs = {}
    helpers.translate_attributes(helpers.REBUILD, rebuild_dict, kwargs)
    for (request_attribute, instance_attribute) in attr_map.items():
        try:
            if (request_attribute == 'name'):
                kwargs[instance_attribute] = common.normalize_name(rebuild_dict[request_attribute])
            else:
                kwargs[instance_attribute] = rebuild_dict[request_attribute]
        except (KeyError, TypeError):
            pass
    try:
        self.compute_api.rebuild(context, instance, image_href, password, **kwargs)
    except exception.InstanceIsLocked as e:
        raise exc.HTTPConflict(explanation=e.format_message())
    except exception.InstanceInvalidState as state_error:
        common.raise_http_conflict_for_instance_invalid_state(state_error, 'rebuild', id)
    except exception.InstanceNotFound:
        msg = _('Instance    could    not    be    found')
        raise exc.HTTPNotFound(explanation=msg)
    except exception.InstanceUnknownCell as e:
        raise exc.HTTPNotFound(explanation=e.format_message())
    except exception.ImageNotFound:
        msg = _('Cannot    find    image    for    rebuild')
        raise exc.HTTPBadRequest(explanation=msg)
    except exception.QuotaError as error:
        raise exc.HTTPForbidden(explanation=error.format_message())
    except (exception.ImageNotActive, exception.FlavorDiskTooSmall, exception.FlavorMemoryTooSmall, exception.InvalidMetadata, exception.AutoDiskConfigDisabledByImage) as error:
        raise exc.HTTPBadRequest(explanation=error.format_message())
    instance = self._get_server(context, req, id, is_detail=True)
    view = self._view_builder.show(req, instance, extend_address=False)
    if CONF.api.enable_instance_password:
        view['server']['adminPass'] = password
    robj = wsgi.ResponseObject(view)
    return self._add_location(robj)
