Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (26.8 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2015, 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 "RequestManagerAllocate.h"
18

    
19
#include "Nebula.h"
20
#include "PoolObjectSQL.h"
21
#include "MarketPlacePool.h"
22
#include "MarketPlaceAppPool.h"
23

    
24
/* -------------------------------------------------------------------------- */
25
/* -------------------------------------------------------------------------- */
26

    
27
bool RequestManagerAllocate::allocate_authorization(
28
        Template *          tmpl,
29
        RequestAttributes&  att,
30
        PoolObjectAuth *    cluster_perms)
31
{
32
    if ( att.uid == 0 )
33
    {
34
        return true;
35
    }
36

    
37
    string tmpl_str = "";
38

    
39
    AuthRequest ar(att.uid, att.group_ids);
40

    
41
    if ( tmpl != 0 )
42
    {
43
        tmpl->to_xml(tmpl_str);
44
    }
45

    
46
    ar.add_create_auth(att.uid, att.gid, auth_object, tmpl_str);
47

    
48
    if ( cluster_perms->oid != ClusterPool::NONE_CLUSTER_ID )
49
    {
50
        ar.add_auth(AuthRequest::ADMIN, *cluster_perms); // ADMIN CLUSTER
51
    }
52

    
53
    if (UserPool::authorize(ar) == -1)
54
    {
55
        att.resp_msg = ar.message;
56
        failure_response(AUTHORIZATION, att);
57

    
58
        return false;
59
    }
60

    
61
    return true;
62
}
63

    
64
/* -------------------------------------------------------------------------- */
65
/* -------------------------------------------------------------------------- */
66

    
67
bool VirtualMachineAllocate::allocate_authorization(
68
        Template *          tmpl,
69
        RequestAttributes&  att,
70
        PoolObjectAuth *    cluster_perms)
71
{
72
    if ( att.uid == 0 )
73
    {
74
        return true;
75
    }
76

    
77
    AuthRequest ar(att.uid, att.group_ids);
78
    string      t64;
79
    string      aname;
80

    
81
    VirtualMachineTemplate * ttmpl = static_cast<VirtualMachineTemplate *>(tmpl);
82

    
83
    // ------------ Check template for restricted attributes -------------------
84

    
85
    if ( att.uid != 0 && att.gid != GroupPool::ONEADMIN_ID )
86
    {
87
        if (ttmpl->check(aname))
88
        {
89
            att.resp_msg = "VM Template includes a restricted attribute "+aname;
90
            failure_response(AUTHORIZATION, att);
91

    
92
            return false;
93
        }
94
    }
95

    
96
    // ------------------ Authorize VM create operation ------------------------
97

    
98
    ar.add_create_auth(att.uid, att.gid, auth_object, tmpl->to_xml(t64));
99

    
100
    VirtualMachine::set_auth_request(att.uid, ar, ttmpl);
101

    
102
    if (UserPool::authorize(ar) == -1)
103
    {
104
        att.resp_msg = ar.message;
105
        failure_response(AUTHORIZATION, att);
106

    
107
        return false;
108
    }
109

    
110
    // -------------------------- Check Quotas  ----------------------------
111

    
112
    VirtualMachineTemplate aux_tmpl(*ttmpl);
113

    
114
    VirtualMachine::disk_extended_info(att.uid, &aux_tmpl);
115

    
116
    if ( quota_authorization(&aux_tmpl, Quotas::VIRTUALMACHINE, att) == false )
117
    {
118
        return false;
119
    }
120

    
121
    return true;
122
}
123

    
124
/* -------------------------------------------------------------------------- */
125
/* -------------------------------------------------------------------------- */
126

    
127
void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params,
128
                                             RequestAttributes& att)
