1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-22 13:33:52 +03:00

Feature #407: Image uses new DB blob schema

This commit is contained in:
Carlos Martín 2011-02-25 19:02:29 +01:00
parent f1157f524b
commit d3828b30da
7 changed files with 147 additions and 289 deletions

View File

@ -59,13 +59,6 @@ public:
// Image Public Methods
// *************************************************************************
/**
* Function to print the Image object into a string in plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the Image object into a string in XML format
* @param xml the resulting XML string
@ -73,6 +66,14 @@ public:
*/
string& to_xml(string& xml) const;
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str);
/**
* Get the Image unique identifier IID, that matches the OID of the object
* @return IID Image identifier
@ -356,10 +357,15 @@ private:
// -------------------------------------------------------------------------
/**
* Owner if the image
* Owner of the image
*/
int uid;
/**
* Image owner's name
*/
string user_name;
/**
* The name of the Image
*/
@ -422,15 +428,6 @@ private:
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a Image object (Image::select)
* @param num the number of columns read from the DB
* @param names the column names
* @param values the column values
* @return 0 on success
*/
int select_cb(void *nil, int num, char **values, char **names);
/**
* Bootstraps the database table(s) associated to the Image
*/
@ -455,7 +452,7 @@ protected:
// Constructor
// *************************************************************************
Image(int uid=-1, ImageTemplate *img_template = 0);
Image(int uid, string _user_name, ImageTemplate *img_template);
virtual ~Image();
@ -463,37 +460,12 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0, /* Image identifier (IID) */
UID = 1, /* Image owner id */
NAME = 2, /* Image name */
TYPE = 3, /* 0) OS 1) CDROM 2) DATABLOCK */
PUBLIC = 4, /* Public scope (YES OR NO) */
PERSISTENT = 5, /* Peristency (YES OR NO) */
REGTIME = 6, /* Time of registration */
SOURCE = 7, /* Path to the image */
STATE = 8, /* 0) INIT 1) ALLOCATED */
RUNNING_VMS = 9, /* Number of VMs using the img */
TEMPLATE = 10, /* Image template xml data */
LIMIT = 11
};
static const char * extended_db_names;
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Reads the Image (identified with its OID=IID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db);
/**
* Writes the Image in the database.
* @param db pointer to the db
@ -507,23 +479,6 @@ protected:
* @return 0 on success
*/
virtual int update(SqlDB *db);
/**
* Drops Image and associated template from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output an Image object in to an stream in XML format
* @param oss the output stream
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
static int dump(ostringstream& oss, int num, char **values, char **names);
};
#endif /*IMAGE_H_*/

View File

