mirror of
https://github.com/OpenNebula/one.git
synced 2025-01-10 01:17:40 +03:00
Feature #1112: Create Datastore and DatastorePool classes, and RM methods
This commit is contained in:
parent
03ddf136b7
commit
9ec4b450a4
@ -57,6 +57,7 @@ main_env.Append(LIBPATH=[
|
|||||||
cwd+'/src/log',
|
cwd+'/src/log',
|
||||||
cwd+'/src/sql',
|
cwd+'/src/sql',
|
||||||
cwd+'/src/host',
|
cwd+'/src/host',
|
||||||
|
cwd+'/src/datastore',
|
||||||
cwd+'/src/group',
|
cwd+'/src/group',
|
||||||
cwd+'/src/mad',
|
cwd+'/src/mad',
|
||||||
cwd+'/src/nebula',
|
cwd+'/src/nebula',
|
||||||
@ -187,6 +188,7 @@ build_scripts=[
|
|||||||
'src/common/SConstruct',
|
'src/common/SConstruct',
|
||||||
'src/template/SConstruct',
|
'src/template/SConstruct',
|
||||||
'src/host/SConstruct',
|
'src/host/SConstruct',
|
||||||
|
'src/datastore/SConstruct',
|
||||||
'src/group/SConstruct',
|
'src/group/SConstruct',
|
||||||
'src/mad/SConstruct',
|
'src/mad/SConstruct',
|
||||||
'src/mad/utils/SConstruct',
|
'src/mad/utils/SConstruct',
|
||||||
@ -236,6 +238,7 @@ if testing=='yes':
|
|||||||
'src/authm/test/SConstruct',
|
'src/authm/test/SConstruct',
|
||||||
'src/common/test/SConstruct',
|
'src/common/test/SConstruct',
|
||||||
'src/host/test/SConstruct',
|
'src/host/test/SConstruct',
|
||||||
|
'src/datastore/test/SConstruct',
|
||||||
'src/group/test/SConstruct',
|
'src/group/test/SConstruct',
|
||||||
'src/image/test/SConstruct',
|
'src/image/test/SConstruct',
|
||||||
'src/lcm/test/SConstruct',
|
'src/lcm/test/SConstruct',
|
||||||
|
138
include/Datastore.h
Normal file
138
include/Datastore.h
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */
|
||||||
|
/* */
|
||||||
|
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
|
||||||
|
/* not use this file except in compliance with the License. You may obtain */
|
||||||
|
/* a copy of the License at */
|
||||||
|
/* */
|
||||||
|
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
||||||
|
/* */
|
||||||
|
/* Unless required by applicable law or agreed to in writing, software */
|
||||||
|
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
||||||
|
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
||||||
|
/* See the License for the specific language governing permissions and */
|
||||||
|
/* limitations under the License. */
|
||||||
|
/* -------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef DATASTORE_H_
|
||||||
|
#define DATASTORE_H_
|
||||||
|
|
||||||
|
#include "PoolSQL.h"
|
||||||
|
#include "ObjectCollection.h"
|
||||||
|
//#include "Image.h"
|
||||||
|
|
||||||
|
//using namespace std;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Datastore class.
|
||||||
|
*/
|
||||||
|
class Datastore : public PoolObjectSQL, ObjectCollection
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to print the Datastore object into a string in XML format
|
||||||
|
* @param xml the resulting XML string
|
||||||
|
* @return a reference to the generated string
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds this image's ID to the set.
|
||||||
|
* @param id of the image to be added to the Datastore
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
int add_image(int id)
|
||||||
|
{
|
||||||
|
return add_collection_id(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes this image's ID from the set.
|
||||||
|
* @param id of the image to be deleted from the Datastore
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
int del_image(int id)
|
||||||
|
{
|
||||||
|
return del_collection_id(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Friends
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
friend class DatastorePool;
|
||||||
|
|
||||||
|
// *************************************************************************
|
||||||
|
// Constructor
|
||||||
|
// *************************************************************************
|
||||||
|
|
||||||
|
Datastore(int id, const string& name):
|
||||||
|
PoolObjectSQL(id,DATASTORE,name,-1,-1,"","",table),
|
||||||
|
ObjectCollection("IMAGES"){};
|
||||||
|
|
||||||
|
virtual ~Datastore(){};
|
||||||
|
|
||||||
|
// *************************************************************************
|
||||||
|
// DataBase implementation (Private)
|
||||||
|
// *************************************************************************
|
||||||
|
|
||||||
|
static const char * db_names;
|
||||||
|
|
||||||
|
static const char * db_bootstrap;
|
||||||
|
|
||||||
|
static const char * table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute an INSERT or REPLACE Sql query.
|
||||||
|
* @param db The SQL DB
|
||||||
|
* @param replace Execute an INSERT or a REPLACE
|
||||||
|
* @param error_str Returns the error reason, if any
|
||||||
|
* @return 0 one success
|
||||||
|
*/
|
||||||
|
int insert_replace(SqlDB *db, bool replace, string& error_str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bootstraps the database table(s) associated to the Datastore
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
static int bootstrap(SqlDB * db)
|
||||||
|
{
|
||||||
|
ostringstream oss(Datastore::db_bootstrap);
|
||||||
|
|
||||||
|
return db->exec(oss);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the Datastore in the database.
|
||||||
|
* @param db pointer to the db
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
int insert(SqlDB *db, string& error_str)
|
||||||
|
{
|
||||||
|
return insert_replace(db, false, error_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes/updates the Datastore's data fields in the database.
|
||||||
|
* @param db pointer to the db
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
int update(SqlDB *db)
|
||||||
|
{
|
||||||
|
string error_str;
|
||||||
|
return insert_replace(db, true, error_str);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /*DATASTORE_H_*/
|
146
include/DatastorePool.h
Normal file
146
include/DatastorePool.h
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */
|
||||||
|
/* */
|
||||||
|
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
|
||||||
|
/* not use this file except in compliance with the License. You may obtain */
|
||||||
|
/* a copy of the License at */
|
||||||
|
/* */
|
||||||
|
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
||||||
|
/* */
|
||||||
|
/* Unless required by applicable law or agreed to in writing, software */
|
||||||
|
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
||||||
|
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
||||||
|
/* See the License for the specific language governing permissions and */
|
||||||
|
/* limitations under the License. */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
#ifndef DATASTORE_POOL_H_
|
||||||
|
#define DATASTORE_POOL_H_
|
||||||
|
|
||||||
|
#include "Datastore.h"
|
||||||
|
#include "SqlDB.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
class DatastorePool : public PoolSQL
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DatastorePool(SqlDB * db):PoolSQL(db, Datastore::table){};
|
||||||
|
|
||||||
|
~DatastorePool(){};
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------- */
|
||||||
|
/* Constants for DB management */
|
||||||
|
/* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------- */
|
||||||
|
/* Methods for DB management */
|
||||||
|
/* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocates a new Datastore, writing it in the pool database. No memory is
|
||||||
|
* allocated for the object.
|
||||||
|
* @param name Datastore name
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to get a Datastore from the pool, if the object is not in memory
|
||||||
|
* it is loaded from the DB
|
||||||
|
* @param oid Datastore unique id
|
||||||
|
* @param lock locks the Datastore mutex
|
||||||
|
* @return a pointer to the Datastore, 0 if the Datastore could not be loaded
|
||||||
|
*/
|
||||||
|
Datastore * get(int oid, bool lock)
|
||||||
|
{
|
||||||
|
return static_cast<Datastore *>(PoolSQL::get(oid,lock));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an object from the pool (if needed the object is loaded from the
|
||||||
|
* database).
|
||||||
|
* @param name of the object
|
||||||
|
* @param lock locks the object if true
|
||||||
|
*
|
||||||
|
* @return a pointer to the object, 0 in case of failure
|
||||||
|
*/
|
||||||
|
Datastore * get(const string& name, bool lock)
|
||||||
|
{
|
||||||
|
// The owner is set to -1, because it is not used in the key() method
|
||||||
|
return static_cast<Datastore *>(PoolSQL::get(name,-1,lock));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an index key for the object
|
||||||
|
* @param name of the object
|
||||||
|
* @param uid owner of the object, only used if needed
|
||||||
|
*
|
||||||
|
* @return the key, a string
|
||||||
|
*/
|
||||||
|
string key(const string& name, int uid)
|
||||||
|
{
|
||||||
|
// Name is enough key because Datastores can't repeat names.
|
||||||
|
return name;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Update a particular Datastore
|
||||||
|
* @param user pointer to Datastore
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
int update(Datastore * datastore)
|
||||||
|
{
|
||||||
|
return datastore->update(db);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drops the Datastore data in the data base. The object mutex SHOULD be
|
||||||
|
* locked.
|
||||||
|
* @param objsql a pointer to the Datastore object
|
||||||
|
* @param error_msg Error reason, if any
|
||||||
|
* @return 0 on success, -1 DB error
|
||||||
|
* -3 Datastore's Image IDs set is not empty
|
||||||
|
*/
|
||||||
|
int drop(PoolObjectSQL * objsql, string& error_msg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bootstraps the database table(s) associated to the Datastore pool
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
static int bootstrap(SqlDB * _db)
|
||||||
|
{
|
||||||
|
return Datastore::bootstrap(_db);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dumps the Datastore pool in XML format. A filter can be also added to the
|
||||||
|
* query
|
||||||
|
* @param oss the output stream to dump the pool contents
|
||||||
|
* @param where filter for the objects, defaults to all
|
||||||
|
*
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
int dump(ostringstream& oss, const string& where)
|
||||||
|
{
|
||||||
|
return PoolSQL::dump(oss, "DATASTORE_POOL", Datastore::table, where);
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory method to produce objects
|
||||||
|
* @return a pointer to the new object
|
||||||
|
*/
|
||||||
|
PoolObjectSQL * create()
|
||||||
|
{
|
||||||
|
return new Datastore(-1,"");
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /*DATASTORE_POOL_H_*/
|
@ -31,7 +31,7 @@ public:
|
|||||||
~GroupPool(){};
|
~GroupPool(){};
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------- */
|
||||||
/* Constants r DB management */
|
/* Constants for DB management */
|
||||||
/* ---------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include "UserPool.h"
|
#include "UserPool.h"
|
||||||
#include "VMTemplatePool.h"
|
#include "VMTemplatePool.h"
|
||||||
#include "GroupPool.h"
|
#include "GroupPool.h"
|
||||||
|
#include "DatastorePool.h"
|
||||||
|
|
||||||
#include "VirtualMachineManager.h"
|
#include "VirtualMachineManager.h"
|
||||||
#include "LifeCycleManager.h"
|
#include "LifeCycleManager.h"
|
||||||
@ -91,6 +92,11 @@ public:
|
|||||||
return tpool;
|
return tpool;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
DatastorePool * get_dspool()
|
||||||
|
{
|
||||||
|
return dspool;
|
||||||
|
};
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
// Manager Accessors
|
// Manager Accessors
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
@ -421,6 +427,7 @@ private:
|
|||||||
ImagePool * ipool;
|
ImagePool * ipool;
|
||||||
GroupPool * gpool;
|
GroupPool * gpool;
|
||||||
VMTemplatePool * tpool;
|
VMTemplatePool * tpool;
|
||||||
|
DatastorePool * dspool;
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
// Nebula Managers
|
// Nebula Managers
|
||||||
|
@ -49,14 +49,15 @@ public:
|
|||||||
*/
|
*/
|
||||||
enum ObjectType
|
enum ObjectType
|
||||||
{
|
{
|
||||||
VM = 0x0000001000000000LL,
|
VM = 0x0000001000000000LL,
|
||||||
HOST = 0x0000002000000000LL,
|
HOST = 0x0000002000000000LL,
|
||||||
NET = 0x0000004000000000LL,
|
NET = 0x0000004000000000LL,
|
||||||
IMAGE = 0x0000008000000000LL,
|
IMAGE = 0x0000008000000000LL,
|
||||||
USER = 0x0000010000000000LL,
|
USER = 0x0000010000000000LL,
|
||||||
TEMPLATE = 0x0000020000000000LL,
|
TEMPLATE = 0x0000020000000000LL,
|
||||||
GROUP = 0x0000040000000000LL,
|
GROUP = 0x0000040000000000LL,
|
||||||
ACL = 0x0000080000000000LL
|
ACL = 0x0000080000000000LL,
|
||||||
|
DATASTORE = 0x0000100000000000LL
|
||||||
};
|
};
|
||||||
|
|
||||||
static string type_to_str(ObjectType ob)
|
static string type_to_str(ObjectType ob)
|
||||||
|
@ -279,6 +279,32 @@ public:
|
|||||||
RequestAttributes& att);
|
RequestAttributes& att);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
class DatastoreAllocate: public RequestManagerAllocate
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DatastoreAllocate():
|
||||||
|
RequestManagerAllocate("DatastoreAllocate",
|
||||||
|
"Allocates a new Datastore",
|
||||||
|
"A:ss",
|
||||||
|
false)
|
||||||
|
{
|
||||||
|
Nebula& nd = Nebula::instance();
|
||||||
|
pool = nd.get_dspool();
|
||||||
|
auth_object = PoolObjectSQL::DATASTORE;
|
||||||
|
};
|
||||||
|
|
||||||
|
~DatastoreAllocate(){};
|
||||||
|
|
||||||
|
int pool_allocate(xmlrpc_c::paramList const& _paramList,
|
||||||
|
Template * tmpl,
|
||||||
|
int& id,
|
||||||
|
string& error_str,
|
||||||
|
RequestAttributes& att);
|
||||||
|
};
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
@ -178,6 +178,24 @@ public:
|
|||||||
int drop(int oid, PoolObjectSQL * object, string& error_msg);
|
int drop(int oid, PoolObjectSQL * object, string& error_msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
class DatastoreDelete: public RequestManagerDelete
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DatastoreDelete():
|
||||||
|
RequestManagerDelete("DatastoreDelete", "Deletes a datastore")
|
||||||
|
{
|
||||||
|
Nebula& nd = Nebula::instance();
|
||||||
|
pool = nd.get_upool();
|
||||||
|
auth_object = PoolObjectSQL::DATASTORE;
|
||||||
|
auth_op = AuthRequest::ADMIN;
|
||||||
|
};
|
||||||
|
|
||||||
|
~DatastoreDelete(){};
|
||||||
|
};
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
@ -196,6 +196,24 @@ public:
|
|||||||
~UserInfo(){};
|
~UserInfo(){};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
class DatastoreInfo: public RequestManagerInfo
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DatastoreInfo():
|
||||||
|
RequestManagerInfo("UserInfo",
|
||||||
|
"Returns datastore information")
|
||||||
|
{
|
||||||
|
Nebula& nd = Nebula::instance();
|
||||||
|
pool = nd.get_dspool();
|
||||||
|
auth_object = PoolObjectSQL::DATASTORE;
|
||||||
|
};
|
||||||
|
|
||||||
|
~DatastoreInfo(){};
|
||||||
|
};
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
@ -224,6 +224,30 @@ public:
|
|||||||
xmlrpc_c::paramList const& paramList, RequestAttributes& att);
|
xmlrpc_c::paramList const& paramList, RequestAttributes& att);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
class DatastorePoolInfo: public RequestManagerPoolInfoFilter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DatastorePoolInfo():
|
||||||
|
RequestManagerPoolInfoFilter("DatastorePoolInfo",
|
||||||
|
"Returns the datastore pool",
|
||||||
|
"A:s")
|
||||||
|
{
|
||||||
|
Nebula& nd = Nebula::instance();
|
||||||
|
pool = nd.get_dspool();
|
||||||
|
auth_object = PoolObjectSQL::DATASTORE;
|
||||||
|
};
|
||||||
|
|
||||||
|
~DatastorePoolInfo(){};
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
void request_execute(
|
||||||
|
xmlrpc_c::paramList const& paramList, RequestAttributes& att);
|
||||||
|
};
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
190
src/datastore/Datastore.cc
Normal file
190
src/datastore/Datastore.cc
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */
|
||||||
|
/* */
|
||||||
|
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
|
||||||
|
/* not use this file except in compliance with the License. You may obtain */
|
||||||
|
/* a copy of the License at */
|
||||||
|
/* */
|
||||||
|
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
||||||
|
/* */
|
||||||
|
/* Unless required by applicable law or agreed to in writing, software */
|
||||||
|
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
||||||
|
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
||||||
|
/* See the License for the specific language governing permissions and */
|
||||||
|
/* limitations under the License. */
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "Datastore.h"
|
||||||
|
#include "GroupPool.h"
|
||||||
|
|
||||||
|
const char * Datastore::table = "datastore_pool";
|
||||||
|
|
||||||
|
const char * Datastore::db_names =
|
||||||
|
"oid, name, body, uid, gid, owner_u, group_u, other_u";
|
||||||
|
|
||||||
|
const char * Datastore::db_bootstrap =
|
||||||
|
"CREATE TABLE IF NOT EXISTS datastore_pool ("
|
||||||
|
"oid INTEGER PRIMARY KEY, name VARCHAR(128), body TEXT, uid INTEGER, "
|
||||||
|
"gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, "
|
||||||
|
"UNIQUE(name))";
|
||||||
|
|
||||||
|
/* ************************************************************************ */
|
||||||
|
/* Datastore :: Database Access Functions */
|
||||||
|
/* ************************************************************************ */
|
||||||
|
|
||||||
|
int Datastore::insert_replace(SqlDB *db, bool replace, string& error_str)
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
|
||||||
|
int rc;
|
||||||
|
string xml_body;
|
||||||
|
|
||||||
|
char * sql_name;
|
||||||
|
char * sql_xml;
|
||||||
|
|
||||||
|
// Set the owner and group to oneadmin
|
||||||
|
set_user(0, "");
|
||||||
|
set_group(GroupPool::ONEADMIN_ID, GroupPool::ONEADMIN_NAME);
|
||||||
|
|
||||||
|
// Update the Datastore
|
||||||
|
|
||||||
|
sql_name = db->escape_str(name.c_str());
|
||||||
|
|
||||||
|
if ( sql_name == 0 )
|
||||||
|
{
|
||||||
|
goto error_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
sql_xml = db->escape_str(to_xml(xml_body).c_str());
|
||||||
|
|
||||||
|
if ( sql_xml == 0 )
|
||||||
|
{
|
||||||
|
goto error_body;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( validate_xml(sql_xml) != 0 )
|
||||||
|
{
|
||||||
|
goto error_xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( replace )
|
||||||
|
{
|
||||||
|
oss << "REPLACE";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
oss << "INSERT";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct the SQL statement to Insert or Replace
|
||||||
|
|
||||||
|
oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
|
||||||
|
<< oid << ","
|
||||||
|
<< "'" << sql_name << "',"
|
||||||
|
<< "'" << sql_xml << "',"
|
||||||
|
<< uid << ","
|
||||||
|
<< gid << ","
|
||||||
|
<< owner_u << ","
|
||||||
|
<< group_u << ","
|
||||||
|
<< other_u << ")";
|
||||||
|
|
||||||
|
|
||||||
|
rc = db->exec(oss);
|
||||||
|
|
||||||
|
db->free_str(sql_name);
|
||||||
|
db->free_str(sql_xml);
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
error_xml:
|
||||||
|
db->free_str(sql_name);
|
||||||
|
db->free_str(sql_xml);
|
||||||
|
|
||||||
|
error_str = "Error transforming the Datastore to XML.";
|
||||||
|
|
||||||
|
goto error_common;
|
||||||
|
|
||||||
|
error_body:
|
||||||
|
db->free_str(sql_name);
|
||||||
|
goto error_generic;
|
||||||
|
|
||||||
|
error_name:
|
||||||
|
goto error_generic;
|
||||||
|
|
||||||
|
error_generic:
|
||||||
|
error_str = "Error inserting Datastore in DB.";
|
||||||
|
error_common:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
string& Datastore::to_xml(string& xml) const
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
string collection_xml;
|
||||||
|
|
||||||
|
ObjectCollection::to_xml(collection_xml);
|
||||||
|
|
||||||
|
oss <<
|
||||||
|
"<DATASTORE>" <<
|
||||||
|
"<ID>" << oid << "</ID>" <<
|
||||||
|
"<NAME>" << name << "</NAME>" <<
|
||||||
|
collection_xml <<
|
||||||
|
"</DATASTORE>";
|
||||||
|
|
||||||
|
xml = oss.str();
|
||||||
|
|
||||||
|
return xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
int Datastore::from_xml(const string& xml)
|
||||||
|
{
|
||||||
|
int rc = 0;
|
||||||
|
vector<xmlNodePtr> content;
|
||||||
|
|
||||||
|
// Initialize the internal XML object
|
||||||
|
update_from_str(xml);
|
||||||
|
|
||||||
|
// Get class base attributes
|
||||||
|
rc += xpath(oid, "/DATASTORE/ID", -1);
|
||||||
|
rc += xpath(name,"/DATASTORE/NAME", "not_found");
|
||||||
|
|
||||||
|
// Set the owner and group to oneadmin
|
||||||
|
set_user(0, "");
|
||||||
|
set_group(GroupPool::ONEADMIN_ID, GroupPool::ONEADMIN_NAME);
|
||||||
|
|
||||||
|
// Get associated classes
|
||||||
|
ObjectXML::get_nodes("/DATASTORE/IMAGES", content);
|
||||||
|
|
||||||
|
if (content.empty())
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set of IDs
|
||||||
|
rc += ObjectCollection::from_xml_node(content[0]);
|
||||||
|
|
||||||
|
ObjectXML::free_nodes(content);
|
||||||
|
|
||||||
|
if (rc != 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
/* ------------------------------------------------------------------------ */
|
||||||
|
|
103
src/datastore/DatastorePool.cc
Normal file
103
src/datastore/DatastorePool.cc
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */
|
||||||
|
/* */
|
||||||
|
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
|
||||||
|
/* not use this file except in compliance with the License. You may obtain */
|
||||||
|
/* a copy of the License at */
|
||||||
|
/* */
|
||||||
|
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
||||||
|
/* */
|
||||||
|
/* Unless required by applicable law or agreed to in writing, software */
|
||||||
|
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
||||||
|
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
||||||
|
/* See the License for the specific language governing permissions and */
|
||||||
|
/* limitations under the License. */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
#include "DatastorePool.h"
|
||||||
|
#include "Nebula.h"
|
||||||
|
#include "NebulaLog.h"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
int DatastorePool::allocate(string name, int * oid, string& error_str)
|
||||||
|
{
|
||||||
|
Datastore * datastore;
|
||||||
|
ostringstream oss;
|
||||||
|
|
||||||
|
if ( name.empty() )
|
||||||
|
{
|
||||||
|
goto error_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( name.length() > 128 )
|
||||||
|
{
|
||||||
|
goto error_name_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for duplicates
|
||||||
|
datastore = get(name, false);
|
||||||
|
|
||||||
|
if( datastore != 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);
|
||||||
|
|
||||||
|
return *oid;
|
||||||
|
|
||||||
|
error_name:
|
||||||
|
oss << "NAME cannot be empty.";
|
||||||
|
goto error_common;
|
||||||
|
|
||||||
|
error_name_length:
|
||||||
|
oss << "NAME is too long; max length is 128 chars.";
|
||||||
|
goto error_common;
|
||||||
|
|
||||||
|
error_duplicated:
|
||||||
|
oss << "NAME is already taken by DATASTORE " << datastore->get_oid() << ".";
|
||||||
|
|
||||||
|
error_common:
|
||||||
|
*oid = -1;
|
||||||
|
error_str = oss.str();
|
||||||
|
|
||||||
|
return *oid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
int DatastorePool::drop(PoolObjectSQL * objsql, string& error_msg)
|
||||||
|
{
|
||||||
|
Datastore * datastore = static_cast<Datastore*>(objsql);
|
||||||
|
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
if( datastore->get_collection_size() > 0 )
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
oss << "Datastore " << datastore->get_oid() << " is not empty.";
|
||||||
|
error_msg = oss.str();
|
||||||
|
NebulaLog::log("DATASTORE", Log::ERROR, error_msg);
|
||||||
|
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = datastore->drop(db);
|
||||||
|
|
||||||
|
if( rc != 0 )
|
||||||
|
{
|
||||||
|
error_msg = "SQL DB error";
|
||||||
|
rc = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
30
src/datastore/SConstruct
Normal file
30
src/datastore/SConstruct
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# SConstruct for src/datastore
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------- #
|
||||||
|
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
|
||||||
|
# #
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
||||||
|
# not use this file except in compliance with the License. You may obtain #
|
||||||
|
# a copy of the License at #
|
||||||
|
# #
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0 #
|
||||||
|
# #
|
||||||
|
# Unless required by applicable law or agreed to in writing, software #
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, #
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
||||||
|
# See the License for the specific language governing permissions and #
|
||||||
|
# limitations under the License. #
|
||||||
|
#--------------------------------------------------------------------------- #
|
||||||
|
|
||||||
|
Import('env')
|
||||||
|
|
||||||
|
lib_name='nebula_datastore'
|
||||||
|
|
||||||
|
# Sources to generate the library
|
||||||
|
source_files=[
|
||||||
|
'DatastorePool.cc',
|
||||||
|
'Datastore.cc'
|
||||||
|
]
|
||||||
|
|
||||||
|
# Build library
|
||||||
|
env.StaticLibrary(lib_name, source_files)
|
@ -35,7 +35,7 @@ HostPool::HostPool(SqlDB* db,
|
|||||||
const string& remotes_location)
|
const string& remotes_location)
|
||||||
: PoolSQL(db,Host::table)
|
: PoolSQL(db,Host::table)
|
||||||
{
|
{
|
||||||
// ------------------ Initialize Hooks fot the pool ----------------------
|
// ------------------ Initialize Hooks for the pool ----------------------
|
||||||
|
|
||||||
const VectorAttribute * vattr;
|
const VectorAttribute * vattr;
|
||||||
|
|
||||||
|
@ -311,6 +311,8 @@ void Nebula::start()
|
|||||||
img_restricted_attrs);
|
img_restricted_attrs);
|
||||||
|
|
||||||
tpool = new VMTemplatePool(db);
|
tpool = new VMTemplatePool(db);
|
||||||
|
|
||||||
|
dspool = new DatastorePool(db);
|
||||||
}
|
}
|
||||||
catch (exception&)
|
catch (exception&)
|
||||||
{
|
{
|
||||||
|
@ -41,6 +41,7 @@ env.Prepend(LIBS=[
|
|||||||
'nebula_dm',
|
'nebula_dm',
|
||||||
'nebula_tm',
|
'nebula_tm',
|
||||||
'nebula_um',
|
'nebula_um',
|
||||||
|
'nebula_datastore',
|
||||||
'nebula_group',
|
'nebula_group',
|
||||||
'nebula_authm',
|
'nebula_authm',
|
||||||
'nebula_acl',
|
'nebula_acl',
|
||||||
|
@ -263,6 +263,7 @@ void RequestManager::register_xml_methods()
|
|||||||
xmlrpc_c::methodPtr template_allocate(new TemplateAllocate());
|
xmlrpc_c::methodPtr template_allocate(new TemplateAllocate());
|
||||||
xmlrpc_c::methodPtr host_allocate(new HostAllocate());
|
xmlrpc_c::methodPtr host_allocate(new HostAllocate());
|
||||||
xmlrpc_c::methodPtr user_allocate(new UserAllocate());
|
xmlrpc_c::methodPtr user_allocate(new UserAllocate());
|
||||||
|
xmlrpc_c::methodPtr datastore_allocate(new DatastoreAllocate());
|
||||||
|
|
||||||
// Delete Methods
|
// Delete Methods
|
||||||
xmlrpc_c::methodPtr host_delete(new HostDelete());
|
xmlrpc_c::methodPtr host_delete(new HostDelete());
|
||||||
@ -271,6 +272,7 @@ void RequestManager::register_xml_methods()
|
|||||||
xmlrpc_c::methodPtr vn_delete(new VirtualNetworkDelete());
|
xmlrpc_c::methodPtr vn_delete(new VirtualNetworkDelete());
|
||||||
xmlrpc_c::methodPtr user_delete(new UserDelete());
|
xmlrpc_c::methodPtr user_delete(new UserDelete());
|
||||||
xmlrpc_c::methodPtr image_delete(new ImageDelete());
|
xmlrpc_c::methodPtr image_delete(new ImageDelete());
|
||||||
|
xmlrpc_c::methodPtr datastore_delete(new DatastoreDelete());
|
||||||
|
|
||||||
// Info Methods
|
// Info Methods
|
||||||
xmlrpc_c::methodPtr vm_info(new VirtualMachineInfo());
|
xmlrpc_c::methodPtr vm_info(new VirtualMachineInfo());
|
||||||
@ -280,13 +282,13 @@ void RequestManager::register_xml_methods()
|
|||||||
xmlrpc_c::methodPtr vn_info(new VirtualNetworkInfo());
|
xmlrpc_c::methodPtr vn_info(new VirtualNetworkInfo());
|
||||||
xmlrpc_c::methodPtr user_info(new UserInfo());
|
xmlrpc_c::methodPtr user_info(new UserInfo());
|
||||||
xmlrpc_c::methodPtr image_info(new ImageInfo());
|
xmlrpc_c::methodPtr image_info(new ImageInfo());
|
||||||
|
xmlrpc_c::methodPtr datastore_info(new DatastoreInfo());
|
||||||
|
|
||||||
// PoolInfo Methods
|
// PoolInfo Methods
|
||||||
xmlrpc_c::methodPtr hostpool_info(new HostPoolInfo());
|
xmlrpc_c::methodPtr hostpool_info(new HostPoolInfo());
|
||||||
xmlrpc_c::methodPtr grouppool_info(new GroupPoolInfo());
|
xmlrpc_c::methodPtr grouppool_info(new GroupPoolInfo());
|
||||||
xmlrpc_c::methodPtr userpool_info(new UserPoolInfo());
|
xmlrpc_c::methodPtr userpool_info(new UserPoolInfo());
|
||||||
|
xmlrpc_c::methodPtr datastorepool_info(new DatastorePoolInfo());
|
||||||
// PoolInfo Methods with Filtering
|
|
||||||
xmlrpc_c::methodPtr vm_pool_info(new VirtualMachinePoolInfo());
|
xmlrpc_c::methodPtr vm_pool_info(new VirtualMachinePoolInfo());
|
||||||
xmlrpc_c::methodPtr template_pool_info(new TemplatePoolInfo());
|
xmlrpc_c::methodPtr template_pool_info(new TemplatePoolInfo());
|
||||||
xmlrpc_c::methodPtr vnpool_info(new VirtualNetworkPoolInfo());
|
xmlrpc_c::methodPtr vnpool_info(new VirtualNetworkPoolInfo());
|
||||||
@ -399,6 +401,14 @@ void RequestManager::register_xml_methods()
|
|||||||
RequestManagerRegistry.addMethod("one.acl.addrule", acl_addrule);
|
RequestManagerRegistry.addMethod("one.acl.addrule", acl_addrule);
|
||||||
RequestManagerRegistry.addMethod("one.acl.delrule", acl_delrule);
|
RequestManagerRegistry.addMethod("one.acl.delrule", acl_delrule);
|
||||||
RequestManagerRegistry.addMethod("one.acl.info", acl_info);
|
RequestManagerRegistry.addMethod("one.acl.info", acl_info);
|
||||||
|
|
||||||
|
/* Datastore related methods */
|
||||||
|
RequestManagerRegistry.addMethod("one.datastore.allocate",datastore_allocate);
|
||||||
|
RequestManagerRegistry.addMethod("one.datastore.delete", datastore_delete);
|
||||||
|
RequestManagerRegistry.addMethod("one.datastore.info", datastore_info);
|
||||||
|
|
||||||
|
RequestManagerRegistry.addMethod("one.datastorepool.info",datastorepool_info);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
@ -265,3 +265,19 @@ int GroupAllocate::pool_allocate(xmlrpc_c::paramList const& paramList,
|
|||||||
return gpool->allocate(gname, &id, error_str);
|
return gpool->allocate(gname, &id, error_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
int DatastoreAllocate::pool_allocate(
|
||||||
|
xmlrpc_c::paramList const& paramList,
|
||||||
|
Template * tmpl,
|
||||||
|
int& id,
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
@ -121,6 +121,16 @@ void UserPoolInfo::request_execute(
|
|||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
void DatastorePoolInfo::request_execute(
|
||||||
|
xmlrpc_c::paramList const& paramList,
|
||||||
|
RequestAttributes& att)
|
||||||
|
{
|
||||||
|
dump(att, ALL, -1, -1, "", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
void RequestManagerPoolInfoFilter::dump(
|
void RequestManagerPoolInfoFilter::dump(
|
||||||
RequestAttributes& att,
|
RequestAttributes& att,
|
||||||
int filter_flag,
|
int filter_flag,
|
||||||
|
Loading…
Reference in New Issue
Block a user