1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

feature #966: Vnet can now be referred by name with NETWORK

This commit is contained in:
Ruben S. Montero 2011-11-12 23:39:10 +01:00
parent 0b417d6eb1
commit 535d1c06c4
4 changed files with 99 additions and 59 deletions

View File

@ -94,8 +94,7 @@ public:
* @param vid of the VM requesting the lease
* @return 0 on success,
* -1 error,
* -2 not using the pool,
* -3 unsupported NETWORK attribute
* -2 not using the pool
*/
int nic_attribute(VectorAttribute * nic, int uid, int vid);
@ -165,6 +164,19 @@ private:
{
return new VirtualNetwork(-1,-1,"","",0);
};
/**
* Function to get a VirtualNetwork by its name, as provided by a VM
* template
*/
VirtualNetwork * get_nic_by_name(VectorAttribute * nic,
const string& name,
int _uid);
/**
* Function to get a VirtualNetwork by its id, as provided by a VM template
*/
VirtualNetwork * get_nic_by_id(const string& id_s);
};
#endif /*VIRTUAL_NETWORK_POOL_H_*/

View File

@ -875,22 +875,12 @@ int VirtualMachine::get_network_leases(string& estr)
{
goto error_vnet;
}
else if ( rc == -3 )
{
goto error_name;
}
}
return 0;
error_vnet:
estr = "Could not get virtual network for VM.";
goto error_common;
error_name:
estr = "NETWORK is not supported for NIC. Use NETWORK_ID instead.";
error_common:
return -1;
}

View File

@ -15,7 +15,9 @@
/* -------------------------------------------------------------------------- */
#include "VirtualNetworkPool.h"
#include "UserPool.h"
#include "NebulaLog.h"
#include "Nebula.h"
#include "AuthManager.h"
#include <sstream>
@ -137,35 +139,86 @@ error_common:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualNetwork * VirtualNetworkPool::get_nic_by_name(VectorAttribute * nic,
const string& name,
int _uid)
{
istringstream is;
string uid_s ;
string uname;
int uid;
if (!(uid_s = nic->vector_value("NETWORK_UID")).empty())
{
is.str(uid_s);
is >> uid;
if( is.fail() )
{
return 0;
}
}
else if (!(uname = nic->vector_value("NETWORK_UNAME")).empty())
{
User * user;
Nebula& nd = Nebula::instance();
UserPool * upool = nd.get_upool();
user = upool->get(uname,true);
if ( user == 0 )
{
return 0;
}
uid = user->get_oid();
user->unlock();
}
else
{
uid = _uid;
}
return get(name,uid,true);
}
/* -------------------------------------------------------------------------- */
VirtualNetwork * VirtualNetworkPool::get_nic_by_id(const string& id_s)
{
istringstream is;
int id;
is.str(id_s);
is >> id;
if( is.fail() )
{
return 0;
}
return get(id,true);
}
int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, int uid, int vid)
{
string network;
VirtualNetwork * vnet = 0;
istringstream is;
int network_id;
network = nic->vector_value("NETWORK");
if (!network.empty())
if (!(network = nic->vector_value("NETWORK")).empty())
{
return -3;
vnet = get_nic_by_name (nic, network, uid);
}
network = nic->vector_value("NETWORK_ID");
if(network.empty())
else if (!(network = nic->vector_value("NETWORK_ID")).empty())
{
vnet = get_nic_by_id(network);
}
else //Not using a pre-defined network
{
return -2;
}
is.str(network);
is >> network_id;
if( !is.fail() )
{
vnet = get(network_id,true);
}
}
if (vnet == 0)
{
@ -194,23 +247,18 @@ void VirtualNetworkPool::authorize_nic(VectorAttribute * nic,
string network;
VirtualNetwork * vnet = 0;
istringstream is;
int network_id;
network = nic->vector_value("NETWORK_ID");
if(network.empty())
if (!(network = nic->vector_value("NETWORK")).empty())
{
vnet = get_nic_by_name (nic, network, uid);
}
else if (!(network = nic->vector_value("NETWORK_ID")).empty())
{
vnet = get_nic_by_id(network);
}
else //Not using a pre-defined network
{
return;
}
is.str(network);
is >> network_id;
if( !is.fail() )
{
vnet = get(network_id,true);
}
}
if (vnet == 0)
{
@ -225,4 +273,4 @@ void VirtualNetworkPool::authorize_nic(VectorAttribute * nic,
vnet->isPublic());
vnet->unlock();
}
}

View File

@ -1151,17 +1151,7 @@ public:
// Disk using network 0
disk = new VectorAttribute("DISK");
disk->replace("NETWORK", "A net");
int rc = ((VirtualNetworkPool*)vnp)->nic_attribute(disk, 0, 0);
CPPUNIT_ASSERT( rc == -3 );
delete disk;
// Disk using network 0
disk = new VectorAttribute("DISK");
disk->replace("NETWORK_ID", "0");
disk->replace("NETWORK", "Net 0");
((VirtualNetworkPool*)vnp)->nic_attribute(disk, 0, 0);