示例#1
0
JsonReply *RulesHandler::EditRule(const QVariantMap &params)
{
    // check rule consistency
    RuleEngine::RuleError ruleConsistencyError = JsonTypes::verifyRuleConsistency(params);
    if (ruleConsistencyError !=  RuleEngine::RuleErrorNoError) {
        QVariantMap returns;
        returns.insert("ruleError", JsonTypes::ruleErrorToString(ruleConsistencyError));
        return createReply(returns);
    }

    // Check and upack eventDescriptorList
    QPair<QList<EventDescriptor>, RuleEngine::RuleError> eventDescriptorVerification = JsonTypes::verifyEventDescriptors(params);
    QList<EventDescriptor> eventDescriptorList = eventDescriptorVerification.first;
    if (eventDescriptorVerification.second != RuleEngine::RuleErrorNoError) {
        QVariantMap returns;
        returns.insert("ruleError", JsonTypes::ruleErrorToString(eventDescriptorVerification.second));
        return createReply(returns);
    }

    // Check and unpack stateEvaluator
    qCDebug(dcJsonRpc) << "unpacking stateEvaluator:" << params.value("stateEvaluator").toMap();
    StateEvaluator stateEvaluator = JsonTypes::unpackStateEvaluator(params.value("stateEvaluator").toMap());
    if (!stateEvaluator.isValid()) {
        QVariantMap returns;
        returns.insert("ruleError", JsonTypes::ruleErrorToString(RuleEngine::RuleErrorInvalidStateEvaluatorValue));
        return createReply(returns);
    }

    // Check and unpack actions
    QPair<QList<RuleAction>, RuleEngine::RuleError> actionsVerification = JsonTypes::verifyActions(params, eventDescriptorList);
    QList<RuleAction> actions = actionsVerification.first;
    if (actionsVerification.second != RuleEngine::RuleErrorNoError) {
        QVariantMap returns;
        returns.insert("ruleError", JsonTypes::ruleErrorToString(actionsVerification.second));
        return createReply(returns);
    }

    // Check and unpack exitActions
    QPair<QList<RuleAction>, RuleEngine::RuleError> exitActionsVerification = JsonTypes::verifyExitActions(params);
    QList<RuleAction> exitActions = exitActionsVerification.first;
    if (exitActionsVerification.second != RuleEngine::RuleErrorNoError) {
        QVariantMap returns;
        returns.insert("ruleError", JsonTypes::ruleErrorToString(exitActionsVerification.second));
        return createReply(returns);
    }

    QString name = params.value("name", QString()).toString();
    bool enabled = params.value("enabled", true).toBool();
    bool executable = params.value("executable", true).toBool();

    RuleId ruleId = RuleId(params.value("ruleId").toString());
    RuleEngine::RuleError status = GuhCore::instance()->editRule(ruleId, name, eventDescriptorList, stateEvaluator, actions, exitActions, enabled, executable);
    QVariantMap returns;
    if (status ==  RuleEngine::RuleErrorNoError) {
        returns.insert("rule", JsonTypes::packRule(GuhCore::instance()->findRule(ruleId)));
    }
    returns.insert("ruleError", JsonTypes::ruleErrorToString(status));
    return createReply(returns);
}
示例#2
0
JsonReply *RulesHandler::GetRuleDetails(const QVariantMap &params)
{
    RuleId ruleId = RuleId(params.value("ruleId").toString());
    Rule rule = GuhCore::instance()->findRule(ruleId);
    if (rule.id().isNull()) {
        return createReply(statusToReply(RuleEngine::RuleErrorRuleNotFound));
    }
    QVariantMap returns = statusToReply(RuleEngine::RuleErrorNoError);
    returns.insert("rule", JsonTypes::packRule(rule));
    return createReply(returns);
}
示例#3
0
/*! This method will be used to process the given \a request and the given \a urlTokens. The request
    has to be in this namespace. Returns the resulting \l HttpReply.

    \sa HttpRequest, HttpReply, RestResource::proccessRequest()
*/
HttpReply *RulesResource::proccessRequest(const HttpRequest &request, const QStringList &urlTokens)
{
    // /api/v1/rules/{ruleId}/
    if (urlTokens.count() >= 4) {
        m_ruleId = RuleId(urlTokens.at(3));
        if (m_ruleId.isNull()) {
            qCWarning(dcRest) << "Could not parse RuleId:" << urlTokens.at(3);
            return createRuleErrorReply(HttpReply::BadRequest, RuleEngine::RuleErrorRuleNotFound);
        }

        if (!GuhCore::instance()->ruleEngine()->findRule(m_ruleId).isValid()) {
            qCWarning(dcRest) << "Could not find rule with id" << m_ruleId.toString();
            return createRuleErrorReply(HttpReply::NotFound, RuleEngine::RuleErrorRuleNotFound);
        }

    }

    // check method
    HttpReply *reply;
    switch (request.method()) {
    case HttpRequest::Get:
        reply = proccessGetRequest(request, urlTokens);
        break;
    case HttpRequest::Put:
        reply = proccessPutRequest(request, urlTokens);
        break;
    case HttpRequest::Post:
        reply = proccessPostRequest(request, urlTokens);
        break;
    case HttpRequest::Delete:
        reply = proccessDeleteRequest(request, urlTokens);
        break;
    case HttpRequest::Options:
        reply = proccessOptionsRequest(request, urlTokens);
        break;
    default:
        reply = createErrorReply(HttpReply::BadRequest);
        break;
    }
    return reply;
}