protected Collection findAllVirtualMachines()

in cloud-vmware-server/src/main/java/jetbrains/buildServer/clouds/vmware/connector/VMWareApiConnectorImpl.java [255:313]


  protected Collection<VmwareInstance> findAllVirtualMachines() throws VmwareCheckedCloudException {
    final AtomicReference<VmwareCheckedCloudException> exceptionRef = new AtomicReference<>();
    final Collection<VmwareInstance> result = findWithDatacenter(dc -> {
      final String datacenterId = dc.getMOR().getVal();
      try {
        final ObjectContent[] ocs = getObjectContents(dc, new String[][]{{
          "VirtualMachine", "name", "config.extraConfig", "config.template" , "config.changeVersion"
          , "runtime.powerState", "runtime.bootTime",
          "guest.ipAddress", "parent"
        },});
        if (ocs == null){
          return Stream.empty();
        }
        return Arrays.stream(ocs)
                     .map(oc->{
            final Map<String, Object> mappedProperties = Arrays.stream(oc.getPropSet()).collect(Collectors.toMap(
              DynamicProperty::getName, DynamicProperty::getVal
            ));

          final String vmName = String.valueOf(mappedProperties.get("name"));
          try {
            return new VmwareInstance(
              vmName,
              oc.getObj().getVal(),
              ((ArrayOfOptionValue)mappedProperties.get("config.extraConfig")).getOptionValue(),
              (VirtualMachinePowerState)mappedProperties.get("runtime.powerState"),
              (Boolean)mappedProperties.get("config.template"),
              String.valueOf(mappedProperties.get("config.changeVersion")),
              (Calendar)mappedProperties.get("runtime.bootTime"),
              (String)mappedProperties.get("guest.ipAddress"),
              (ManagedObjectReference)mappedProperties.get("parent"),
              datacenterId
            );
          } catch (Exception ex) {
            LOG.warnAndDebugDetails("Unable to process VM with name '" + vmName + "'. Not all properties are available", ex);
            return null;
          }}).filter(Objects::nonNull);
      } catch (RemoteException e) {
        LOG.warnAndDebugDetails("An error occurred while searching for all folders", e);
        exceptionRef.set(new VmwareCheckedCloudException(e));
        return Stream.empty();
      }
    });
    if (LOG.isDebugEnabled()) {
      LOG.debug(
        String.format("[%s]. All instances: [%s]"
          , myProfileId, String.join(",", result
            .stream()
            .map(VmwareInstance::getName).collect(Collectors.toList())
          )
        )
      );
    }
    if (exceptionRef.get() != null){
      LOG.warnAndDebugDetails("An exception occurred while processing findAllVirtualMachines", exceptionRef.get());
      throw exceptionRef.get();
    }
    return result;
  }