<commit_before>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2015, 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 "RequestManagerClone.h"
#include "RequestManagerImage.h"
#include "RequestManagerDelete.h"
#include "PoolObjectAuth.h"
#include "Nebula.h"

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

void RequestManagerClone::request_execute(
        xmlrpc_c::paramList const&  paramList,
        RequestAttributes&          att)
{
    int    source_id = xmlrpc_c::value_int(paramList.getInt(1));
    string name      = xmlrpc_c::value_string(paramList.getString(2));

    int    new_id;

    ErrorCode ec = clone(source_id, name, new_id, att);

    if ( ec == SUCCESS )
    {
        success_response(new_id, att);
    }
    else
    {
        failure_response(ec, att);
    }
}

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

Request::ErrorCode RequestManagerClone::clone(
        int             source_id,
        const string    &name,
        int             &new_id,
        RequestAttributes& att)
{
    int rc;

    PoolObjectAuth  perms;

    Template *      tmpl;
    PoolObjectSQL * source_obj;

    source_obj = pool->get(source_id, true);

    if ( source_obj == 0 )
    {
        att.resp_id = source_id;
        return NO_EXISTS;
    }

    tmpl = clone_template(source_obj);

    source_obj->get_permissions(perms);

    source_obj->unlock();

    tmpl->erase("NAME");
    tmpl->set(new SingleAttribute("NAME",name));

    if ( att.uid != 0 )
    {
        string tmpl_str = "";

        AuthRequest ar(att.uid, att.group_ids);

        ar.add_auth(auth_op, perms); //USE OBJECT

        tmpl->to_xml(tmpl_str);

        ar.add_create_auth(att.uid, att.gid, auth_object, tmpl_str);

        if (UserPool::authorize(ar) == -1)
        {
            att.resp_msg = ar.message;

            delete tmpl;
            return AUTHORIZATION;
        }
    }

    rc = pool_allocate(source_id, tmpl, new_id, att);

    if ( rc < 0 )
    {
        return ALLOCATE;
    }

    return SUCCESS;
}

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

void VMTemplateClone::request_execute(
        xmlrpc_c::paramList const&  paramList,
        RequestAttributes&          att)
{
    int    source_id = xmlrpc_c::value_int(paramList.getInt(1));
    string name      = xmlrpc_c::value_string(paramList.getString(2));
    bool   recursive = false;

    if (paramList.size() > 3)
    {
        recursive = xmlrpc_c::value_boolean(paramList.getBoolean(3));
    }

    int             new_id;
    VMTemplate *    vmtmpl;
    VMTemplatePool* tpool = static_cast<VMTemplatePool*>(pool);
    ErrorCode       ec;
    ostringstream   oss;

    vector<VectorAttribute *> disks;
    vector<int> new_img_ids;
    vector<int>::iterator i;

    RequestAttributes img_att(att);
    img_att.resp_obj = PoolObjectSQL::IMAGE;

    ec = clone(source_id, name, new_id, att);

    if ( ec != SUCCESS )
    {
        failure_response(ec, att);
    }

    if (recursive)
    {
        Nebula&    nd    = Nebula::instance();
        ImagePool* ipool = nd.get_ipool();

        int img_id;
        int new_img_id;

        vmtmpl = tpool->get(new_id, true);

        if (vmtmpl == 0)
        {
            att.resp_msg = object_name(PoolObjectSQL::TEMPLATE) +
                " was cloned, but it was deleted before the disks could also be cloned.";

            failure_response(ACTION, att);
            return;
        }

        vmtmpl->get_disks(disks);

        vmtmpl->unlock();

        int ndisk = 0;

        for (vector<VectorAttribute*>::iterator it = disks.begin(); it != disks.end(); it++)
        {<commit_msg>Feature #4400: Fix error handling for duplicated names<commit_after>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2015, 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 "RequestManagerClone.h"
#include "RequestManagerImage.h"
#include "RequestManagerDelete.h"
#include "PoolObjectAuth.h"
#include "Nebula.h"

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

void RequestManagerClone::request_execute(
        xmlrpc_c::paramList const&  paramList,
        RequestAttributes&          att)
{
    int    source_id = xmlrpc_c::value_int(paramList.getInt(1));
    string name      = xmlrpc_c::value_string(paramList.getString(2));

    int    new_id;

    ErrorCode ec = clone(source_id, name, new_id, att);

    if ( ec == SUCCESS )
    {
        success_response(new_id, att);
    }
    else
    {
        failure_response(ec, att);
    }
}

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

Request::ErrorCode RequestManagerClone::clone(
        int             source_id,
        const string    &name,
        int             &new_id,
        RequestAttributes& att)
{
    int rc;

    PoolObjectAuth  perms;

    Template *      tmpl;
    PoolObjectSQL * source_obj;

    source_obj = pool->get(source_id, true);

    if ( source_obj == 0 )
    {
        att.resp_id = source_id;
        return NO_EXISTS;
    }

    tmpl = clone_template(source_obj);

    source_obj->get_permissions(perms);

    source_obj->unlock();

    tmpl->erase("NAME");
    tmpl->set(new SingleAttribute("NAME",name));

    if ( att.uid != 0 )
    {
        string tmpl_str = "";

        AuthRequest ar(att.uid, att.group_ids);

        ar.add_auth(auth_op, perms); //USE OBJECT

        tmpl->to_xml(tmpl_str);

        ar.add_create_auth(att.uid, att.gid, auth_object, tmpl_str);

        if (UserPool::authorize(ar) == -1)
        {
            att.resp_msg = ar.message;

            delete tmpl;
            return AUTHORIZATION;
        }
    }

    rc = pool_allocate(source_id, tmpl, new_id, att);

    if ( rc < 0 )
    {
        return ALLOCATE;
    }

    return SUCCESS;
}

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

void VMTemplateClone::request_execute(
        xmlrpc_c::paramList const&  paramList,
        RequestAttributes&          att)
{
    int    source_id = xmlrpc_c::value_int(paramList.getInt(1));
    string name      = xmlrpc_c::value_string(paramList.getString(2));
    bool   recursive = false;

    if (paramList.size() > 3)
    {
        recursive = xmlrpc_c::value_boolean(paramList.getBoolean(3));
    }

    int             new_id;
    VMTemplate *    vmtmpl;
    VMTemplatePool* tpool = static_cast<VMTemplatePool*>(pool);
    ErrorCode       ec;
    ostringstream   oss;

    vector<VectorAttribute *> disks;
    vector<int> new_img_ids;
    vector<int>::iterator i;

    RequestAttributes img_att(att);
    img_att.resp_obj = PoolObjectSQL::IMAGE;

    ec = clone(source_id, name, new_id, att);

    if ( ec != SUCCESS )
    {
        failure_response(ec, att);
        return;
    }

    if (recursive)
    {
        Nebula&    nd    = Nebula::instance();
        ImagePool* ipool = nd.get_ipool();

        int img_id;
        int new_img_id;

        vmtmpl = tpool->get(new_id, true);

        if (vmtmpl == 0)
        {
            att.resp_msg = object_name(PoolObjectSQL::TEMPLATE) +
                " was cloned, but it was deleted before the disks could also be cloned.";

            failure_response(ACTION, att);
            return;
        }

        vmtmpl->get_disks(disks);

        vmtmpl->unlock();

        int ndisk = 0;

        for (vector<VectorAttribute*>::iterator it = disks.begin(); it != disks.end(); it++)
        {
{'fraction_non_alphanumeric': 0.2146986771190593, 'fraction_numerical': 0.005095541401273885, 'mean_word_length': 1.273557585208287, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, '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': '6318913', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2626, 'n_words': 604}