129
{
130
    Template * tmpl = 0;
131

    
132
    int    rc, id;
133

    
134
    Cluster *       cluster      = 0;
135
    int             cluster_id   = ClusterPool::NONE_CLUSTER_ID;
136
    string          cluster_name = ClusterPool::NONE_CLUSTER_NAME;
137
    PoolObjectAuth  cluster_perms;
138

    
139
    if ( do_template == true )
140
    {
141
        string str_tmpl  = xmlrpc_c::value_string(params.getString(1));
142

    
143
        tmpl = get_object_template();
144

    
145
        rc   = tmpl->parse_str_or_xml(str_tmpl, att.resp_msg);
146

    
147
        if ( rc != 0 )
148
        {
149
            failure_response(INTERNAL, att);
150
            delete tmpl;
151

    
152
            return;
153
        }
154
    }
155

    
156
    cluster_id = get_cluster_id(params);
157

    
158
    if ( cluster_id != ClusterPool::NONE_CLUSTER_ID )
159
    {
160
        rc = get_info(clpool, cluster_id, PoolObjectSQL::CLUSTER, att,
161
                cluster_perms, cluster_name, true);
162

    
163
        if ( rc != 0 )
164
        {
165
            delete tmpl;
166
            return;
167
        }
168
    }
169
    else
170
    {
171
        cluster_perms.oid = ClusterPool::NONE_CLUSTER_ID;
172
    }
173

    
174
    if ( allocate_authorization(tmpl, att, &cluster_perms) == false )
175
    {
176
        delete tmpl;
177
        return;
178
    }
179

    
180
    rc = pool_allocate(params, tmpl, id, att, cluster_id);
181

    
182
    if ( rc < 0 )
183
    {
184
        failure_response(INTERNAL, att);
185
        return;
186
    }
187

    
188
    if ( cluster_id != ClusterPool::NONE_CLUSTER_ID )
189
    {
190
        cluster = clpool->get(cluster_id, true);
191

    
192
        if ( cluster == 0 )
193
        {
194
            att.resp_obj = PoolObjectSQL::CLUSTER;
195
            att.resp_id  = cluster_id;
196
            failure_response(NO_EXISTS, att);
197
            return;
198
        }
199

    
200
        rc = add_to_cluster(cluster, id, att.resp_msg);
201

    
202
        if ( rc < 0 )
203
        {
204
            string drop_err;
205
            PoolObjectSQL * obj = 0;
206

    
207
            cluster->unlock();
208

    
209
            obj = pool->get(id, true);
210

    
211
            if ( obj != 0 )
212
            {
213
                pool->drop(obj, drop_err);
214
                obj->unlock();
215
            }
216

    
217
            failure_response(INTERNAL, att);
218
            return;
219
        }
220

    
221
        clpool->update(cluster);
222

    
223
        cluster->unlock();
224
    }
225

    
226
    success_response(id, att);
227
}
228

    
229
/* -------------------------------------------------------------------------- */
230
/* -------------------------------------------------------------------------- */
231

    
232
int VirtualMachineAllocate::pool_allocate(
233
        xmlrpc_c::paramList const&  paramList,
234
        Template *                  tmpl,
235
        int&                        id,
236
        RequestAttributes&          att)
237
{
238
    bool on_hold = false;
239

    
240
    if ( paramList.size() > 2 )
241
    {
242
        on_hold = xmlrpc_c::value_boolean(paramList.getBoolean(2));
243
    }
244

    
245
    VirtualMachineTemplate * ttmpl= static_cast<VirtualMachineTemplate *>(tmpl);
246
    VirtualMachinePool * vmpool   = static_cast<VirtualMachinePool *>(pool);
247

    
248
    Template tmpl_back(*tmpl);
249

    
250
    int rc = vmpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
251
            ttmpl, &id, att.resp_msg, on_hold);
252

    
253
    if ( rc < 0 )
254
    {
255
        quota_rollback(&tmpl_back, Quotas::VIRTUALMACHINE, att);
256
    }
257

    
258
    return rc;
259
}
260

    
261

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

    
265
int VirtualNetworkAllocate::pool_allocate(
266
        xmlrpc_c::paramList const&  paramList,
267
        Template *                  tmpl,
268
        int&                        id,
269
        RequestAttributes&          att,
270
        int                         cluster_id)
271
{
272
    VirtualNetworkPool * vpool = static_cast<VirtualNetworkPool *>(pool);
273
    VirtualNetworkTemplate * vtmpl=static_cast<VirtualNetworkTemplate *>(tmpl);
274

    
275
    set<int> cluster_ids;
276

    
277
    if (cluster_id != ClusterPool::NONE_CLUSTER_ID)
278
    {
279
        cluster_ids.insert(cluster_id);
280
    }
281

    
282
    return vpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,-1,
283
            vtmpl, &id, cluster_ids, att.resp_msg);
284
}
285

    
286
/* -------------------------------------------------------------------------- */
287
/* -------------------------------------------------------------------------- */
288

    
289
void ImageAllocate::request_execute(xmlrpc_c::paramList const& params,
290
                                             RequestAttributes& att)
291
{
292
    string size_str;
293

    
294
    long long     size_mb;
295
    istringstream iss;
296
    ostringstream oss;
297

    
298
    string ds_name;
299
    string ds_data;
300

    
301
    bool   ds_persistent_only;
302

    
303
    Datastore::DatastoreType ds_type;
304

    
305
    int    rc, id;
306

    
307
    PoolObjectAuth ds_perms;
308

    
309
    string str_tmpl = xmlrpc_c::value_string(params.getString(1));
310
    int    ds_id    = xmlrpc_c::value_int(params.getInt(2));
311

    
312
    Nebula&  nd  = Nebula::instance();
313

    
314
    DatastorePool * dspool = nd.get_dspool();
315
    ImagePool *     ipool  = static_cast<ImagePool *>(pool);
316
    ImageManager *  imagem = nd.get_imagem();
317

    
318
    MarketPlacePool *     marketpool = nd.get_marketpool();
319
    MarketPlaceAppPool *  apppool    = nd.get_apppool();
320

    
321
    ImageTemplate * tmpl;
322
    Template        img_usage;
323

    
324
    Datastore *     ds;
325
    Image::DiskType ds_disk_type;
326

    
327
    MarketPlaceApp *  app;
328
    MarketPlace *     market;
329
    int               app_id;
330
    int               market_id;
331

    
332
    long long       avail;
333

    
334
    bool ds_check;
335
    bool persistent_attr;
336

    
337
    string extra_data = "";
338

    
339
    // ------------------------- Parse image template --------------------------
340

    
341
    tmpl = new ImageTemplate;
342

    
343
    rc = tmpl->parse_str_or_xml(str_tmpl, att.resp_msg);
344

    
345
    if ( rc != 0 )
346
    {
347
        failure_response(INTERNAL, att);
348

    
349
        delete tmpl;
350
        return;
351
    }
352

    
353
    // ------------------------- Check Datastore exists ------------------------
354

    
355
    if ((ds = dspool->get(ds_id,true)) == 0 )
356
    {
357
        att.resp_id  = ds_id;
358
        att.resp_obj = PoolObjectSQL::DATASTORE;
359
        failure_response(NO_EXISTS, att);
360

    
361
        delete tmpl;
362
        return;
363
    }
364

    
365
    ds_type = ds->get_type();
366

    
367
    if ( ds_type == Datastore::SYSTEM_DS )
368
    {
369
        ds->unlock();
370

    
371
        att.resp_msg = "New images cannot be allocated in a system datastore.";
372
        failure_response(ALLOCATE, att);
373

    
374
        delete tmpl;
375
        return;
376
    }
377

    
378
    ds->get_permissions(ds_perms);
379

    
380
    ds_name            = ds->get_name();
381
    ds_disk_type       = ds->get_disk_type();
382
    ds_check           = ds->get_avail_mb(avail);
383
    ds_persistent_only = ds->is_persistent_only();
384

    
385
    ds->to_xml(ds_data);
386

    
387
    ds->unlock();
388

    
389
    // --------------- Get the SIZE for the Image, (DS driver) -----------------
390

    
391
    if ( tmpl->get("FROM_APP", app_id ) )
392
    {
393
        // This image comes from a MarketPlaceApp. Get the Market info and
394
        // the size.
395
        app = apppool->get(app_id, true);
396

    
397
        if ( app == 0 )
398
        {
399
            att.resp_msg = "Cannot determine image SIZE.";
400
            failure_response(INTERNAL, att);
401

    
402
            delete tmpl;
403
            return;
404
        }
405

    
406
        app->to_template(tmpl);
407

    
408
        size_mb   = app->get_size();
409
        market_id = app->get_market_id();
410

    
411
        app->unlock();
412

    
413
        market = marketpool->get(market_id, true);
414

    
415
        if ( market == 0 )
416
        {
417
            att.resp_msg = "Could not get the appliance's market.";
418
            failure_response(INTERNAL, att);
419

    
420
            delete tmpl;
421
            return;
422
        }
423

    
424
        market->to_xml(extra_data);
425

    
426
        market->unlock();
427

    
428
        oss << size_mb;
429
        size_str = oss.str();
430
    }
431
    else
432
    {
433
        rc = imagem->stat_image(tmpl, ds_data, size_str);
434

    
435
        if ( rc == -1 )
436
        {
437
            att.resp_msg = "Cannot parse image SIZE: " + size_str;
438
            failure_response(INTERNAL, att);
439

    
440
            delete tmpl;
441
            return;
442
        }
443

    
444
        iss.str(size_str);
445
        iss >> size_mb;
446

    
447
        if ( iss.fail() )
448
        {
449
            att.resp_msg = "Cannot parse image SIZE: " + size_str;
450
            failure_response(INTERNAL, att);
451

    
452
            delete tmpl;
453
            return;
454
        }
455
    }
456

    
457
    if (ds_check && (size_mb > avail))
458
    {
459
        att.resp_msg = "Not enough space in datastore";
460
        failure_response(ACTION, att);
461

    
462
        delete tmpl;
463
        return;
464
    }
465

    
466
    tmpl->erase("SIZE");
467
    tmpl->add("SIZE", size_str);
468

    
469
    // ------------- Set authorization request for non-oneadmin's --------------
470

    
471
    img_usage.add("DATASTORE", ds_id);
472
    img_usage.add("SIZE", size_str);
473

    
474
    if ( att.uid != 0 )
475
    {
476
        AuthRequest ar(att.uid, att.group_ids);
477
        string  tmpl_str;
478
        string  aname;
479

    
480
        // ------------ Check template for restricted attributes  --------------
481

    
482
        if ( att.uid != UserPool::ONEADMIN_ID && att.gid != GroupPool::ONEADMIN_ID )
483
        {
484
            if (tmpl->check(aname))
485
            {
486
                att.resp_msg = "Template includes a restricted attribute "+aname;
487
                failure_response(AUTHORIZATION, att);
488

    
489
                delete tmpl;
490
                return;
491
            }
492
        }
493

    
494
        // ------------------ Check permissions and ACLs  ----------------------
495
        tmpl->to_xml(tmpl_str);
496

    
497
        ar.add_create_auth(att.uid, att.gid, auth_object, tmpl_str); // CREATE IMAGE
498

    
499
        ar.add_auth(AuthRequest::USE, ds_perms); // USE DATASTORE
500

    
501
        if (UserPool::authorize(ar) == -1)
502
        {
503
            att.resp_msg = ar.message;
504
            failure_response(AUTHORIZATION, att);
505

    
506
            delete tmpl;
507
            return;
508
        }
509

    
510
        // -------------------------- Check Quotas  ----------------------------
511

    
512
        if ( quota_authorization(&img_usage, Quotas::DATASTORE, att) == false )
513
        {
514
            delete tmpl;
515
            return;
516
        }
517
    }
518

    
519
    // ------------------------- Check persistent only -------------------------
520

    
521
    tmpl->get("PERSISTENT", persistent_attr);
522

    
523
    if ( ds_persistent_only && persistent_attr == false )
524
    {
525
        att.resp_msg = "This Datastore only accepts persistent images.";
526
        failure_response(ALLOCATE, att);
527

    
528
        delete tmpl;
529
        return;
530
    }
531

    
532
    // ------------------------- Allocate --------------------------------------
533

    
534
    rc = ipool->allocate(att.uid,
535
                         att.gid,
536
                         att.uname,
537
                         att.gname,
538
                         att.umask,
539
                         tmpl,
540
                         ds_id,
541
                         ds_name,
542
                         ds_disk_type,
543
                         ds_data,
544
                         ds_type,
545
                         extra_data,
546
                         -1,
547
                         &id,
548
                         att.resp_msg);
549
    if ( rc < 0 )
550
    {
551
        quota_rollback(&img_usage, Quotas::DATASTORE, att);
552

    
553
        failure_response(ALLOCATE, att);
554
        return;
555
    }
556

    
557
    ds = dspool->get(ds_id, true);
558

    
559
    if ( ds != 0 )  // TODO: error otherwise or leave image in ERROR?
560
    {
561
        ds->add_image(id);
562

    
563
        dspool->update(ds);
564

    
565
        ds->unlock();
566
    }
567

    
568
    success_response(id, att);
569
}
570

    
571
/* -------------------------------------------------------------------------- */
572
/* -------------------------------------------------------------------------- */
573

    
574
int TemplateAllocate::pool_allocate(
575
        xmlrpc_c::paramList const&  paramList,
576
        Template *                  tmpl,
577
        int&                        id,
578
        RequestAttributes&          att)
