1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-22 17:57:46 +03:00

Bug #1306: Add Template contents to Clusters

This commit is contained in:
Carlos Martín 2012-06-28 17:51:21 +02:00
parent ffc99eea2f
commit a6b2c86bfb
12 changed files with 165 additions and 10 deletions

View File

@ -20,6 +20,7 @@
#include "PoolSQL.h"
#include "ObjectCollection.h"
#include "DatastorePool.h"
#include "ClusterTemplate.h"
using namespace std;
@ -184,11 +185,9 @@ private:
// Constructor
// *************************************************************************
Cluster(int id, const string& name):
PoolObjectSQL(id,CLUSTER,name,-1,-1,"","",table),
hosts("HOSTS"),
datastores("DATASTORES"),
vnets("VNETS"){};
Cluster(int id,
const string& name,
ClusterTemplate* cl_template);
virtual ~Cluster(){};
@ -259,6 +258,14 @@ private:
* @return 0 if cluster can be dropped, -1 otherwise
*/
int check_drop(string& error_msg);
/**
* Factory method for cluster templates
*/
Template * get_new_template() const
{
return new ClusterTemplate;
}
};
#endif /*CLUSTER_H_*/

View File

@ -151,7 +151,7 @@ private:
*/
PoolObjectSQL * create()
{
return new Cluster(-1,"");
return new Cluster(-1,"",0);
};
};

39
include/ClusterTemplate.h Normal file
View File

