1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-26 06:50:09 +03:00

Feature #3456: Add a state to DS, and a enable/disable action

This commit is contained in:
Carlos Martín 2015-02-13 16:37:24 +01:00
parent d60a5860b3
commit ab5483df14
8 changed files with 234 additions and 3 deletions

View File

@ -62,6 +62,30 @@ public:
*/
static DatastoreType str_to_type(string& str_type);
/**
* Datastore State
*/
enum DatastoreState
{
READY = 0, /** < Datastore ready to use */
DISABLED = 1 /** < System Datastore can not be used */
};
/**
* Returns the string representation of a DatastoreState
* @param state The state
* @return the string representation
*/
static string state_to_str(DatastoreState state)
{
switch(state)
{
case READY: return "READY"; break;
case DISABLED: return "DISABLED"; break;
default: return "";
}
};
/**
* Function to print the Datastore object into a string in XML format
* @param xml the resulting XML string
@ -200,6 +224,15 @@ public:
return shared;
};
/**
* Enable or disable the DS. Only for System DS.
* @param enable true to enable
* @param error_str Returns the error reason, if any
*
* @return 0 on success
*/
int enable(bool enable, string& error_str);
private:
// -------------------------------------------------------------------------
@ -252,6 +285,11 @@ private:
*/
long long used_mb;
/**
* Datastore state
*/
DatastoreState state;
// *************************************************************************
// Constructor
// *************************************************************************

View File