579
{
580
    VMTemplatePool * tpool = static_cast<VMTemplatePool *>(pool);
581

    
582
    VirtualMachineTemplate * ttmpl=static_cast<VirtualMachineTemplate *>(tmpl);
583

    
584
    return tpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
585
        ttmpl, &id, att.resp_msg);
586
}
587

    
588
/* -------------------------------------------------------------------------- */
589
/* -------------------------------------------------------------------------- */
590

    
591
bool TemplateAllocate::allocate_authorization(
592
        Template *          tmpl,
593
        RequestAttributes&  att,
594
        PoolObjectAuth *    cluster_perms)
595
{
596
    if ( att.uid == UserPool::ONEADMIN_ID || att.gid == GroupPool::ONEADMIN_ID )
597
    {
598
        return true;
599
    }
600

    
601
    AuthRequest ar(att.uid, att.group_ids);
602
    string      t64;
603
    string      aname;
604

    
605
    VirtualMachineTemplate * ttmpl = static_cast<VirtualMachineTemplate *>(tmpl);
606

    
607
    // ------------ Check template for restricted attributes -------------------
608
    if (ttmpl->check(aname))
609
    {
610
        att.resp_msg = "VM Template includes a restricted attribute " + aname;
611

    
612
        failure_response(AUTHORIZATION, att);
613
        return false;
614
    }
615

    
616
    return true;
617
}
618

    
619
/* -------------------------------------------------------------------------- */
620
/* -------------------------------------------------------------------------- */
621

    
622
int HostAllocate::pool_allocate(
623
        xmlrpc_c::paramList const&  paramList,
624
        Template *                  tmpl,
625
        int&                        id,
626
        RequestAttributes&          att,
627
        int                         cluster_id)
