Vmware vCenter API: Step 1 创建数据中心,集群,主机

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

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);
		}


转载于:https://my.oschina.net/osgit/blog/101865

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值