2012-06-08 15:45:15 +04:00
/* -------------------------------------------------------------------------- */
2015-09-23 16:03:22 +03:00
/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems */
2012-06-08 15:45:15 +04:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef QUOTAS_H_
# define QUOTAS_H_
# include "QuotaDatastore.h"
# include "QuotaNetwork.h"
# include "QuotaVirtualMachine.h"
# include "QuotaImage.h"
2012-06-15 14:28:20 +04:00
class ObjectXML ;
2014-01-22 02:21:18 +04:00
class Quotas
2012-06-08 15:45:15 +04:00
{
public :
2012-06-16 01:33:50 +04:00
/**
* Different quota types
*/
enum QuotaType {
DATASTORE , /**< Checks Datastore usage */
VM , /**< Checks VM usage (MEMORY, CPU and VMS) */
NETWORK , /**< Checks Network usage (leases) */
IMAGE , /**< Checks Image usage (RVMs using it) */
2016-02-01 19:07:04 +03:00
VIRTUALMACHINE , /**< Checks all VM associated resources VM, NETWORK, IMAGE */
VIRTUALROUTER /**< Checks the Virtual Router NETWORK usage (leases) */
2012-06-16 01:33:50 +04:00
} ;
2012-06-08 15:45:15 +04:00
/**
* Set the quotas
* @ param tmpl contains the user quota limits
* @ param error describes error when setting the quotas
*
* @ return 0 on success , - 1 otherwise
*/
2012-11-29 20:26:21 +04:00
int set ( Template * tmpl , string & error ) ;
2012-06-08 15:45:15 +04:00
/**
* Delete usage from quota counters .
* @ param tmpl template for the image , with usage
*/
void ds_del ( Template * tmpl )
{
2012-06-16 01:33:50 +04:00
datastore_quota . del ( tmpl ) ;
2012-06-08 15:45:15 +04:00
}
2012-11-19 18:19:09 +04:00
/**
* Gets a Datastore quota identified by its ID .
*
* @ param id of the quota
* @ param va The quota , if it is found
*
* @ return 0 on success , - 1 if not found
*/
int ds_get ( const string & id , VectorAttribute * * va )
{
return datastore_quota . get_quota ( id , va ) ;
}
/**
* Gets a VM quota identified by its ID .
*
* @ param id of the quota
* @ param va The quota , if it is found
*
* @ return 0 on success , - 1 if not found
*/
int vm_get ( const string & id , VectorAttribute * * va )
{
return vm_quota . get_quota ( id , va ) ;
}
/**
* Gets a Network quota identified by its ID .
*
* @ param id of the quota
* @ param va The quota , if it is found
*
* @ return 0 on success , - 1 if not found
*/
int network_get ( const string & id , VectorAttribute * * va )
{
return network_quota . get_quota ( id , va ) ;
}
/**
* Gets an Image quota identified by its ID .
*
* @ param id of the quota
* @ param va The quota , if it is found
*
* @ return 0 on success , - 1 if not found
*/
int image_get ( const string & id , VectorAttribute * * va )
{
return image_quota . get_quota ( id , va ) ;
}
2012-06-16 01:33:50 +04:00
/**
* Check quota , it updates usage counters if quotas are not exceeded .
* @ param type the quota to work with
* @ param tmpl template for the VirtualMachine
2012-11-19 19:38:39 +04:00
* @ param default_quotas Quotas that contain the default limits
2012-06-16 01:33:50 +04:00
* @ param error_str string describing the error
2013-02-23 22:49:06 +04:00
* @ return true if resource can be allocated , false otherwise
2012-06-16 01:33:50 +04:00
*/
2012-11-19 19:38:39 +04:00
bool quota_check ( QuotaType type ,
Template * tmpl ,
Quotas & default_quotas ,
string & error_str ) ;
2012-06-16 01:33:50 +04:00
2013-07-23 00:08:01 +04:00
/**
* Update usage of an existing quota ( e . g . size of an image ) , it updates
* the usage counters if quotas are not exceeded .
* @ param type the quota to work with
* @ param tmpl template for the VirtualMachine
* @ param default_quotas Quotas that contain the default limits
* @ param error_str string describing the error
* @ return true if resource can be updated , false otherwise
*/
bool quota_update ( QuotaType type ,
Template * tmpl ,
Quotas & default_quotas ,
string & error_str ) ;
2012-06-16 01:33:50 +04:00
/**
* Delete usage from the given quota counters .
* @ param type the quota to work with
* @ param tmpl template for the image , with usage
*/
void quota_del ( QuotaType type , Template * tmpl ) ;
2012-06-08 15:45:15 +04:00
/**
* Generates a string representation of the quotas in XML format
* @ param xml the string to store the XML
* @ return the same xml string to use it in < < compounds
*/
2012-06-15 14:28:20 +04:00
string & to_xml ( string & xml ) const ;
2012-06-08 15:45:15 +04:00
/**
* Builds quota object from an ObjectXML
2012-11-30 02:53:34 +04:00
* @ param object_xml pointer to the ObjectXML
2012-06-08 15:45:15 +04:00
* @ return 0 if success
*/
2012-06-15 14:28:20 +04:00
int from_xml ( ObjectXML * object_xml ) ;
2012-06-08 15:45:15 +04:00
2012-06-15 14:28:20 +04:00
/**
* Delete VM related usage ( network , image and compute ) from quota counters .
* for the given user and group
* @ param uid of the user
* @ param gid of the group
* @ param tmpl template for the image , with usage
*/
2012-06-16 01:33:50 +04:00
static void vm_del ( int uid , int gid , Template * tmpl )
{
quota_del ( VIRTUALMACHINE , uid , gid , tmpl ) ;
}
2012-06-08 15:45:15 +04:00
2012-06-15 14:28:20 +04:00
/**
* Delete Datastore related usage from quota counters .
* for the given user and group
* @ param uid of the user
* @ param gid of the group
* @ param tmpl template for the image , with usage
*/
2012-06-16 01:33:50 +04:00
static void ds_del ( int uid , int gid , Template * tmpl )
{
quota_del ( DATASTORE , uid , gid , tmpl ) ;
}
2015-11-12 15:23:58 +03:00
/**
* Delete a set of Datastore usage attributes from quota counters . Each
* quota datastore is associate to a given image . NOTE : The templates
* * ARE FREED * by this function
* @ param ds_quotas a map with image_id and a tmpl with usage attributes
*/
2015-11-13 00:30:36 +03:00
static void ds_del ( map < int , Template * > & ds_quotas ) ;
2015-11-12 15:23:58 +03:00
2012-06-16 01:33:50 +04:00
/**
* Delete usage from the given quota counters .
* for the given user and group
* @ param type the quota to work with
* @ param uid of the user
* @ param gid of the group
* @ param tmpl template for the image , with usage
*/
static void quota_del ( QuotaType type , int uid , int gid , Template * tmpl ) ;
2012-06-08 15:45:15 +04:00
2012-11-26 15:48:10 +04:00
protected :
2012-11-30 03:16:52 +04:00
/**
* This is an specialized constructor only for derived Quotas classes .
* It allows to set the defaultness attribute
*/
Quotas ( const char * _ds_xpath ,
const char * _net_xpath ,
const char * _img_xpath ,
const char * _vm_xpath ,
bool is_deafult ) :
datastore_quota ( is_deafult ) ,
network_quota ( is_deafult ) ,
image_quota ( is_deafult ) ,
vm_quota ( is_deafult ) ,
ds_xpath ( _ds_xpath ) ,
net_xpath ( _net_xpath ) ,
img_xpath ( _img_xpath ) ,
2014-01-22 02:21:18 +04:00
vm_xpath ( _vm_xpath ) { } ;
2014-01-15 19:27:50 +04:00
2014-01-22 02:21:18 +04:00
virtual ~ Quotas ( ) { } ;
2014-01-15 19:27:50 +04:00
2012-11-30 03:16:52 +04:00
private :
2012-06-08 15:45:15 +04:00
//--------------------------------------------------------------------------
2012-11-30 02:53:34 +04:00
// Usage Counters and Quotas
2012-06-08 15:45:15 +04:00
//--------------------------------------------------------------------------
/**
2012-11-30 02:53:34 +04:00
* Datastore Quotas
*/
2012-06-15 14:28:20 +04:00
QuotaDatastore datastore_quota ;
2012-06-08 15:45:15 +04:00
/**
2012-11-30 02:53:34 +04:00
* Network Quotas
2012-06-08 15:45:15 +04:00
*/
2012-06-15 14:28:20 +04:00
QuotaNetwork network_quota ;
2012-06-08 15:45:15 +04:00
/**
2012-11-30 02:53:34 +04:00
* Image Quotas
*/
2012-06-15 14:28:20 +04:00
QuotaImage image_quota ;
2012-06-08 15:45:15 +04:00
/**
2012-11-30 02:53:34 +04:00
* Virtual Machine Quotas
*/
2012-06-08 15:45:15 +04:00
QuotaVirtualMachine vm_quota ;
//--------------------------------------------------------------------------
// XPaths
//--------------------------------------------------------------------------
/**
* Path for the datastore quota object
*/
const char * ds_xpath ;
/**
* Path for the network quota object
*/
const char * net_xpath ;
/**
* Path for the image quota object
*/
const char * img_xpath ;
/**
* Path for the vm quota object
*/
const char * vm_xpath ;
} ;
# endif /*QUOTABLE_H_*/