Vmware vCenter API: Step 1 建立數據中心,集羣,主機

使用開源sdk vijava (http://vijava.sourceforge.net/) java

1.建立數據中心

String vCenterAddress = "172.16.211.31";
String vCenterUserName = "administrator";
String vCenterPassword = "P@ssw0rd!";
//創建到vCenter 的連接
ServiceInstance si = new ServiceInstance(new URL("https://"
				+ vCenterAddress + "/sdk"), vCenterUserName, vCenterPassword,
				true);
String datacenterName = "Test-Datacenter";
//建立數據中心
Datacenter dc = si.getRootFolder().createDatacenter(datacenterName);

2.在數據中心中添加集羣


String clusterName = "Test-Cluster";
//在新建立的數據中心下建立集羣
ClusterConfigSpecEx clusterSpec = new ClusterConfigSpecEx();
ClusterComputeResource cluster = dc.getHostFolder().createClusterEx(clusterName, clusterSpec );

3.在數據中心中添加ESXi主機


HostConnectSpec hostSpec = new HostConnectSpec();
		String userName = "root";
		String password = "password";
		String host ="172.16.210.111";
		hostSpec.setUserName(userName);
		hostSpec.setPassword(password);
		hostSpec.setHostName(host);
		hostSpec.setForce(true); // forcely take over the host
		//添加主機到集羣中
		Task morTask = cluster.addHost_Task(hostSpec, true, null, null);
		String taskResult = morTask.waitForTask();

		if (!Task.SUCCESS.equalsIgnoreCase(taskResult)) {
			throw new RuntimeException("Unable to add host " + host
					+ " to vSphere cluster due to " + taskResult);
		}
所有代碼:
String vCenterAddress = "172.16.211.31";
		String vCenterUserName = "administrator";
		String vCenterPassword = "P@ssw0rd!";
		
		ServiceInstance si = new ServiceInstance(new URL("https://"
				+ vCenterAddress + "/sdk"), vCenterUserName, vCenterPassword,
				true);
		String datacenterName = "Test-Datacenter";
		//建立數據中心
		Datacenter dc = si.getRootFolder().createDatacenter(datacenterName);
		String clusterName = "Test-Cluster";
		//在新建立的數據中心下建立集羣
		ClusterConfigSpecEx clusterSpec = new ClusterConfigSpecEx();
		ClusterComputeResource cluster = dc.getHostFolder().createClusterEx(clusterName, clusterSpec );

		HostConnectSpec hostSpec = new HostConnectSpec();
		String userName = "root";
		String password = "password";
		String host ="172.16.210.111";
		hostSpec.setUserName(userName);
		hostSpec.setPassword(password);
		hostSpec.setHostName(host);
		hostSpec.setForce(true); // forcely take over the host
		//添加主機到集羣中
		Task morTask = cluster.addHost_Task(hostSpec, true, null, null);
		String taskResult = morTask.waitForTask();

		if (!Task.SUCCESS.equalsIgnoreCase(taskResult)) {
			throw new RuntimeException("Unable to add host " + host
					+ " to vSphere cluster due to " + taskResult);
		}
相關文章
相關標籤/搜索