Statistics
| Branch: | Tag: | Revision:

one / src / market / MarketPlaceAppPool.cc @ fed93cda

History | View | Annotate | Download (9.14 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                */
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 "MarketPlaceAppPool.h"
18
#include "Nebula.h"
19
#include "Client.h"
20

    
21
/* -------------------------------------------------------------------------- */
22
/* -------------------------------------------------------------------------- */
23

    
24
static int master_allocate(MarketPlaceApp * mp, string& error)
25
{
26
    Client * client = Client::client();
27

    
28
    xmlrpc_c::value         result;
29
    vector<xmlrpc_c::value> values;
30

    
31
    std::string        mp_xml;
32
    std::ostringstream oss("Cannot allocate marketapp at federation master: ",
33
            std::ios::ate);
34

    
35
    mp->to_xml(mp_xml);
36

    
37
    try
38
    {
39
        client->call("one.marketapp.allocatedb", "s", &result, mp_xml.c_str());
40
    }
41
    catch (exception const& e)
42
    {
43
        oss << e.what();
44
        error = oss.str();
45

    
46
        return -1;
47
    }
48

    
49
    values = xmlrpc_c::value_array(result).vectorValueValue();
50

    
51
    if ( xmlrpc_c::value_boolean(values[0]) == false )
52
    {
53
        std::string error_xml = xmlrpc_c::value_string(values[1]);
54

    
55
        oss << error_xml;
56
        error = oss.str();
57

    
58
        return -1;
59
    }
60

    
61
    int oid = xmlrpc_c::value_int(values[1]);
62

    
63
    return oid;
64
}
65

    
66
/* -------------------------------------------------------------------------- */
67

    
68
int MarketPlaceAppPool:: allocate(
69
            int                uid,
70
            int                gid,
71
            const std::string& uname,
72
            const std::string& gname,
73
            int                umask,
74
            MarketPlaceAppTemplate * apptemplate,
75
            int                mp_id,
76
            const std::string& mp_name,
77
            int *              oid,
78
            std::string&       error_str)
79
{
80
    MarketPlaceApp * mp;
81
    MarketPlaceApp * mp_aux = 0;
82

    
83
    std::string name;
84

    
85
    std::ostringstream oss;
86

    
87
    // -------------------------------------------------------------------------
88
    // Build the marketplace app object
89
    // -------------------------------------------------------------------------
90
    mp = new MarketPlaceApp(uid, gid, uname, gname, umask, apptemplate);
91

    
92
    mp->market_id   = mp_id;
93
    mp->market_name = mp_name;
94
    mp->zone_id     = Nebula::instance().get_zone_id();
95

    
96
    mp->state = MarketPlaceApp::INIT;
97

    
98
    mp->get_template_attribute("NAME", name);
99

    
100
    if ( !PoolObjectSQL::name_is_valid(name, error_str) )
101
    {
102
        goto error_name;
103
    }
104

    
105
    mp_aux = get(name, uid, false);
106

    
107
    if( mp_aux != 0 )
108
    {
109
        goto error_duplicated;
110
    }
111

    
112
    if ( mp->parse_template(error_str) != 0 )
113
    {
114
        goto error_template;
115
    }
116

    
117
    // -------------------------------------------------------------------------
118
    // Insert the object in the Database
119
    // -------------------------------------------------------------------------
120
    if (Nebula::instance().is_federation_slave())
121
    {
122
        *oid = master_allocate(mp, error_str);
123

    
124
        delete mp;
125

    
126
        return *oid;
127
    }
128

    
129
    *oid = PoolSQL::allocate(mp, error_str);
130

    
131
    // ------------------------------------------------------------------------
132
    // Insert id into map_check
133
    // --------------------------------------------------------------------------
134
    insert_map_check(name);
135

    
136
    return *oid;
137

    
138
error_duplicated:
139
    oss << "NAME is already taken by MARKETPLACEAPP " << mp_aux->get_oid();
140
    error_str = oss.str();
141

    
142
error_name:
143
error_template:
144
    delete mp;
145
    *oid = -1;
146

    
147
    return *oid;
148
}
149

    
150
/* -------------------------------------------------------------------------- */
151
/* -------------------------------------------------------------------------- */
152

    
153
int MarketPlaceAppPool::drop(PoolObjectSQL * objsql, std::string& error_msg)
154
{
155
    if (Nebula::instance().is_federation_slave())
156
    {
157
        Client * client = Client::client();
158

    
159
        xmlrpc_c::value result;
160
        vector<xmlrpc_c::value> values;
161

    
162
        std::ostringstream oss("Cannot drop marketapp at federation master: ",
163
                std::ios::ate);
164
        try
165
        {
166
            client->call("one.marketapp.dropdb", "i", &result, objsql->get_oid());
167
        }
168
        catch (exception const& e)
169
        {
170
            oss << e.what();
171

    
172
            error_msg = oss.str();
173
            return -1;
174
        }
175

    
176
        values = xmlrpc_c::value_array(result).vectorValueValue();
177

    
178
        if ( xmlrpc_c::value_boolean(values[0]) == false )
179
        {
180
            std::string error = xmlrpc_c::value_string(values[1]);
181

    
182
            oss << error;
183

    
184
            error_msg = oss.str();
185
            return -1;
186
        }
187

    
188
        return 0;
189
    }
190

    
191
    drop_map_check(objsql->get_name());
192

    
193
    return PoolSQL::drop(objsql, error_msg);
194
}
195

    
196
/* -------------------------------------------------------------------------- */
197
/* -------------------------------------------------------------------------- */
198

    
199
int MarketPlaceAppPool::import(const std::string& t64, int mp_id,
200
        const std::string& mp_name, std::string& error_str)
201
{
202
    // -------------------------------------------------------------------------
203
    // Build the marketplace app object
204
    // -------------------------------------------------------------------------
205
    MarketPlaceApp * app = new MarketPlaceApp(UserPool::ONEADMIN_ID,
206
        GroupPool::ONEADMIN_ID, UserPool::oneadmin_name, GroupPool::ONEADMIN_NAME
207
        ,0133, 0);
208

    
209
    if ( app->from_template64(t64, error_str) != 0 )
210
    {
211
        app->lock();
212
        delete app;
213

    
214
        return -1;
215
    }
216

    
217
    app->market_id   = mp_id;
218
    app->market_name = mp_name;
219
        app->zone_id     = Nebula::instance().get_zone_id();
220

    
221
    if ( !PoolObjectSQL::name_is_valid(app->name, error_str) )
222
    {
223
        std::ostringstream oss;
224

    
225
        oss << "imported-" << app->get_origin_id();
226
        app->name = oss.str();
227

    
228
        if ( !PoolObjectSQL::name_is_valid(app->name, error_str) )
229
        {
230
            error_str = "Cannot generate a valida name for app";
231
            return -1;
232
        }
233
    }
234

    
235
    MarketPlaceApp * mp_aux = get(app->name, 0, true);
236

    
237
    if( mp_aux != 0 ) //Marketplace app already imported
238
    {
239
        reset_map_check(app->name);
240
        if ( mp_aux->version != app->version || mp_aux->md5 != app->md5 )
241
        {
242
            mp_aux->from_template64(t64, error_str);
243
            update(mp_aux);
244
        }
245

    
246
        mp_aux->unlock();
247

    
248
        app->lock();
249
        delete app;
250

    
251
        return -2;
252
    }
253

    
254
    // -------------------------------------------------------------------------
255
    // Insert the object in the Database
256
    // -------------------------------------------------------------------------
257
    if (Nebula::instance().is_federation_slave())
258
    {
259
        int oid = master_allocate(app, error_str);
260

    
261
        app->lock();
262
        delete app;
263

    
264
        return oid;
265
    }
266
    insert_map_check(app->name);
267
    return PoolSQL::allocate(app, error_str);
268
}
269

    
270
/* -------------------------------------------------------------------------- */
271
/* -------------------------------------------------------------------------- */
272

    
273
int MarketPlaceAppPool::update(PoolObjectSQL * objsql)
274
{
275
    if (Nebula::instance().is_federation_slave())
276
    {
277
        std::string tmpl_xml;
278
        Client * client = Client::client();
279

    
280
        xmlrpc_c::value result;
281
        vector<xmlrpc_c::value> values;
282

    
283
        std::ostringstream oss("Cannot update marketapp at federation master: ",
284
                std::ios::ate);
285

    
286
        try
287
        {
288
            client->call("one.marketapp.updatedb", "is", &result,
289
                objsql->get_oid(), objsql->to_xml(tmpl_xml).c_str());
290
        }
291
        catch (exception const& e)
292
        {
293
            oss << e.what();
294
            NebulaLog::log("MKP", Log::ERROR, oss);
295

    
296
            return -1;
297
        }
298

    
299
        values = xmlrpc_c::value_array(result).vectorValueValue();
300

    
301
        if ( xmlrpc_c::value_boolean(values[0]) == false )
302
        {
303
            std::string error = xmlrpc_c::value_string(values[1]);
304

    
305
            oss << error;
306
            NebulaLog::log("MKP", Log::ERROR, oss);
307

    
308
            return -1;
309
        }
310

    
311
        return 0;
312
    }
313

    
314
    return PoolSQL::update(objsql);
315
}
316

    
317
/* -------------------------------------------------------------------------- */
318
/* -------------------------------------------------------------------------- */
319