1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-08 21:17:43 +03:00

Bug #1363: Check correct value types for VM CPU, VCPU, MEM, and Image SIZE

This commit is contained in:
Carlos Martín 2012-07-17 15:51:50 +02:00
parent c94bfcab5e
commit c27c27ac4f
6 changed files with 92 additions and 22 deletions

View File

@ -307,12 +307,31 @@ public:
* @param name of the attribute
* @param value of the attribute (an int), will be 0 if not defined or
* not a single attribute
*
* @return True if the Single attribute was found and is a valid integer
* value
*/
void get_template_attribute(
bool get_template_attribute(
const char * name,
int& value) const
{
obj_template->get(name,value);
return obj_template->get(name,value);
}
/**
* Gets a float based attribute (single)
* @param name of the attribute
* @param value of the attribute (a float), will be 0 if not defined or
* not a single attribute
*
* @return True if the Single attribute was found and is a valid float
* value
*/
bool get_template_attribute(
const char * name,
float& value) const
{
return obj_template->get(name,value);
}
/**
@ -329,6 +348,23 @@ public:
return obj_template->replace(name, value);
}
/**
* Adds a new attribute to the template (replacing it if
* already defined), the object's mutex SHOULD be locked
* @param name of the new attribute
* @param value of the new attribute
* @return 0 on success
*/
int replace_template_attribute(
const string& name,
const int& value)
{
ostringstream oss;
oss << value;
return replace_template_attribute(name, oss.str());
}
/**
* Generates a XML string for the template of the Object
* @param xml the string to store the XML description.

View File

@ -245,7 +245,8 @@ public:
* @param value the attribute value, an int, 0 if the attribute is not
* defined or not Single
*
* @return True if the Single attribute was found
* @return True if the Single attribute was found and is a valid integer
* value
*/
virtual bool get(
const string& name,
@ -257,7 +258,8 @@ public:
* @param value the attribute value, an int, 0 if the attribute is not
* defined or not Single
*
* @return True if the Single attribute was found
* @return True if the Single attribute was found and is a valid float
* value
*/
virtual bool get(
const string& name,

View File

@ -430,6 +430,13 @@ bool Template::get(
istringstream iss(sval);
iss >> value;
if ( iss.fail() )
{
value = 0;
return false;
}
return true;
}
@ -453,6 +460,13 @@ bool Template::get(
istringstream iss(sval);
iss >> value;
if ( iss.fail() )
{
value = 0;
return false;
}
return true;
}

View File

@ -41,9 +41,9 @@ bool QuotaDatastore::check(Template * tmpl, string& error)
return false;
}
if ( tmpl->get("SIZE", size) == false )
if ( tmpl->get("SIZE", size) == false || size < 0 )
{
error = "Size not defined for image";
error = "Image size must be a positive integer value";
return false;
}

View File

@ -55,14 +55,16 @@ bool QuotaVirtualMachine::check(Template * tmpl, string& error)
int memory;
float cpu;
if ( tmpl->get("MEMORY", memory) == false )
if ( tmpl->get("MEMORY", memory) == false || memory <= 0 )
{
memory = 0;
error = "MEMORY attribute must be a positive integer value";
return false;
}
if ( tmpl->get("CPU", cpu) == false )
if ( tmpl->get("CPU", cpu) == false || cpu <= 0 )
{
cpu = 0;
error = "CPU attribute must be a positive float or integer value";
return false;
}
vm_request.insert(make_pair("VMS",1));

View File

@ -213,6 +213,8 @@ int VirtualMachine::insert(SqlDB * db, string& error_str)
string name;
string value;
int ivalue;
float fvalue;
ostringstream oss;
// ------------------------------------------------------------------------
@ -242,21 +244,31 @@ int VirtualMachine::insert(SqlDB * db, string& error_str)
this->name = name;
// ------------------------------------------------------------------------
// Check for CPU and MEMORY attributes
// Check for CPU, VCPU and MEMORY attributes
// ------------------------------------------------------------------------
get_template_attribute("MEMORY", value);
if ( value.empty())
if ( get_template_attribute("MEMORY", ivalue) == false || ivalue <= 0 )
{
goto error_no_memory;
goto error_memory;
}
get_template_attribute("CPU", value);
replace_template_attribute("MEMORY", ivalue);
if ( value.empty())
if ( get_template_attribute("CPU", fvalue) == false || fvalue <= 0 )
{
goto error_no_cpu;
goto error_cpu;
}
get_template_attribute("VCPU", value);
if ( value.empty() == false )
{
if ( get_template_attribute("VCPU", ivalue) == false || ivalue <= 0 )
{
goto error_vcpu;
}
replace_template_attribute("VCPU", ivalue);
}
// ------------------------------------------------------------------------
@ -339,12 +351,16 @@ error_leases_rollback:
release_network_leases();
goto error_common;
error_no_cpu:
error_str = "CPU attribute missing.";
error_cpu:
error_str = "CPU attribute must be a positive float or integer value.";
goto error_common;
error_no_memory:
error_str = "MEMORY attribute missing.";
error_vcpu:
error_str = "VCPU attribute must be a positive integer value.";
goto error_common;
error_memory:
error_str = "MEMORY attribute must be a positive integer value.";
goto error_common;
error_name_length: