參考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