VMware {code} Community
LessClicks
Contributor
Contributor

Rest API to list port groups for a specific cluster?

Struggling with this one, I know in powercli I can run a for each loop to get at a list of port group names that are present for a specific cluster.

$Networks = (Get-Cluster ClusterNAme).ExtensionData.Network

foreach ($Network in $Networks){get-view $Network -Property name |Select name}

Has anyone done this with an API call? 

Reply
0 Kudos
1 Reply
doskiran
Enthusiast
Enthusiast

Not all vSphere REST APIs are available currently, we may expect them in the next vSphere major releases.

For now to achieve your task use SOAP-based APIs ( vSphere Management SDKs) :

Here is the java sample method using VI Java API: Getting started with vSphere API using VI Java 

 

public void portgroupList(String vCenterIP, String username, String password, String clusterName) {
		Map<String, String> vssPGs = new HashMap<>();
		Map<String, String> dvsPGs = new HashMap<>();
		try {
			ServiceInstance serviceInstance = new ServiceInstance(new URL("https://" + vCenterIP + "/sdk"), username,
					password, true);
			ClusterComputeResource cls = (ClusterComputeResource) new InventoryNavigator(
					serviceInstance.getRootFolder()).searchManagedEntity("ClusterComputeResource", clusterName);
			HostSystem[] hsArr = cls.getHosts();
			for (HostSystem hs : hsArr) {
				Network[] netArr = hs.getNetworks();
				for (Network net : netArr) {
					if (net.getMOR().getType().equals("Network"))
						vssPGs.put(net.getName(), net.getMOR().getVal());
					else if (net.getMOR().getType().equals("DistributedVirtualPortgroup"))
						dvsPGs.put(net.getName(), net.getMOR().getVal());
				}
			}
			if (!vssPGs.isEmpty()) {
				System.out.println("Virtual Standard Switch portgroups in cluster::" + clusterName);
				vssPGs.forEach((k, v) -> {
					System.out.println(k + " = " + v);
				});
			}
			if (!dvsPGs.isEmpty()) {
				System.out.println("Virtual Distributed Switch portgroups in cluster::" + clusterName);
				dvsPGs.forEach((k, v) -> {
					System.out.println(k + " = " + v);
				});
			}

		} catch (Exception e) {
			System.err.println("Error::" + e.getLocalizedMessage());
		}
	}