1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-22 13:33:52 +03:00

Feature #965: New one.vn.hold and one.vn.release methods to mark IPs as used, without an associated VM. Includes Ruby OCA and CLI

This commit is contained in:
Carlos Martín 2011-11-29 16:12:00 +01:00
parent d591f01e6b
commit 4904cc964b
9 changed files with 250 additions and 19 deletions

View File

@ -102,6 +102,26 @@ public:
virtual int remove_leases(vector<const Attribute*>& vector_leases, virtual int remove_leases(vector<const Attribute*>& vector_leases,
string& error_msg) = 0; string& error_msg) = 0;
/**
* Holds a Lease, marking it as used
* @param vector_leases vector of VectorAttribute objects. For the
* moment, the vector can only contain one LEASE.
* @param error_msg If the action fails, this message contains
* the reason.
* @return 0 on success
*/
int hold_leases(vector<const Attribute*>& vector_leases, string& error_msg);
/**
* Releases a Lease on hold
* @param vector_leases vector of VectorAttribute objects. For the
* moment, the vector can only contain one LEASE.
* @param error_msg If the action fails, this message contains
* the reason.
* @return 0 on success
*/
int free_leases(vector<const Attribute*>& vector_leases, string& error_msg);
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------

View File

@ -93,6 +93,44 @@ public:
} }
}; };
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class VirtualNetworkHold : public RequestManagerVirtualNetwork
{
public:
VirtualNetworkHold():
RequestManagerVirtualNetwork("VirtualNetworkHold",
"Holds a virtual network Lease as used"){};
~VirtualNetworkHold(){};
int leases_action(VirtualNetwork * vn,
VirtualNetworkTemplate * tmpl,
string& error_str)
{
return vn->hold_leases(tmpl, error_str);
}
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class VirtualNetworkRelease : public RequestManagerVirtualNetwork
{
public:
VirtualNetworkRelease():
RequestManagerVirtualNetwork("VirtualNetworkRelease",
"Releases a virtual network Lease on hold"){};
~VirtualNetworkRelease(){};
int leases_action(VirtualNetwork * vn,
VirtualNetworkTemplate * tmpl,
string& error_str)
{
return vn->free_leases(tmpl, error_str);
}
};
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View File

@ -85,7 +85,7 @@ public:
/** /**
* Adds Leases to the virtual network (Only implemented for FIXED networks) * Adds Leases to the virtual network (Only implemented for FIXED networks)
* @param leases_template template in the form LEASES = [IP=XX, MAC=XX]. * @param leases template in the form LEASES = [IP=XX, MAC=XX].
* MAC is optional. The template can only contain one LEASE * MAC is optional. The template can only contain one LEASE
* definition. * definition.
* @param error_msg If the action fails, this message contains the reason. * @param error_msg If the action fails, this message contains the reason.
@ -96,7 +96,7 @@ public:
/** /**
* Removes Leases from the virtual network; if they are not used.(Only * Removes Leases from the virtual network; if they are not used.(Only
* implemented for FIXED networks) * implemented for FIXED networks)
* @param leases_template template in the form LEASES = [IP=XX]. * @param leases template in the form LEASES = [IP=XX].
* The template can only contain one LEASE definition. * The template can only contain one LEASE definition.
* @param error_msg If the action fails, this message contains * @param error_msg If the action fails, this message contains
* the reason. * the reason.
@ -104,6 +104,25 @@ public:
*/ */
int remove_leases(VirtualNetworkTemplate* leases, string& error_msg); int remove_leases(VirtualNetworkTemplate* leases, string& error_msg);
/**
* Holds a Lease, marking it as used
* @param leases template in the form LEASES = [IP=XX].
* The template can only contain one LEASE definition.
* @param error_msg If the action fails, this message contains the reason.
* @return 0 on success
*/
int hold_leases(VirtualNetworkTemplate * leases, string& error_msg);
/**
* Releases a Lease on hold
* @param leases template in the form LEASES = [IP=XX].
* The template can only contain one LEASE definition.
* @param error_msg If the action fails, this message contains
* the reason.
* @return 0 on success
*/
int free_leases(VirtualNetworkTemplate* leases, string& error_msg);
/** /**
* Gets a new lease for a specific VM * Gets a new lease for a specific VM
* @param vid VM identifier * @param vid VM identifier

View File

@ -93,8 +93,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Adds a lease to the Virtual Network Adds a lease to the Virtual Network
EOT EOT
command :addleases, 'Adds a lease to the Virtual Network', :vnetid, :ip, command :addleases, addleases_desc, :vnetid, :ip, [:mac, nil] do
[:mac, nil] do
helper.perform_action(args[0],options,"lease added") do |vn| helper.perform_action(args[0],options,"lease added") do |vn|
vn.addleases(args[1], args[2]) vn.addleases(args[1], args[2])
end end
@ -110,6 +109,26 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end end
end end
hold_desc = <<-EOT.unindent
Holds a Virtual Network lease, marking it as used
EOT
command :hold, hold_desc, :vnetid, :ip do
helper.perform_action(args[0],options,"lease on hold") do |vn|
vn.hold(args[1])
end
end
release_desc = <<-EOT.unindent
Releases a Virtual Network lease on hold
EOT
command :release, release_desc, :vnetid, :ip do
helper.perform_action(args[0],options,"lease released") do |vn|
vn.release(args[1])
end
end
publish_desc = <<-EOT.unindent publish_desc = <<-EOT.unindent
Publishes the given Virtual Network. A public Virtual Network can be Publishes the given Virtual Network. A public Virtual Network can be
seen and used by other Users in the Virtual Network's group seen and used by other Users in the Virtual Network's group

View File

@ -32,7 +32,9 @@ module OpenNebula
:addleases => "vn.addleases", :addleases => "vn.addleases",
:rmleases => "vn.rmleases", :rmleases => "vn.rmleases",
:chown => "vn.chown", :chown => "vn.chown",
:update => "vn.update" :update => "vn.update",
:hold => "vn.hold",
:release => "vn.release"
} }
VN_TYPES=%w{RANGED FIXED} VN_TYPES=%w{RANGED FIXED}
@ -128,6 +130,32 @@ module OpenNebula
return rc return rc
end end
# Holds a virtual network Lease as used
# @param ip [String] IP to hold
def hold(ip)
return Error.new('ID not defined') if !@pe_id
lease_template = "LEASES = [ IP = #{ip} ]"
rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
# Releases a virtual network Lease on hold
# @param ip [String] IP to release
def release(ip)
return Error.new('ID not defined') if !@pe_id
lease_template = "LEASES = [ IP = #{ip} ]"
rc = @client.call(VN_METHODS[:release], @pe_id, lease_template)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
# Changes the owner/group # Changes the owner/group
# uid:: _Integer_ the new owner id. Set to -1 to leave the current one # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
# gid:: _Integer_ the new group id. Set to -1 to leave the current one # gid:: _Integer_ the new group id. Set to -1 to leave the current one

View File

@ -246,6 +246,8 @@ void RequestManager::register_xml_methods()
// VirtualNetwork Methods // VirtualNetwork Methods
xmlrpc_c::methodPtr vn_addleases(new VirtualNetworkAddLeases()); xmlrpc_c::methodPtr vn_addleases(new VirtualNetworkAddLeases());
xmlrpc_c::methodPtr vn_rmleases(new VirtualNetworkRemoveLeases()); xmlrpc_c::methodPtr vn_rmleases(new VirtualNetworkRemoveLeases());
xmlrpc_c::methodPtr vn_hold(new VirtualNetworkHold());
xmlrpc_c::methodPtr vn_release(new VirtualNetworkRelease());
// Update Template Methods // Update Template Methods
xmlrpc_c::methodPtr image_update(new ImageUpdateTemplate()); xmlrpc_c::methodPtr image_update(new ImageUpdateTemplate());
@ -357,6 +359,8 @@ void RequestManager::register_xml_methods()
/* Network related methods*/ /* Network related methods*/
RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases); RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases);
RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases); RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases);
RequestManagerRegistry.addMethod("one.vn.hold", vn_hold);
RequestManagerRegistry.addMethod("one.vn.release", vn_release);
RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate); RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate);
RequestManagerRegistry.addMethod("one.vn.publish", vn_publish); RequestManagerRegistry.addMethod("one.vn.publish", vn_publish);
RequestManagerRegistry.addMethod("one.vn.update", vn_update); RequestManagerRegistry.addMethod("one.vn.update", vn_update);

