Statistics
| Branch: | Tag: | Revision:

one / include / Request.h @ bfaabf35

History | View | Annotate | Download (6.9 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
#ifndef REQUEST_H_
18
#define REQUEST_H_
19

    
20
#include <xmlrpc-c/base.hpp>
21
#include <xmlrpc-c/registry.hpp>
22

    
23
#include "RequestManager.h"
24
#include "AuthManager.h"
25

    
26
using namespace std;
27

    
28
/**
29
 *  The Request Class represents the basic abstraction for the OpenNebula
30
 *  XML-RPC API. This interface must be implemented by any XML-RPC API call
31
 */
32
class Request: public xmlrpc_c::method
33
{
34
public:
35
    /**
36
     *  Wraps the actual execution function by authorizing the user
37
     *  and calling the request_execute virtual function
38
     *    @param _paramlist list of XML parameters
39
     *    @param _retval value to be returned to the client
40
     */
41
    virtual void execute(
42
        xmlrpc_c::paramList const& _paramList,
43
        xmlrpc_c::value *   const  _retval);
44

    
45
    /**
46
     *  Error codes for the XML-RPC API
47
     */
48
    enum ErrorCode {
49
        SUCCESS        = 0x0000,
50
        AUTHENTICATION = 0x0100,
51
        AUTHORIZATION  = 0x0200,
52
        NO_EXISTS      = 0x0400,
53
        ACTION         = 0x0800,
54
        XML_RPC_API    = 0x1000,
55
        INTERNAL       = 0x2000,
56
    };
57

    
58
protected:
59

    
60
    /* ------------------- Attributes of the Request ---------------------- */
61

    
62
    int                 uid;    /**< id of the user performing the request */
63
    
64
    int                 gid;            /**< id of the user's group */
65

    
66
    PoolSQL *           pool;           /**< Pool of objects */
67

    
68
    string              method_name;    /**< The name of the XML-RPC method */
69

    
70
    AuthRequest::Object    auth_object; /**< Auth object for the request */
71

    
72
    AuthRequest::Operation auth_op;     /**< Auth operation for the request  */
73

    
74

    
75
    /* -------------------- Constructors ---------------------------------- */
76

    
77
    Request(const string& mn, 
78
            const string& signature, 
79
            const string& help): uid(-1),gid(-1),pool(0),method_name(mn),retval(0)
80
    {
81
        _signature = signature;
82
        _help      = help;
83
    };
84

    
85
    virtual ~Request(){};
86

    
87
    /* -------------------------------------------------------------------- */
88
    /* -------------------------------------------------------------------- */
89

    
90
    /**
91
     *  Performs a basic authorization for this request using the uid/gid
92
     *  from the request. The function gets the object from the pool to get 
93
     *  the public attribute and its owner. The authorization is based on 
94
     *  object and type of operation for the request.
95
     *    @param oid of the object.
96
     *
97
     *    @return true if the user is authorized.
98
     */
99
    bool basic_authorization(int oid);
100
            
101
    /**
102
     *  Actual Execution method for the request. Must be implemented by the
103
     *  XML-RPC requests
104
     *    @param _paramlist of the XML-RPC call (complete list)
105
     */
106
    virtual void request_execute(xmlrpc_c::paramList const& _paramList) = 0;
107

    
108
    /**
109
     *  Builds an XML-RPC response updating retval. After calling this function
110
     *  the xml-rpc excute method should return
111
     *    @param val to be returned to the client
112
     */
113
    void success_response(int val);
114

    
115
    /**
116
     *  Builds an XML-RPC response updating retval. After calling this function
117
     *  the xml-rpc excute method should return
118
     *    @param val string to be returned to the client
119
     */
120
    void success_response(const string& val);
121

    
122
    /**
123
     *  Builds an XML-RPC response updating retval. After calling this function
124
     *  the xml-rpc excute method should return
125
     *    @param ec error code for this call
126
     *    @param val string representation of the error
127
     */
128
    void failure_response(ErrorCode ec, const string& val);
129

    
130
    /**
131
     *  Gets a string representation for the Auth object in the
132
     *  request.
133
     *    @param ob object for the auth operation
134
     *    @return string equivalent of the object
135
     */
136
    static string object_name(AuthRequest::Object ob);
137

    
138
    /**
139
     *  Logs authorization errors
140
     *    @param message with the authorization error details
141
     *    @return string for logging
142
     */
143
    string authorization_error (const string &message);
144
    /**
145
     *  Logs authenticate errors
146
     *    @return string for logging
147
     */
148
    string authenticate_error ();
149

    
150
    /**
151
     *  Logs get object errors
152
     *    @param object over which the get failed
153
     *    @param id of the object over which the get failed
154
     *    @return string for logging
155
     */
156
    string get_error (const string &object, int id);
157

    
158
    /**
159
     *  Logs action errors
160
     *    @param err_desc brief description of the error
161
     *    @param err_detail additional error details from Managers & Pools
162
     *    @return string for logging
163
     */
164
    string request_error (const string &err_desc, const string &err_detail);
165

    
166
    /**
167
     *  Logs allocate errors
168
     *    @param message with the allocate error details
169
     *    @return string for logging
170
     */
171
    string allocate_error (const string& error);
172

    
173
    /**
174
     *  Logs allocate errors for a given resource
175
     *    @param obj the resource
176
     *    @param message with the allocate error details
177
     *    @return string for logging
178
     */
179
    string allocate_error (AuthRequest::Object obj, const string& error);
180

    
181
    /**
182
     *  Logs allocate errors
183
     *    @param message with the allocate error details (parsing)
184
     *    @return string for logging
185
     */
186
    string allocate_error (char *error);
187

    
188
private:
189
    /**
190
     *  Session token from the OpenNebula XML-RPC API
191
     */
192
    string             session;
193

    
194
    /**
195
     *  Return value of the request from libxmlrpc-c
196
     */
197
    xmlrpc_c::value * retval;
198
};
199

    
200
/* -------------------------------------------------------------------------- */
201
/* -------------------------------------------------------------------------- */
202
/* -------------------------------------------------------------------------- */
203

    
204
#endif //REQUEST_H_
205