Statistics
| Branch: | Tag: | Revision:

one / src / rm / RequestManagerAllocate.cc @ 0a132883

History | View | Annotate | Download (9.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 "RequestManagerAllocate.h"
18
#include "NebulaLog.h"
19

    
20
#include "Nebula.h"
21

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

    
25
string RequestManagerAllocate::allocate_error (const string& error)
26
{
27
    ostringstream oss;
28

    
29
    oss << "[" << method_name << "]" << " Error allocating a new "
30
        << object_name(auth_object) << ".";
31

    
32
    if (!error.empty())
33
    {
34
        oss << " " << error;
35
    }
36

    
37
    return oss.str();
38
}
39

    
40
/* -------------------------------------------------------------------------- */
41
/* -------------------------------------------------------------------------- */
42

    
43
string RequestManagerAllocate::allocate_error (char *error)
44
{
45
    ostringstream oss;
46

    
47
    oss << "Parse error";
48

    
49
    if ( error != 0 )
50
    {
51
        oss << ": " << error;
52
        free(error);
53
    }
54
    else
55
    {
56
        oss << ".";
57
    }
58

    
59
    return allocate_error(oss.str());
60
}
61

    
62
/* -------------------------------------------------------------------------- */
63
/* -------------------------------------------------------------------------- */
64

    
65
bool RequestManagerAllocate::allocate_authorization(Template * tmpl)
66
{
67
    if ( uid == 0 )
68
    {
69
        return true;
70
    }
71

    
72
    AuthRequest ar(uid);
73

    
74
    if ( tmpl == 0 )
75
    { 
76
        ar.add_auth(auth_object,-1,auth_op,uid,false);
77
    }
78
    else
79
    {
80
        string t64;
81

    
82
        ar.add_auth(auth_object,tmpl->to_xml(t64),auth_op,uid,false);
83
    }
84

    
85
   if (UserPool::authorize(ar) == -1)
86
   {
87
        failure_response(AUTHORIZATION, //TODO
88
                 authorization_error("INFO","USER",uid,-1));
89

    
90
        return false;
91
    }
92

    
93
    return true;
94
}
95

    
96
/* -------------------------------------------------------------------------- */
97
/* -------------------------------------------------------------------------- */
98

    
99
bool VirtualMachineAllocate::allocate_authorization(Template * tmpl)
100
{
101
    if ( uid == 0 )
102
    {
103
        return true;
104
    }
105

    
106
    AuthRequest ar(uid);
107
    string      t64;
108

    
109
    VirtualMachineTemplate * ttmpl = static_cast<VirtualMachineTemplate *>(tmpl);
110

    
111
    ar.add_auth(auth_object,tmpl->to_xml(t64),auth_op,uid,false);
112

    
113
    VirtualMachine::set_auth_request(uid, ar, ttmpl);
114

    
115
   if (UserPool::authorize(ar) == -1)
116
   {
117
        failure_response(AUTHORIZATION, //TODO
118
                 authorization_error("INFO","USER",uid,-1));
119

    
120
        return false;
121
    }
122

    
123
    return true;
124
}
125

    
126
/* -------------------------------------------------------------------------- */
127
/* -------------------------------------------------------------------------- */
128

    
129
void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params)
130
{
131
    Template * tmpl = 0;
132

    
133
    string error_str;
134
    int    rc, id;
135

    
136
    if ( do_template == true )
137
    {
138
        char * error_msg = 0;
139
        string str_tmpl  = xmlrpc_c::value_string(params.getString(1));
140

    
141
        tmpl = get_object_template();
142

    
143
        rc   = tmpl->parse(str_tmpl, &error_msg);
144

    
145
        if ( rc != 0 )
146
        {
147
            failure_response(INTERNAL, allocate_error(error_msg));
148
            delete tmpl;
149

    
150
            return;
151
        }
152
    }
153

    
154
    if ( allocate_authorization(tmpl) == false )
155
    {
156
        return;
157
    }
158

    
159
    rc = pool_allocate(params, tmpl, id, error_str);
160

    
161
    if ( rc < 0 )
162
    {
163
        failure_response(INTERNAL, allocate_error(error_str));
164
        return;
165
    }
166
    
167
    success_response(id);
168
}
169

    
170
/* -------------------------------------------------------------------------- */
171
/* -------------------------------------------------------------------------- */
172

    
173
int VirtualMachineAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
174
                                          Template * tmpl,
