1 package org.sentrysoftware.vcenter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.net.InetAddress;
24 import java.net.URL;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.function.Consumer;
28 import java.util.function.Supplier;
29 import java.util.stream.Collectors;
30
31 import com.vmware.vim25.HostServiceTicket;
32 import com.vmware.vim25.InvalidLogin;
33 import com.vmware.vim25.mo.Datacenter;
34 import com.vmware.vim25.mo.Folder;
35 import com.vmware.vim25.mo.HostSystem;
36 import com.vmware.vim25.mo.InventoryNavigator;
37 import com.vmware.vim25.mo.ManagedEntity;
38 import com.vmware.vim25.mo.ServiceInstance;
39
40
41
42
43 public class VCenterClient {
44
45 private static final String ENTITY_HOST_SYSTEM = "HostSystem";
46 private static final String ENTITY_DATA_CENTER = "Datacenter";
47
48 private static Supplier<Boolean> isDebugEnabled;
49 private static Consumer<String> debug;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public static void setDebug(Supplier<Boolean> isDebugEnabledMethod, Consumer<String> debugMethod) {
68 isDebugEnabled = isDebugEnabledMethod;
69 debug = debugMethod;
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public static String requestCertificate(String vCenterName, String username, String password, String hostname) throws InvalidLogin, Exception {
90
91 ServiceInstance serviceInstance = null;
92
93 try {
94
95 URL vCenterURL = new URL("https://" + vCenterName + "/sdk");
96 debug.accept("Connecting to " + vCenterURL.toString() + "...");
97 serviceInstance = new ServiceInstance(vCenterURL, username, password, true);
98
99
100
101 HostSystem hostSystem = getHostSystemManagedEntity(serviceInstance, hostname);
102
103 if (hostSystem == null) {
104 throw new Exception("Unable to find host " + hostname + " in VCenter " + vCenterName);
105 }
106 else {
107 HostServiceTicket ticket = hostSystem.acquireCimServicesTicket();
108 return ticket.getSessionId();
109 }
110
111 } finally {
112 if (serviceInstance != null) {
113 serviceInstance.getServerConnection().logout();
114 }
115 }
116 }
117
118
119
120
121
122
123
124
125
126 private static HostSystem getHostSystemManagedEntity(ServiceInstance serviceInstance, String systemName) throws Exception {
127
128
129 String entityName;
130 String shortEntityName;
131 ManagedEntity[] managedEntities;
132
133
134 Folder rootFolder = serviceInstance.getRootFolder();
135 if (rootFolder == null) {
136 throw new Exception("Couldn't get the root folder");
137 }
138
139
140
141 InventoryNavigator inventoryNavigator = new InventoryNavigator(rootFolder);
142 managedEntities = inventoryNavigator.searchManagedEntities(ENTITY_HOST_SYSTEM);
143 if (managedEntities != null) {
144
145 for (ManagedEntity managedEntity : managedEntities) {
146 entityName = managedEntity.getName();
147 if (systemName.equalsIgnoreCase(entityName)) {
148
149 return (HostSystem)managedEntity;
150 }
151 }
152
153
154 if (systemName.indexOf('.') == -1) {
155
156 for (ManagedEntity managedEntity : managedEntities) {
157 entityName = managedEntity.getName();
158 int dotIndex = entityName.indexOf('.');
159 if (dotIndex > 1) {
160 shortEntityName = entityName.substring(0, dotIndex);
161 if (systemName.equalsIgnoreCase(shortEntityName)) {
162
163 return (HostSystem)managedEntity;
164 }
165 }
166 }
167 }
168
169
170 if (isDebugEnabled.get()) {
171 String managedEntitiesString = "";
172 for (ManagedEntity managedEntity : managedEntities) {
173 managedEntitiesString = managedEntitiesString + " - " + managedEntity.getName() + "\n";
174 }
175 StringBuilder entityList = new StringBuilder();
176 for (ManagedEntity managedEntity : managedEntities) {
177 entityList.append(" - ").append(managedEntity.getName()).append("\n");
178 }
179 debug.accept("VCenterClient: Couldn't find host " + systemName + " in the list of managed entities in VCenter " + serviceInstance.getServerConnection().getUrl().getHost() + ":\n" + entityList);
180 debug.accept("VCenterClient: Will now try with the IP address of " + systemName);
181 }
182 }
183
184
185 managedEntities = inventoryNavigator.searchManagedEntities(ENTITY_DATA_CENTER);
186 if (null == managedEntities || managedEntities.length == 0) {
187 throw new Exception("No Datacenter-type managed entity");
188 }
189
190
191 InetAddress[] hostIPaddresses;
192 try {
193 hostIPaddresses = InetAddress.getAllByName(systemName);
194 }
195 catch (Exception e) {
196 throw new Exception("Couldn't resolve " + systemName + " into a valid IP address");
197 }
198
199
200 for (ManagedEntity datacenterEntity : managedEntities) {
201
202
203 for (InetAddress hostIPaddress : hostIPaddresses) {
204
205
206 ManagedEntity[] managedEntitiesInDatacenter = serviceInstance.getSearchIndex().findAllByIp((Datacenter)datacenterEntity, hostIPaddress.getHostAddress(), false);
207
208
209 if (managedEntitiesInDatacenter != null && managedEntitiesInDatacenter.length != 0) {
210 return (HostSystem)managedEntitiesInDatacenter[0];
211 }
212 }
213 }
214
215 return null;
216 }
217
218
219
220
221
222
223
224
225
226
227 public static List<String> getAllHostSystemManagedEntities(String vCenterName, String username, String password) throws Exception {
228
229 ServiceInstance serviceInstance = null;
230
231 try {
232
233 URL vCenterURL = new URL("https://" + vCenterName + "/sdk");
234 debug.accept("Connecting to " + vCenterURL.toString() + "...");
235 serviceInstance = new ServiceInstance(vCenterURL, username, password, true);
236
237
238 Folder rootFolder = serviceInstance.getRootFolder();
239 if (rootFolder == null) {
240 throw new Exception("Couldn't get the root folder");
241 }
242
243
244 InventoryNavigator inventoryNavigator = new InventoryNavigator(rootFolder);
245
246
247 return Arrays.stream(inventoryNavigator.searchManagedEntities(ENTITY_HOST_SYSTEM))
248 .map(ManagedEntity::getName)
249 .collect(Collectors.toList())
250 ;
251
252 } finally {
253 if (serviceInstance != null) {
254 serviceInstance.getServerConnection().logout();
255 }
256 }
257
258 }
259
260 }