I have the username/password/url to one VC in linked mode. How do I get all the VCs that are in linked mode?
Here is my code to retrieve the UserSession. I know it's possible if you get access to com.vmware.vise.UserSession but I'm not able to do so with com.vmware.vim25.UserSession.
public UserSession connServer(String vcenterIP, String username, String password) throws Exception {
String url = "https://" + vcenterIP + "/sdk/vimService";
try {
ManagedObjectReference managedObjRef = new ManagedObjectReference();
managedObjRef.setType("ServiceInstance");
managedObjRef.setValue("ServiceInstance");
VimService vimService = new VimService();
VimPortType vimPort = vimService.getVimPort();
Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
serviceContent = vimPort.retrieveServiceContent(managedObjRef);
if (serviceContent.getSessionManager() != null) {
currentSession = vimPort.login(serviceContent.getSessionManager(), username, password, null);
}
} catch (Exception e) {
System.out.println(e);
}
return currentSession;
}
Here is the sample code to get all linked vCenters by using userAgent , here userAgent is the name of user agent or application. In my case I opened my linked VC atleast onetime with web-client, so userAgent comparing with "web-client" in the code.
Eg:: userAgents starts with - "web-client" , "Java" , "cl" ,"VMware vim-java" .....
public List<String> getAllLinkedVCServers(String vcenterIP, String userName,String password) {
List<String> vcList = new ArrayList<String>();
try {
ServiceInstance si = new ServiceInstance(new URL("https://"+ vcenterIP + "/sdk"), userName, password, true);
UserSession[] userSessions = si.getSessionManager();
for (int i = 0; i < userSessions.length; i++) {
if (userSessions[i].getUserAgent().contains("web-client")) {
vcList.add(userSessions[i].getIpAddress());
} }
} catch (Exception e) {
e.printStackTrace(); }
return vcList;
}