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

Feature #1112: Associate VNets to Clusters. New VNets are created in the "default" cluster

This commit is contained in:
Carlos Martín 2012-02-29 18:09:47 +01:00
parent a04102ce78
commit b49a249943
12 changed files with 175 additions and 24 deletions

View File

@ -156,6 +156,7 @@ public:
RequestManagerAllocate("VirtualNetworkAllocate", RequestManagerAllocate("VirtualNetworkAllocate",
"Allocates a new virtual network", "Allocates a new virtual network",
"A:ss", "A:ss",
true,
true) true)
{ {
Nebula& nd = Nebula::instance(); Nebula& nd = Nebula::instance();
@ -172,11 +173,17 @@ public:
return new VirtualNetworkTemplate; return new VirtualNetworkTemplate;
}; };
int pool_allocate(xmlrpc_c::paramList const& _paramList, int pool_allocate(xmlrpc_c::paramList const& _paramList,
Template * tmpl, Template * tmpl,
int& id, int& id,
string& error_str, string& error_str,
RequestAttributes& att); RequestAttributes& att,
int cluster_id,
const string& cluster_name);
int get_cluster_id(xmlrpc_c::paramList const& paramList);
int add_to_cluster(Cluster* cluster, int id, string& error_msg);
}; };
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */

View File

@ -38,6 +38,7 @@ protected:
clpool = nd.get_clpool(); clpool = nd.get_clpool();
hpool = nd.get_hpool(); hpool = nd.get_hpool();
dspool = nd.get_dspool(); dspool = nd.get_dspool();
vnpool = nd.get_vnpool();
auth_object = PoolObjectSQL::CLUSTER; auth_object = PoolObjectSQL::CLUSTER;
auth_op = AuthRequest::ADMIN; auth_op = AuthRequest::ADMIN;
@ -50,6 +51,7 @@ protected:
ClusterPool * clpool; ClusterPool * clpool;
HostPool * hpool; HostPool * hpool;
DatastorePool * dspool; DatastorePool * dspool;
VirtualNetworkPool * vnpool;
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
@ -145,6 +147,44 @@ public:
}; };
}; };
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class ClusterAddVNet : public RequestManagerCluster
{
public:
ClusterAddVNet():
RequestManagerCluster("ClusterAddVNet",
"Adds a virtual network to the cluster",
"A:sii"){};
~ClusterAddVNet(){};
void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att)
{
return add_generic(_paramList, att, vnpool, PoolObjectSQL::NET);
}
virtual int add_object(Cluster* cluster, int id, string& error_msg)
{
return cluster->add_vnet(id, error_msg);
};
virtual int del_object(Cluster* cluster, int id, string& error_msg)
{
return cluster->del_vnet(id, error_msg);
};
virtual void get(int oid, bool lock, PoolObjectSQL ** object, Clusterable ** cluster_obj)
{
VirtualNetwork * vnet = vnpool->get(oid, lock);
*object = static_cast<PoolObjectSQL *>(vnet);
*cluster_obj = static_cast<Clusterable *>(vnet);
};
};
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View File

@ -105,7 +105,8 @@ class VirtualNetworkDelete: public RequestManagerDelete
public: public:
VirtualNetworkDelete(): VirtualNetworkDelete():
RequestManagerDelete("VirtualNetworkDelete", RequestManagerDelete("VirtualNetworkDelete",
"Deletes a virtual network") "Deletes a virtual network",
true)
{ {
Nebula& nd = Nebula::instance(); Nebula& nd = Nebula::instance();
pool = nd.get_vnpool(); pool = nd.get_vnpool();
@ -113,6 +114,18 @@ public:
}; };
~VirtualNetworkDelete(){}; ~VirtualNetworkDelete(){};
/* -------------------------------------------------------------------- */
int get_cluster_id(PoolObjectSQL * object)
{
return static_cast<VirtualNetwork*>(object)->get_cluster_id();
};
int del_from_cluster(Cluster* cluster, int id, string& error_msg)
{
return cluster->del_vnet(id, error_msg);
};
}; };
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */

View File

@ -21,6 +21,7 @@
#include "PoolSQL.h" #include "PoolSQL.h"
#include "Leases.h" #include "Leases.h"
#include "VirtualNetworkTemplate.h" #include "VirtualNetworkTemplate.h"
#include "Clusterable.h"
#include <vector> #include <vector>
#include <string> #include <string>
@ -39,7 +40,7 @@ using namespace std;
* leases. One lease is formed by one IP and one MAC address. * leases. One lease is formed by one IP and one MAC address.
* MAC address are derived from IP addresses. * MAC address are derived from IP addresses.
*/ */
class VirtualNetwork : public PoolObjectSQL class VirtualNetwork : public PoolObjectSQL, public Clusterable
{ {
public: public:
@ -287,6 +288,8 @@ private:
int gid, int gid,
const string& _uname, const string& _uname,
const string& _gname, const string& _gname,
int _cluster_id,
const string& _cluster_name,
VirtualNetworkTemplate * _vn_template = 0); VirtualNetworkTemplate * _vn_template = 0);
~VirtualNetwork(); ~VirtualNetwork();

View File

@ -45,6 +45,8 @@ public:
* @param gid the id of the group this object is assigned to * @param gid the id of the group this object is assigned to
* @param vn_template a VirtualNetworkTemplate describing the VNET * @param vn_template a VirtualNetworkTemplate describing the VNET
* @param oid the id assigned to the VM (output) * @param oid the id assigned to the VM (output)
* @param cluster_id the id of the cluster this VNET will belong to
* @param cluster_name the name of the cluster this VNET will belong to
* @param error_str Returns the error reason, if any * @param error_str Returns the error reason, if any
* @return oid on success, -1 error * @return oid on success, -1 error
*/ */
@ -55,6 +57,8 @@ public:
const string& gname, const string& gname,
VirtualNetworkTemplate * vn_template, VirtualNetworkTemplate * vn_template,
int * oid, int * oid,
int cluster_id,
const string& cluster_name,
string& error_str); string& error_str);
/** /**
@ -163,7 +167,7 @@ private:
*/ */
PoolObjectSQL * create() PoolObjectSQL * create()
{ {
return new VirtualNetwork(-1,-1,"","",0); return new VirtualNetwork(-1,-1,"","",-1,"",0);
}; };
/** /**

View File

@ -78,5 +78,10 @@ class OneClusterHelper < OpenNebulaHelper::OneHelper
puts "%-15s" % [id] puts "%-15s" % [id]
end end
puts
CLIHelper.print_header("%-15s" % ["VNETS"])
cluster.vnet_ids.each do |id|
puts "%-15s" % [id]
end
end end
end end

View File

@ -117,4 +117,15 @@ cmd=CommandParser::CmdParser.new(ARGV) do
cluster.adddatastore(args[1].to_i) cluster.adddatastore(args[1].to_i)
end end
end end
addvnet_desc = <<-EOT.unindent
Adds a Virtual Network to the given Cluster
EOT
# TODO: allow the second param to be [:range, :vnetid_list]
command :addvnet, addvnet_desc,:clusterid, :vnetid do
helper.perform_actions(args[0],options,"updated") do |cluster|
cluster.addvnet(args[1].to_i)
end
end
end end

View File

@ -28,7 +28,8 @@ module OpenNebula
:allocate => "cluster.allocate", :allocate => "cluster.allocate",
:delete => "cluster.delete", :delete => "cluster.delete",
:addhost => "cluster.addhost", :addhost => "cluster.addhost",
:adddatastore => "cluster.adddatastore" :adddatastore => "cluster.adddatastore",
:addvnet => "cluster.addvnet"
} }
# Creates a Cluster description with just its identifier # Creates a Cluster description with just its identifier
@ -100,6 +101,19 @@ module OpenNebula
return rc return rc
end end
# Adds a VNet to this Cluster
# @param vnet_id [Integer] VNet ID
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def addvnet(vnet_id)
return Error.new('ID not defined') if !@pe_id
rc = @client.call(CLUSTER_METHODS[:addvnet], @pe_id, vnet_id)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
# --------------------------------------------------------------------- # ---------------------------------------------------------------------
# Helpers to get information # Helpers to get information
# --------------------------------------------------------------------- # ---------------------------------------------------------------------
@ -149,5 +163,28 @@ module OpenNebula
return array return array
end end
# Returns whether or not the vnet with 'id' is part of this cluster
# @param id [Integer] vnet ID
# @return [Boolean] true if found
def contains_vnet(id)
#This doesn't work in ruby 1.8.5
#return self["HOSTS/ID[.=#{uid}]"] != nil
id_array = retrieve_elements('VNETS/ID')
return id_array != nil && id_array.include?(id.to_s)
end
# Returns an array with the numeric vnet ids
# @return [Array<Integer>]
def vnet_ids
array = Array.new
self.each("VNETS/ID") do |id|
array << id.text.to_i
end
return array
end
end end
end end

View File

@ -328,6 +328,7 @@ void RequestManager::register_xml_methods()
// Cluster Methods // Cluster Methods
xmlrpc_c::methodPtr cluster_addhost(new ClusterAddHost()); xmlrpc_c::methodPtr cluster_addhost(new ClusterAddHost());
xmlrpc_c::methodPtr cluster_addds(new ClusterAddDatastore()); xmlrpc_c::methodPtr cluster_addds(new ClusterAddDatastore());
xmlrpc_c::methodPtr cluster_addvnet(new ClusterAddVNet());
/* VM related methods */ /* VM related methods */
RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy); RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy);
@ -424,6 +425,7 @@ void RequestManager::register_xml_methods()
RequestManagerRegistry.addMethod("one.cluster.info", cluster_info); RequestManagerRegistry.addMethod("one.cluster.info", cluster_info);
RequestManagerRegistry.addMethod("one.cluster.addhost", cluster_addhost); RequestManagerRegistry.addMethod("one.cluster.addhost", cluster_addhost);
RequestManagerRegistry.addMethod("one.cluster.adddatastore", cluster_addds); RequestManagerRegistry.addMethod("one.cluster.adddatastore", cluster_addds);
RequestManagerRegistry.addMethod("one.cluster.addvnet", cluster_addvnet);
RequestManagerRegistry.addMethod("one.clusterpool.info",clusterpool_info); RequestManagerRegistry.addMethod("one.clusterpool.info",clusterpool_info);
}; };

View File

@ -218,17 +218,35 @@ int VirtualMachineAllocate::pool_allocate(xmlrpc_c::paramList const& paramList,
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualNetworkAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, int VirtualNetworkAllocate::pool_allocate(
Template * tmpl, xmlrpc_c::paramList const& paramList,
int& id, Template * tmpl,
string& error_str, int& id,
RequestAttributes& att) string& error_str,
RequestAttributes& att,
int cluster_id,
const string& cluster_name)
{ {
VirtualNetworkPool * vpool = static_cast<VirtualNetworkPool *>(pool); VirtualNetworkPool * vpool = static_cast<VirtualNetworkPool *>(pool);
VirtualNetworkTemplate * vtmpl=static_cast<VirtualNetworkTemplate *>(tmpl); VirtualNetworkTemplate * vtmpl=static_cast<VirtualNetworkTemplate *>(tmpl);
return vpool->allocate(att.uid, att.gid, att.uname, att.gname, vtmpl, &id, return vpool->allocate(att.uid, att.gid, att.uname, att.gname, vtmpl, &id,
error_str); cluster_id, cluster_name, error_str);
}
/* -------------------------------------------------------------------------- */
int VirtualNetworkAllocate::get_cluster_id(xmlrpc_c::paramList const& paramList)
{
return ClusterPool::DEFAULT_CLUSTER_ID;
}
/* -------------------------------------------------------------------------- */
int VirtualNetworkAllocate::add_to_cluster(
Cluster* cluster, int id, string& error_msg)
{
return cluster->add_vnet(id, error_msg);
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View File

@ -35,8 +35,11 @@ VirtualNetwork::VirtualNetwork(int _uid,
int _gid, int _gid,
const string& _uname, const string& _uname,
const string& _gname, const string& _gname,
int _cluster_id,
const string& _cluster_name,
VirtualNetworkTemplate * _vn_template): VirtualNetworkTemplate * _vn_template):
PoolObjectSQL(-1,NET,"",_uid,_gid,_uname,_gname,table), PoolObjectSQL(-1,NET,"",_uid,_gid,_uname,_gname,table),
Clusterable(_cluster_id, _cluster_name),
bridge(""), bridge(""),
type(UNINITIALIZED), type(UNINITIALIZED),
leases(0) leases(0)
@ -474,16 +477,18 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const
os << os <<
"<VNET>" << "<VNET>" <<
"<ID>" << oid << "</ID>" << "<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" << "<UID>" << uid << "</UID>" <<
"<GID>" << gid << "</GID>" << "<GID>" << gid << "</GID>" <<
"<UNAME>" << uname << "</UNAME>" << "<UNAME>" << uname << "</UNAME>" <<
"<GNAME>" << gname << "</GNAME>" << "<GNAME>" << gname << "</GNAME>" <<
"<NAME>" << name << "</NAME>" << "<NAME>" << name << "</NAME>" <<
perms_to_xml(perm_str) << perms_to_xml(perm_str) <<
"<TYPE>" << type << "</TYPE>" << "<CLUSTER_ID>" << cluster_id << "</CLUSTER_ID>" <<
"<BRIDGE>" << bridge << "</BRIDGE>"<< "<CLUSTER>" << cluster << "</CLUSTER>" <<
"<VLAN>" << vlan << "</VLAN>"; "<TYPE>" << type << "</TYPE>" <<
"<BRIDGE>" << bridge << "</BRIDGE>"<<
"<VLAN>" << vlan << "</VLAN>";
if (!phydev.empty()) if (!phydev.empty())
{ {
@ -566,6 +571,9 @@ int VirtualNetwork::from_xml(const string &xml_str)
rc += xpath(bridge, "/VNET/BRIDGE", "not_found"); rc += xpath(bridge, "/VNET/BRIDGE", "not_found");
rc += xpath(vlan, "/VNET/VLAN", 0); rc += xpath(vlan, "/VNET/VLAN", 0);
rc += xpath(cluster_id, "/VNET/CLUSTER_ID", -1);
rc += xpath(cluster, "/VNET/CLUSTER", "not_found");
// Permissions // Permissions
rc += perms_from_xml(); rc += perms_from_xml();

View File

@ -78,6 +78,8 @@ int VirtualNetworkPool::allocate (
const string& gname, const string& gname,
VirtualNetworkTemplate * vn_template, VirtualNetworkTemplate * vn_template,
int * oid, int * oid,
int cluster_id,
const string& cluster_name,
string& error_str) string& error_str)
{ {
VirtualNetwork * vn; VirtualNetwork * vn;
@ -85,7 +87,8 @@ int VirtualNetworkPool::allocate (
string name; string name;
ostringstream oss; ostringstream oss;
vn = new VirtualNetwork(uid, gid, uname, gname, vn_template); vn = new VirtualNetwork(uid, gid, uname, gname,
cluster_id, cluster_name, vn_template);
// Check name // Check name
vn->get_template_attribute("NAME", name); vn->get_template_attribute("NAME", name);