628
{
629
    string host    = xmlrpc_c::value_string(paramList.getString(1));
630
    string im_mad  = xmlrpc_c::value_string(paramList.getString(2));
631
    string vmm_mad = xmlrpc_c::value_string(paramList.getString(3));
632
    string vnm_mad = xmlrpc_c::value_string(paramList.getString(4));
633

    
634
    HostPool * hpool = static_cast<HostPool *>(pool);
635

    
636
    set<int> cluster_ids;
637

    
638
    if (cluster_id != ClusterPool::NONE_CLUSTER_ID)
639
    {
640
        cluster_ids.insert(cluster_id);
641
    }
642

    
643
    return hpool->allocate(&id, host, im_mad, vmm_mad, vnm_mad,
644
                           cluster_ids, att.resp_msg);
645
}
646

    
647
/* -------------------------------------------------------------------------- */
648
/* -------------------------------------------------------------------------- */
649

    
650
int UserAllocate::pool_allocate(
651
        xmlrpc_c::paramList const&  paramList,
652
        Template *                  tmpl,
653
        int&                        id,
654
        RequestAttributes&          att)
655
{
656
    string uname  = xmlrpc_c::value_string(paramList.getString(1));
657
    string passwd = xmlrpc_c::value_string(paramList.getString(2));
658
    string driver = xmlrpc_c::value_string(paramList.getString(3));
659

    
660
    UserPool * upool = static_cast<UserPool *>(pool);
661

    
662
    int      ugid   = att.gid;
663
    string   ugname = att.gname;
664

    
665
    if ( att.gid == GroupPool::ONEADMIN_ID )
666
    {
667
        ugid   = GroupPool::USERS_ID;
668
        ugname = GroupPool::USERS_NAME;
669
    }
670

    
671
    if (driver.empty())
672
    {
673
        driver = UserPool::CORE_AUTH;
674
    }
675

    
676
    return upool->allocate(&id,ugid,uname,ugname,passwd,driver,true,att.resp_msg);
677
}
678

    
679
/* -------------------------------------------------------------------------- */
680
/* -------------------------------------------------------------------------- */
681

    
682
int GroupAllocate::pool_allocate(
683
        xmlrpc_c::paramList const&  paramList,
684
        Template *                  tmpl,
685
        int&                        id,
686
        RequestAttributes&          att)
