2012-02-09 20:56:47 +04:00
/* ------------------------------------------------------------------------ */
2013-01-24 19:03:57 +04:00
/* Copyright 2002-2013, OpenNebula Project Leads (OpenNebula.org) */
2012-02-09 20:56:47 +04:00
/* */
/* 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"
2012-02-16 22:37:08 +04:00
# include "DatastoreTemplate.h"
2012-02-28 15:17:33 +04:00
# include "Clusterable.h"
2012-04-19 13:44:19 +04:00
# include "Image.h"
2012-02-09 20:56:47 +04:00
/**
* The Datastore class .
*/
2012-02-28 15:17:33 +04:00
class Datastore : public PoolObjectSQL , ObjectCollection , public Clusterable
2012-02-09 20:56:47 +04:00
{
public :
2012-11-16 05:18:43 +04:00
/**
* Type of Datastore
*/
enum DatastoreType
{
IMAGE_DS = 0 , /** < Standard datastore for disk images */
SYSTEM_DS = 1 , /** < System datastore for disks of running VMs */
FILE_DS = 2 /** < File datastore for context, kernel, initrd files */
} ;
/**
2012-12-05 19:48:56 +04:00
* Return the string representation of a DatastoreType
2012-11-16 05:18:43 +04:00
* @ param ob the type
* @ return the string
*/
static string type_to_str ( DatastoreType ob )
{
switch ( ob )
{
case IMAGE_DS : return " IMAGE_DS " ; break ;
case SYSTEM_DS : return " SYSTEM_DS " ; break ;
case FILE_DS : return " FILE_DS " ; break ;
default : return " " ;
}
} ;
/**
2012-12-05 19:48:56 +04:00
* Return the string representation of a DatastoreType
2012-11-16 05:18:43 +04:00
* @ param str_type string representing the DatastoreTypr
2012-12-05 19:48:56 +04:00
* @ return the DatastoreType ( defaults to IMAGE_DS )
2012-11-16 05:18:43 +04:00
*/
static DatastoreType str_to_type ( string & str_type ) ;
2012-02-09 20:56:47 +04:00
/**
* 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 ) ;
2012-02-22 21:22:41 +04:00
} ;
2012-02-09 20:56:47 +04:00
/**
* 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 ) ;
2012-02-22 21:22:41 +04:00
} ;
/**
* Retrieves TM mad name
* @ return string tm mad name
*/
const string & get_tm_mad ( ) const
{
return tm_mad ;
} ;
2012-02-25 02:13:22 +04:00
/**
* Retrieves the base path
* @ return base path string
*/
const string & get_base_path ( ) const
{
return base_path ;
} ;
2012-04-19 13:44:19 +04:00
/**
* Retrieves the disk type
* @ return disk type
*/
Image : : DiskType get_disk_type ( ) const
{
return disk_type ;
} ;
2012-06-29 17:09:25 +04:00
/**
2012-11-16 05:18:43 +04:00
* Returns the datastore type
* @ return datastore type
2012-06-29 17:09:25 +04:00
*/
2012-11-16 05:18:43 +04:00
DatastoreType get_type ( ) const
2012-06-29 17:09:25 +04:00
{
2012-11-16 05:18:43 +04:00
return type ;
2012-06-29 17:09:25 +04:00
} ;
2012-02-22 21:22:41 +04:00
/**
* Modifies the given VM disk attribute adding the relevant datastore
* attributes
*
* @ param disk
* @ return 0 on success
*/
int disk_attribute ( VectorAttribute * disk ) ;
2012-02-09 20:56:47 +04:00
2012-03-05 19:47:59 +04:00
/**
* Replace template for this object . Object should be updated
* after calling this method
* @ param tmpl string representation of the template
*/
int replace_template ( const string & tmpl_str , string & error ) ;
2012-02-09 20:56:47 +04:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class DatastorePool ;
2012-02-16 22:37:08 +04:00
// *************************************************************************
// Datastore Private Attributes
// *************************************************************************
/**
2012-03-06 18:13:55 +04:00
* Name of the datastore driver used to register new images
2012-02-16 22:37:08 +04:00
*/
2012-03-06 18:13:55 +04:00
string ds_mad ;
2012-02-16 22:37:08 +04:00
2012-02-22 21:22:41 +04:00
/**
* Name of the TM driver used to transfer file to and from the hosts
*/
string tm_mad ;
2012-02-16 22:37:08 +04:00
/**
* Base path for the storage
*/
string base_path ;
2012-06-29 14:44:04 +04:00
/**
2012-11-16 05:18:43 +04:00
* The datastore type
2012-06-29 14:44:04 +04:00
*/
2012-11-16 05:18:43 +04:00
DatastoreType type ;
2012-06-29 14:44:04 +04:00
2012-04-19 13:44:19 +04:00
/**
* Disk types for the Images created in this datastore
*/
Image : : DiskType disk_type ;
2012-02-09 20:56:47 +04:00
// *************************************************************************
// Constructor
// *************************************************************************
2012-02-28 15:17:33 +04:00
Datastore (
2012-03-01 21:50:17 +04:00
int uid ,
int gid ,
const string & uname ,
const string & gname ,
2013-01-18 21:34:51 +04:00
int umask ,
2012-02-28 15:17:33 +04:00
DatastoreTemplate * ds_template ,
int cluster_id ,
const string & cluster_name ) ;
2012-02-09 20:56:47 +04:00
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
*/
2012-02-16 22:37:08 +04:00
int insert ( SqlDB * db , string & error_str ) ;
2012-02-09 20:56:47 +04:00
/**
* 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 ) ;
}
2012-03-02 20:25:42 +04:00
/**
2012-06-28 19:51:21 +04:00
* Factory method for datastore templates
2012-03-02 20:25:42 +04:00
*/
2012-03-14 18:48:06 +04:00
Template * get_new_template ( ) const
2012-03-02 20:25:42 +04:00
{
return new DatastoreTemplate ;
}
2012-02-09 20:56:47 +04:00
} ;
# endif /*DATASTORE_H_*/