@ -0,0 +1,72 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* 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 REQUEST_MANAGER_DATASTORE_H
#define REQUEST_MANAGER_DATASTORE_H
#include "Request.h"
#include "Nebula.h"
using namespace std;
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class RequestManagerDatastore: public Request
{
protected:
RequestManagerDatastore(const string& method_name,
const string& help,
const string& params)
:Request(method_name,params,help)
{
Nebula& nd = Nebula::instance();
pool = nd.get_dspool();
auth_object = PoolObjectSQL::DATASTORE;
auth_op = AuthRequest::MANAGE;
};
~RequestManagerDatastore(){};
/* --------------------------------------------------------------------- */
virtual void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att) = 0;
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class DatastoreEnable : public RequestManagerDatastore
{
public:
DatastoreEnable():
RequestManagerDatastore("DatastoreEnable", "Enables or disables an datastore",
"A:sib"){};
~DatastoreEnable(){};
void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif

View File

@ -30,6 +30,7 @@
<xs:element name="BASE_PATH" type="xs:string"/>
<xs:element name="TYPE" type="xs:integer"/>
<xs:element name="DISK_TYPE" type="xs:integer"/>
<xs:element name="STATE" type="xs:integer"/>
<xs:element name="CLUSTER_ID" type="xs:integer"/>
<xs:element name="CLUSTER" type="xs:string"/>
<xs:element name="TOTAL_MB" type="xs:integer"/>

View File

@ -52,7 +52,8 @@ Datastore::Datastore(
type(IMAGE_DS),
total_mb(0),
free_mb(0),
used_mb(0)
used_mb(0),
state(READY)
{
if (ds_template != 0)
{
@ -76,6 +77,41 @@ Datastore::~Datastore()
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Datastore::enable(bool enable, string& error_str)
{
if (type != SYSTEM_DS)
{
error_str = "Only SYSTEM_DS can be disabled or enabled";
return -1;
}
if (enable)
{
if (state == READY)
{
error_str = "Datastore state is already READY";
return -1;
}
state = READY;
}
else
{
if (state == DISABLED)
{
error_str = "Datastore state is already DISABLED";
return -1;
}
state = DISABLED;
}
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Datastore::disk_attribute(
VectorAttribute * disk,
const vector<string>& inherit_attrs)
@ -495,6 +531,7 @@ string& Datastore::to_xml(string& xml) const
"<BASE_PATH><![CDATA[" << base_path << "]]></BASE_PATH>"<<
"<TYPE>" << type << "</TYPE>" <<
"<DISK_TYPE>" << disk_type << "</DISK_TYPE>" <<
"<STATE>" << state << "</STATE>" <<
"<CLUSTER_ID>" << cluster_id << "</CLUSTER_ID>" <<
"<CLUSTER>" << cluster << "</CLUSTER>" <<
"<TOTAL_MB>" << total_mb << "</TOTAL_MB>" <<
@ -517,6 +554,7 @@ int Datastore::from_xml(const string& xml)
int rc = 0;
int int_disk_type;
int int_ds_type;
int int_state;
vector<xmlNodePtr> content;
// Initialize the internal XML object
@ -532,8 +570,9 @@ int Datastore::from_xml(const string& xml)
rc += xpath(ds_mad, "/DATASTORE/DS_MAD", "not_found");
rc += xpath(tm_mad, "/DATASTORE/TM_MAD", "not_found");
rc += xpath(base_path, "/DATASTORE/BASE_PATH", "not_found");
rc += xpath(int_ds_type, "/DATASTORE/TYPE", -1);
rc += xpath(int_ds_type, "/DATASTORE/TYPE", -1);
rc += xpath(int_disk_type,"/DATASTORE/DISK_TYPE", -1);
rc += xpath(int_state, "/DATASTORE/STATE", 0);
rc += xpath(cluster_id, "/DATASTORE/CLUSTER_ID", -1);
rc += xpath(cluster, "/DATASTORE/CLUSTER", "not_found");
@ -547,6 +586,7 @@ int Datastore::from_xml(const string& xml)
disk_type = static_cast<Image::DiskType>(int_disk_type);
type = static_cast<Datastore::DatastoreType>(int_ds_type);
state = static_cast<DatastoreState>(int_state);
// Get associated classes
ObjectXML::get_nodes("/DATASTORE/IMAGES", content);

View File

@ -38,6 +38,7 @@
#include "RequestManagerCluster.h"
#include "RequestManagerGroup.h"
#include "RequestManagerVdc.h"
#include "RequestManagerDatastore.h"
#include "RequestManagerSystem.h"
#include "RequestManagerProxy.h"
@ -385,6 +386,9 @@ void RequestManager::register_xml_methods()
xmlrpc_c::methodPtr image_chtype(new ImageChangeType());
xmlrpc_c::methodPtr image_clone(new ImageClone());
// Datastore Methods
xmlrpc_c::methodPtr datastore_enable(new DatastoreEnable());
// Chown Methods
xmlrpc_c::methodPtr vm_chown(new VirtualMachineChown());
xmlrpc_c::methodPtr template_chown(new TemplateChown());
@ -669,6 +673,7 @@ void RequestManager::register_xml_methods()
RequestManagerRegistry.addMethod("one.datastore.chown", datastore_chown);
RequestManagerRegistry.addMethod("one.datastore.chmod", datastore_chmod);
RequestManagerRegistry.addMethod("one.datastore.rename", datastore_rename);
RequestManagerRegistry.addMethod("one.datastore.enable", datastore_enable);
RequestManagerRegistry.addMethod("one.datastorepool.info",datastorepool_info);

View File

@ -0,0 +1,74 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* 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 "RequestManagerDatastore.h"
using namespace std;
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
void DatastoreEnable::request_execute(xmlrpc_c::paramList const& paramList,
RequestAttributes& att)
{
int id = xmlrpc_c::value_int(paramList.getInt(1));
bool enable_flag = xmlrpc_c::value_boolean(paramList.getBoolean(2));
int rc;
Datastore * ds;
string err_msg;
if ( basic_authorization(id, att) == false )
{
return;
}
ds = static_cast<Datastore *>(pool->get(id,true));
if ( ds == 0 )
{
failure_response(NO_EXISTS,
get_error(object_name(auth_object),id),
att);
return;
}
rc = ds->enable(enable_flag, err_msg);
if ( rc != 0 )
{
if (enable_flag == true)
{
err_msg = "Could not enable Datastore: " + err_msg;
}
else
{
err_msg = "Could not disable Datastore: " + err_msg;
}
failure_response(INTERNAL,request_error(err_msg,""), att);
ds->unlock();
return;
}
pool->update(ds);
ds->unlock();
success_response(id, att);
}

View File

@ -45,6 +45,7 @@ source_files=[
'RequestManagerRename.cc',
'RequestManagerProxy.cc',
'RequestManagerVdc.cc',
'RequestManagerDatastore.cc',
]
# Build library

View File

@ -62,7 +62,7 @@ public:
protected:
int get_suitable_nodes(vector<xmlNodePtr>& content)
{
return get_nodes("/DATASTORE_POOL/DATASTORE[TYPE=1]", content);
return get_nodes("/DATASTORE_POOL/DATASTORE[TYPE=1 and STATE=0]", content);
};
};