1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

Feature #607, #662: Java OCA, change 'update' method for Image and Template, delete 'rmatt'

This commit is contained in:
Carlos Martín 2011-06-08 16:17:38 +02:00
parent e3e08b5a77
commit f1331453b7
4 changed files with 99 additions and 120 deletions

View File

@ -32,7 +32,6 @@ public class Image extends PoolElement
private static final String INFO = METHOD_PREFIX + "info";
private static final String DELETE = METHOD_PREFIX + "delete";
private static final String UPDATE = METHOD_PREFIX + "update";
private static final String RMATTR = METHOD_PREFIX + "rmattr";
private static final String ENABLE = METHOD_PREFIX + "enable";
private static final String PUBLISH = METHOD_PREFIX + "publish";
private static final String CHOWN = METHOD_PREFIX + "chown";
@ -111,31 +110,16 @@ public class Image extends PoolElement
}
/**
* Modifies an image attribute.
* Replaces the template contents.
*
* @param client XML-RPC Client.
* @param id The image id of the target image we want to modify.
* @param att_name The name of the attribute to update.
* @param att_val The new value for the attribute.
* @param new_template New template contents
* @return If successful the message contains the image id.
*/
public static OneResponse update(Client client, int id,
String att_name, String att_val)
public static OneResponse update(Client client, int id, String new_template)
{
return client.call(UPDATE, id, att_name, att_val);
}
/**
* Removes an image attribute.
*
* @param client XML-RPC Client.
* @param id The image id of the target image we want to modify.
* @param att_name The name of the attribute to remove.
* @return If successful the message contains the image id.
*/
public static OneResponse rmattr(Client client, int id, String att_name)
{
return client.call(RMATTR, id, att_name);
return client.call(UPDATE, id, new_template);
}
/**
@ -206,26 +190,14 @@ public class Image extends PoolElement
}
/**
* Modifies an image attribute.
* Replaces the template contents.
*
* @param att_name The name of the attribute to update.
* @param att_val The new value for the attribute.
* @param new_template New template contents
* @return If successful the message contains the image id.
*/
public OneResponse update(String att_name, String att_val)
public OneResponse update(String new_template)
{
return update(client, id, att_name, att_val);
}
/**
* Removes an image attribute.
*
* @param att_name The name of the attribute to remove.
* @return If successful the message contains the image id.
*/
public OneResponse rmattr(String att_name)
{
return rmattr(client, id, att_name);
return update(client, id, new_template);
}
/**

View File

@ -32,9 +32,9 @@ public class Template extends PoolElement
private static final String INFO = METHOD_PREFIX + "info";
private static final String DELETE = METHOD_PREFIX + "delete";
private static final String UPDATE = METHOD_PREFIX + "update";
private static final String RMATTR = METHOD_PREFIX + "rmattr";
private static final String PUBLISH = METHOD_PREFIX + "publish";
private static final String CHOWN = METHOD_PREFIX + "chown";
private static final String INSTANTIATE = METHOD_PREFIX + "instantiate";
/**
* Creates a new Template representation.
@ -98,31 +98,16 @@ public class Template extends PoolElement
}
/**
* Modifies a template attribute.
* Replaces the template contents.
*
* @param client XML-RPC Client.
* @param id The template id of the target template we want to modify.
* @param att_name The name of the attribute to update.
* @param att_val The new value for the attribute.
* @param new_template New template contents.
* @return If successful the message contains the template id.
*/
public static OneResponse update(Client client, int id,
String att_name, String att_val)
public static OneResponse update(Client client, int id, String new_template)
{
return client.call(UPDATE, id, att_name, att_val);
}
/**
* Removes a template attribute.
*
* @param client XML-RPC Client.
* @param id The template id of the target template we want to modify.
* @param att_name The name of the attribute to remove.
* @return If successful the message contains the template id.
*/
public static OneResponse rmattr(Client client, int id, String att_name)
{
return client.call(RMATTR, id, att_name);
return client.call(UPDATE, id, new_template);
}
/**
@ -152,6 +137,19 @@ public class Template extends PoolElement
return client.call(CHOWN, id, uid, gid);
}
/**
* Creates a VM instance from a Template
*
* @param client XML-RPC Client.
* @param id The template id of the target template.
* @param name A string containing the name of the VM instance, can be empty.
* @return If successful the message contains the VM Instance ID.
*/
public static OneResponse instantiate(Client client, int id, String name)
{
return client.call(INSTANTIATE, id, name);
}
// =================================
// Instanced object XML-RPC methods
// =================================
@ -180,26 +178,14 @@ public class Template extends PoolElement
}
/**
* Modifies a template attribute.
* Replaces the template contents.
*
* @param att_name The name of the attribute to update.
* @param att_val The new value for the attribute.
* @param new_template New template contents.
* @return If successful the message contains the template id.
*/
public OneResponse update(String att_name, String att_val)
public OneResponse update(String new_template)
{
return update(client, id, att_name, att_val);
}
/**
* Removes a template attribute.
*
* @param att_name The name of the attribute to remove.
* @return If successful the message contains the template id.
*/
public OneResponse rmattr(String att_name)
{
return rmattr(client, id, att_name);
return update(client, id, new_template);
}
/**
@ -267,6 +253,27 @@ public class Template extends PoolElement
return chown(-1, gid);
}
/**
* Creates a VM instance from a Template
*
* @param name A string containing the name of the VM instance, can be empty.
* @return If successful the message contains the VM Instance ID.
*/
public OneResponse instantiate(String name)
{
return instantiate(client, id, name);
}
/**
* Creates a VM instance from a Template
*
* @return If successful the message contains the VM Instance ID.
*/
public OneResponse instantiate()
{
return instantiate(client, id, "");
}
// =================================
// Helpers
// =================================