@ -56,6 +56,7 @@ public:
*/
int allocate (
int uid,
string user_name,
ImageTemplate * img_template,
int * oid,
string& error_str);
@ -206,7 +207,7 @@ private:
*/
PoolObjectSQL * create()
{
return new Image;
return new Image(-1, "", 0);
};
/**

View File

@ -243,12 +243,21 @@ int Host::from_xml(const string& xml)
xpath(cluster, "/HOST/CLUSTER", "not_found");
ObjectXML::get_nodes("/HOST/HOST_SHARE", content);
if( content.size() < 1 )
{
return -1;
}
host_share.from_xml_node( content[0] );
content.clear();
ObjectXML::get_nodes("/HOST/TEMPLATE", content);
if( content.size() < 1 )
{
return -1;
}
host_template.from_xml_node( content[0] );
// TODO: check for errors (missing mandatory elements)
return 0;
}

View File

@ -32,9 +32,10 @@
/* Image :: Constructor/Destructor */
/* ************************************************************************ */
Image::Image(int _uid, ImageTemplate * _image_template):
PoolObjectSQL(-1),
Image::Image(int _uid, string _user_name, ImageTemplate * _image_template):
PoolObjectSQL(-1,table),
uid(_uid),
user_name(_user_name),
name(""),
type(OS),
regtime(time(0)),
@ -66,87 +67,11 @@ Image::~Image()
const char * Image::table = "image_pool";
const char * Image::db_names = "oid, uid, name, type, public, persistent, regtime, "
"source, state, running_vms, template";
const char * Image::extended_db_names = "image_pool.oid, image_pool.uid, "
"image_pool.name, image_pool.type, image_pool.public, "
"image_pool.persistent, image_pool.regtime, image_pool.source, "
"image_pool.state, image_pool.running_vms, image_pool.template";
const char * Image::db_names = "oid, name, body, uid, public";
const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool ("
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(128), "
"type INTEGER, public INTEGER, persistent INTEGER, regtime INTEGER, source TEXT, state INTEGER, "
"running_vms INTEGER, template TEXT, UNIQUE(name) )";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::select_cb(void * nil, int num, char **values, char ** names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[PUBLIC]) ||
(!values[PERSISTENT]) ||
(!values[REGTIME]) ||
(!values[SOURCE]) ||
(!values[STATE]) ||
(!values[RUNNING_VMS]) ||
(!values[TEMPLATE]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
type = static_cast<ImageType>(atoi(values[TYPE]));
public_img = atoi(values[PUBLIC]);
persistent_img = atoi(values[PERSISTENT]);
regtime = static_cast<time_t>(atoi(values[REGTIME]));
source = values[SOURCE];
state = static_cast<ImageState>(atoi(values[STATE]));
running_vms = atoi(values[RUNNING_VMS]);
image_template->from_xml(values[TEMPLATE]);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(static_cast<Callbackable::Callback>(&Image::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
}
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, "
"public INTEGER, UNIQUE(name) )";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
@ -296,11 +221,10 @@ int Image::insert_replace(SqlDB *db, bool replace)
int rc;
string xml_template;
string xml_body;
char * sql_name;
char * sql_source;
char * sql_template;
char * sql_xml;
// Update the Image
@ -311,19 +235,11 @@ int Image::insert_replace(SqlDB *db, bool replace)
goto error_name;
}
sql_source = db->escape_str(source.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_source == 0 )
if ( sql_xml == 0 )
{
goto error_source;
}
image_template->to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
if ( sql_template == 0 )
{
goto error_template;
goto error_body;
}
if(replace)
@ -339,100 +255,24 @@ int Image::insert_replace(SqlDB *db, bool replace)
oss <<" INTO "<< table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< uid << ","
<< "'" << sql_name << "',"
<< type << ","
<< public_img << ","
<< persistent_img << ","
<< regtime << ","
<< "'" << sql_source << "',"
<< state << ","
<< running_vms << ","
<< "'" << sql_template << "')";
<< "'" << sql_xml << "',"
<< uid << ","
<< public_img << ")";
rc = db->exec(oss);
db->free_str(sql_name);
db->free_str(sql_source);
db->free_str(sql_template);
db->free_str(sql_xml);
return rc;
error_template:
db->free_str(sql_source);
error_source:
error_body:
db->free_str(sql_name);
error_name:
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[PUBLIC]) ||
(!values[PERSISTENT]) ||
(!values[REGTIME]) ||
(!values[SOURCE]) ||
(!values[STATE]) ||
(!values[RUNNING_VMS]) ||
(!values[TEMPLATE]) ||
(num != LIMIT + 1))
{
return -1;
}
oss <<
"<IMAGE>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT] << "</USERNAME>" <<
"<NAME>" << values[NAME] << "</NAME>" <<
"<TYPE>" << values[TYPE] << "</TYPE>" <<
"<PUBLIC>" << values[PUBLIC] << "</PUBLIC>" <<
"<PERSISTENT>" << values[PERSISTENT] << "</PERSISTENT>" <<
"<REGTIME>" << values[REGTIME] << "</REGTIME>" <<
"<SOURCE>" << values[SOURCE] << "</SOURCE>" <<
"<STATE>" << values[STATE] << "</STATE>" <<
"<RUNNING_VMS>" << values[RUNNING_VMS] << "</RUNNING_VMS>" <<
values[TEMPLATE] <<
"</IMAGE>";
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::drop(SqlDB * db)
{
ostringstream oss;
int rc;
// Only delete the VM
if (running_vms != 0)
{
return -1;
}
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}
/* ************************************************************************ */
/* Image :: Misc */
/* ************************************************************************ */
@ -458,6 +298,7 @@ string& Image::to_xml(string& xml) const
"<IMAGE>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<USERNAME>" << user_name << "</USERNAME>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<PUBLIC>" << public_img << "</PUBLIC>" <<
@ -477,30 +318,41 @@ string& Image::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Image::to_str(string& str) const
int Image::from_xml(const string& xml)
{
string template_str;
vector<xmlNodePtr> content;
int int_state;
int int_type;
ostringstream os;
// Initialize the internal XML object
update_from_str(xml);
os <<
"ID = " << oid << endl <<
"UID = " << uid << endl <<
"NAME = " << name << endl <<
"TYPE = " << type << endl <<
"PUBLIC = " << public_img << endl <<
"PERSISTENT = " << persistent_img << endl <<
"REGTIME = " << regtime << endl <<
"SOURCE = " << source << endl <<
"STATE = " << state << endl <<
"RUNNING_VMS = " << running_vms << endl <<
"TEMPLATE" << endl
<< image_template->to_str(template_str)
<< endl;
str = os.str();
xpath(oid, "/IMAGE/ID", -1);
xpath(uid, "/IMAGE/UID", -1);
xpath(user_name,"/IMAGE/USERNAME", "not_found");
xpath(name, "/IMAGE/NAME", "not_found");
return str;
xpath(int_type, "/IMAGE/TYPE", 0);
type = static_cast<ImageType>(int_type);
xpath(public_img, "/IMAGE/PUBLIC", 0);
xpath(persistent_img, "/IMAGE/PERSISTENT", 0);
xpath(regtime, "/IMAGE/REGTIME", 0);
xpath(source, "/IMAGE/SOURCE", "not_found");
xpath(int_state, "/IMAGE/STATE", 0);
state = static_cast<ImageState>(int_state);
xpath(running_vms, "/IMAGE/RUNNING_VMS", -1);
ObjectXML::get_nodes("/IMAGE/TEMPLATE", content);
if( content.size() < 1 )
{
return -1;
}
return image_template->from_xml_node(content[0]);
}
/* ------------------------------------------------------------------------ */

