From 057d2e11cdc61362584834267a5b55186a17c8b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 6 Jun 2011 18:00:34 +0200 Subject: [PATCH] Conflicts: include/Cluster.h include/Group.h include/ObjectCollection.h include/PoolObjectSQL.h include/RequestManager.h src/cluster/test/ClusterPoolTest.cc src/group/Group.cc src/host/Host.cc src/oca/ruby/OpenNebula/Host.rb src/rm/RequestManagerAddDelGroup.cc src/rm/RequestManagerClusterAdd.cc src/rm/RequestManagerClusterRemove.cc src/um/User.cc --- src/cli/oneuser | 33 +++ .../org/opennebula/client/PoolElement.java | 24 ++ .../opennebula/client/cluster/Cluster.java | 198 -------------- .../org/opennebula/client/group/Group.java | 165 ++++++++++++ .../ClusterPool.java => group/GroupPool.java} | 47 ++-- .../src/org/opennebula/client/host/Host.java | 10 - .../org/opennebula/client/image/Image.java | 48 ++++ .../opennebula/client/template/Template.java | 48 ++++ .../src/org/opennebula/client/user/User.java | 87 ++++++ .../opennebula/client/vm/VirtualMachine.java | 48 ++++ .../client/vnet/VirtualNetwork.java | 49 ++++ src/oca/java/test/ClusterTest.java | 166 ------------ src/oca/java/test/GroupTest.java | 247 ++++++++++++++++++ src/oca/java/test/ImageTest.java | 6 +- src/oca/java/test/TemplateTest.java | 41 ++- src/oca/java/test/all_tests.sh | 4 +- src/oca/ruby/OpenNebula/Group.rb | 17 +- src/oca/ruby/OpenNebula/Host.rb | 9 +- src/oca/ruby/OpenNebula/User.rb | 45 +++- src/oca/ruby/OpenNebula/XMLUtils.rb | 4 +- 20 files changed, 881 insertions(+), 415 deletions(-) delete mode 100644 src/oca/java/src/org/opennebula/client/cluster/Cluster.java create mode 100644 src/oca/java/src/org/opennebula/client/group/Group.java rename src/oca/java/src/org/opennebula/client/{cluster/ClusterPool.java => group/GroupPool.java} (68%) delete mode 100644 src/oca/java/test/ClusterTest.java create mode 100644 src/oca/java/test/GroupTest.java diff --git a/src/cli/oneuser b/src/cli/oneuser index 0d97126270..0c120e488b 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -127,6 +127,12 @@ Commands: * chgrp (Changes the User group) oneuser chgrp +* addgroup (Adds a secondary group) + oneuser addgroup + +* delgroup (Deletes a secondary group. Fails if the group is the main one) + oneuser delgroup + Information Columns: * UID User ID @@ -280,6 +286,33 @@ when "chgrp" puts "User group changed" if ops[:verbose] end +when "addgroup" + check_parameters("addgroup", 2) + + obj_id = get_user_id(ARGV[0]) + new_gid = ARGV[1].to_i + + obj = OpenNebula::User.new_with_id(obj_id, get_one_client) + + result = obj.addgroup( new_gid ) + if is_successful?(result) + puts "User group added" if ops[:verbose] + end + +when "delgroup" + check_parameters("delgroup", 2) + + obj_id = get_user_id(ARGV[0]) + new_gid = ARGV[1].to_i + + obj = OpenNebula::User.new_with_id(obj_id, get_one_client) + + result = obj.delgroup( new_gid ) + if is_successful?(result) + puts "User group deleted" if ops[:verbose] + end + + when "list" if !ops[:xml] uplist=UPShow.new diff --git a/src/oca/java/src/org/opennebula/client/PoolElement.java b/src/oca/java/src/org/opennebula/client/PoolElement.java index fe364f9b63..d2355fc990 100644 --- a/src/oca/java/src/org/opennebula/client/PoolElement.java +++ b/src/oca/java/src/org/opennebula/client/PoolElement.java @@ -134,6 +134,30 @@ public abstract class PoolElement { return state != null ? Integer.parseInt( state ) : -1; } + /** + * Returns the owner User's ID, or -1 if the element doesn't have one. + * + * @return the owner User's ID, or -1 if the element doesn't have one. + */ + public int uid() + { + String uid = xpath("UID"); + + return uid != null ? Integer.parseInt( uid ) : -1; + } + + /** + * Returns the element group's ID, or -1 if the element doesn't have one. + * + * @return the element group's ID, or -1 if the element doesn't have one. + */ + public int gid() + { + String gid = xpath("GID"); + + return gid != null ? Integer.parseInt( gid ) : -1; + } + /** * Evaluates an XPath expression and returns the result as a String. * If the internal xml representation is not built, returns null. The diff --git a/src/oca/java/src/org/opennebula/client/cluster/Cluster.java b/src/oca/java/src/org/opennebula/client/cluster/Cluster.java deleted file mode 100644 index 8bf6b9647d..0000000000 --- a/src/oca/java/src/org/opennebula/client/cluster/Cluster.java +++ /dev/null @@ -1,198 +0,0 @@ -/******************************************************************************* - * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) - * - * 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.cluster; - -import org.opennebula.client.Client; -import org.opennebula.client.OneResponse; -import org.opennebula.client.PoolElement; -import org.opennebula.client.host.Host; -import org.w3c.dom.Node; - -/** - * This class represents an OpenNebula cluster. - * It also offers static XML-RPC call wrappers. - */ -public class Cluster extends PoolElement -{ - - private static final String METHOD_PREFIX = "cluster."; - private static final String ALLOCATE = METHOD_PREFIX + "allocate"; - private static final String INFO = METHOD_PREFIX + "info"; - private static final String DELETE = METHOD_PREFIX + "delete"; - private static final String ADD = METHOD_PREFIX + "add"; - private static final String REMOVE = METHOD_PREFIX + "remove"; - - - /** - * Creates a new Image representation. - * @param id The image id. - * @param client XML-RPC Client. - */ - public Cluster(int id, Client client) - { - super(id, client); - } - - /** - * @see PoolElement - */ - protected Cluster(Node xmlElement, Client client) - { - super(xmlElement, client); - } - - - // ================================= - // Static XML-RPC methods - // ================================= - - - /** - * Allocates a new Cluster in OpenNebula. - * - * @param client XML-RPC Client. - * @param name Name for the cluster we want to add. - * @return If successful the message contains the associated - * id generated for this Cluster. - */ - public static OneResponse allocate(Client client, String name) - { - return client.call(ALLOCATE, name); - } - - /** - * Retrieves the information of the given Cluster. - * - * @param client XML-RPC Client. - * @param id The cluster id for the cluster 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 cluster from OpenNebula. - * - * @param client XML-RPC Client. - * @param id The cluster id of the target cluster we want to delete. - * @return A encapsulated response. - */ - public static OneResponse delete(Client client, int id) - { - return client.call(DELETE, id); - } - - /** - * Adds a host to a cluster. - * - * @param client XML-RPC Client. - * @param id The cluster id of the cluster where the host will be assigned. - * @param hid The host id (hid) of the host. - * @return If an error occurs the error message contains the reason. - */ - public static OneResponse add(Client client, int id, int hid) - { - return client.call(ADD, hid, id); - } - - /** - * Removes a host from its cluster. - * - * @param client XML-RPC Client. - * @param hid The host id (hid) of the host. - * @return If an error occurs the error message contains the reason. - */ - public static OneResponse remove(Client client, int hid) - { - return client.call(REMOVE, hid); - } - - - // ================================= - // Instanced object XML-RPC methods - // ================================= - - - /** - * Retrieves the information of the Cluster. - * - * @return If successful the message contains the string - * with the information returned by OpenNebula. - */ - public OneResponse info() - { - OneResponse response = info(client, id); - super.processInfo(response); - return response; - } - - /** - * Deletes the cluster from OpenNebula. - * - * @return A encapsulated response. - */ - public OneResponse delete() - { - return delete(client, id); - } - - /** - * Adds a host to the cluster. - * - * @param hid The host id (hid) of the host. - * @return If an error occurs the error message contains the reason. - */ - public OneResponse add(int hid) - { - return add(client, id, hid); - } - - /** - * Adds a host to the cluster. - * - * @param host The Host to add. - * @return If an error occurs the error message contains the reason. - */ - public OneResponse add(Host host) - { - return add(client, id, host.id()); - } - - /** - * Removes a host from its cluster. - * - * @param hid The host id (hid) of the host. - * @return If an error occurs the error message contains the reason. - */ - public OneResponse remove(int hid) - { - return remove(client, hid); - } - - /** - * Removes a host from its cluster. - * - * @param host The Host to remove. - * @return If an error occurs the error message contains the reason. - */ - public OneResponse remove(Host host) - { - return remove(client, host.id()); - } -} diff --git a/src/oca/java/src/org/opennebula/client/group/Group.java b/src/oca/java/src/org/opennebula/client/group/Group.java new file mode 100644 index 0000000000..330b9f6873 --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/group/Group.java @@ -0,0 +1,165 @@ +/******************************************************************************* + * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) + * + * 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.group; + + +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 group. + * It also offers static XML-RPC call wrappers. + */ +public class Group extends PoolElement{ + + private static final String METHOD_PREFIX = "group."; + private static final String ALLOCATE = METHOD_PREFIX + "allocate"; + private static final String INFO = METHOD_PREFIX + "info"; + private static final String DELETE = METHOD_PREFIX + "delete"; + private static final String CHOWN = METHOD_PREFIX + "chown"; + + + /** + * Creates a new Group representation. + * + * @param id The group id (hid) of the machine. + * @param client XML-RPC Client. + */ + public Group(int id, Client client) + { + super(id, client); + } + + /** + * @see PoolElement + */ + protected Group(Node xmlElement, Client client) + { + super(xmlElement, client); + } + + + // ================================= + // Static XML-RPC methods + // ================================= + + /** + * Allocates a new group in OpenNebula + * + * @param client XML-RPC Client. + * @param name Name for the new group. + * @return If successful the message contains the associated + * id generated for this group. + */ + public static OneResponse allocate(Client client, String name) + { + return client.call(ALLOCATE, name); + } + + /** + * Retrieves the information of the given group. + * + * @param client XML-RPC Client. + * @param id The group id. + * @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 group from OpenNebula. + * + * @param client XML-RPC Client. + * @param id The group id. + * @return A encapsulated response. + */ + public static OneResponse delete(Client client, int id) + { + return client.call(DELETE, id); + } + + /** + * Changes the group's owner + * + * @param client XML-RPC Client. + * @param id The group id. + * @param uid The new owner user ID. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse chown(Client client, int id, int uid) + { + return client.call(CHOWN, id, uid, -1); + } + + // ================================= + // Instanced object XML-RPC methods + // ================================= + + /** + * Loads the xml representation of the group. + * The info is also stored internally. + * + * @see Group#info(Client, int) + */ + public OneResponse info() + { + OneResponse response = info(client, id); + super.processInfo(response); + return response; + } + + /** + * Changes the group's 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(client, id, uid); + } + + /** + * Deletes the group from OpenNebula. + * + * @see Group#delete(Client, int) + */ + public OneResponse delete() + { + return delete(client, id); + } + + // ================================= + // Helpers + // ================================= + + /** + * Returns whether or not the user is part of this group + * + * @param uid The user ID. + * @return Whether or not the user is part of this group. + */ + public boolean contains(int uid) + { + String res = xpath("USERS/ID[.="+uid+"]"); + return res != null && res.equals(""+uid); + } +} diff --git a/src/oca/java/src/org/opennebula/client/cluster/ClusterPool.java b/src/oca/java/src/org/opennebula/client/group/GroupPool.java similarity index 68% rename from src/oca/java/src/org/opennebula/client/cluster/ClusterPool.java rename to src/oca/java/src/org/opennebula/client/group/GroupPool.java index 737f7466ef..bfdc346305 100644 --- a/src/oca/java/src/org/opennebula/client/cluster/ClusterPool.java +++ b/src/oca/java/src/org/opennebula/client/group/GroupPool.java @@ -1,23 +1,24 @@ /******************************************************************************* * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) - * + * * 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.cluster; +package org.opennebula.client.group; import java.util.AbstractList; import java.util.Iterator; + import org.opennebula.client.Client; import org.opennebula.client.OneResponse; import org.opennebula.client.Pool; @@ -25,36 +26,32 @@ import org.opennebula.client.PoolElement; import org.w3c.dom.Node; /** - * This class represents an OpenNebula Cluster pool. + * This class represents an OpenNebula group pool. * It also offers static XML-RPC call wrappers. */ -public class ClusterPool extends Pool implements Iterable -{ - private static final String ELEMENT_NAME = "CLUSTER"; - private static final String INFO_METHOD = "clusterpool.info"; +public class GroupPool extends Pool implements Iterable{ + + private static final String ELEMENT_NAME = "GROUP"; + private static final String INFO_METHOD = "grouppool.info"; /** - * Creates a new Image pool - * + * Creates a new group pool * @param client XML-RPC Client. */ - public ClusterPool(Client client) + public GroupPool(Client client) { super(ELEMENT_NAME, client); } - /* (non-Javadoc) - * @see org.opennebula.client.Pool#factory(org.w3c.dom.Node) - */ @Override public PoolElement factory(Node node) { - return new Cluster(node, client); + return new Group(node, client); } /** - * Returns the Cluster pool information. - * + * Retrieves all the hosts in the pool. + * * @param client XML-RPC Client. * @return If successful the message contains the string * with the information returned by OpenNebula. @@ -65,9 +62,9 @@ public class ClusterPool extends Pool implements Iterable } /** - * Loads the xml representation of the Cluster pool. - * - * @see ClusterPool#info(Client) + * Loads the xml representation of the group pool. + * + * @see GroupPool#info(Client) */ public OneResponse info() { @@ -76,18 +73,18 @@ public class ClusterPool extends Pool implements Iterable return response; } - public Iterator iterator() + public Iterator iterator() { - AbstractList ab = new AbstractList() + AbstractList ab = new AbstractList() { public int size() { return getLength(); } - public Cluster get(int index) + public Group get(int index) { - return (Cluster) item(index); + return (Group) item(index); } }; diff --git a/src/oca/java/src/org/opennebula/client/host/Host.java b/src/oca/java/src/org/opennebula/client/host/Host.java index e4a8d08fa3..2b5cf12cec 100644 --- a/src/oca/java/src/org/opennebula/client/host/Host.java +++ b/src/oca/java/src/org/opennebula/client/host/Host.java @@ -227,14 +227,4 @@ public class Host extends PoolElement{ { return state() != 4; } - - /** - * Returns the name of the cluster this host is assigned to. - * - * @return The name of the cluster this host is assigned to. - */ - public String getCluster() - { - return xpath("CLUSTER"); - } } 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 2f9f04e4c5..27c1b5a263 100644 --- a/src/oca/java/src/org/opennebula/client/image/Image.java +++ b/src/oca/java/src/org/opennebula/client/image/Image.java @@ -35,6 +35,7 @@ public class Image extends PoolElement 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"; private static final String[] IMAGE_STATES = {"INIT", "READY", "USED", "DISABLED"}; @@ -163,6 +164,19 @@ public class Image extends PoolElement return client.call(PUBLISH, id, publish); } + /** + * Changes the owner/group + * + * @param client XML-RPC Client. + * @param id The image id of the target image we want to modify. + * @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); + } // ================================= // Instanced object XML-RPC methods @@ -276,6 +290,40 @@ public class Image extends PoolElement return publish(false); } + /** + * 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); + } + // ================================= // Helpers // ================================= 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 acdb2530c5..7c417b4aaa 100644 --- a/src/oca/java/src/org/opennebula/client/template/Template.java +++ b/src/oca/java/src/org/opennebula/client/template/Template.java @@ -34,6 +34,7 @@ public class Template extends PoolElement 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"; /** * Creates a new Template representation. @@ -137,6 +138,19 @@ public class Template extends PoolElement return client.call(PUBLISH, id, publish); } + /** + * Changes the owner/group + * + * @param client XML-RPC Client. + * @param id The template id of the target template we want to modify. + * @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); + } // ================================= // Instanced object XML-RPC methods @@ -219,6 +233,40 @@ public class Template extends PoolElement return publish(false); } + /** + * 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); + } + // ================================= // Helpers // ================================= diff --git a/src/oca/java/src/org/opennebula/client/user/User.java b/src/oca/java/src/org/opennebula/client/user/User.java index 40c153045b..d3f0d338f5 100644 --- a/src/oca/java/src/org/opennebula/client/user/User.java +++ b/src/oca/java/src/org/opennebula/client/user/User.java @@ -32,6 +32,9 @@ public class User extends PoolElement{ private static final String INFO = METHOD_PREFIX + "info"; private static final String DELETE = METHOD_PREFIX + "delete"; private static final String PASSWD = METHOD_PREFIX + "passwd"; + private static final String CHOWN = METHOD_PREFIX + "chown"; + private static final String ADDGROUP = METHOD_PREFIX + "addgroup"; + private static final String DELGROUP = METHOD_PREFIX + "delgroup"; /** * Creates a new User representation. @@ -111,6 +114,45 @@ public class User extends PoolElement{ return client.call(PASSWD, id, password); } + /** + * Changes the main group of the given user + * + * @param client XML-RPC Client. + * @param id The user id (uid) of the target user we want to modify. + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse chgrp(Client client, int id, int gid) + { + return client.call(CHOWN, id, -1, gid); + } + + /** + * Adds this user to a secondary group + * + * @param client XML-RPC Client. + * @param id The user id (uid) of the target user we want to modify. + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse addgroup(Client client, int id, int gid) + { + return client.call(ADDGROUP, id, gid); + } + + /** + * Removes this user from a secondary group + * + * @param client XML-RPC Client. + * @param id The user id (uid) of the target user we want to modify. + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse delgroup(Client client, int id, int gid) + { + return client.call(DELGROUP, id, gid); + } + // ================================= // Instanced object XML-RPC methods // ================================= @@ -150,6 +192,39 @@ public class User extends PoolElement{ return passwd(client, id, password); } + /** + * Changes the main group of the given user + * + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chgrp(int gid) + { + return chgrp(client, id, gid); + } + + /** + * Adds this user to a secondary group + * + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse addgroup(int gid) + { + return addgroup(client, id, gid); + } + + /** + * Removes this user from a secondary group + * + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse delgroup(int gid) + { + return delgroup(client, id, gid); + } + // ================================= // Helpers // ================================= @@ -164,4 +239,16 @@ public class User extends PoolElement{ String enabled = xpath("ENABLED"); return enabled != null && enabled.equals("1"); } + + /** + * Returns whether or not the user is part of the group + * + * @param gid The group ID. + * @return whether or not the user is part of the group + */ + public boolean isPartOf(int gid) + { + String res = xpath("GROUPS/ID[.="+gid+"]"); + return res != null && res.equals(""+gid); + } } diff --git a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java index 692a5a2a8c..b15db02bdb 100644 --- a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java +++ b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java @@ -35,6 +35,7 @@ public class VirtualMachine extends PoolElement{ private static final String ACTION = METHOD_PREFIX + "action"; private static final String MIGRATE = METHOD_PREFIX + "migrate"; private static final String SAVEDISK = METHOD_PREFIX + "savedisk"; + private static final String CHOWN = METHOD_PREFIX + "chown"; private static final String[] VM_STATES = { @@ -198,6 +199,19 @@ public class VirtualMachine extends PoolElement{ return client.call(INFO, id); } + /** + * Changes the owner/group + * + * @param client XML-RPC Client. + * @param id The virtual machine id (vid) of the target instance. + * @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); + } // ================================= // Instanced object XML-RPC methods @@ -281,6 +295,40 @@ public class VirtualMachine extends PoolElement{ return client.call(SAVEDISK, id ,diskId, imageName); } + /** + * 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); + } + // ================================= // Helpers // ================================= diff --git a/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java b/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java index bf7bb97e08..8a13cffdd2 100644 --- a/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java +++ b/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java @@ -34,6 +34,7 @@ public class VirtualNetwork extends PoolElement{ private static final String PUBLISH = METHOD_PREFIX + "publish"; private static final String ADDLEASES = METHOD_PREFIX + "addleases"; private static final String RMLEASES = METHOD_PREFIX + "rmleases"; + private static final String CHOWN = METHOD_PREFIX + "chown"; /** @@ -138,6 +139,20 @@ public class VirtualNetwork extends PoolElement{ return client.call(RMLEASES, id, template); } + /** + * Changes the owner/group + * + * @param client XML-RPC Client. + * @param id The virtual network id (nid) of the target network. + * @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); + } + // ================================= // Instanced object XML-RPC methods // ================================= @@ -242,6 +257,40 @@ public class VirtualNetwork extends PoolElement{ return rmLeases(client, id, lease_template); } + /** + * 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); + } + // ================================= // Helpers // ================================= diff --git a/src/oca/java/test/ClusterTest.java b/src/oca/java/test/ClusterTest.java deleted file mode 100644 index 7431a1cb85..0000000000 --- a/src/oca/java/test/ClusterTest.java +++ /dev/null @@ -1,166 +0,0 @@ -/******************************************************************************* - * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) - * - * 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.cluster.Cluster; -import org.opennebula.client.cluster.ClusterPool; -import org.opennebula.client.host.Host; - - -public class ClusterTest -{ - - private static Cluster cluster; - private static ClusterPool clusterPool; - - private static Host host; - - private static Client client; - - private static OneResponse res; - private static String name = "new_test_cluster"; - private static int hid = -1; - - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception - { - client = new Client(); - clusterPool = new ClusterPool(client); - - res = Host.allocate(client, "new_test_host", - "im_dummy", "vmm_dummy", "tm_dummy"); - try{ - hid = Integer.parseInt( res.getMessage() ); - }catch(NumberFormatException e) - { - System.err.println("Test initilization failed (setUpBeforeClass)."); - } - host = new Host(hid, client); - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception - { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception - { - res = Cluster.allocate(client, name); - - int clid = Integer.parseInt(res.getMessage()); - cluster = new Cluster(clid, client); - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception - { - cluster.delete(); - } - - @Test - public void allocate() - { - String allocate_name = "allocation_test"; - res = Cluster.allocate(client, allocate_name); - assertTrue( !res.isError() ); - - clusterPool.info(); - - boolean found = false; - for(Cluster c : clusterPool) - { - found = found || c.getName().equals(allocate_name); - } - - assertTrue( found ); - } - - @Test - public void update() - { - res = cluster.info(); - assertTrue( !res.isError() ); - -// assertTrue( cluster.getId().equals("1") ); -// assertTrue( cluster.id() == 1 ); - assertTrue( cluster.getName().equals(name) ); - } - - @Test - public void addHost() - { - res = cluster.add(hid); - assertTrue( !res.isError() ); - - res = host.info(); - assertTrue( !res.isError() ); - assertTrue( host.getCluster().equals(name) ); - } - - @Test - public void removeHost() - { - assertTrue( hid > -1 ); - - res = cluster.remove(hid); - assertTrue( !res.isError() ); - - res = host.info(); - assertTrue( !res.isError() ); - - assertTrue( host.getCluster().equals("default") ); - } - - @Test - public void attributes() - { - res = cluster.info(); - assertTrue( !res.isError() ); - -// assertTrue( cluster.xpath("ID").equals("1") ); - assertTrue( cluster.xpath("NAME").equals(name) ); - } - - @Test - public void delete() - { - res = cluster.delete(); - assertTrue( !res.isError() ); - - res = cluster.info(); - assertTrue( res.isError() ); - } -} diff --git a/src/oca/java/test/GroupTest.java b/src/oca/java/test/GroupTest.java new file mode 100644 index 0000000000..68b274447a --- /dev/null +++ b/src/oca/java/test/GroupTest.java @@ -0,0 +1,247 @@ +/******************************************************************************* + * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) + * + * 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.*; + +import java.util.Hashtable; + +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.*; +import org.opennebula.client.user.User; + +public class GroupTest +{ + + private static Group group; + private static GroupPool groupPool; + + private static Client client; + + private static OneResponse res; + + private static String group_name = "test_group"; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception + { + client = new Client(); + groupPool = new GroupPool(client); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception + { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception + { + res = Group.allocate(client, group_name); + + int group_id = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + group = new Group(group_id, client); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception + { + group.delete(); + } + + @Test + public void allocate() + { + group.delete(); + + res = Group.allocate(client, group_name); + assertTrue( !res.isError() ); + + int group_id = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + group = new Group(group_id, client); + + + groupPool.info(); + + boolean found = false; + for(Group img : groupPool) + { + found = found || img.getName().equals(group_name); + } + + assertTrue( found ); + } + + @Test + public void info() + { + res = group.info(); + assertTrue( !res.isError() ); + + assertTrue( group.id() >= 100 ); + assertTrue( group.getName().equals(group_name) ); + assertTrue( group.uid() == 0 ); + } + + @Test + public void delete() + { + res = group.delete(); + assertTrue( !res.isError() ); + + res = group.info(); + assertTrue( res.isError() ); + + res = groupPool.info(); + assertTrue( !res.isError() ); + + boolean found = false; + for(Group g : groupPool) + { + found = found || g.getName().equals(group_name); + } + + assertTrue( !found ); + } + + @Test + public void chown() + { + // Create a new user + res = User.allocate(client, "test_user", "password"); + assertTrue( !res.isError() ); + + int uid = Integer.parseInt(res.getMessage()); + + res = group.info(); + assertTrue( !res.isError() ); + assertTrue( group.uid() == 0 ); + + res = group.chown(uid); + assertTrue( !res.isError() ); + + res = group.info(); + assertTrue( !res.isError() ); + assertTrue( group.uid() == uid ); + + } + + @Test + public void userGroupRelations() + { + Hashtable users = new Hashtable(); + Hashtable groups = new Hashtable(); + + // Create all users and groups. Add user_* to corresponding group_* + String[] names = {"a", "b", "c", "d"}; + for(String name : names) + { + res = User.allocate(client, "user_"+name, "password"); + assertTrue( !res.isError() ); + + users.put( name, + new User(Integer.parseInt(res.getMessage()), client ) + ); + + res = Group.allocate(client, "group_"+name); + assertTrue( !res.isError() ); + + groups.put( name, + new Group(Integer.parseInt(res.getMessage()), client ) + ); + + users.get(name).addgroup( groups.get(name).id() ); + } + + // Add all users to group_b + for( User u : users.values() ) + { + u.addgroup( groups.get("b").id() ); + } + + // Change user_c & _d main group + users.get("c").chgrp( groups.get("d").id() ); + users.get("d").chgrp( groups.get("c").id() ); + + + // Check cross-references so far + for( User u : users.values() ) + { + assertTrue( !u.info().isError() ); + } + + for( Group g : groups.values() ) + { + assertTrue( !g.info().isError() ); + } + + assertTrue( users.get("a").isPartOf( groups.get("a").id() ) ); + assertTrue( users.get("a").isPartOf( groups.get("b").id() ) ); + assertFalse( users.get("a").isPartOf( groups.get("c").id() ) ); + assertFalse( users.get("a").isPartOf( groups.get("d").id() ) ); + + assertFalse( users.get("b").isPartOf( groups.get("a").id() ) ); + assertTrue( users.get("b").isPartOf( groups.get("b").id() ) ); + assertFalse( users.get("b").isPartOf( groups.get("c").id() ) ); + assertFalse( users.get("b").isPartOf( groups.get("d").id() ) ); + + assertFalse( users.get("c").isPartOf( groups.get("a").id() ) ); + assertTrue( users.get("c").isPartOf( groups.get("b").id() ) ); + assertTrue( users.get("c").isPartOf( groups.get("c").id() ) ); + assertTrue( users.get("c").isPartOf( groups.get("d").id() ) ); + + assertFalse( users.get("d").isPartOf( groups.get("a").id() ) ); + assertTrue( users.get("d").isPartOf( groups.get("b").id() ) ); + assertTrue( users.get("d").isPartOf( groups.get("c").id() ) ); + assertTrue( users.get("d").isPartOf( groups.get("d").id() ) ); + + assertTrue( groups.get("a").contains( users.get("a").id() ) ); + assertFalse( groups.get("a").contains( users.get("b").id() ) ); + assertFalse( groups.get("a").contains( users.get("c").id() ) ); + assertFalse( groups.get("a").contains( users.get("d").id() ) ); + + assertTrue( groups.get("b").contains( users.get("a").id() ) ); + assertTrue( groups.get("b").contains( users.get("b").id() ) ); + assertTrue( groups.get("b").contains( users.get("c").id() ) ); + assertTrue( groups.get("b").contains( users.get("d").id() ) ); + + assertFalse( groups.get("c").contains( users.get("a").id() ) ); + assertFalse( groups.get("c").contains( users.get("b").id() ) ); + assertTrue( groups.get("c").contains( users.get("c").id() ) ); + assertTrue( groups.get("c").contains( users.get("d").id() ) ); + assertFalse( groups.get("d").contains( users.get("a").id() ) ); + assertFalse( groups.get("d").contains( users.get("b").id() ) ); + assertTrue( groups.get("d").contains( users.get("c").id() ) ); + assertTrue( groups.get("d").contains( users.get("d").id() ) ); + } + +} diff --git a/src/oca/java/test/ImageTest.java b/src/oca/java/test/ImageTest.java index b0acbdfde3..d857ef76f3 100644 --- a/src/oca/java/test/ImageTest.java +++ b/src/oca/java/test/ImageTest.java @@ -203,13 +203,13 @@ public class ImageTest assertTrue( image.xpath("NAME").equals("test_img_"+cont) ); } -// @Test + @Test public void delete() { res = image.delete(); assertTrue( !res.isError() ); - res = image.info(); - assertTrue( res.isError() ); +// res = image.info(); +// assertTrue( res.isError() ); } } diff --git a/src/oca/java/test/TemplateTest.java b/src/oca/java/test/TemplateTest.java index 9d97769798..1108ef7e2b 100644 --- a/src/oca/java/test/TemplateTest.java +++ b/src/oca/java/test/TemplateTest.java @@ -22,7 +22,9 @@ 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.template.*; +import org.opennebula.client.user.User; import org.opennebula.client.vm.VirtualMachine; @@ -191,11 +193,48 @@ public class TemplateTest @Test public void allocateFromTemplate() { - template.info(); + res = template.info(); assertTrue( !res.isError() ); res = VirtualMachine.allocateFromTemplate(client, template); assertTrue( !res.isError() ); assertTrue( res.getMessage().equals("0") ); } + + @Test + public void chown() + { + // Create a new User and Group + res = User.allocate(client, "template_test_user", "password"); + assertTrue( !res.isError() ); + + int uid = Integer.parseInt(res.getMessage()); + + res = Group.allocate(client, "template_test_group"); + assertTrue( !res.isError() ); + + int gid = Integer.parseInt(res.getMessage()); + + res = template.info(); + assertTrue( !res.isError() ); + + assertTrue( template.uid() == 0 ); + assertTrue( template.gid() == 0 ); + + res = template.chown(uid, gid); + + res = template.info(); + assertTrue( !res.isError() ); + + assertTrue( template.uid() == uid ); + assertTrue( template.gid() == gid ); + + res = template.chgrp(0); + + res = template.info(); + assertTrue( !res.isError() ); + + assertTrue( template.uid() == uid ); + assertTrue( template.gid() == 0 ); + } } diff --git a/src/oca/java/test/all_tests.sh b/src/oca/java/test/all_tests.sh index 4b01299520..163a923e51 100755 --- a/src/oca/java/test/all_tests.sh +++ b/src/oca/java/test/all_tests.sh @@ -17,11 +17,11 @@ cp oned.conf $ONEDCONF_LOCATION export ONE_XMLRPC=http://localhost:2666/RPC2 -./test.sh ClusterTest ./test.sh HostTest ./test.sh ImageTest ./test.sh SessionTest ./test.sh UserTest ./test.sh VirtualMachineTest ./test.sh VirtualNetworkTest -./test.sh TemplateTest \ No newline at end of file +./test.sh TemplateTest +./test.sh GroupTest \ No newline at end of file diff --git a/src/oca/ruby/OpenNebula/Group.rb b/src/oca/ruby/OpenNebula/Group.rb index f55878fe82..1db73866ba 100644 --- a/src/oca/ruby/OpenNebula/Group.rb +++ b/src/oca/ruby/OpenNebula/Group.rb @@ -75,7 +75,7 @@ module OpenNebula super(GROUP_METHODS[:delete]) end - # Changes the owner/group + # Changes the owner # uid:: _Integer_ the new owner id. Set to -1 to leave the current one # [return] nil in case of success or an Error object def chown(uid) @@ -91,5 +91,20 @@ module OpenNebula self['UID'].to_i end + # Returns whether or not the user with id 'uid' is part of this group + def contains(uid) + return self["USERS/ID[.=#{uid}]"] != nil + end + + # Returns an array with the numeric user ids + def user_ids + array = Array.new + + self.each("USERS/ID") do |id| + array << id.text.to_i + end + + return array + end end end diff --git a/src/oca/ruby/OpenNebula/Host.rb b/src/oca/ruby/OpenNebula/Host.rb index fd150ce136..a6536187cf 100644 --- a/src/oca/ruby/OpenNebula/Host.rb +++ b/src/oca/ruby/OpenNebula/Host.rb @@ -122,11 +122,10 @@ module OpenNebula end # Returns the cluster ID of the Host - # TODO -# def cluster_id -# self['CID'] -# end -# + def cluster_id + self['CID'] + end + private def set_enabled(enabled) diff --git a/src/oca/ruby/OpenNebula/User.rb b/src/oca/ruby/OpenNebula/User.rb index 68d4ce1dc0..75d064884b 100644 --- a/src/oca/ruby/OpenNebula/User.rb +++ b/src/oca/ruby/OpenNebula/User.rb @@ -26,7 +26,9 @@ module OpenNebula :allocate => "user.allocate", :delete => "user.delete", :passwd => "user.passwd", - :chown => "user.chown" + :chown => "user.chown", + :addgroup => "user.addgroup", + :delgroup => "user.delgroup" } # Creates a User description with just its identifier @@ -90,13 +92,37 @@ module OpenNebula return rc end - # Changes the owner/group + # Changes the main group # gid:: _Integer_ the new group id. Set to -1 to leave the current one # [return] nil in case of success or an Error object def chgrp(gid) chown(USER_METHODS[:chown], -1, gid) end + # Adds a secondary group + # gid:: _Integer_ the new group id. + # [return] nil in case of success or an Error object + def addgroup(gid) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(USER_METHODS[:addgroup], @pe_id, gid) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + + # Deletes a secondary group. Fails if the group is the main one + # gid:: _Integer_ the group id. + # [return] nil in case of success or an Error object + def delgroup(gid) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(USER_METHODS[:delgroup], @pe_id, gid) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + # --------------------------------------------------------------------- # Helpers to get User information # --------------------------------------------------------------------- @@ -107,5 +133,20 @@ module OpenNebula self['GID'].to_i end + # Returns whether or not the user is part of the group 'gid' + def is_part_of(gid) + return self["GROUPS/ID[.=#{gid}]"] != nil + end + + # Returns an array with the numeric group ids + def group_ids + array = Array.new + + self.each("GROUPS/ID") do |id| + array << id.text.to_i + end + + return array + end end end diff --git a/src/oca/ruby/OpenNebula/XMLUtils.rb b/src/oca/ruby/OpenNebula/XMLUtils.rb index 19c6795df1..fbdee3b79e 100644 --- a/src/oca/ruby/OpenNebula/XMLUtils.rb +++ b/src/oca/ruby/OpenNebula/XMLUtils.rb @@ -109,11 +109,11 @@ module OpenNebula if NOKOGIRI @xml.xpath(filter.to_s).each { |pelem| - elements_array << pelem.text if !pelem.text + elements_array << pelem.text if pelem.text } else @xml.elements.each(filter.to_s) { |pelem| - elements_array << pelem.text if !pelem.text + elements_array << pelem.text if pelem.text } end