1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-24 02:03:52 +03:00

Feature #1095: Templates in one.*.allocate, one.*.update & one.vn.add/rmleases can be in XML

This commit is contained in:
Carlos Martín 2012-01-30 19:31:21 +01:00
parent bb35437206
commit 5cf1b27e2a
9 changed files with 68 additions and 62 deletions

View File

@ -219,13 +219,6 @@ protected:
* @return string for logging * @return string for logging
*/ */
string allocate_error (PoolObjectSQL::ObjectType obj, const string& error); string allocate_error (PoolObjectSQL::ObjectType obj, const string& error);
/**
* Logs allocate errors
* @param message with the allocate error details (parsing)
* @return string for logging
*/
string allocate_error (char *error);
}; };
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View File

@ -52,7 +52,7 @@ protected:
string& error_str) = 0; string& error_str) = 0;
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
string leases_error (char * error); string leases_error (const string& error);
}; };
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */

View File

@ -70,7 +70,7 @@ public:
* Parse a string representing the template, each attribute is inserted * Parse a string representing the template, each attribute is inserted
* in the template class. * in the template class.
* @param parse_str string with template attributes * @param parse_str string with template attributes
* @param error_msg error string, must be freed by the calling funtion. * @param error_msg error string, must be freed by the calling function.
* This string is null if no error occurred. * This string is null if no error occurred.
* @return 0 on success. * @return 0 on success.
*/ */
@ -79,12 +79,23 @@ public:
/** /**
* Parse a template file. * Parse a template file.
* @param filename of the template file * @param filename of the template file
* @param error_msg error string, must be freed by the calling funtion. * @param error_msg error string, must be freed by the calling function.
* This string is null if no error occurred. * This string is null if no error occurred.
* @return 0 on success. * @return 0 on success.
*/ */
int parse(const char * filename, char **error_msg); int parse(const char * filename, char **error_msg);
/**
* Parse a string representing the template, automatically detecting if
* it is the default syntax, or an XML template. Each attribute is inserted
* in the template class.
* @param parse_str string with template attributes, or XML template
* @param error_msg error string, must be freed by the calling function.
* This string is null if no error occurred.
* @return 0 on success.
*/
int parse_str_or_xml(const string &parse_str, string& error_msg);
/** /**
* Marshall a template. This function generates a single string with the * Marshall a template. This function generates a single string with the
* template attributes ("VAR=VAL<delim>..."). * template attributes ("VAR=VAL<delim>...").

View File

@ -166,7 +166,6 @@ void PoolObjectSQL::set_template_error_message(const string& message)
int PoolObjectSQL::replace_template(const string& tmpl_str, string& error) int PoolObjectSQL::replace_template(const string& tmpl_str, string& error)
{ {
Template * new_tmpl = get_new_template(); Template * new_tmpl = get_new_template();
char * error_msg = 0;
if ( new_tmpl == 0 ) if ( new_tmpl == 0 )
{ {
@ -174,19 +173,8 @@ int PoolObjectSQL::replace_template(const string& tmpl_str, string& error)
return -1; return -1;
} }
if ( new_tmpl->parse(tmpl_str, &error_msg) != 0 ) if ( new_tmpl->parse_str_or_xml(tmpl_str, error) != 0 )
{ {
ostringstream oss;
oss << "Parse error";
if (error_msg != 0)
{
oss << ": " << error_msg;
}
error = oss.str();
return -1; return -1;
} }

View File

@ -293,22 +293,3 @@ string Request::allocate_error (const string& error)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
string Request::allocate_error (char *error)
{
ostringstream oss;
oss << "Parse error";
if ( error != 0 )
{
oss << ": " << error;
free(error);
}
else
{
oss << ".";
}
return allocate_error(oss.str());
}

View File

@ -99,16 +99,15 @@ void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params,
if ( do_template == true ) if ( do_template == true )
{ {
char * error_msg = 0;
string str_tmpl = xmlrpc_c::value_string(params.getString(1)); string str_tmpl = xmlrpc_c::value_string(params.getString(1));
tmpl = get_object_template(); tmpl = get_object_template();
rc = tmpl->parse(str_tmpl, &error_msg); rc = tmpl->parse_str_or_xml(str_tmpl, error_str);
if ( rc != 0 ) if ( rc != 0 )
{ {
failure_response(INTERNAL, allocate_error(error_msg), att); failure_response(INTERNAL, allocate_error(error_str), att);
delete tmpl; delete tmpl;
return; return;

View File

@ -420,7 +420,6 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis
int rc; int rc;
ostringstream oss; ostringstream oss;
string error_str; string error_str;
char * error_char;
// ------------------ Template for the new image ------------------ // ------------------ Template for the new image ------------------
@ -437,7 +436,7 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis
itemplate = new ImageTemplate; itemplate = new ImageTemplate;
itemplate->parse(oss.str(), &error_char); itemplate->parse_str_or_xml(oss.str(), error_str);
// ------------------ Authorize the operation ------------------ // ------------------ Authorize the operation ------------------

View File

@ -22,19 +22,9 @@ using namespace std;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
string RequestManagerVirtualNetwork::leases_error (char *error) string RequestManagerVirtualNetwork::leases_error (const string& error)
{ {
ostringstream oss; return request_error("Error modifying network leases",error);
oss << "Parse error.";
if ( error != 0 )
{
oss << " " << error;
free(error);
}
return request_error("Error modifying network leases",oss.str());
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@ -50,7 +40,6 @@ void RequestManagerVirtualNetwork::
VirtualNetworkTemplate tmpl; VirtualNetworkTemplate tmpl;
VirtualNetwork * vn; VirtualNetwork * vn;
char * error_msg = 0;
string error_str; string error_str;
int rc; int rc;
@ -59,11 +48,11 @@ void RequestManagerVirtualNetwork::
return; return;
} }
rc = tmpl.parse(str_tmpl, &error_msg); rc = tmpl.parse_str_or_xml(str_tmpl, error_str);
if ( rc != 0 ) if ( rc != 0 )
{ {
failure_response(INTERNAL, leases_error(error_msg), att); failure_response(INTERNAL, leases_error(error_str), att);
return; return;
} }

View File

@ -135,6 +135,52 @@ error_yy:
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int Template::parse_str_or_xml(const string &parse_str, string& error_msg)
{
int rc;
if ( parse_str[0] == '<' )
{
rc = from_xml(parse_str);
if ( rc != 0 )
{
error_msg = "Parse error: XML Template malformed.";
}
}
else
{
char * error_char = 0;
rc = parse(parse_str, &error_char);
if ( rc != 0 )
{
ostringstream oss;
oss << "Parse error";
if (error_char != 0)
{
oss << ": " << error_char;
}
else
{
oss << ".";
}
error_msg = oss.str();
free(error_char);
}
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Template::marshall(string &str, const char delim) void Template::marshall(string &str, const char delim)
{ {
multimap<string,Attribute *>::iterator it; multimap<string,Attribute *>::iterator it;