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

Feature #1112: Datastores are now defined with templates. New att. type and base path

This commit is contained in:
Carlos Martín 2012-02-16 19:37:08 +01:00
parent bbf7bbadd5
commit 4e2bd36415
9 changed files with 152 additions and 43 deletions

View File

@ -19,9 +19,7 @@
#include "PoolSQL.h"
#include "ObjectCollection.h"
//#include "Image.h"
//using namespace std;
#include "DatastoreTemplate.h"
/**
* The Datastore class.
@ -73,13 +71,25 @@ private:
friend class DatastorePool;
// *************************************************************************
// Datastore Private Attributes
// *************************************************************************
/**
* Type of driver
*/
string type;
/**
* Base path for the storage
*/
string base_path;
// *************************************************************************
// Constructor
// *************************************************************************
Datastore(int id, const string& name):
PoolObjectSQL(id,DATASTORE,name,-1,-1,"","",table),
ObjectCollection("IMAGES"){};
Datastore(int id, DatastoreTemplate* ds_template);
virtual ~Datastore(){};
@ -118,10 +128,7 @@ private:
* @param db pointer to the db
* @return 0 on success
*/
int insert(SqlDB *db, string& error_str)
{
return insert_replace(db, false, error_str);
}
int insert(SqlDB *db, string& error_str);
/**
* Writes/updates the Datastore's data fields in the database.

View File

@ -51,15 +51,15 @@ public:
/**
* Allocates a new Datastore, writing it in the pool database. No memory is
* allocated for the object.
* @param name Datastore name
* @param ds_template Datastore definition template
* @param oid the id assigned to the Datastore
* @param error_str Returns the error reason, if any
*
* @return the oid assigned to the object, -1 in case of failure
*/
int allocate(string name,
int * oid,
string& error_str);
int allocate(DatastoreTemplate * ds_template,
int * oid,
string& error_str);
/**
* Function to get a Datastore from the pool, if the object is not in memory
@ -149,7 +149,7 @@ private:
*/
PoolObjectSQL * create()
{
return new Datastore(-1,"");
return new Datastore(-1, 0);
};
};

View File

