Statistics
| Branch: | Tag: | Revision:

one / src / rm / RequestManagerClusterAdd.cc @ 6329d8b3

History | View | Annotate | Download (4.46 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#include "RequestManager.h"
18
#include "NebulaLog.h"
19

    
20
#include "AuthManager.h"
21

    
22
/* -------------------------------------------------------------------------- */
23
/* -------------------------------------------------------------------------- */
24

    
25
void RequestManager::ClusterAdd::execute(
26
    xmlrpc_c::paramList const& paramList,
27
    xmlrpc_c::value *   const  retval)
28
{
29
    string  session;
30

    
31
    int     hid;
32
    int     clid;
33
    int     rc;
34
    
35
    const string        method_name = "ClusterAdd";
36

    
37
    Host *      host;
38
    Cluster *   cluster;
39

    
40
    ostringstream oss;
41

    
42
    /*   -- RPC specific vars --  */
43
    vector<xmlrpc_c::value> arrayData;
44
    xmlrpc_c::value_array * arrayresult;
45

    
46
    NebulaLog::log("ReM",Log::DEBUG,"ClusterAdd method invoked");
47

    
48
    // Get the parameters
49
    session      = xmlrpc_c::value_string(paramList.getString(0));
50
    hid          = xmlrpc_c::value_int   (paramList.getInt(1));
51
    clid         = xmlrpc_c::value_int   (paramList.getInt(2));
52

    
53
    //Authenticate the user
54
    rc = ClusterAdd::upool->authenticate(session);
55

    
56
    if ( rc == -1 )
57
    {
58
        goto error_authenticate;
59
    }
60

    
61
     //Authorize the operation
62
    if ( rc != 0 ) // rc == 0 means oneadmin
63
    {
64
        AuthRequest ar(rc);
65
        
66
        ar.add_auth(AuthRequest::HOST,hid,AuthRequest::MANAGE,0,false);
67
        ar.add_auth(AuthRequest::CLUSTER,clid,AuthRequest::USE,0,false);
68

    
69
        if (UserPool::authorize(ar) == -1)
70
        {
71
            goto error_authorize;
72
        }
73
    }
74

    
75
    // Check if cluster exists
76
    cluster = ClusterAdd::cpool->get(clid,true);
77

    
78
    if ( cluster == 0 )
79
    {
80
        goto error_cluster_get;
81
    }
82

    
83
    // Check if host exists
84
    host = ClusterAdd::hpool->get(hid,true);
85

    
86
    if ( host == 0 )
87
    {
88
        goto error_host_get;
89
    }
90

    
91
    // Set cluster
92
    rc = host->set_cluster(cluster->get_oid());
93

    
94
    if ( rc != 0 )
95
    {
96
        goto error_cluster_add;
97
    }
98

    
99
    // Update the DB
100
    ClusterAdd::hpool->update(host);
101

    
102
    host->unlock();
103

    
104
    cluster->unlock();
105

    
106
    // All nice, return success to the client
107
    arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS
108

    
109
    // Copy arrayresult into retval mem space
110
    arrayresult = new xmlrpc_c::value_array(arrayData);
111
    *retval = *arrayresult;
112

    
113
    delete arrayresult; // and get rid of the original
114

    
115
    return;
116

    
117
error_authenticate:
118
    oss.str(authenticate_error(method_name));
119
    goto error_common;
120

    
121
error_authorize:
122
    oss.str(authorization_error(method_name, "USE", "CLUSTER", rc, clid));
123
    goto error_common;
124

    
125
error_host_get:
126
    cluster->unlock();
127
    oss.str(get_error(method_name, "HOST", hid));
128
    goto error_common;
129

    
130
error_cluster_get:
131
    oss.str(get_error(method_name, "CLUSTER", clid));
132
    goto error_common;
133

    
134
error_cluster_add:
135
    host->unlock();
136
    cluster->unlock();
137
    oss.str(action_error(method_name, "USE", "CLUSTER", clid, rc));
138
    goto error_common;
139

    
140
error_common:
141

    
142
    arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE
143
    arrayData.push_back(xmlrpc_c::value_string(oss.str()));
144

    
145
    NebulaLog::log("ReM",Log::ERROR,oss);
146

    
147
    xmlrpc_c::value_array arrayresult_error(arrayData);
148

    
149
    *retval = arrayresult_error;
150

    
151
    return;
152
}
153

    
154
/* -------------------------------------------------------------------------- */
155
/* -------------------------------------------------------------------------- */