Revision cad2422f

View differences:

include/DefaultQuotas.h
1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2012, 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 DEFAULT_QUOTAS_H_
18
#define DEFAULT_QUOTAS_H_
19

  
20
#include "Quotas.h"
21

  
22
class DefaultQuotas : public Quotas
23
{
24
public:
25
    DefaultQuotas(const char * _ds_xpath,
26
           const char * _net_xpath,
27
           const char * _img_xpath,
28
           const char * _vm_xpath): 
29
               Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath)
30
    {};
31

  
32
    ~DefaultQuotas(){};
33

  
34
    /**
35
     *  Set the quotas
36
     *    @param tmpl contains the user quota limits
37
     *    @param error describes error when setting the quotas
38
     *
39
     *    @return 0 on success, -1 otherwise
40
     */
41
    int set(Template *tmpl, string& error)
42
    {
43
        return Quotas::set(tmpl, false, error);
44
    };
45

  
46
private:
47
};
48

  
49
#endif /*DEFAULT_QUOTAS_H_*/
include/Nebula.h
42 42
#include "AclManager.h"
43 43
#include "ImageManager.h"
44 44

  
45
#include "Quotas.h"
45
#include "DefaultQuotas.h"
46 46

  
47 47
#include "Callbackable.h"
48 48

  
......
527 527
    // Default quotas
528 528
    // ---------------------------------------------------------------
529 529

  
530
    Quotas default_user_quota;
531
    Quotas default_group_quota;
530
    DefaultQuotas default_user_quota;
531
    DefaultQuotas default_group_quota;
532 532

  
533 533

  
534 534
    // ---------------------------------------------------------------
include/Quota.h
35 35
    /**
36 36
     *  Set the quotas. If the quota previously exists its limit is updated.
37 37
     *    @param quota_str the quota template in ASCII or XML formats
38
     *    @param default_allowed whether or not the limit -1 is allowed
38 39
     *    @param error describe the error in case of error
40
     *
39 41
     *    @return 0 on success -1 otherwise
40 42
     */
41
    int set(vector<Attribute*> * quotas, string& error);
43
    int set(vector<Attribute*> * quotas, bool default_allowed, string& error);
42 44

  
43 45
    /**
44 46
     *  Check if the resource allocation will exceed the quota limits. If not 
......
178 180
     *  Creates an empty quota based on the given attribute. The attribute va
179 181
     *  contains the limits for the quota.
180 182
     *    @param va limits for the new quota if 0 limits will be 0
183
     *    @param default_allowed whether or not the limit -1 is allowed
184
     *
181 185
     *    @return a new attribute representing the quota
182 186
     */
183
    VectorAttribute * new_quota(VectorAttribute* va);
187
    VectorAttribute * new_quota(VectorAttribute* va, bool default_allowed);
184 188

  
185 189
    /**
186 190
     *  Adds a new quota, it also updates an internal index for fast accessing
......
204 208
     *  Sets new limit values for the quota
205 209
     *    @param quota to be updated
206 210
     *    @param va attribute with the new limits
211
     *    @param default_allowed whether or not the limit -1 is allowed
212
     *
207 213
     *    @return 0 on success or -1 if wrong limits
208 214
     */
209
    int update_limits(VectorAttribute* quota, const VectorAttribute* va);
215
    int update_limits(VectorAttribute* quota,
216
            const VectorAttribute* va,
217
            bool default_allowed);
210 218

  
211 219
    /**
212 220
     *  Extract the limits for the defined quota metrics from a given attribute
include/Quotas.h
37 37
                vm_xpath(_vm_xpath)
38 38
    {};
39 39

  
40
    virtual ~Quotas(){};
40
    ~Quotas(){};
41 41

  
42 42
    /**
43 43
     *  Different quota types
......
57 57
     *
58 58
     *    @return 0 on success, -1 otherwise
59 59
     */
60
    int set(Template *tmpl, string& error);
60
    virtual int set(Template *tmpl, string& error)
61
    {
62
        return set(tmpl, true, error);
63
    };
61 64

  
62 65
    /**
63 66
     *  Delete usage from quota counters.
......
199 202
     */
200 203
    static void quota_del(QuotaType type, int uid, int gid, Template * tmpl);
201 204

  
205
protected:
206
    /**
207
     *  Set the quotas
208
     *    @param tmpl contains the user quota limits
209
     *    @param default_allowed whether or not the limit -1 is allowed
210
     *    @param error describes error when setting the quotas
211
     *
212
     *    @return 0 on success, -1 otherwise
213
     */
214
    int set(Template *tmpl, bool default_allowed, string& error);