@ -23,6 +23,7 @@
#include "VirtualNetworkTemplate.h"
#include "ImageTemplate.h"
#include "VirtualMachineTemplate.h"
#include "DatastoreTemplate.h"
using namespace std;
@ -285,7 +286,7 @@ public:
RequestManagerAllocate("DatastoreAllocate",
"Allocates a new Datastore",
"A:ss",
false)
true)
{
Nebula& nd = Nebula::instance();
pool = nd.get_dspool();
@ -294,6 +295,13 @@ public:
~DatastoreAllocate(){};
/* -------------------------------------------------------------------- */
Template * get_object_template()
{
return new DatastoreTemplate;
};
int pool_allocate(xmlrpc_c::paramList const& _paramList,
Template * tmpl,
int& id,

View File

@ -61,12 +61,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do
########################################################################
create_desc = <<-EOT.unindent
Creates a new Datastore
Creates a new Datastore from the given template file
EOT
command :create, create_desc, :name do
command :create, create_desc, :file do
helper.create_resource(options) do |datastore|
datastore.allocate(args[0])
template=File.read(args[0])
datastore.allocate(template)
end
end

View File

@ -22,6 +22,7 @@
#include "Datastore.h"
#include "GroupPool.h"
#include "NebulaLog.h"
const char * Datastore::table = "datastore_pool";
@ -35,9 +36,83 @@ const char * Datastore::db_bootstrap =
"UNIQUE(name))";
/* ************************************************************************ */
/* Datastore :: Database Access Functions */
/* Datastore :: Constructor/Destructor */
/* ************************************************************************ */
Datastore::Datastore(int id,
DatastoreTemplate* ds_template):
PoolObjectSQL(id,DATASTORE,"",-1,-1,"","",table),
ObjectCollection("IMAGES"),
type(""),
base_path("")
{
if (ds_template != 0)
{
obj_template = ds_template;
}
else
{
obj_template = new DatastoreTemplate;
}
}
/* ************************************************************************ */
/* Datastore :: Database Access Functions */
/* ************************************************************************ */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Datastore::insert(SqlDB *db, string& error_str)
{
int rc;
// ---------------------------------------------------------------------
// Check default datastore attributes
// ---------------------------------------------------------------------
erase_template_attribute("NAME", name);
// NAME is checked in DatastorePool::allocate
erase_template_attribute("TYPE", type);
if ( type.empty() == true )
{
goto error_type;
}
erase_template_attribute("BASE_PATH", base_path);
if ( base_path.empty() == true )
{
goto error_base_path;
}
//--------------------------------------------------------------------------
// Insert the Datastore
//--------------------------------------------------------------------------
rc = insert_replace(db, false, error_str);
return rc;
error_type:
error_str = "No NAME in template.";
goto error_common;
error_base_path:
error_str = "No BASE_PATH in template.";
goto error_common;
error_common:
NebulaLog::log("DATASTORE", Log::ERROR, error_str);
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Datastore::insert_replace(SqlDB *db, bool replace, string& error_str)
{
ostringstream oss;
@ -135,8 +210,10 @@ string& Datastore::to_xml(string& xml) const
oss <<
"<DATASTORE>" <<
"<ID>" << oid << "</ID>" <<
"<NAME>" << name << "</NAME>" <<
"<ID>" << oid << "</ID>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<BASE_PATH>" << base_path << "</BASE_PATH>" <<
collection_xml <<
"</DATASTORE>";
@ -157,8 +234,10 @@ int Datastore::from_xml(const string& xml)
update_from_str(xml);
// Get class base attributes
rc += xpath(oid, "/DATASTORE/ID", -1);
rc += xpath(name,"/DATASTORE/NAME", "not_found");
rc += xpath(oid, "/DATASTORE/ID", -1);
rc += xpath(name, "/DATASTORE/NAME", "not_found");
rc += xpath(type, "/DATASTORE/TYPE", "not_found");
rc += xpath(base_path, "/DATASTORE/BASE_PATH", "not_found");
// Set the owner and group to oneadmin
set_user(0, "");

View File

@ -43,6 +43,8 @@ DatastorePool::DatastorePool(SqlDB * db):PoolSQL(db, Datastore::table)
Datastore * ds;
// Build the default datastore
// TODO: create template with default name, type and base path
/*
ds = new Datastore(SYSTEM_DS_ID, SYSTEM_DS_NAME);
rc = PoolSQL::allocate(ds, error_str);
@ -51,7 +53,7 @@ DatastorePool::DatastorePool(SqlDB * db):PoolSQL(db, Datastore::table)
{
goto error_bootstrap;
}
*/
set_update_lastOID(99);
}
@ -67,11 +69,23 @@ error_bootstrap:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int DatastorePool::allocate(string name, int * oid, string& error_str)
int DatastorePool::allocate(DatastoreTemplate * ds_template,
int * oid,
string& error_str)
{
Datastore * datastore;
Datastore * ds;
Datastore * ds_aux = 0;
string name;
ostringstream oss;
ds = new Datastore(-1, ds_template);
// -------------------------------------------------------------------------
// Check name & duplicates
// -------------------------------------------------------------------------
ds->get_template_attribute("NAME", name);
if ( name.empty() )
{
goto error_name;
@ -82,19 +96,14 @@ int DatastorePool::allocate(string name, int * oid, string& error_str)
goto error_name_length;
}
// Check for duplicates
datastore = get(name, false);
ds_aux = get(name,false);
if( datastore != 0 )
if( ds_aux != 0 )
{
goto error_duplicated;
}
// Build a new Datastore object
datastore = new Datastore(-1, name);
// Insert the Object in the pool
*oid = PoolSQL::allocate(datastore, error_str);
*oid = PoolSQL::allocate(ds, error_str);
return *oid;
@ -107,9 +116,11 @@ error_name_length:
goto error_common;
error_duplicated:
oss << "NAME is already taken by DATASTORE " << datastore->get_oid() << ".";
oss << "NAME is already taken by DATASTORE " << ds_aux->get_oid() << ".";
error_common:
delete ds;
*oid = -1;
error_str = oss.str();

View File

@ -62,9 +62,12 @@ module OpenNebula
# Allocates a new Datastore in OpenNebula
#
# +datastorename+ A string containing the name of the Datastore.
def allocate(datastorename)
super(DATASTORE_METHODS[:allocate], datastorename)
# @param description [String] The template of the Datastore.
#
# @return [Integer, OpenNebula::Error] the new ID in case of
# success, error otherwise
def allocate(description)
super(DATASTORE_METHODS[:allocate], description)
end
# Deletes the Datastore

View File

@ -83,7 +83,7 @@ module OpenNebula
# @param vmm [String] Name of the vmm_driver
# @param tm [String] Name of the tm_driver
#
# @return [Integer, OpenNebula::Error] the new VM ID in case of
# @return [Integer, OpenNebula::Error] the new ID in case of
# success, error otherwise
def allocate(hostname,im,vmm,vnm,tm)
super(HOST_METHODS[:allocate],hostname,im,vmm,vnm,tm)

View File

@ -371,9 +371,9 @@ int DatastoreAllocate::pool_allocate(
string& error_str,
RequestAttributes& att)
{
string name = xmlrpc_c::value_string(paramList.getString(1));
DatastorePool * dspool = static_cast<DatastorePool *>(pool);
return dspool->allocate(name, &id, error_str);
DatastoreTemplate * ds_tmpl = static_cast<DatastoreTemplate *>(tmpl);
return dspool->allocate(ds_tmpl, &id, error_str);
}