Refer to the documentation of VMWare's Webservice API to create an ESX|ESXi user and assign a user group to the user.
Version: ESX|ESXi 4.0
The JAVA code is as follows:
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>
* Create ESX or ESXi user (non-vCenter)
* </p>
* @author forandever
* @date 2009-12-31
*/
public class CreateUser {
/**
* Create user
* @param hostName host IP address. For example: 202.101.1.121
* @param userName Connection user name. For example: root
* @param password connection password. For example: 123456
* @throwsException
*/
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("This is a newly created user through API");
conn.get_service().createUser(hostLocalAccountManager,
hostAccountSpec);
System.out.println(new_userName + " " + new_password);
ManagedObjectReference rootFolder =
conn.get_sic().getRootFolder();
//Set permission group
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("New user created successfully");
}
public static void main(String [] args) throws Exception {
CreateUser createUser = new CreateUser();
createUser.createUser("202.101.1.121", "root", "123456");
}
}
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/forandever/archive/2009/12/30/5105408.aspx