215

  
202 216
private:
203 217
    //--------------------------------------------------------------------------
204 218
    // Usage Counters and Quotas 
src/um/Quota.cc
80 80
/* -------------------------------------------------------------------------- */
81 81
/* -------------------------------------------------------------------------- */
82 82

  
83
int Quota::set(vector<Attribute*> * new_quotas, string& error)
83
int Quota::set(vector<Attribute*> * new_quotas, bool default_allowed, string& error)
84 84
{
85 85
    vector<Attribute *>::iterator  it;
86 86

  
......
108 108
        {
109 109
            VectorAttribute * nq;
110 110

  
111
            if ((nq = new_quota(iq)) == 0)
111
            if ((nq = new_quota(iq, default_allowed)) == 0)
112 112
            {
113 113
                goto error_limits;
114 114
            }
......
117 117
        }
118 118
        else
119 119
        {
120
            if (update_limits(tq, iq) != 0)
120
            if (update_limits(tq, iq, default_allowed) != 0)
121 121
            {
122 122
                goto error_limits;
123 123
            } 
......
368 368
/* -------------------------------------------------------------------------- */
369 369
/* -------------------------------------------------------------------------- */
370 370

  
371
int Quota::update_limits(VectorAttribute * quota, const VectorAttribute * va)
372
{        
371
int Quota::update_limits(
372
        VectorAttribute *       quota,
373
        const VectorAttribute * va,
374
        bool                    default_allowed)
375
{
373 376
    string limit;
374 377
    float  limit_i;
375 378

  
......
377 380
    {
378 381
        limit = va->vector_value_str(metrics[i], limit_i);
379 382

  
380
        //No quota, NaN or negative. -1 is allowed, it means default limit
381
        if ( limit_i < -1 ||
382
            ( limit_i == -1 && limit == "" ))
383
        //No quota, NaN or negative
384
        if ((default_allowed &&
385
                ( limit_i < -1 || ( limit_i == -1 && limit == "" ))) ||
386
            (!default_allowed && limit_i < 0) )
383 387
        {
384 388
            return -1;
385 389
        }
......
395 399
/* -------------------------------------------------------------------------- */
396 400
/* -------------------------------------------------------------------------- */
397 401

  
398
VectorAttribute * Quota::new_quota(VectorAttribute * va)
402
VectorAttribute * Quota::new_quota(VectorAttribute * va, bool default_allowed)
399 403
{
400 404
    map<string,string> limits;
401 405

  
......
409 413
        metrics_used += "_USED";
410 414

  
411 415
        limit = va->vector_value_str(metrics[i], limit_i);
412
        
413
        if ( limit_i < 0 ) //No quota, NaN or negative
416

  
417
        //No quota, NaN or negative
418
        if ( (default_allowed && limit_i < -1) ||
419
             (!default_allowed && limit_i < 0) )
414 420
        {
415 421
            limit = "0";
416 422
        }
src/um/Quotas.cc
19 19

  
20 20
#include "ObjectXML.h"
21 21

  
22

  
23
int Quotas::set(Template *tmpl, string& error)
22
int Quotas::set(Template *tmpl, bool default_allowed, string& error)
24 23
{
25 24
    vector<Attribute *> vquotas;
26 25

  
27 26
    if ( tmpl->get(datastore_quota.get_quota_name(), vquotas) > 0 )
28 27
    {
29
        if ( datastore_quota.set(&vquotas, error) != 0 )
28
        if ( datastore_quota.set(&vquotas, default_allowed, error) != 0 )
30 29
        {
31 30
            return -1;
32 31
        }
......
36 35

  
37 36
    if ( tmpl->get(network_quota.get_quota_name(), vquotas) > 0 )
38 37
    {
39
        if ( network_quota.set(&vquotas, error) != 0 )
38
        if ( network_quota.set(&vquotas, default_allowed, error) != 0 )
40 39
        {
41 40
            return -1;
42 41
        }
......
46 45

  
47 46
    if ( tmpl->get(image_quota.get_quota_name(), vquotas) > 0 )
48 47
    {
49
        if ( image_quota.set(&vquotas, error) != 0 )
48
        if ( image_quota.set(&vquotas, default_allowed, error) != 0 )
50 49
        {
51 50
            return -1;
52 51
        }
......
56 55

  
57 56
    if ( tmpl->get(vm_quota.get_quota_name(), vquotas) > 0 )
58 57
    {
59
        if ( vm_quota.set(&vquotas, error) != 0 )
58
        if ( vm_quota.set(&vquotas, default_allowed, error) != 0 )
60 59
        {
61 60
            return -1;
62 61
        }

Also available in: Unified diff