参考VMWare的Webservice API的文档,实现创建ESX|ESXi用户并给用户赋予用户组。
版本: ESX|ESXi 4.0
JAVA代码如下:
view plaincopy to clipboardprint?
import java.net.URL;
import com.vmware.vim25.HostAccountSpec;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.Permission;
import com.vmware.vim25.ServiceContent;
import com.vmware.vim25.VimPortType;
import com.vmware.vim25.VimServiceLocator;
import com.vmware.security.credstore.*;
/**
* <p>
* 创建ESX or ESXi用户(非vCenter)
* </p>
* @author forandever
* @date 2009-12-31
*/
public class CreateUser {
/**
* 创建用户
* @param hostName 主机IP地址。例如: 202.101.1.121
* @param userName 连接用户名。例如: root
* @param password 连接密码。例如:123456
* @throws Exception
*/
private void createUser(String hostName, String userName, String password) throws Exception {
ServerConn conn = new ServerConn();
conn.prepare(hostName, userName, password);
ManagedObjectReference hostLocalAccountManager =
conn.get_sic().getAccountManager();
ManagedObjectReference hostAuthorizationManager =
conn.get_sic().getAuthorizationManager();
String new_userName = generateUserName();
String new_password = generatePassword();
HostAccountSpec hostAccountSpec = new HostAccountSpec();
hostAccountSpec.setId(new_userName);
hostAccountSpec.setPassword(new_password);
hostAccountSpec.setDescription("这是通过API新创建的用户");
conn.get_service().createUser(hostLocalAccountManager,
hostAccountSpec);
System.out.println(new_userName + " " + new_password);
ManagedObjectReference rootFolder =
conn.get_sic().getRootFolder();
// 设置权限组
Permission per = new Permission();
per.setGroup(false);
per.setPrincipal(new_userName);
per.setRoleId(-1);
per.setPropagate(true);
per.setEntity(rootFolder);
conn.get_service().setEntityPermissions(hostAuthorizationManager,
rootFolder,
new Permission [] {per});
// CredentialStore csObj = CredentialStoreFactory.getCredentialStore();
// csObj.addPassword(getServerName(),userName,password.toCharArray());
System.out.println("新用户创建成功");
}
public static void main(String [] args) throws Exception {
CreateUser createUser = new CreateUser();
createUser.createUser("202.101.1.121", "root", "123456");
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/forandever/archive/2009/12/30/5105408.aspx