687
{
688
    int rc;
689

    
690
    string gname = xmlrpc_c::value_string(paramList.getString(1));
691

    
692
    GroupPool * gpool = static_cast<GroupPool *>(pool);
693

    
694
    rc = gpool->allocate(gname, &id, att.resp_msg);
695

    
696
    if (rc == -1)
697
    {
698
        return rc;
699
    }
700

    
701
    Vdc* vdc = vdcpool->get(VdcPool::DEFAULT_ID, true);
702

    
703
    if (vdc != 0)
704
    {
705
        rc = vdc->add_group(id, att.resp_msg);
706

    
707
        vdcpool->update(vdc);
708

    
709
        vdc->unlock();
710
    }
711

    
712
    return rc;
713
}
714

    
715
/* -------------------------------------------------------------------------- */
716
/* -------------------------------------------------------------------------- */
717

    
718
int DatastoreAllocate::pool_allocate(
719
        xmlrpc_c::paramList const&  paramList,
720
        Template *                  tmpl,
721
        int&                        id,
722
        RequestAttributes&          att,
723
        int                         cluster_id)
724
{
725
    DatastorePool * dspool      = static_cast<DatastorePool *>(pool);
726
    DatastoreTemplate * ds_tmpl = static_cast<DatastoreTemplate *>(tmpl);
727

    
728
    set<int> cluster_ids;
729

    
730
    if (cluster_id != ClusterPool::NONE_CLUSTER_ID)
731
    {
732
        cluster_ids.insert(cluster_id);
733
    }
734

    
735
    return dspool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
736
            ds_tmpl, &id, cluster_ids, att.resp_msg);
737
}
738

    
739
/* -------------------------------------------------------------------------- */
740
/* -------------------------------------------------------------------------- */
741

    
742
int ClusterAllocate::pool_allocate(
743
        xmlrpc_c::paramList const&  paramList,
744
        Template *                  tmpl,
745
        int&                        id,
746
        RequestAttributes&          att)
747
{
748
    string name = xmlrpc_c::value_string(paramList.getString(1));
749

    
750
    ClusterPool * clpool = static_cast<ClusterPool *>(pool);
751

    
752
    return clpool->allocate(name, &id, att.resp_msg);
753
}
754

    
755
/* -------------------------------------------------------------------------- */
756
/* -------------------------------------------------------------------------- */
757

    
758
int DocumentAllocate::pool_allocate(
759
        xmlrpc_c::paramList const&  paramList,
760
        Template *                  tmpl,
761
        int&                        id,
762
        RequestAttributes&          att)
763
{
764
    int type = xmlrpc_c::value_int(paramList.getInt(2));
765

    
766
    DocumentPool * docpool = static_cast<DocumentPool *>(pool);
767

    
768
    return docpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
769
            type, tmpl, &id, att.resp_msg);
770
}
771

    
772
/* -------------------------------------------------------------------------- */
773
/* -------------------------------------------------------------------------- */
774

    
775
void ZoneAllocate::request_execute(xmlrpc_c::paramList const& params,
776
                                             RequestAttributes& att)
777
{
778
    if(!Nebula::instance().is_federation_master())
779
    {
780
        att.resp_msg = "New zones can only be created at federation master";
781
        failure_response(ALLOCATE, att);
782
        return;
783
    }
784

    
785
    RequestManagerAllocate::request_execute(params, att);
786
}
787

    
788
/* -------------------------------------------------------------------------- */
789
/* -------------------------------------------------------------------------- */
790

    
791
int ZoneAllocate::pool_allocate(
792
        xmlrpc_c::paramList const&  paramList,
793
        Template *                  tmpl,
794
        int&                        id,
795
        RequestAttributes&          att)
796
{
797
    string name = xmlrpc_c::value_string(paramList.getString(1));
798

    
799
    ZonePool * zonepool = static_cast<ZonePool *>(pool);
800

    
801
    return zonepool->allocate(tmpl, &id, att.resp_msg);
802
}
803

    
804
/* -------------------------------------------------------------------------- */
805
/* -------------------------------------------------------------------------- */
806

    
807
int SecurityGroupAllocate::pool_allocate(
808
        xmlrpc_c::paramList const&  paramList,
809
        Template *                  tmpl,
810
        int&                        id,
811
        RequestAttributes&          att)
812
{
813
    SecurityGroupPool * sgpool = static_cast<SecurityGroupPool *>(pool);
814

    
815
    return sgpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
816
        tmpl, &id, att.resp_msg);
817
}
818

    
819
/* -------------------------------------------------------------------------- */
820
/* -------------------------------------------------------------------------- */
821

    
822
int VdcAllocate::pool_allocate(
823
        xmlrpc_c::paramList const&  paramList,
824
        Template *                  tmpl,
825
        int&                        id,
826
        RequestAttributes&          att)
827
{
828
    string name = xmlrpc_c::value_string(paramList.getString(1));
829

    
830
    VdcPool * vdcpool = static_cast<VdcPool *>(pool);
831

    
832
    return vdcpool->allocate(tmpl, &id, att.resp_msg);
833
}
834

    
835
/* -------------------------------------------------------------------------- */
836
/* -------------------------------------------------------------------------- */
837

    
838
int VirtualRouterAllocate::pool_allocate(
839
        xmlrpc_c::paramList const&  paramList,
840
        Template *                  tmpl,
841
        int&                        id,
842
        RequestAttributes&          att)
843
{
844
    VirtualRouterPool * vrpool = static_cast<VirtualRouterPool *>(pool);
845

    
846
    return vrpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
847
        tmpl, &id, att.resp_msg);