View File

@ -43,7 +43,8 @@ public class ImageTest
return "NAME = \"test_img_" + cont + "\"\n" +
"PATH = /etc/hosts\n" +
"ATT1 = \"val1\"";
"ATT1 = \"VAL1\"\n" +
"ATT2 = \"VAL2\"";
}
/**
@ -122,34 +123,26 @@ public class ImageTest
@Test
public void update()
{
// Update an existing att.
res = image.update("ATT1", "new_val_1");
res = image.info();
assertTrue( !res.isError() );
assertTrue( image.xpath("TEMPLATE/ATT1").equals( "VAL1" ) );
assertTrue( image.xpath("TEMPLATE/ATT2").equals( "VAL2" ) );
String new_template = "ATT2 = NEW_VAL\n" +
"ATT3 = VAL3";
res = image.update(new_template);
assertTrue( !res.isError() );
res = image.info();
assertTrue( !res.isError() );
assertTrue( image.xpath("TEMPLATE/ATT1").equals("new_val_1") );
// Create a new att.
res = image.update("ATT2", "new_val_2");
assertTrue( !res.isError() );
res = image.info();
assertTrue( !res.isError() );
assertTrue( image.xpath("TEMPLATE/ATT2").equals("new_val_2") );
assertTrue( image.xpath("TEMPLATE/ATT1").equals( "" ) );
assertTrue( image.xpath("TEMPLATE/ATT2").equals( "NEW_VAL" ) );
assertTrue( image.xpath("TEMPLATE/ATT3").equals( "VAL3" ) );
}
@Test
public void rmattr()
{
res = image.rmattr("ATT1");
assertTrue( !res.isError() );
res = image.info();
assertTrue( !res.isError() );
assertTrue( image.xpath("ATT1").equals("") );
}
// TODO
// @Test

View File

@ -43,7 +43,8 @@ public class TemplateTest
private static String template_str =
"NAME = \"" + name + "\"\n" +
"ATT1 = \"val1\"";
"ATT1 = \"VAL1\"\n" +
"ATT2 = \"VAL2\"";
/**
* @throws java.lang.Exception
@ -121,33 +122,24 @@ public class TemplateTest
@Test
public void update()
{
// Update an existing att.
res = template.update("ATT1", "new_val_1");
assertTrue( !res.isError() );
res = template.info();
assertTrue( !res.isError() );
assertTrue( template.xpath("TEMPLATE/ATT1").equals("new_val_1") );
// Create a new att.
res = template.update("ATT2", "new_val_2");
assertTrue( !res.isError() );
res = template.info();
assertTrue( !res.isError() );
assertTrue( template.xpath("TEMPLATE/ATT2").equals("new_val_2") );
}
@Test
public void rmattr()
{
res = template.rmattr("ATT1");
assertTrue( !res.isError() );
res = template.info();
assertTrue( !res.isError() );
assertTrue( template.xpath("ATT1").equals("") );
assertTrue( template.xpath("TEMPLATE/ATT1").equals( "VAL1" ) );
assertTrue( template.xpath("TEMPLATE/ATT2").equals( "VAL2" ) );
String new_template = "ATT2 = NEW_VAL\n" +
"ATT3 = VAL3";
res = template.update(new_template);
assertTrue( !res.isError() );
res = template.info();
assertTrue( !res.isError() );
assertTrue( template.xpath("TEMPLATE/ATT1").equals( "" ) );
assertTrue( template.xpath("TEMPLATE/ATT2").equals( "NEW_VAL" ) );
assertTrue( template.xpath("TEMPLATE/ATT3").equals( "VAL3" ) );
}
@Test
@ -237,4 +229,19 @@ public class TemplateTest
assertTrue( template.uid() == uid );
assertTrue( template.gid() == 0 );
}
@Test
public void instantiate()
{
res = template.instantiate("new_vm_name");
assertTrue( !res.isError() );
int vm_id = Integer.parseInt(res.getMessage());
VirtualMachine vm = new VirtualMachine(vm_id, client);
res = vm.info();
assertTrue( !res.isError() );
assertTrue( vm.getName().equals( "new_vm_name" ) );
}
}