Ejemplo n.º 1
0
  /**
   * Return the list of machines in the range of ips from the parameter {ipFrom} to parameter {ipTo}
   * that matches with the hypervisortype, user, and password. For any error in the retrieve, the
   * machine will be ignored. Uses the method {@link #getRemoteHypervisor(RemoteService, IPAddress,
   * HypervisorType, String, String, Integer)} for all the links.
   *
   * @param nodecollector {@link RemoteService} object
   * @param ipFrom first address to search.
   * @param ipTo last address to search.
   * @param hypType {@link HypervisorType} object.
   * @param user user to login
   * @param password password to login
   * @param port aim port in case we want to retrieve KVM or XEN.
   * @return list of Machines.
   */
  public List<Machine> getRemoteHypervisors(
      final RemoteService nodecollector,
      final IPAddress ipFrom,
      final IPAddress ipTo,
      final HypervisorType hypType,
      final String user,
      final String password,
      final Integer port) {
    if (ipFrom.isBiggerThan(ipTo)) {
      // TODO: Test here
      addConflictErrors(APIError.NETWORK_IP_FROM_BIGGER_THAN_IP_TO);
      flushErrors();
    }

    List<Machine> listOfMachines = new ArrayList<Machine>();
    IPAddress currentIp = ipFrom;
    while (!currentIp.isBiggerThan(ipTo)) {
      try {

        Machine machine =
            this.getRemoteHypervisor(nodecollector, currentIp, hypType, user, password, port);
        listOfMachines.add(machine);
      } catch (ConflictException e) {
        // any conflict exception we found, we ignore it. We return only
        // the machines that are 'exactly' with the user, password and hypType we have
        // informed.
      }
      currentIp = currentIp.nextIPAddress();
    }

    return listOfMachines;
  }
Ejemplo n.º 2
0
  /**
   * Return the remote machine using its hypervisor APIs through the {@link
   * NodeCollectorPremiumRESTClient} client.
   *
   * @param nodecollector object nodecollector used in the datacenter we want to add the machine.
   * @param hypervisorIp ip address of the hypervisor.
   * @param hypType kind of hypervisor API we want to use.
   * @param user use to log-in
   * @param password password
   * @param port (optional) AIM port we connect to if we use KVM or XEN.
   * @return the remote {@link Machine} entity, null if we don't find anything.
   */
  public Machine getRemoteHypervisor(
      final RemoteService nodecollector,
      final IPAddress hypervisorIp,
      final HypervisorType hypType,
      final String user,
      final String password,
      final Integer port) {
    NodeCollectorRESTClient restCli = initializeRESTClient(nodecollector);
    HostDto host = new HostDto();
    try {
      host = restCli.getRemoteHostInfo(hypervisorIp.toString(), hypType, user, password, port);
    } catch (BadRequestException e) {
      // InternalServerError -> A Bad Request NEVER should be thrown from here.
      logger.error(e.getMessage());
      addUnexpectedErrors(APIError.NC_UNEXPECTED_EXCEPTION);
    } catch (LoginException e) {
      logger.debug(e.getMessage());
      addConflictErrors(APIError.NC_BAD_CREDENTIALS_TO_MACHINE);
    } catch (ConnectionException e) {
      logger.debug(e.getMessage());
      addConflictErrors(APIError.NC_CONNECTION_EXCEPTION);
    } catch (UnprovisionedException e) {
      logger.debug(e.getMessage());
      addConflictErrors(APIError.NC_NOT_FOUND_EXCEPTION);
    } catch (CollectorException e) {
      logger.error(e.getMessage());
      addUnexpectedErrors(APIError.NC_UNEXPECTED_EXCEPTION);
    } catch (ServiceUnavailableException e) {
      logger.error(e.getMessage());
      addServiceUnavailableErrors(APIError.NC_UNAVAILABLE_EXCEPTION);
    } catch (CannotExecuteException e) {
      addConflictErrors(new CommonError(APIError.STATUS_CONFLICT.getCode(), e.getMessage()));
      flushErrors();
    }

    flushErrors();

    Machine machine = hostToMachine(nodecollector.getDatacenter(), host);
    Hypervisor hypervisor =
        machine.createHypervisor(
            hypType, hypervisorIp.toString(), hypervisorIp.toString(), port, user, password);
    machine.setHypervisor(hypervisor);

    return machine;
  }