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

Feature #4215: Add support for %i name replacement in vrouter.instantiate

This commit is contained in:
Carlos Martín 2016-01-20 12:03:19 +01:00
parent c41d43f09a
commit 383babc8f7
4 changed files with 74 additions and 3 deletions

View File

@ -174,6 +174,29 @@ namespace one_util
* @return trimed string
*/
std::string trim(const std::string& str);
/**
* Returns a copy of st with the all occurrences of "find" substituted
* for "replacement"
* @param st string input
* @param find string to search for
* @param replacement string to replace occurrences with
* @return a string copy
*/
std::string gsub(const std::string& st,
const char *find, const char *replacement);
/**
* Returns a copy of st with the all occurrences of "find" substituted
* for "replacement"
* @param st string input
* @param find string to search for
* @param replacement string to replace occurrences with
* @return a string copy
*/
std::string gsub(const std::string& st,
const char *find, const std::string& replacement);
};
#endif /* _NEBULA_UTIL_H_ */

View File

@ -25,6 +25,7 @@
#include <string>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <math.h>
@ -306,3 +307,36 @@ std::string one_util::trim(const std::string& str)
return tstr;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string one_util::gsub(const string& st,
const char *find, const string& replacement)
{
return gsub(st, find, replacement.c_str());
}
/* -------------------------------------------------------------------------- */
string one_util::gsub(const string& st,
const char *find, const char *replacement)
{
string result = st;
string::size_type pos = 0;
size_t find_len = strlen(find);
size_t replacement_len = strlen(replacement);
pos = result.find(find, pos);
while(pos != std::string::npos)
{
result.replace(pos, find_len, replacement);
pos += replacement_len;
pos = result.find(find, pos);
}
return result;
}

View File

@ -68,6 +68,8 @@ void VirtualRouterInstantiate::request_execute(
Template* extra_attrs;
bool has_vmids;
string errorstr;
string vr_name;
ostringstream oss;
vector<int> vms;
vector<int>::iterator vmid;
@ -93,6 +95,8 @@ void VirtualRouterInstantiate::request_execute(
has_vmids = vr->has_vmids();
vr_name = vr->get_name();
vr->unlock();
if ( att.uid != 0 )
@ -120,9 +124,21 @@ void VirtualRouterInstantiate::request_execute(
return;
}
if (name.empty())
{
oss.str("");
oss << "vr-" << vr_name << "-%i";
name = oss.str();
}
for (int i=0; i<n_vms; i++)
{
int vid = instantiate(att, tmpl_id, name, true, str_uattrs, extra_attrs);
oss.str("");
oss << i;
string tmp_name = one_util::gsub(name, "%i", oss.str());
int vid = instantiate(att, tmpl_id, tmp_name, true, str_uattrs, extra_attrs);
if (vid == -1)
{

View File

@ -165,8 +165,6 @@ define(function(require) {
timeout: true,
success: function (request, response) {
//TODO: add support for vm_name.replace(/%i/gi, i), or remove tooltip
var tmpl = WizardFields.retrieve($(".template_user_inputs", context));
var extra_info = {