Add VMGroup to oca/java

This commit is contained in:
abelCoronado93 committed 2017-03-08 16:04:42 +01:00
1 parent 3a7e02dd8a
commit 2f34cdb3a8
3 files changed
+876

No files matched your search

@@ -0,0 +1,345 @@
/*******************************************************************************
* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.opennebula.client.vmgroup;
import org.opennebula.client.Client;
import org.opennebula.client.OneResponse;
import org.opennebula.client.PoolElement;
import org.w3c.dom.Node;
/**
* This class represents an OpenNebula vmgroup.
* It also offers static XML-RPC call wrappers.
*/
public class VMGroup extends PoolElement{
private static final String METHOD_PREFIX = "vmgroup.";
private static final String ALLOCATE = METHOD_PREFIX + "allocate";
private static final String INFO = METHOD_PREFIX + "info";
private static final String UPDATE = METHOD_PREFIX + "update";
private static final String DELETE = METHOD_PREFIX + "delete";
private static final String CHOWN = METHOD_PREFIX + "chown";
private static final String CHMOD = METHOD_PREFIX + "chmod";
private static final String RENAME = METHOD_PREFIX + "rename";
/**
* Creates a new vmgroup representation.
*
* @param id The vmgroup id.
* @param client XML-RPC Client.
*/
public VMGroup(int id, Client client)
{
super(id, client);
}
/**
* @see PoolElement
*/
protected VMGroup(Node xmlElement, Client client)
{
super(xmlElement, client);
}
// =================================
// Static XML-RPC methods
// =================================
/**
* Allocates a new vmgroup in OpenNebula.
*
* @param client XML-RPC Client.
* @param description A string containing the template
* of the vmgroup.
*
* @return If successful the message contains the associated
* id generated for this vmgroup.
*/
public static OneResponse allocate(
Client client,
String description)
{
return client.call(ALLOCATE, description);
}
/**
* Retrieves the information of the given vmgroup
*
* @param client XML-RPC Client.
* @param id the id for the vmgroup to
* retrieve the information from.
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse info(Client client, int id)
{
return client.call(INFO, id);
}
/**
* Deletes a vmgroup from OpenNebula.
*
* @param client XML-RPC Client.
* @param id The id of the target vmgroup.
* @return A encapsulated response.
*/
public static OneResponse delete(Client client, int id)
{
return client.call(DELETE, id);
}
/**
* Changes the owner/group
*
* @param client XML-RPC Client.
* @param id The id of the target vmgroup.
* @param uid The new owner user ID. Set it to -1 to leave the current one.
* @param gid The new group ID. Set it to -1 to leave the current one.
* @return If an error occurs the error message contains the reason.
*/
public static OneResponse chown(Client client, int id, int uid, int gid)
{
return client.call(CHOWN, id, uid, gid);
}
/**
* Changes the vmgroup permissions
*
* @param client XML-RPC Client.
* @param id The id of the target vmgroup.
* @param owner_u 1 to allow, 0 deny, -1 do not change
* @param owner_m 1 to allow, 0 deny, -1 do not change
* @param owner_a 1 to allow, 0 deny, -1 do not change
* @param group_u 1 to allow, 0 deny, -1 do not change
* @param group_m 1 to allow, 0 deny, -1 do not change
* @param group_a 1 to allow, 0 deny, -1 do not change
* @param other_u 1 to allow, 0 deny, -1 do not change
* @param other_m 1 to allow, 0 deny, -1 do not change
* @param other_a 1 to allow, 0 deny, -1 do not change
* @return If an error occurs the error message contains the reason.
*/
public static OneResponse chmod(Client client, int id,
int owner_u, int owner_m, int owner_a,
int group_u, int group_m, int group_a,
int other_u, int other_m, int other_a)
{
return chmod(client, CHMOD, id,
owner_u, owner_m, owner_a,
group_u, group_m, group_a,
other_u, other_m, other_a);
}
/**
* Changes the permissions
*
* @param client XML-RPC Client.
* @param id The id of the target object.
* @param octet Permissions octed , e.g. 640
* @return If an error occurs the error message contains the reason.
*/
public static OneResponse chmod(Client client, int id, String octet)
{
return chmod(client, CHMOD, id, octet);
}
/**
* Changes the permissions
*
* @param client XML-RPC Client.
* @param id The id of the target object.
* @param octet Permissions octed , e.g. 640
* @return If an error occurs the error message contains the reason.
*/
public static OneResponse chmod(Client client, int id, int octet)
{
return chmod(client, CHMOD, id, octet);
}
/**
* Replaces the vmgroup template contents.
*
* @param client XML-RPC Client.
* @param id The id of the target vmgroup we want to modify.
* @param new_template New template contents.
* @param append True to append new attributes instead of replace the whole template
* @return If successful the message contains the vmgroup id.
*/
public static OneResponse update(Client client, int id, String new_template,
boolean append)
{
return client.call(UPDATE, id, new_template, append ? 1 : 0);
}
/**
* Renames this vmgroup
*
* @param client XML-RPC Client.
* @param id The id of the target vmgroup.
* @param name New name for the vmgroup.
* @return If an error occurs the error message contains the reason.
*/
public static OneResponse rename(Client client, int id, String name)
{
return client.call(RENAME, id, name);
}
// =================================
// Instanced object XML-RPC methods
// =================================
/**
* Loads the xml representation of the vmgroup.
* The info is also stored internally.
*
* @see VMGroup#info(Client, int)
*/
public OneResponse info()
{
OneResponse response = info(client, id);
super.processInfo(response);
return response;
}
/**
* Deletes the vmgroup from OpenNebula.
*
* @return A encapsulated response.
*/
public OneResponse delete()
{
return delete(client, id);
}
/**
* Changes the owner/group
*
* @param uid The new owner user ID. Set it to -1 to leave the current one.
* @param gid The new group ID. Set it to -1 to leave the current one.
* @return If an error occurs the error message contains the reason.
*/
public OneResponse chown(int uid, int gid)
{
return chown(client, id, uid, gid);
}
/**
* Changes the owner
*
* @param uid The new owner user ID.
* @return If an error occurs the error message contains the reason.
*/
public OneResponse chown(int uid)
{
return chown(uid, -1);
}
/**
* Changes the group
*
* @param gid The new group ID.
* @return If an error occurs the error message contains the reason.
*/
public OneResponse chgrp(int gid)
{
return chown(-1, gid);
}
/**
* Changes the vmgroup permissions
*
* @param owner_u 1 to allow, 0 deny, -1 do not change
* @param owner_m 1 to allow, 0 deny, -1 do not change
* @param owner_a 1 to allow, 0 deny, -1 do not change
* @param group_u 1 to allow, 0 deny, -1 do not change
* @param group_m 1 to allow, 0 deny, -1 do not change
* @param group_a 1 to allow, 0 deny, -1 do not change
* @param other_u 1 to allow, 0 deny, -1 do not change
* @param other_m 1 to allow, 0 deny, -1 do not change
* @param other_a 1 to allow, 0 deny, -1 do not change
* @return If an error occurs the error message contains the reason.
*/
public OneResponse chmod(int owner_u, int owner_m, int owner_a,
int group_u, int group_m, int group_a,
int other_u, int other_m, int other_a)
{
return chmod(client, id,
owner_u, owner_m, owner_a,
group_u, group_m, group_a,
other_u, other_m, other_a);
}
/**
* Changes the permissions
*
* @param octet Permissions octed , e.g. 640
* @return If an error occurs the error message contains the reason.
*/
public OneResponse chmod(String octet)
{
return chmod(client, id, octet);
}
/**
* Changes the permissions
*
* @param octet Permissions octed , e.g. 640
* @return If an error occurs the error message contains the reason.
*/
public OneResponse chmod(int octet)
{
return chmod(client, id, octet);
}
/**
* Replaces the vmgroup template contents.
*
* @param new_template New template contents.
* @return If successful the message contains the vmgroup id.
*/
public OneResponse update(String new_template)
{
return update(new_template, false);
}
/**
* Replaces the vmgroup template contents.
*
* @param new_template New template contents.
* @param append True to append new attributes instead of replace the whole template
* @return If successful the message contains the vmgroup id.
*/
public OneResponse update(String new_template, boolean append)
{
return update(client, id, new_template, append);
}
/**
* Renames this vmgroup
*
* @param name New name for the vmgroup.
* @return If an error occurs the error message contains the reason.
*/
public OneResponse rename(String name)
{
return rename(client, id, name);
}
// =================================
// Helpers
// =================================
}
@@ -0,0 +1,265 @@
/*******************************************************************************
* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.opennebula.client.vmgroup;
import java.util.AbstractList;
import java.util.Iterator;
import org.opennebula.client.Client;
import org.opennebula.client.OneResponse;
import org.opennebula.client.Pool;
import org.opennebula.client.PoolElement;
import org.w3c.dom.Node;
/**
* This class represents an OpenNebula VMGroup pool.
* It also offers static XML-RPC call wrappers.
*/
public class VMGroupPool extends Pool implements Iterable<VMGroup>{
private static final String ELEMENT_NAME = "VM_GROUP";
private static final String INFO_METHOD = "vmgrouppool.info";
private int filter;
/**
* Creates a new VMGroup pool with the default filter flag value
* set to {@link Pool#MINE_GROUP} (VMGroups belonging to the connected user,
* and the ones in his group)
*
* @param client XML-RPC Client.
*
* @see VMGroupPool#VMGroupPool(Client, int)
*/
public VMGroupPool(Client client)
{
super(ELEMENT_NAME, client, INFO_METHOD);
this.filter = MINE_GROUP;
}
/**
* Creates a new VMGroup pool.
*
* @param client XML-RPC Client.
* @param filter Filter flag to use by default in the method
* {@link VMGroupPool#info()}. Possible values:
* <ul>
* <li>{@link Pool#ALL}: All VMGroups</li>
* <li>{@link Pool#MINE}: Connected user's VMGroups</li>
* <li>{@link Pool#MINE_GROUP}: Connected user's VMGroups, and the ones in
* his group</li>
* <li>{@link Pool#GROUP}: User's primary group VMGroups</li>
* <li>&gt;= 0 UID User's VMGroups</li>
* </ul>
*/
public VMGroupPool(Client client, int filter)
{
super(ELEMENT_NAME, client, INFO_METHOD);
this.filter = filter;
}
@Override
public PoolElement factory(Node node)
{
return new VMGroup(node, client);
}
/**
* Retrieves all or part of the VMGroups in the pool.
*
* @param client XML-RPC Client.
* @param filter Filter flag to use. Possible values:
* <ul>
* <li>{@link Pool#ALL}: All VMGroups</li>
* <li>{@link Pool#MINE}: Connected user's VMGroups</li>
* <li>{@link Pool#MINE_GROUP}: Connected user's VMGroups, and the ones in
* his group</li>
* <li>{@link Pool#GROUP}: User's primary group VMGroups</li>
* <li>&gt;= 0 UID User's VMGroups</li>
* </ul>
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse info(Client client, int filter)
{
return Pool.info(client, INFO_METHOD, filter, -1, -1);
}
/**
* Retrieves all the VMGroups in the pool.
*
* @param client XML-RPC Client.
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse infoAll(Client client)
{
return Pool.infoAll(client, INFO_METHOD);
}
/**
* Retrieves all the connected user's VMGroups.
*
* @param client XML-RPC Client.
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse infoMine(Client client)
{
return Pool.infoMine(client, INFO_METHOD);
}
/**
* Retrieves all the connected user's VMGroups and the ones in
* his group.
*
* @param client XML-RPC Client.
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse infoGroup(Client client)
{
return Pool.infoGroup(client, INFO_METHOD);
}
/**
* Retrieves all or part of the VMGroups in the pool. The VMGroups to retrieve
* can be also filtered by Id, specifying the first and last Id to include.
*
* @param client XML-RPC Client.
* @param filter Filter flag to use. Possible values:
* <ul>
* <li>{@link Pool#ALL}: All VMGroups</li>
* <li>{@link Pool#MINE}: Connected user's VMGroups</li>
* <li>{@link Pool#MINE_GROUP}: Connected user's VMGroups, and the ones in
* his group</li>
* <li>{@link Pool#GROUP}: User's primary group VMGroups</li>
* <li>&gt;= 0 UID User's VMGroups</li>
* </ul>
* @param startId Lowest Id to retrieve
* @param endId Biggest Id to retrieve
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public static OneResponse info(Client client, int filter,
int startId, int endId)
{
return Pool.info(client, INFO_METHOD, filter, startId, endId);
}
/**
* Loads the xml representation of all or part of the
* VMGroups in the pool. The filter used is the one set in
* the constructor.
*
* @see VMGroupPool#info(Client, int)
*
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public OneResponse info()
{
return super.info(filter, -1, -1);
}
/**
* Loads the xml representation of all the VMGroups in the pool.
*
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public OneResponse infoAll()
{
return super.infoAll();
}
/**
* Loads the xml representation of all the connected user's VMGroups.
*
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public OneResponse infoMine()
{
return super.infoMine();
}
/**
* Loads the xml representation of all the connected user's VMGroups and
* the ones in his group.
*
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public OneResponse infoGroup()
{
return super.infoGroup();
}
/**
* Retrieves all or part of the VMGroups in the pool. The VMGroups to retrieve
* can be also filtered by Id, specifying the first and last Id to include.
*
* @param filter Filter flag to use. Possible values:
* <ul>
* <li>{@link Pool#ALL}: All VMGroups</li>
* <li>{@link Pool#MINE}: Connected user's VMGroups</li>
* <li>{@link Pool#MINE_GROUP}: Connected user's VMGroups, and the ones in
* his group</li>
* <li>{@link Pool#GROUP}: User's primary group VMGroups</li>
* <li>&gt;= 0 UID User's VMGroups</li>
* </ul>
* @param startId Lowest Id to retrieve
* @param endId Biggest Id to retrieve
* @return If successful the message contains the string
* with the information returned by OpenNebula.
*/
public OneResponse info(int filter, int startId, int endId)
{
return super.info(filter, startId, endId);
}
public Iterator<VMGroup> iterator()
{
AbstractList<VMGroup> ab = new AbstractList<VMGroup>()
{
public int size()
{
return getLength();
}
public VMGroup get(int index)
{
return (VMGroup) item(index);
}
};
return ab.iterator();
}
/**
* Returns the VMGroup with the given Id from the pool. If it is not found,
* then returns null. The method {@link #info()} must be called before.
*
* @param id of the VMGroup to retrieve
* @return The Image with the given Id, or null if it was not found.
*/
public VMGroup getById(int id)
{
return (VMGroup) super.getById(id);
}
}
+266
View File
@@ -0,0 +1,266 @@
/*******************************************************************************
* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opennebula.client.Client;
import org.opennebula.client.OneResponse;
import org.opennebula.client.group.Group;
import org.opennebula.client.vmgroup.*;
import org.opennebula.client.user.User;
public class VMGroupTest
{
private static VMGroup vmg;
private static VMGroupPool pool;
private static Client client;
private static OneResponse res;
private static String name = "new_test_vmg";
private static String template_str =
"NAME = \"" + name + "\"\n" +
"DESCRIPTION = \"test vmgroup\"\n"+
"ATT1 = \"VAL1\"\n" +
"ATT2 = \"VAL2\"";
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception
{
client = new Client();
pool = new VMGroupPool(client);
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception
{
}
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
{
res = VMGroup.allocate(client, template_str);
int oid = res.isError() ? -1 : Integer.parseInt(res.getMessage());
vmg = new VMGroup(oid, client);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception
{
vmg.delete();
}
@Test
public void allocate()
{
vmg.delete();
res = VMGroup.allocate(client, template_str);
assertTrue( res.getErrorMessage(), !res.isError() );
int oid = res.isError() ? -1 : Integer.parseInt(res.getMessage());
vmg = new VMGroup(oid, client);
pool.info();
boolean found = false;
for(VMGroup temp : pool)
{
found = found || temp.getName().equals(name);
}
assertTrue( found );
}
@Test
public void info()
{
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.getName().equals(name) );
}
@Test
public void update()
{
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.xpath("TEMPLATE/ATT1").equals( "VAL1" ) );
assertTrue( vmg.xpath("TEMPLATE/ATT2").equals( "VAL2" ) );
String new_vmg = "ATT2 = NEW_VAL\n" +
"ATT3 = VAL3";
res = vmg.update(new_vmg);
assertTrue( res.getErrorMessage(), !res.isError() );
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.xpath("TEMPLATE/ATT1").equals( "" ) );
assertTrue( vmg.xpath("TEMPLATE/ATT2").equals( "NEW_VAL" ) );
assertTrue( vmg.xpath("TEMPLATE/ATT3").equals( "VAL3" ) );
}
@Test
public void chmod()
{
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
String owner_a = vmg.xpath("PERMISSIONS/OWNER_A");
String group_a = vmg.xpath("PERMISSIONS/GROUP_A");
res = vmg.chmod(0, 1, -1, 1, 0, -1, 1, 1, 0);
assertTrue( res.getErrorMessage(), !res.isError() );
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_U").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_M").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_A").equals(owner_a) );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_U").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_M").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_A").equals(group_a) );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_U").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_M").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_A").equals("0") );
}
@Test
public void chmod_octet()
{
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
res = vmg.chmod(640);
assertTrue( res.getErrorMessage(), !res.isError() );
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_U").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_M").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_A").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_U").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_M").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_A").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_U").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_M").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_A").equals("0") );
res = vmg.chmod("147");
assertTrue( res.getErrorMessage(), !res.isError() );
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_U").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_M").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/OWNER_A").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_U").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_M").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/GROUP_A").equals("0") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_U").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_M").equals("1") );
assertTrue( vmg.xpath("PERMISSIONS/OTHER_A").equals("1") );
}
@Test
public void attributes()
{
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
// assertTrue( vmg.xpath("ID").equals("0") );
assertTrue( vmg.xpath("NAME").equals(name) );
}
@Test
public void delete()
{
res = vmg.delete();
assertTrue( res.getErrorMessage(), !res.isError() );
res = vmg.info();
assertTrue( res.isError() );
}
@Test
public void chown()
{
// Create a new User and Group
res = User.allocate(client, "vmg_test_user", "password");
assertTrue( res.getErrorMessage(), !res.isError() );
int uid = Integer.parseInt(res.getMessage());
res = Group.allocate(client, "vmg_test_group");
assertTrue( res.getErrorMessage(), !res.isError() );
int gid = Integer.parseInt(res.getMessage());
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.uid() == 0 );
assertTrue( vmg.gid() == 0 );
res = vmg.chown(uid, gid);
assertTrue( res.getErrorMessage(), !res.isError() );
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.uid() == uid );
assertTrue( vmg.gid() == gid );
res = vmg.chgrp(0);
res = vmg.info();
assertTrue( res.getErrorMessage(), !res.isError() );
assertTrue( vmg.uid() == uid );
assertTrue( vmg.gid() == 0 );
}
}