Statistics
| Branch: | Tag: | Revision:

one / src / rm / RequestManagerAllocate.cc @ 5bde303f

History | View | Annotate | Download (8.18 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 << "[" << method_name << "]" << " Error allocating a new "
48
        << object_name(auth_object) << ". Parse error";
49

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

    
60
    return oss.str();
61
}
62

    
63
/* -------------------------------------------------------------------------- */
64
/* -------------------------------------------------------------------------- */
65

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

    
73
    AuthRequest ar(uid);
74

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

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

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

    
91
        return false;
92
    }
93

    
94
    return true;
95
}
96

    
97
/* -------------------------------------------------------------------------- */
98
/* -------------------------------------------------------------------------- */
99

    
100
void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params)
101
{
102
    Template * tmpl = 0;
103

    
104
    string error_str;
105
    int    rc, id;
106

    
107
    if ( do_template == true )
108
    {
109
        char * error_msg = 0;
110
        string str_tmpl  = xmlrpc_c::value_string(params.getString(1));
111

    
112
        tmpl = get_object_template();
113

    
114
        rc   = tmpl->parse(str_tmpl, &error_msg);
115

    
116
        if ( rc != 0 )
117
        {
118
            failure_response(INTERNAL, allocate_error(error_msg));
119
            delete tmpl;
120

    
121
            return;
122
        }
123
    }
124

    
125
    if ( allocate_authorization(tmpl) == false )
126
    {
127
        return;
128
    }
129

    
130
    rc = pool_allocate(params, tmpl, id, error_str);
131

    
132
    if ( rc < 0 )
133
    {
134
        failure_response(INTERNAL, allocate_error(error_str));
135
        return;
136
    }
137
    
138
    success_response(id);
139
}
140

    
141
/* -------------------------------------------------------------------------- */
142
/* -------------------------------------------------------------------------- */
143

    
144
int VirtualNetworkAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, 
145
                                          Template * tmpl,
146
                                          int& id, 
147
                                          string& error_str)
148
{
149
    VirtualNetworkPool * vpool = static_cast<VirtualNetworkPool *>(pool);
150
    VirtualNetworkTemplate * vtmpl=static_cast<VirtualNetworkTemplate *>(tmpl);
151

    
152
    return vpool->allocate(uid, gid, vtmpl, &id, error_str);
153
}
154

    
155
/* -------------------------------------------------------------------------- */
156
/* -------------------------------------------------------------------------- */
157

    
158
int ImageAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, 
159
                                 Template * tmpl,
160
                                 int& id, 
161
                                 string& error_str)
162
{
163
    ImagePool * ipool = static_cast<ImagePool *>(pool);
164
    ImageTemplate * itmpl = static_cast<ImageTemplate *>(tmpl);
165

    
166
    return ipool->allocate(uid, gid, itmpl, &id, error_str);
167
}
168

    
169
/* -------------------------------------------------------------------------- */
170
/* -------------------------------------------------------------------------- */
171

    
172
int TemplateAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, 
173
                                    Template * tmpl,
174
                                    int& id, 
175
                                    string& error_str)
176
{
177
    VMTemplatePool * tpool = static_cast<VMTemplatePool *>(pool);
178

    
179
    VirtualMachineTemplate * ttmpl=static_cast<VirtualMachineTemplate *>(tmpl);
180

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

    
184
/* -------------------------------------------------------------------------- */
185
/* -------------------------------------------------------------------------- */
186

    
187
int HostAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
188
                                Template * tmpl,
189
                                int& id, 
190
                                string& error_str)
191
{
192
    string host    = xmlrpc_c::value_string(paramList.getString(1));
193
    string im_mad  = xmlrpc_c::value_string(paramList.getString(2));
194
    string vmm_mad = xmlrpc_c::value_string(paramList.getString(3));
195
    string tm_mad  = xmlrpc_c::value_string(paramList.getString(4));
196

    
197
    HostPool * hpool = static_cast<HostPool *>(pool);
198

    
199
    return hpool->allocate(&id, host, im_mad, vmm_mad, tm_mad, error_str);
200
}
201

    
202
/* -------------------------------------------------------------------------- */
203
/* -------------------------------------------------------------------------- */
204

    
205
int UserAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
206
                                Template * tmpl,
207
                                int& id, 
208
                                string& error_str)
209
{
210
    string uname  = xmlrpc_c::value_string(paramList.getString(1));
211
    string passwd = xmlrpc_c::value_string(paramList.getString(2));
212

    
213
    UserPool * upool = static_cast<UserPool *>(pool);
214

    
215
    return upool->allocate(&id,GroupPool::USERS_ID,uname,passwd,true,error_str);
216
}
217

    
218
/* -------------------------------------------------------------------------- */
219
/* -------------------------------------------------------------------------- */
220

    
221
int ClusterAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
222
                                   Template * tmpl,
223
                                   int& id, 
224
                                   string& error_str)
225
{
226
    string cname = xmlrpc_c::value_string(paramList.getString(1));
227

    
228
    ClusterPool * cpool = static_cast<ClusterPool *>(pool);
229

    
230
    return cpool->allocate(&id, cname, error_str);
231
}
232

    
233
/* -------------------------------------------------------------------------- */
234
/* -------------------------------------------------------------------------- */
235

    
236
int GroupAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, 
237
                                 Template * tmpl,
238
                                 int& id, 
239
                                 string& error_str)
240
{
241
    string gname = xmlrpc_c::value_string(paramList.getString(1));
242

    
243
    GroupPool * gpool = static_cast<GroupPool *>(pool);
244

    
245
    return gpool->allocate(uid, gname, &id, error_str);
246
}
247