Skip to content

Commit cd6d42c

Browse files
committed
get all vms for a host
1 parent 1ae6166 commit cd6d42c

File tree

7 files changed

+158
-73
lines changed

7 files changed

+158
-73
lines changed

plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
import com.cloud.vm.dao.UserVmCloneSettingDao;
174174
import com.cloud.vm.dao.VMInstanceDao;
175175

176-
// TODO move these items upstream
176+
// TODO move these items upstream?
177177
import com.vmware.pbm.PbmProfile;
178178
import com.vmware.vim25.AboutInfo;
179179
import com.vmware.vim25.ManagedObjectReference;
@@ -1590,14 +1590,25 @@ public List<HostMO> listHostsInDatacenter(ListVmwareDcHostsCmd cmd) {
15901590
public Pair<String, List<UnmanagedInstanceTO>> listVMsInDatacenter(ListVmwareDcVmsCmd cmd) {
15911591
Integer maxObjects = cmd.getBatchSize();
15921592
String token = cmd.getToken();
1593+
String host = cmd.getHost();
15931594

15941595
VcenterData vmwaredc = getVcenterData(cmd);
15951596

15961597
try {
15971598
VmwareContext context = getVmwareContext(vmwaredc);
15981599

15991600
DatacenterMO dcMo = getDatacenterMO(context, vmwaredc);
1600-
return dcMo.getVmsOnDatacenter(maxObjects, token);
1601+
1602+
if (com.cloud.utils.StringUtils.isNotBlank(host)) {
1603+
ManagedObjectReference hostMor = dcMo.findHost(host);
1604+
if (hostMor == null) {
1605+
throw new VmwareClientException(String.format("no host '%s' found.",host));
1606+
}
1607+
HostMO hostMo = new HostMO(context, hostMor);
1608+
return hostMo.getVms(maxObjects, token);
1609+
} else {
1610+
return dcMo.getVms(maxObjects, token);
1611+
}
16011612
} catch (InvalidParameterValueException | VmwareClientException | InvalidLocaleFaultMsg | InvalidLoginFaultMsg |
16021613
RuntimeFaultFaultMsg | URISyntaxException | InvalidPropertyFaultMsg | InvocationTargetException |
16031614
NoSuchMethodException | IllegalAccessException e) {

plugins/hypervisors/vmware/src/main/java/org/apache/cloudstack/api/command/admin/zone/ListVmwareDcVmsCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class ListVmwareDcVmsCmd extends BaseCmd implements ListVmwareDcItems {
7070
@Parameter(name = ApiConstants.PASSWORD, type = CommandType.STRING, description = "The password for specified username.")
7171
private String password;
7272

73+
@Parameter(name = ApiConstants.HOST, type = CommandType.STRING, description = "get only the VMs from the specified host.")
74+
private String host;
75+
7376
@Parameter(name = ApiConstants.BATCH_SIZE, type = CommandType.INTEGER, description = "The maximum number of results to return.")
7477
private Integer batchSize;
7578

@@ -95,6 +98,10 @@ public Integer getBatchSize() {
9598
return batchSize;
9699
}
97100

101+
public String getHost() {
102+
return host;
103+
}
104+
98105
public String getToken() {
99106
return token;
100107
}

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/BaseMO.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@
1616
// under the License.
1717
package com.cloud.hypervisor.vmware.mo;
1818

19+
import com.cloud.hypervisor.vmware.util.VmwareHelper;
20+
import com.cloud.hypervisor.vmware.util.VmwareContext;
21+
import com.cloud.utils.Pair;
22+
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
23+
import org.apache.cloudstack.vm.UnmanagedInstanceTO;
24+
1925
import org.apache.log4j.Logger;
2026

2127
import com.vmware.vim25.CustomFieldDef;
2228
import com.vmware.vim25.CustomFieldStringValue;
2329
import com.vmware.vim25.ManagedObjectReference;
30+
import com.vmware.vim25.InvalidPropertyFaultMsg;
31+
import com.vmware.vim25.RuntimeFaultFaultMsg;
32+
import com.vmware.vim25.ObjectContent;
33+
import com.vmware.vim25.RetrieveResult;
2434

25-
import com.cloud.hypervisor.vmware.util.VmwareContext;
35+
import java.lang.reflect.InvocationTargetException;
36+
import java.util.List;
2637

2738
public class BaseMO {
2839
private static final Logger s_logger = Logger.getLogger(BaseMO.class);
@@ -50,6 +61,15 @@ public BaseMO(VmwareContext context, String morType, String morValue) {
5061
_mor.setValue(morValue);
5162
}
5263

64+
protected static Pair<String, List<ObjectContent>> createReturnObjectPair(RetrieveResult result) {
65+
if (s_logger.isDebugEnabled()) {
66+
s_logger.debug("vmware result : " + ReflectionToStringBuilderUtils.reflectCollection(result));
67+
}
68+
String tokenForRetrievingNewResults = result.getToken();
69+
List<ObjectContent> listOfObjects = result.getObjects();
70+
return new Pair<>(tokenForRetrievingNewResults, listOfObjects);
71+
}
72+
5373
public VmwareContext getContext() {
5474
return _context;
5575
}
@@ -137,11 +157,11 @@ public String getCustomFieldValue(String fieldName) throws Exception {
137157
return null;
138158
}
139159

140-
public int getCustomFieldKey(String fieldName) throws Exception {
160+
public int getCustomFieldKey(String fieldName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
141161
return getCustomFieldKey(getMor().getType(), fieldName);
142162
}
143163

144-
public int getCustomFieldKey(String morType, String fieldName) throws Exception {
164+
public int getCustomFieldKey(String morType, String fieldName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
145165
assert (morType != null);
146166

147167
ManagedObjectReference cfmMor = _context.getServiceContent().getCustomFieldsManager();
@@ -153,4 +173,30 @@ public int getCustomFieldKey(String morType, String fieldName) throws Exception
153173

154174
return cfmMo.getCustomFieldKey(morType, fieldName);
155175
}
176+
177+
protected Pair<String, List<ObjectContent>> retrieveNextSetOfProperties(String tokenForPriorQuery) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
178+
RetrieveResult result = _context.getService().continueRetrievePropertiesEx(_context.getPropertyCollector(), tokenForPriorQuery);
179+
return BaseMO.createReturnObjectPair(result);
180+
}
181+
182+
protected void objectContentToUnmanagedInstanceTO(Pair<String, List<ObjectContent>> objectContents, List<UnmanagedInstanceTO> vms) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
183+
List<ObjectContent> ocs = objectContents.second();
184+
if (ocs != null) {
185+
for (ObjectContent oc : ocs) {
186+
ManagedObjectReference vmMor = oc.getObj();
187+
if (vmMor != null) {
188+
VirtualMachineMO vmMo = new VirtualMachineMO(_context, vmMor);
189+
try {
190+
if (!vmMo.isTemplate()) {
191+
HostMO hostMO = vmMo.getRunningHost();
192+
UnmanagedInstanceTO unmanagedInstance = VmwareHelper.getUnmanagedInstance(hostMO, vmMo);
193+
vms.add(unmanagedInstance);
194+
}
195+
} catch (Exception e) {
196+
s_logger.debug(String.format("Unexpected error checking unmanaged instance %s, excluding it: %s", vmMo.getVmName(), e.getMessage()), e);
197+
}
198+
}
199+
}
200+
}
201+
}
156202
}

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/CustomFieldsManagerMO.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
// under the License.
1717
package com.cloud.hypervisor.vmware.mo;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.util.List;
2021

2122
import com.vmware.vim25.CustomFieldDef;
2223
import com.vmware.vim25.ManagedObjectReference;
2324
import com.vmware.vim25.PrivilegePolicyDef;
25+
import com.vmware.vim25.InvalidPropertyFaultMsg;
26+
import com.vmware.vim25.RuntimeFaultFaultMsg;
2427

2528
import com.cloud.hypervisor.vmware.util.VmwareContext;
2629

@@ -50,12 +53,12 @@ public void setField(ManagedObjectReference morEntity, int key, String value) th
5053
_context.getService().setField(getMor(), morEntity, key, value);
5154
}
5255

53-
public List<CustomFieldDef> getFields() throws Exception {
56+
public List<CustomFieldDef> getFields() throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
5457
return _context.getVimClient().getDynamicProperty(getMor(), "field");
5558
}
5659

5760
@Override
58-
public int getCustomFieldKey(String morType, String fieldName) throws Exception {
61+
public int getCustomFieldKey(String morType, String fieldName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
5962
List<CustomFieldDef> fields = getFields();
6063
if (fields != null) {
6164
for (CustomFieldDef field : fields) {

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/DatacenterMO.java

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.List;
2424

25-
import com.cloud.hypervisor.vmware.util.VmwareHelper;
2625
import com.cloud.utils.StringUtils;
27-
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
2826
import org.apache.cloudstack.vm.UnmanagedInstanceTO;
2927
import org.apache.commons.collections.CollectionUtils;
3028
import org.apache.log4j.Logger;
@@ -81,7 +79,7 @@ public VirtualMachineMO findVm(String vmName) throws Exception {
8179
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
8280
}
8381
String instanceNameCustomField = "value[" + key + "]";
84-
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", instanceNameCustomField});
82+
List<ObjectContent> ocs = getVmProperties(new String[] {"name", instanceNameCustomField});
8583
return HypervisorHostHelper.findVmFromObjectContent(_context, ocs.toArray(new ObjectContent[0]), vmName, instanceNameCustomField);
8684
}
8785

@@ -92,7 +90,7 @@ public List<VirtualMachineMO> findVmByNameAndLabel(String vmLabel) throws Except
9290

9391
List<VirtualMachineMO> list = new ArrayList<>();
9492

95-
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", String.format("value[%d]", key)});
93+
List<ObjectContent> ocs = getVmProperties(new String[] {"name", String.format("value[%d]", key)});
9694
if (CollectionUtils.isNotEmpty(ocs)) {
9795
for (ObjectContent oc : ocs) {
9896
List<DynamicProperty> props = oc.getPropSet();
@@ -125,7 +123,7 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
125123
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
126124
}
127125

128-
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", String.format("value[%d]", key)});
126+
List<ObjectContent> ocs = getVmProperties(new String[] {"name", String.format("value[%d]", key)});
129127
if (CollectionUtils.isNotEmpty(ocs)) {
130128
for (ObjectContent oc : ocs) {
131129
List<DynamicProperty> props = oc.getPropSet();
@@ -151,31 +149,15 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
151149
return null;
152150
}
153151

154-
public Pair<String, List<UnmanagedInstanceTO>> getVmsOnDatacenter(Integer maxObjects, String token) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
152+
public Pair<String, List<UnmanagedInstanceTO>> getVms(Integer maxObjects, String token) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
155153
List<UnmanagedInstanceTO> vms = new ArrayList<>();
156-
Pair<String, List<ObjectContent>> objectContents = getVmPropertiesOnDatacenterVmFolder(new String[] {"name"}, maxObjects, token);
154+
Pair<String, List<ObjectContent>> objectContents = getVmProperties(new String[] {"name"}, maxObjects, token);
157155
if (s_logger.isDebugEnabled()) {
158156
s_logger.debug(String.format("returning token %s for future retrievals, currently %d objects retrieved.", objectContents.first(), objectContents.second().size()));
159157
}
160158
Pair<String, List<UnmanagedInstanceTO>> retval = new Pair<>(objectContents.first(), vms);
161-
List<ObjectContent> ocs = objectContents.second();
162-
if (ocs != null) {
163-
for (ObjectContent oc : ocs) {
164-
ManagedObjectReference vmMor = oc.getObj();
165-
if (vmMor != null) {
166-
VirtualMachineMO vmMo = new VirtualMachineMO(_context, vmMor);
167-
try {
168-
if (!vmMo.isTemplate()) {
169-
HostMO hostMO = vmMo.getRunningHost();
170-
UnmanagedInstanceTO unmanagedInstance = VmwareHelper.getUnmanagedInstance(hostMO, vmMo);
171-
vms.add(unmanagedInstance);
172-
}
173-
} catch (Exception e) {
174-
s_logger.debug(String.format("Unexpected error checking unmanaged instance %s, excluding it: %s", vmMo.getVmName(), e.getMessage()), e);
175-
}
176-
}
177-
}
178-
}
159+
160+
objectContentToUnmanagedInstanceTO(objectContents, vms);
179161

180162
return retval;
181163
}
@@ -207,7 +189,7 @@ public ManagedObjectReference findDatastore(String name) throws Exception {
207189
return null;
208190
}
209191

210-
public ManagedObjectReference findHost(String name) throws Exception {
192+
public ManagedObjectReference findHost(String name) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
211193
List<ObjectContent> ocs = getHostPropertiesOnDatacenterHostFolder(new String[] {"name"});
212194

213195
if (ocs != null) {
@@ -291,8 +273,8 @@ public List<ObjectContent> getDatastorePropertiesOnDatacenter(String[] propertyP
291273

292274
}
293275

294-
public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] propertyPaths) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
295-
return getVmPropertiesOnDatacenterVmFolder(propertyPaths, null, null).second();
276+
public List<ObjectContent> getVmProperties(String[] propertyPaths) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
277+
return getVmProperties(propertyPaths, null, null).second();
296278
}
297279

298280
/**
@@ -304,26 +286,21 @@ public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] property
304286
* @throws InvalidPropertyFaultMsg property does not exist as thrown by Vmware.
305287
* @throws RuntimeFaultFaultMsg generic vmware runtime exception
306288
*/
307-
public Pair<String, List<ObjectContent>> getVmPropertiesOnDatacenterVmFolder(String[] propertyPaths, Integer maxObjects, String tokenForPriorQuery) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
289+
public Pair<String, List<ObjectContent>> getVmProperties(String[] propertyPaths, Integer maxObjects, String tokenForPriorQuery) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
308290
if(StringUtils.isNotBlank(tokenForPriorQuery)) {
309291
if (s_logger.isDebugEnabled()) {
310292
s_logger.debug(String.format("running repeat query with token \'%s\'", tokenForPriorQuery));
311293
}
312-
return retrieveNextSetOfPropertiesOnDatacenterVmFolder(tokenForPriorQuery);
294+
return retrieveNextSetOfProperties(tokenForPriorQuery);
313295
} else {
314296
if (s_logger.isDebugEnabled()) {
315297
s_logger.debug(String.format("running query for %d propertypaths and max %d objects", propertyPaths.length, maxObjects));
316298
}
317-
return retrieveNextSetOfPropertiesOnDatacenterVmFolder(propertyPaths, maxObjects);
299+
return retrieveNextSetOfProperties(propertyPaths, maxObjects);
318300
}
319301
}
320302

321-
private Pair<String, List<ObjectContent>> retrieveNextSetOfPropertiesOnDatacenterVmFolder(String tokenForPriorQuery) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
322-
RetrieveResult result = _context.getService().continueRetrievePropertiesEx(_context.getPropertyCollector(), tokenForPriorQuery);
323-
return createReturnObjectPair(result);
324-
}
325-
326-
private Pair<String, List<ObjectContent>> retrieveNextSetOfPropertiesOnDatacenterVmFolder(String[] propertyPaths, Integer maxObjects) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
303+
private Pair<String, List<ObjectContent>> retrieveNextSetOfProperties(String[] propertyPaths, Integer maxObjects) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
327304
PropertySpec pSpec = new PropertySpec();
328305
pSpec.setType("VirtualMachine");
329306
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
@@ -363,15 +340,6 @@ private Pair<String, List<ObjectContent>> retrieveNextSetOfPropertiesOnDatacente
363340
return createReturnObjectPair(result);
364341
}
365342

366-
private static Pair<String, List<ObjectContent>> createReturnObjectPair(RetrieveResult result) {
367-
if (s_logger.isDebugEnabled()) {
368-
s_logger.debug("vmware result : " + ReflectionToStringBuilderUtils.reflectCollection(result));
369-
}
370-
String tokenForRetrievingNewResults = result.getToken();
371-
List<ObjectContent> listOfObjects = result.getObjects();
372-
return new Pair<>(tokenForRetrievingNewResults, listOfObjects);
373-
}
374-
375343
public static Pair<DatacenterMO, String> getOwnerDatacenter(VmwareContext context, ManagedObjectReference morEntity) throws Exception {
376344

377345
PropertySpec pSpec = new PropertySpec();

0 commit comments

Comments
 (0)