diff --git a/include/Cluster.h b/include/Cluster.h index 2aa93e5e8d..274720ff3f 100644 --- a/include/Cluster.h +++ b/include/Cluster.h @@ -32,20 +32,13 @@ class Cluster : public PoolObjectSQL public: /** - * Returns the SYSTEM_DS attribute, or the system DS id if it is not defined + * Returns the SYSTEM_DS attribute * - * @return the SYSTEM_DS attribute, or the system DS id if it is not defined + * @return the SYSTEM_DS attribute */ int get_ds_id() { - int ds_id; - - if ( obj_template->get("SYSTEM_DS", ds_id) == false ) - { - ds_id = DatastorePool::SYSTEM_DS_ID; - } - - return ds_id; + return system_ds; } /** @@ -103,29 +96,7 @@ public: * @param error_msg Error message, if any * @return 0 on success */ - int add_datastore(int id, string& error_msg) - { - // TODO: should fail for any system DS? - if ( id == DatastorePool::SYSTEM_DS_ID ) - { - ostringstream oss; - oss << "Datastore '"<< DatastorePool::SYSTEM_DS_NAME - << "' cannot be added to any cluster."; - - error_msg = oss.str(); - - return -1; - } - - int rc = datastores.add_collection_id(id); - - if ( rc < 0 ) - { - error_msg = "Datastore ID is already in the cluster set."; - } - - return rc; - } + int add_datastore(int id, string& error_msg); /** * Deletes this datastore ID from the set. @@ -133,17 +104,7 @@ public: * @param error_msg Error message, if any * @return 0 on success */ - int del_datastore(int id, string& error_msg) - { - int rc = datastores.del_collection_id(id); - - if ( rc < 0 ) - { - error_msg = "Datastore ID is not part of the cluster set."; - } - - return rc; - } + int del_datastore(int id, string& error_msg); /** * Adds this vnet ID to the set. @@ -219,13 +180,18 @@ private: virtual ~Cluster(){}; // ************************************************************************* - // Object Collections (Private) + // Attributes (Private) // ************************************************************************* ObjectCollection hosts; ObjectCollection datastores; ObjectCollection vnets; + /** + * System datastore id + */ + int system_ds; + // ************************************************************************* // DataBase implementation (Private) // ************************************************************************* diff --git a/src/cluster/Cluster.cc b/src/cluster/Cluster.cc index 8c25673843..cc220f55e3 100644 --- a/src/cluster/Cluster.cc +++ b/src/cluster/Cluster.cc @@ -45,7 +45,8 @@ Cluster::Cluster( PoolObjectSQL(id,CLUSTER,name,-1,-1,"","",table), hosts("HOSTS"), datastores("DATASTORES"), - vnets("VNETS") + vnets("VNETS"), + system_ds(DatastorePool::SYSTEM_DS_ID) { if (cl_template != 0) { @@ -113,6 +114,82 @@ string& Cluster::get_ds_location(string &ds_location) return ds_location; } +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Cluster::add_datastore(int id, string& error_msg) +{ + if ( id == DatastorePool::SYSTEM_DS_ID ) + { + ostringstream oss; + oss << "Datastore '"<< DatastorePool::SYSTEM_DS_NAME + << "' cannot be added to any cluster."; + + error_msg = oss.str(); + + return -1; + } + + // TODO: Do not lock DS here, take ds_type as an argument + + Datastore *ds = Nebula::instance().get_dspool()->get(id, true); + + if ( ds == 0 ) + { + return -1; + } + + Datastore::DatastoreType ds_type = ds->get_type(); + + ds->unlock(); + + if ( ds_type == Datastore::SYSTEM_DS ) + { + if ( system_ds != DatastorePool::SYSTEM_DS_ID ) + { + ostringstream oss; + oss << "Cluster " << oid << " already contains the System Datastore " + << system_ds << "."; + + error_msg = oss.str(); + + return -1; + } + } + + int rc = datastores.add_collection_id(id); + + if ( rc < 0 ) + { + error_msg = "Datastore ID is already in the cluster set."; + } + else if ( ds_type == Datastore::SYSTEM_DS ) + { + system_ds = id; + } + + return rc; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Cluster::del_datastore(int id, string& error_msg) +{ + int rc = datastores.del_collection_id(id); + + if ( rc < 0 ) + { + error_msg = "Datastore ID is not part of the cluster set."; + } + else if ( system_ds == id ) + { + system_ds = DatastorePool::SYSTEM_DS_ID; + } + + return rc; +} + /* ************************************************************************ */ /* Cluster :: Database Access Functions */ /* ************************************************************************ */ @@ -215,8 +292,9 @@ string& Cluster::to_xml(string& xml) const oss << "" << - "" << oid << "" << - "" << name << "" << + "" << oid << "" << + "" << name << "" << + "" << system_ds << "" << hosts.to_xml(host_collection_xml) << datastores.to_xml(ds_collection_xml) << @@ -241,8 +319,9 @@ int Cluster::from_xml(const string& xml) update_from_str(xml); // Get class base attributes - rc += xpath(oid, "/CLUSTER/ID", -1); - rc += xpath(name,"/CLUSTER/NAME", "not_found"); + rc += xpath(oid, "/CLUSTER/ID", -1); + rc += xpath(name, "/CLUSTER/NAME", "not_found"); + rc += xpath(system_ds, "/CLUSTER/SYSTEM_DS", -1); // Set oneadmin as the owner set_user(0,"");