View File

@ -80,7 +80,7 @@ void RequestManagerVirtualNetwork::
if ( rc < 0 ) if ( rc < 0 )
{ {
failure_response(INTERNAL, failure_response(INTERNAL,
request_error("Error modifiying network leases",error_str), request_error("Error modifying network leases",error_str),
att); att);
vn->unlock(); vn->unlock();

View File

@ -386,23 +386,11 @@ int Leases::update(SqlDB * db)
bool Leases::check(const string& ip) bool Leases::check(const string& ip)
{ {
map<unsigned int,Lease *>::iterator it;
unsigned int _ip; unsigned int _ip;
Leases::Lease::ip_to_number(ip,_ip); Leases::Lease::ip_to_number(ip,_ip);
return check(_ip);
it=leases.find(_ip);
if (it!=leases.end())
{
return it->second->used;
}
else
{
return false;
}
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -424,6 +412,95 @@ bool Leases::check(unsigned int ip)
} }
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::hold_leases(vector<const Attribute*>& vector_leases,
string& error_msg)
{
const VectorAttribute * single_attr_lease = 0;
int rc;
string ip;
string mac;
if ( vector_leases.size() > 0 )
{
single_attr_lease =
dynamic_cast<const VectorAttribute *>(vector_leases[0]);
}
if ( single_attr_lease == 0 )
{
error_msg = "Empty lease description.";
return -1;
}
ip = single_attr_lease->vector_value("IP");
if ( check(ip) )
{
error_msg = "Lease is in use.";
return -1;
}
rc = set(-1, ip, mac);
if ( rc != 0 )
{
error_msg = "Lease is not part of the NET.";
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::free_leases(vector<const Attribute*>& vector_leases,
string& error_msg)
{
const VectorAttribute * single_attr_lease = 0;
map<unsigned int,Lease *>::iterator it;
unsigned int i_ip;
string st_ip;
string mac;
if ( vector_leases.size() > 0 )
{
single_attr_lease =
dynamic_cast<const VectorAttribute *>(vector_leases[0]);
}
if ( single_attr_lease == 0 )
{
error_msg = "Empty lease description.";
return -1;
}
st_ip = single_attr_lease->vector_value("IP");
if ( Leases::Lease::ip_to_number(st_ip,i_ip) != 0 )
{
error_msg = "Wrong Lease format.";
return -1;
}
it = leases.find(i_ip);
if ( it == leases.end() || (it->second->used && it->second->vid != -1) )
{
error_msg = "Lease is not on hold.";
return -1;
}
release(st_ip);
return 0;
}
/* ************************************************************************** */ /* ************************************************************************** */
/* Leases :: Misc */ /* Leases :: Misc */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -646,3 +646,29 @@ int VirtualNetwork::remove_leases(VirtualNetworkTemplate * leases_template,
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualNetwork::hold_leases(VirtualNetworkTemplate * leases_template,
string& error_msg)
{
vector<const Attribute *> vector_leases;
leases_template->get("LEASES", vector_leases);
return leases->hold_leases(vector_leases, error_msg);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::free_leases(VirtualNetworkTemplate * leases_template,
string& error_msg)
{
vector<const Attribute *> vector_leases;
leases_template->get("LEASES", vector_leases);
return leases->free_leases(vector_leases, error_msg);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */