<commit_before>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#include "RequestManagerZone.h"
#include "Nebula.h"
#include "Client.h"

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

static Request::ErrorCode master_update_zone(int oid, const std::string& xml,
        RequestAttributes& att)
{
    Client * client = Client::client();

    xmlrpc_c::value         result;
    vector<xmlrpc_c::value> values;

    std::ostringstream oss("Cannot update zone at federation master: ",
            std::ios::ate);
    try
    {
        client->call("one.zone.updatedb", "is", &result, oid, xml.c_str());
    }
    catch (exception const& e)
    {
        oss << e.what();
        att.resp_msg = oss.str();

        return Request::ACTION;
    }

    values = xmlrpc_c::value_array(result).vectorValueValue();

    if ( xmlrpc_c::value_boolean(values[0]) == false )
    {
        std::string e = xmlrpc_c::value_string(values[1]);
        oss << e;

        att.resp_msg = oss.str();

        return Request::ACTION;
    }

    return Request::SUCCESS;
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void ZoneAddServer::request_execute(xmlrpc_c::paramList const& paramList,
    RequestAttributes& att)
{
    Nebula& nd    = Nebula::instance();

    int    id     = xmlrpc_c::value_int(paramList.getInt(1));
    string zs_str = xmlrpc_c::value_string(paramList.getString(2));
	int    zs_id;

    string error_str, xmlep;

    if ( id != nd.get_zone_id() )
    {
        att.resp_msg = "Servers have to be added through the target zone"
             " endpoints";
        failure_response(ACTION, att);

        return;
    }

    if ( basic_authorization(id, att) == false )
    {
        return;
    }

    Template zs_tmpl;

    int rc = zs_tmpl.parse_str_or_xml(zs_str, error_str);

    if ( rc != 0 )
    {
        att.resp_msg = error_str;
        failure_response(ACTION, att);

        return;
    }

    Zone * zone = (static_cast<ZonePool *>(pool))->get(id, true);

    if ( zone == 0 )
    {
        att.resp_id = id;
        failure_response(NO_EXISTS, att);

        return;
    }

    if ( zone->add_server(zs_tmpl, zs_id, xmlep, att.resp_msg) == -1 )
    {
        failure_response(ACTION, att);

        return;
    }

    if ( nd.is_federation_master() || !nd.is_federation_enabled() )
    {
        std::vector<int> zids;

        pool->update(zone);

        zone->unlock();

        nd.get_frm()->update_zones(zids);
    }
    else
    {
        std::string tmpl_xml;

        int oid = zone->get_oid();

        zone->to_xml(tmpl_xml);

        ErrorCode ec = master_update_zone(oid, tmpl_xml, att);

        zone->unlock();

        if ( ec != SUCCESS )
        {
            NebulaLog::log("ReM", Log::ERROR, att.resp_msg);

            failure_response(ec, att);
            return;
        }
    }

	nd.get_raftm()->add_server(zs_id, xmlep);

    success_response(id, att);
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void ZoneDeleteServer::request_execute(xmlrpc_c::paramList const& paramList,
    RequestAttributes& att)
{
    Nebula& nd = Nebula::instance();

    int id     = xmlrpc_c::value_int(paramList.getInt(1));
    int zs_id  = xmlrpc_c::value_int(paramList.getInt(2));

    string error_str;

    if ( id != nd.get_zone_id() )
    {
        att.resp_msg = "Servers have to be deleted through the target zone"
             " endpoints";
        failure_response(ACTION, att);

        return;
    }

    if ( basic_authorization(id, att) == false )
    {
        return;
    }

    Zone * zone = (static_cast<ZonePool *>(pool))->get(id, true);

    if ( zone == 0 )
    {
        att.resp_id = id;
        failure_response(NO_EXISTS, att);

        return;
    }

    if ( zone->delete_server(zs_id, att.resp_msg) == -1 )
    {
        failure_response(ACTION, att);
        zone->unlock();

        return;
    }

    if ( nd.is_federation_master() )
    {
        std::vector<int> zids;

        pool->update(zone);

        zone->unlock();

        nd.get_frm()->update_zones(zids);
    }
    else
    {
        std::string tmpl_xml;

        int oid = zone->get_oid();

        zone->to_xml(tmpl_xml);

        ErrorCode ec = master_update_zone(oid, tmpl_xml, att);
<commit_msg>F #4809: Fix error when deleting server from zone in standalone mode<commit_after>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#include "RequestManagerZone.h"
#include "Nebula.h"
#include "Client.h"

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

static Request::ErrorCode master_update_zone(int oid, const std::string& xml,
        RequestAttributes& att)
{
    Client * client = Client::client();

    xmlrpc_c::value         result;
    vector<xmlrpc_c::value> values;

    std::ostringstream oss("Cannot update zone at federation master: ",
            std::ios::ate);
    try
    {
        client->call("one.zone.updatedb", "is", &result, oid, xml.c_str());
    }
    catch (exception const& e)
    {
        oss << e.what();
        att.resp_msg = oss.str();

        return Request::ACTION;
    }

    values = xmlrpc_c::value_array(result).vectorValueValue();

    if ( xmlrpc_c::value_boolean(values[0]) == false )
    {
        std::string e = xmlrpc_c::value_string(values[1]);
        oss << e;

        att.resp_msg = oss.str();

        return Request::ACTION;
    }

    return Request::SUCCESS;
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void ZoneAddServer::request_execute(xmlrpc_c::paramList const& paramList,
    RequestAttributes& att)
{
    Nebula& nd    = Nebula::instance();

    int    id     = xmlrpc_c::value_int(paramList.getInt(1));
    string zs_str = xmlrpc_c::value_string(paramList.getString(2));
	int    zs_id;

    string error_str, xmlep;

    if ( id != nd.get_zone_id() )
    {
        att.resp_msg = "Servers have to be added through the target zone"
             " endpoints";
        failure_response(ACTION, att);

        return;
    }

    if ( basic_authorization(id, att) == false )
    {
        return;
    }

    Template zs_tmpl;

    int rc = zs_tmpl.parse_str_or_xml(zs_str, error_str);

    if ( rc != 0 )
    {
        att.resp_msg = error_str;
        failure_response(ACTION, att);

        return;
    }

    Zone * zone = (static_cast<ZonePool *>(pool))->get(id, true);

    if ( zone == 0 )
    {
        att.resp_id = id;
        failure_response(NO_EXISTS, att);

        return;
    }

    if ( zone->add_server(zs_tmpl, zs_id, xmlep, att.resp_msg) == -1 )
    {
        failure_response(ACTION, att);

        return;
    }

    if ( nd.is_federation_master() || !nd.is_federation_enabled() )
    {
        std::vector<int> zids;

        pool->update(zone);

        zone->unlock();

        nd.get_frm()->update_zones(zids);
    }
    else
    {
        std::string tmpl_xml;

        int oid = zone->get_oid();

        zone->to_xml(tmpl_xml);

        ErrorCode ec = master_update_zone(oid, tmpl_xml, att);

        zone->unlock();

        if ( ec != SUCCESS )
        {
            NebulaLog::log("ReM", Log::ERROR, att.resp_msg);

            failure_response(ec, att);
            return;
        }
    }

	nd.get_raftm()->add_server(zs_id, xmlep);

    success_response(id, att);
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void ZoneDeleteServer::request_execute(xmlrpc_c::paramList const& paramList,
    RequestAttributes& att)
{
    Nebula& nd = Nebula::instance();

    int id     = xmlrpc_c::value_int(paramList.getInt(1));
    int zs_id  = xmlrpc_c::value_int(paramList.getInt(2));

    string error_str;

    if ( id != nd.get_zone_id() )
    {
        att.resp_msg = "Servers have to be deleted through the target zone"
             " endpoints";
        failure_response(ACTION, att);

        return;
    }

    if ( basic_authorization(id, att) == false )
    {
        return;
    }

    Zone * zone = (static_cast<ZonePool *>(pool))->get(id, true);

    if ( zone == 0 )
    {
        att.resp_id = id;
        failure_response(NO_EXISTS, att);

        return;
    }

    if ( zone->delete_server(zs_id, att.resp_msg) == -1 )
    {
        failure_response(ACTION, att);
        zone->unlock();

        return;
    }

    if ( nd.is_federation_master() || !nd.is_federation_enabled() )
    {
        std::vector<int> zids;

        pool->update(zone);

        zone->unlock();

        nd.get_frm()->update_zones(zids);
    }
    else
    {
        std::string tmpl_xml;

        int oid = zone->get_oid();

        zone->to_xml(tmpl_xml);

        ErrorCode ec = master_update_zone(oid, tmpl_xml, att);

{'fraction_non_alphanumeric': 0.21772239529248874, 'fraction_numerical': 0.004326756663205262, 'mean_word_length': 1.3696944843141274, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 50}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7459848', 'n_tokens_mistral': 3297, 'n_tokens_neox': 3163, 'n_words': 688}