175
                                          int& id, 
176
                                          string& error_str)
177
{
178
    VirtualMachineTemplate * ttmpl = static_cast<VirtualMachineTemplate *>(tmpl);
179
    VirtualMachinePool * vmpool    = static_cast<VirtualMachinePool *>(pool);
180

    
181
    return vmpool->allocate(uid, gid, ttmpl, &id, error_str, false);
182
}
183

    
184

    
185
/* -------------------------------------------------------------------------- */
186
/* -------------------------------------------------------------------------- */
187

    
188
int VirtualNetworkAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, 
189
                                          Template * tmpl,
190
                                          int& id, 
191
                                          string& error_str)
192
{
193
    VirtualNetworkPool * vpool = static_cast<VirtualNetworkPool *>(pool);
194
    VirtualNetworkTemplate * vtmpl=static_cast<VirtualNetworkTemplate *>(tmpl);
195

    
196
    return vpool->allocate(uid, gid, vtmpl, &id, error_str);
197
}
198

    
199
/* -------------------------------------------------------------------------- */
200
/* -------------------------------------------------------------------------- */
201

    
202
int ImageAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, 
203
                                 Template * tmpl,
204
                                 int& id, 
205
                                 string& error_str)
206
{
207
    ImagePool * ipool = static_cast<ImagePool *>(pool);
208
    ImageTemplate * itmpl = static_cast<ImageTemplate *>(tmpl);
209

    
210
    return ipool->allocate(uid, gid, itmpl, &id, error_str);
211
}
212

    
213
/* -------------------------------------------------------------------------- */
214
/* -------------------------------------------------------------------------- */
215

    
216
int TemplateAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, 
217
                                    Template * tmpl,
218
                                    int& id, 
219
                                    string& error_str)
220
{
221
    VMTemplatePool * tpool = static_cast<VMTemplatePool *>(pool);
222

    
223
    VirtualMachineTemplate * ttmpl=static_cast<VirtualMachineTemplate *>(tmpl);
224

    
225
    return tpool->allocate(uid, gid, ttmpl, &id, error_str);
226
}
227

    
228
/* -------------------------------------------------------------------------- */
229
/* -------------------------------------------------------------------------- */
230

    
231
int HostAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
232
                                Template * tmpl,
233
                                int& id, 
234
                                string& error_str)
235
{
236
    string host    = xmlrpc_c::value_string(paramList.getString(1));
237
    string im_mad  = xmlrpc_c::value_string(paramList.getString(2));
238
    string vmm_mad = xmlrpc_c::value_string(paramList.getString(3));
239
    string tm_mad  = xmlrpc_c::value_string(paramList.getString(4));
240

    
241
    HostPool * hpool = static_cast<HostPool *>(pool);
242

    
243
    return hpool->allocate(&id, host, im_mad, vmm_mad, tm_mad, error_str);
244
}
245

    
246
/* -------------------------------------------------------------------------- */
247
/* -------------------------------------------------------------------------- */
248

    
249
int UserAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
250
                                Template * tmpl,
251
                                int& id, 
252
                                string& error_str)
253
{
254
    string uname  = xmlrpc_c::value_string(paramList.getString(1));
255
    string passwd = xmlrpc_c::value_string(paramList.getString(2));
256

    
257
    UserPool * upool = static_cast<UserPool *>(pool);
258

    
259
    return upool->allocate(&id,GroupPool::USERS_ID,uname,passwd,true,error_str);
260
}
261

    
262
/* -------------------------------------------------------------------------- */
263
/* -------------------------------------------------------------------------- */
264

    
265
int ClusterAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
266
                                   Template * tmpl,
267
                                   int& id, 
268
                                   string& error_str)
269
{
270
    string cname = xmlrpc_c::value_string(paramList.getString(1));
271

    
272
    ClusterPool * cpool = static_cast<ClusterPool *>(pool);
273

    
274
    return cpool->allocate(&id, cname, error_str);
275
}
276

    
277
/* -------------------------------------------------------------------------- */
278
/* -------------------------------------------------------------------------- */
279

    
280
int GroupAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
281
                                 Template * tmpl,
282
                                 int& id, 
283
                                 string& error_str)
284
{
285
    string gname = xmlrpc_c::value_string(paramList.getString(1));
286

    
287
    GroupPool * gpool = static_cast<GroupPool *>(pool);
288

    
289
    return gpool->allocate(uid, gname, &id, error_str);
290
}
291