848
}
849

    
850
/* -------------------------------------------------------------------------- */
851
/* -------------------------------------------------------------------------- */
852

    
853
bool VirtualRouterAllocate::allocate_authorization(
854
        Template *          tmpl,
855
        RequestAttributes&  att,
856
        PoolObjectAuth *    cluster_perms)
857
{
858
    if ( att.uid == 0 )
859
    {
860
        return true;
861
    }
862

    
863
    AuthRequest ar(att.uid, att.group_ids);
864
    string      tmpl_str;
865

    
866
    // ------------------ Authorize create operation ------------------------
867

    
868
    ar.add_create_auth(att.uid, att.gid, auth_object, tmpl->to_xml(tmpl_str));
869

    
870
    VirtualRouter::set_auth_request(att.uid, ar, tmpl);
871

    
872
    if (UserPool::authorize(ar) == -1)
873
    {
874
        att.resp_msg = ar.message;
875
        failure_response(AUTHORIZATION, att);
876

    
877
        return false;
878
    }
879

    
880
    // -------------------------- Check Quotas  ----------------------------
881

    
882
    if (quota_authorization(tmpl, Quotas::VIRTUALROUTER, att, att.resp_msg) == false)
883
    {
884
        return AUTHORIZATION;
885
    }
886

    
887
    return true;
888
}
889

    
890
/* -------------------------------------------------------------------------- */
891
/* -------------------------------------------------------------------------- */
892

    
893
int MarketPlaceAllocate::pool_allocate(
894
        xmlrpc_c::paramList const&  paramList,
895
        Template *                  tmpl,
896
        int&                        id,
897
        RequestAttributes&          att)
898
{
899
    MarketPlacePool *     mppool = static_cast<MarketPlacePool *>(pool);
900
    MarketPlaceTemplate * ttmpl  = static_cast<MarketPlaceTemplate *>(tmpl);
901

    
902
    return mppool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
903
        ttmpl, &id, att.resp_msg);
904
}
905

    
906
/* -------------------------------------------------------------------------- */
907
/* -------------------------------------------------------------------------- */
908

    
909
int MarketPlaceAppAllocate::pool_allocate(
910
        xmlrpc_c::paramList const&  paramList,
911
        Template *                  tmpl,
912
        int&                        id,
913
        RequestAttributes&          att)
914
{
915
    MarketPlaceAppPool *     appool = static_cast<MarketPlaceAppPool *>(pool);
916
    MarketPlaceAppTemplate * ttmpl  = static_cast<MarketPlaceAppTemplate *>(tmpl);
917

    
918
    int mp_id = xmlrpc_c::value_int(paramList.getInt(2));
919

    
920
    MarketPlace * mp = mppool->get(mp_id, true);
921

    
922
    if ( mp == 0 )
923
    {
924
        att.resp_msg = "Cannot find associated MARKETPLACE";
925
        return -1;
926
    }
927

    
928
    std::string mp_name = mp->get_name();
929
    std::string mp_data;
930

    
931
    if ( !mp->is_action_supported(MarketPlaceApp::CREATE) )
932
    {
933
        att.resp_msg = "Create disabled for market: " + mp_name;
934
        mp->unlock();
935

    
936
        return -1;
937
    }
938

    
939
    mp->to_xml(mp_data);
940

    
941
    mp->unlock();
942

    
943
    int rc = appool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
944
        ttmpl, mp_id, mp_name, mp_data, &id, att.resp_msg);
945

    
946
        if (rc < 0)
947
        {
948
                return rc;
949
        }
950

    
951
    mp = mppool->get(mp_id, true);
952

    
953
    if ( mp != 0 )  // TODO: error otherwise or leave app in ERROR?
954
    {
955
        mp->add_marketapp(id);
956

    
957
        mppool->update(mp);
958

    
959
        mp->unlock();
960
    }
961

    
962
    return rc;
963
}