@ -0,0 +1,39 @@
/* -------------------------------------------------------------------------- */
/* 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 CLUSTER_TEMPLATE_H_
#define CLUSTER_TEMPLATE_H_
#include "Template.h"
using namespace std;
/**
* Cluster Template class
*/
class ClusterTemplate : public Template
{
public:
ClusterTemplate():
Template(false,'=',"TEMPLATE"){};
~ClusterTemplate(){};
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif /*CLUSTER_TEMPLATE_H_*/

View File

@ -203,7 +203,7 @@ private:
}
/**
* Factory method for virtual network templates
* Factory method for datastore templates
*/
Template * get_new_template() const
{

View File

@ -170,6 +170,24 @@ public:
~DocumentUpdateTemplate(){};
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class ClusterUpdateTemplate : public RequestManagerUpdateTemplate
{
public:
ClusterUpdateTemplate():
RequestManagerUpdateTemplate("ClusterUpdateTemplate",
"Updates a cluster template")
{
Nebula& nd = Nebula::instance();
pool = nd.get_clpool();
auth_object = PoolObjectSQL::CLUSTER;
};
~ClusterUpdateTemplate(){};
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -99,6 +99,11 @@ class OneClusterHelper < OpenNebulaHelper::OneHelper
puts str % ["NAME", cluster.name]
puts
CLIHelper.print_header(str_h1 % "CLUSTER TEMPLATE", false)
puts cluster.template_str
puts
CLIHelper.print_header("%-15s" % ["HOSTS"])
cluster.host_ids.each do |id|
puts "%-15s" % [id]

View File

@ -173,4 +173,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do
cluster.delvnet(args[1].to_i)
end
end
update_desc = <<-EOT.unindent
Update the template contents. If a path is not provided the editor will
be launched to modify the current content.
EOT
command :update, update_desc, :clusterid, [:file, nil] do
helper.perform_action(args[0],options,"modified") do |obj|
str = OpenNebulaHelper.update_template(args[0], obj, args[1])
obj.update(str)
end
end
end

View File

@ -33,6 +33,29 @@ const char * Cluster::db_bootstrap = "CREATE TABLE IF NOT EXISTS cluster_pool ("
"gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, "
"UNIQUE(name))";
/* ************************************************************************ */
/* Cluster :: Constructor/Destructor */
/* ************************************************************************ */
Cluster::Cluster(
int id,
const string& name,
ClusterTemplate* cl_template):
PoolObjectSQL(id,CLUSTER,name,-1,-1,"","",table),
hosts("HOSTS"),
datastores("DATASTORES"),
vnets("VNETS")
{
if (cl_template != 0)
{
obj_template = cl_template;
}
else
{
obj_template = new ClusterTemplate;
}
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
@ -170,6 +193,7 @@ string& Cluster::to_xml(string& xml) const
string host_collection_xml;
string ds_collection_xml;
string vnet_collection_xml;
string template_xml;
oss <<
"<CLUSTER>" <<
@ -179,7 +203,7 @@ string& Cluster::to_xml(string& xml) const
hosts.to_xml(host_collection_xml) <<
datastores.to_xml(ds_collection_xml) <<
vnets.to_xml(vnet_collection_xml) <<
obj_template->to_xml(template_xml) <<
"</CLUSTER>";
xml = oss.str();
@ -256,6 +280,18 @@ int Cluster::from_xml(const string& xml)
ObjectXML::free_nodes(content);
content.clear();
// Get associated classes
ObjectXML::get_nodes("/CLUSTER/TEMPLATE", content);
if (content.empty())
{
return -1;
}
rc += obj_template->from_xml_node(content[0]);
ObjectXML::free_nodes(content);
if (rc != 0)
{
return -1;

View File

@ -72,7 +72,7 @@ int ClusterPool::allocate(string name, int * oid, string& error_str)
}
// Build a new Cluster object
cluster = new Cluster(-1, name);
cluster = new Cluster(-1, name, 0);
// Insert the Object in the pool
*oid = PoolSQL::allocate(cluster, error_str);

View File

@ -30,6 +30,7 @@ public class Cluster extends PoolElement{
private static final String ALLOCATE = METHOD_PREFIX + "allocate";
private static final String DELETE = METHOD_PREFIX + "delete";
private static final String INFO = METHOD_PREFIX + "info";
private static final String UPDATE = METHOD_PREFIX + "update";
private static final String ADDHOST = METHOD_PREFIX + "addhost";
private static final String DELHOST = METHOD_PREFIX + "delhost";
private static final String ADDDATASTORE = METHOD_PREFIX + "adddatastore";
@ -99,6 +100,19 @@ public class Cluster extends PoolElement{
return client.call(DELETE, id);
}
/**
* Replaces the cluster contents.
*
* @param client XML-RPC Client.
* @param id The id of the target cluster we want to modify.
* @param new_template New template contents.
* @return If successful the message contains the cluster id.
*/
public static OneResponse update(Client client, int id, String new_template)
{
return client.call(UPDATE, id, new_template);
}
/**
* Adds a Host to this Cluster
*
@ -210,6 +224,17 @@ public class Cluster extends PoolElement{
return delete(client, id);
}
/**
* Replaces the cluster template.
*
* @param new_template New cluster template.
* @return If successful the message contains the cluster id.
*/
public OneResponse update(String new_template)
{
return update(client, id, new_template);
}
/**
* Adds a Host to this Cluster
*

View File

@ -32,7 +32,8 @@ module OpenNebula
:adddatastore => "cluster.adddatastore",
:deldatastore => "cluster.deldatastore",
:addvnet => "cluster.addvnet",
:delvnet => "cluster.delvnet"
:delvnet => "cluster.delvnet",
:update => "cluster.update",
}
# Creates a Cluster description with just its identifier
@ -156,6 +157,16 @@ module OpenNebula
return rc
end
# Replaces the template contents
#
# @param new_template [String] New template contents
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def update(new_template)
super(CLUSTER_METHODS[:update], new_template)
end
# ---------------------------------------------------------------------
# Helpers to get information
# ---------------------------------------------------------------------

View File

@ -268,6 +268,7 @@ void RequestManager::register_xml_methods()
xmlrpc_c::methodPtr user_update(new UserUpdateTemplate());
xmlrpc_c::methodPtr datastore_update(new DatastoreUpdateTemplate());
xmlrpc_c::methodPtr doc_update(new DocumentUpdateTemplate());
xmlrpc_c::methodPtr cluster_update(new ClusterUpdateTemplate());
// Allocate Methods
xmlrpc_c::methodPtr vm_allocate(new VirtualMachineAllocate());
@ -468,6 +469,7 @@ void RequestManager::register_xml_methods()
RequestManagerRegistry.addMethod("one.cluster.allocate",cluster_allocate);
RequestManagerRegistry.addMethod("one.cluster.delete", cluster_delete);
RequestManagerRegistry.addMethod("one.cluster.info", cluster_info);
RequestManagerRegistry.addMethod("one.cluster.update", cluster_update);
RequestManagerRegistry.addMethod("one.cluster.addhost", cluster_addhost);
RequestManagerRegistry.addMethod("one.cluster.delhost", cluster_delhost);