diff --git a/src/oca/java/src/org/opennebula/client/image/Image.java b/src/oca/java/src/org/opennebula/client/image/Image.java index 27c1b5a263..00d72212bf 100644 --- a/src/oca/java/src/org/opennebula/client/image/Image.java +++ b/src/oca/java/src/org/opennebula/client/image/Image.java @@ -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); } /** diff --git a/src/oca/java/src/org/opennebula/client/template/Template.java b/src/oca/java/src/org/opennebula/client/template/Template.java index 7c417b4aaa..2dc52b3ae5 100644 --- a/src/oca/java/src/org/opennebula/client/template/Template.java +++ b/src/oca/java/src/org/opennebula/client/template/Template.java @@ -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 // ================================= diff --git a/src/oca/java/test/ImageTest.java b/src/oca/java/test/ImageTest.java index d857ef76f3..1480564dcc 100644 --- a/src/oca/java/test/ImageTest.java +++ b/src/oca/java/test/ImageTest.java @@ -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 diff --git a/src/oca/java/test/TemplateTest.java b/src/oca/java/test/TemplateTest.java index 1108ef7e2b..90bdacb43b 100644 --- a/src/oca/java/test/TemplateTest.java +++ b/src/oca/java/test/TemplateTest.java @@ -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" ) ); + } }