View File

@ -89,6 +89,7 @@ ImagePool::ImagePool( SqlDB * db,
int ImagePool::allocate (
int uid,
string user_name,
ImageTemplate* img_template,
int * oid,
string& error_str)
@ -99,7 +100,7 @@ int ImagePool::allocate (
// ---------------------------------------------------------------------
// Build a new Image object
// ---------------------------------------------------------------------
img = new Image(uid,img_template);
img = new Image(uid, user_name, img_template);
img->get_template_attribute("NAME", name);
@ -144,10 +145,7 @@ int ImagePool::dump(ostringstream& oss, const string& where)
set_callback(static_cast<Callbackable::Callback>(&ImagePool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT "<< Image::extended_db_names << ", user_pool.user_name FROM "
<< Image::table
<< " LEFT OUTER JOIN (SELECT oid, user_name FROM user_pool) "
<< "AS user_pool ON " << Image::table << ".uid = user_pool.oid";
cmd << "SELECT body FROM " << Image::table;
if ( !where.empty() )
{

View File

@ -24,6 +24,7 @@
using namespace std;
const int uids[] = {0,1,2};
const string user_names[] = {"A user","B user","C user"};
const string names[] = {"Image one", "Second Image", "The third image"};
@ -49,19 +50,19 @@ const string templates[] =
const string xmls[] =
{
"<IMAGE><ID>0</ID><UID>0</UID><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>1</ID><UID>1</UID><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>0</ID><UID>2</UID><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
"<IMAGE><ID>0</ID><UID>2</UID><USERNAME>C user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
};
// This xml dump result has the STIMEs modified to 0000000000
const string xml_dump =
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE></IMAGE_POOL>";
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>C user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE></IMAGE_POOL>";
const string xml_dump_where =
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE></IMAGE_POOL>";
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE></IMAGE_POOL>";
const string replacement = "0000000000";
@ -91,7 +92,7 @@ public:
if( rc == 0 )
{
return ImagePool::allocate(uid, img_template, oid, err);
return ImagePool::allocate(uid, user_names[uid], img_template, oid, err);
}
else
{
@ -167,7 +168,12 @@ protected:
((Image*)obj)->to_xml(xml_str);
xml_str.replace( xml_str.find("<REGTIME>")+9, 10, replacement);
//cout << endl << xml_str << endl << xmls[index] << endl;
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << xmls[index] << endl;
}
//*/
CPPUNIT_ASSERT( ((Image*)obj)->get_name() == names[index] );
CPPUNIT_ASSERT( xml_str == xmls[index]);
@ -181,14 +187,11 @@ protected:
UserPool * user_pool = new UserPool(db);
int uid_1, uid_2;
string username_1 = "A user";
string username_2 = "B user";
string pass_1 = "A pass";
string pass_2 = "B pass";
user_pool->allocate(&uid_1, username_1, pass_1, true, err);
user_pool->allocate(&uid_2, username_2, pass_2, true, err);
user_pool->allocate(&uid_1, user_names[0], pass_1, true, err);
user_pool->allocate(&uid_2, user_names[1], pass_2, true, err);
delete user_pool;
};
@ -487,6 +490,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -508,6 +512,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -529,6 +534,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -557,6 +563,7 @@ public:
// Allocate an OS type image
oid = allocate(0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
// ---------------------------------------------------------------------
// A disk without a BUS attribute should not have it added.
@ -865,8 +872,6 @@ public:
int rc;
string nan;
set_up_user_pool();
allocate(0);
allocate(1);
allocate(2);
@ -876,9 +881,16 @@ public:
string result = oss.str();
result.replace(164, 10, replacement);
result.replace(1154, 10, replacement);
result.replace(1684, 10, replacement);
result.replace(157, 10, replacement);
result.replace(1147, 10, replacement);
result.replace(1677, 10, replacement);
/*
if( result != xml_dump )
{
cout << endl << result << endl << xml_dump << endl;
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
}
@ -894,8 +906,6 @@ public:
ostringstream oss;
ostringstream where;
set_up_user_pool();
allocate(0);
allocate(1);
allocate(2);
@ -906,8 +916,15 @@ public:
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
result.replace(164, 10, replacement);
result.replace(1154, 10, replacement);
result.replace(157, 10, replacement);
result.replace(1147, 10, replacement);
/*
if( result != xml_dump_where )
{
cout << endl << result << endl << xml_dump_where << endl;
}
//*/
CPPUNIT_ASSERT( result == xml_dump_where );
}
@ -921,7 +938,5 @@ public:
int main(int argc, char ** argv)
{
OneUnitTest::set_one_auth();
return PoolTest::main(argc, argv, ImagePoolTest::suite());
}

View File

@ -31,8 +31,10 @@ void RequestManager::ImageAllocate::execute(
string session;
string str_template;
string error_str;
string user_name;
ImageTemplate * img_template;
ImageTemplate * img_template = 0;
User * user;
int iid;
int uid;
@ -96,10 +98,28 @@ void RequestManager::ImageAllocate::execute(
}
}
//--------------------------------------------------------------------------
// Get the User Name
//--------------------------------------------------------------------------
user = ImageAllocate::upool->get(uid,true);
if ( user == 0 )
{
goto error_user_get;
}
user_name = user->get_username();
user->unlock();
//--------------------------------------------------------------------------
// Allocate the Image
//--------------------------------------------------------------------------
rc = ImageAllocate::ipool->allocate(uid,img_template,&iid, error_str);
rc = ImageAllocate::ipool->allocate(uid,user_name,
img_template,&iid, error_str);
if ( rc < 0 )
{
@ -118,13 +138,17 @@ void RequestManager::ImageAllocate::execute(
return;
error_user_get:
oss.str(get_error(method_name, "USER", uid));
goto error_common;
error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;
error_authorize:
oss.str(authorization_error(method_name, "CREATE", "IMAGE", uid, -1));
delete img_template;
goto error_common;
error_parse:
@ -135,7 +159,6 @@ error_parse:
free(error_msg);
}
delete img_template;
goto error_common;
error_allocate:
@ -144,6 +167,11 @@ error_allocate:
goto error_common;
error_common:
if( img_template != 0 )
{
delete img_template;
}
arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE
arrayData.push_back(xmlrpc_c::value_string(oss.str()));