mirror of
https://github.com/OpenNebula/one.git
synced 2025-02-22 17:57:46 +03:00
feature #653, #488: Images can only be referenced by ID in DISKs. Names can now be repeated, even for the same user
This commit is contained in:
parent
66485c6ed7
commit
82982a7a31
@ -86,14 +86,6 @@ public:
|
||||
*/
|
||||
Image * acquire_image(int image_id);
|
||||
|
||||
/**
|
||||
* Try to acquire an image from the repository for a VM.
|
||||
* @param name of the image
|
||||
* @param id of owner
|
||||
* @return pointer to the image or 0 if could not be acquired
|
||||
*/
|
||||
Image * acquire_image(const string& name, int uid);
|
||||
|
||||
/**
|
||||
* Releases an image and triggers any needed operations in the repo
|
||||
* @param iid image id of the image to be released
|
||||
|
@ -78,17 +78,6 @@ public:
|
||||
return static_cast<Image *>(PoolSQL::get(oid,lock));
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to get an Image from the pool using the image name
|
||||
* @param name of the image
|
||||
* @param lock locks the User mutex
|
||||
* @return a pointer to the Image, 0 if the image could not be loaded
|
||||
*/
|
||||
Image * get(const string& name, int uid, bool lock)
|
||||
{
|
||||
return static_cast<Image *>(PoolSQL::get(name,uid,lock));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a particular Image
|
||||
* @param image pointer to Image
|
||||
@ -127,7 +116,10 @@ public:
|
||||
* automatically increased.
|
||||
* @param img_type will be set to the used image's type
|
||||
* @param uid of VM owner (to look for the image id within its images)
|
||||
* @return 0 on success, -1 error, -2 not using the pool
|
||||
* @return 0 on success,
|
||||
* -1 error,
|
||||
* -2 not using the pool,
|
||||
* -3 deprecated NAME found
|
||||
*/
|
||||
int disk_attribute(VectorAttribute * disk,
|
||||
int disk_id,
|
||||
|
@ -46,31 +46,6 @@ Image * ImageManager::acquire_image(int image_id)
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
Image * ImageManager::acquire_image(const string& name, int uid)
|
||||
{
|
||||
Image * img;
|
||||
int rc;
|
||||
|
||||
img = ipool->get(name,uid,true);
|
||||
|
||||
if ( img == 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
rc = acquire_image(img);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
img->unlock();
|
||||
img = 0;
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int ImageManager::acquire_image(Image *img)
|
||||
{
|
||||
int rc = 0;
|
||||
|
@ -64,7 +64,6 @@ int ImagePool::allocate (
|
||||
string& error_str)
|
||||
{
|
||||
Image * img;
|
||||
Image * img_aux;
|
||||
string name;
|
||||
ostringstream oss;
|
||||
|
||||
@ -78,14 +77,6 @@ int ImagePool::allocate (
|
||||
goto error_name;
|
||||
}
|
||||
|
||||
// Check for duplicates
|
||||
img_aux = get(name,uid,false);
|
||||
|
||||
if( img_aux != 0 )
|
||||
{
|
||||
goto error_duplicated;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Insert the Object in the pool & Register the image in the repository
|
||||
// ---------------------------------------------------------------------
|
||||
@ -108,12 +99,7 @@ int ImagePool::allocate (
|
||||
|
||||
error_name:
|
||||
oss << "NAME cannot be empty.";
|
||||
goto error_common;
|
||||
|
||||
error_duplicated:
|
||||
oss << "NAME is already taken by IMAGE " << img_aux->get_oid() << ".";
|
||||
|
||||
error_common:
|
||||
delete img;
|
||||
|
||||
*oid = -1;
|
||||
@ -140,41 +126,34 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
|
||||
Nebula& nd = Nebula::instance();
|
||||
ImageManager * imagem = nd.get_imagem();
|
||||
|
||||
istringstream is;
|
||||
int image_id;
|
||||
|
||||
source = disk->vector_value("IMAGE");
|
||||
|
||||
if (source.empty())
|
||||
if (!source.empty())
|
||||
{
|
||||
istringstream is;
|
||||
int image_id;
|
||||
return -3;
|
||||
}
|
||||
|
||||
source = disk->vector_value("IMAGE_ID");
|
||||
source = disk->vector_value("IMAGE_ID");
|
||||
|
||||
if (!source.empty())
|
||||
if (!source.empty())
|
||||
{
|
||||
is.str(source);
|
||||
is >> image_id;
|
||||
|
||||
if( !is.fail() )
|
||||
{
|
||||
is.str(source);
|
||||
is >> image_id;
|
||||
img = imagem->acquire_image(image_id);
|
||||
|
||||
if( !is.fail() )
|
||||
if (img == 0)
|
||||
{
|
||||
img = imagem->acquire_image(image_id);
|
||||
|
||||
if (img == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
img = imagem->acquire_image(source,uid);
|
||||
|
||||
if (img == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (img == 0)
|
||||
{
|
||||
string type = disk->vector_value("TYPE");
|
||||
@ -220,31 +199,22 @@ void ImagePool::authorize_disk(VectorAttribute * disk,int uid, AuthRequest * ar)
|
||||
string source;
|
||||
Image * img = 0;
|
||||
|
||||
source = disk->vector_value("IMAGE");
|
||||
istringstream is;
|
||||
int image_id;
|
||||
|
||||
source = disk->vector_value("IMAGE_ID");
|
||||
|
||||
if (source.empty())
|
||||
{
|
||||
istringstream is;
|
||||
int image_id;
|
||||
|
||||
source = disk->vector_value("IMAGE_ID");
|
||||
|
||||
if (source.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
is.str(source);
|
||||
is >> image_id;
|
||||
|
||||
if( !is.fail() )
|
||||
{
|
||||
img = get(image_id,true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
||||
is.str(source);
|
||||
is >> image_id;
|
||||
|
||||
if( !is.fail() )
|
||||
{
|
||||
img = get(source,uid,true);
|
||||
img = get(image_id,true);
|
||||
}
|
||||
|
||||
if (img == 0)
|
||||
|
@ -139,8 +139,6 @@ class ImagePoolTest : public PoolTest
|
||||
|
||||
CPPUNIT_TEST ( names_initialization );
|
||||
CPPUNIT_TEST ( update );
|
||||
CPPUNIT_TEST ( get_using_name );
|
||||
CPPUNIT_TEST ( wrong_get_name );
|
||||
CPPUNIT_TEST ( duplicates );
|
||||
CPPUNIT_TEST ( extra_attributes );
|
||||
CPPUNIT_TEST ( wrong_templates );
|
||||
@ -244,13 +242,13 @@ public:
|
||||
// allocated images.
|
||||
imp = new ImagePool(db,"OS", "hd");
|
||||
|
||||
img = imp->get(names[0], uids[0], false);
|
||||
img = imp->get(0, false);
|
||||
CPPUNIT_ASSERT( img != 0 );
|
||||
|
||||
img = imp->get(names[1], uids[1], false);
|
||||
img = imp->get(2, false);
|
||||
CPPUNIT_ASSERT( img == 0 );
|
||||
|
||||
img = imp->get(names[2], uids[2], false);
|
||||
img = imp->get(1, false);
|
||||
CPPUNIT_ASSERT( img != 0 );
|
||||
|
||||
|
||||
@ -322,65 +320,6 @@ public:
|
||||
CPPUNIT_ASSERT( no_value == "" );
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void get_using_name()
|
||||
{
|
||||
int oid_0, oid_1;
|
||||
ImagePool * imp = static_cast<ImagePool *>(pool);
|
||||
|
||||
// Allocate two objects
|
||||
oid_0 = allocate(0);
|
||||
oid_1 = allocate(1);
|
||||
|
||||
// ---------------------------------
|
||||
// Get first object and check its integrity
|
||||
obj = pool->get(oid_0, false);
|
||||
CPPUNIT_ASSERT( obj != 0 );
|
||||
check(0, obj);
|
||||
|
||||
// Get using its name
|
||||
obj = imp->get(names[1], uids[1], true);
|
||||
CPPUNIT_ASSERT( obj != 0 );
|
||||
obj->unlock();
|
||||
|
||||
check(1, obj);
|
||||
|
||||
|
||||
// ---------------------------------
|
||||
// Clean the cache, forcing the pool to read the objects from the DB
|
||||
pool->clean();
|
||||
|
||||
// Get first object and check its integrity
|
||||
obj = imp->get(names[0], uids[0], false);
|
||||
check(0, obj);
|
||||
|
||||
// Get using its name
|
||||
obj = imp->get(oid_1, false);
|
||||
check(1, obj);
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void wrong_get_name()
|
||||
{
|
||||
ImagePool * imp = static_cast<ImagePool *>(pool);
|
||||
|
||||
// The pool is empty
|
||||
// Non existing name
|
||||
obj = imp->get("Wrong name", 0, true);
|
||||
CPPUNIT_ASSERT( obj == 0 );
|
||||
|
||||
// Allocate an object
|
||||
allocate(0);
|
||||
|
||||
// Ask again for a non-existing name
|
||||
obj = imp->get("Non existing name",uids[0], true);
|
||||
CPPUNIT_ASSERT( obj == 0 );
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
@ -394,15 +333,15 @@ public:
|
||||
CPPUNIT_ASSERT( oid == 0 );
|
||||
CPPUNIT_ASSERT( oid == rc );
|
||||
|
||||
// Try to allocate twice the same image, should fail
|
||||
// Try to allocate twice the same image, should work
|
||||
rc = imp->allocate(uids[0], templates[0], &oid);
|
||||
CPPUNIT_ASSERT( rc == -1 );
|
||||
CPPUNIT_ASSERT( oid == rc );
|
||||
CPPUNIT_ASSERT( rc == 1 );
|
||||
CPPUNIT_ASSERT( oid == 1 );
|
||||
|
||||
// Try again, this time with different uid. Should be allowed
|
||||
rc = imp->allocate(uids[1], templates[0], &oid);
|
||||
CPPUNIT_ASSERT( rc >= 0 );
|
||||
CPPUNIT_ASSERT( oid == rc );
|
||||
CPPUNIT_ASSERT( oid == 2 );
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
@ -725,6 +725,10 @@ int VirtualMachine::get_disk_images(string& error_str)
|
||||
{
|
||||
goto error_image;
|
||||
}
|
||||
else if ( rc == -3)
|
||||
{
|
||||
goto error_name;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -744,6 +748,9 @@ error_max_db:
|
||||
error_image:
|
||||
error_str = "Could not get disk image for VM.";
|
||||
|
||||
error_name:
|
||||
error_str = "NAME is not supported for DISK. Use IMAGE_ID instead.";
|
||||
|
||||
error_common:
|
||||
NebulaLog::log("ONE",Log::ERROR, error_str);
|
||||
return -1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user