コード例 #1
0
  public FloatingIP createFloatingIP() throws RegionException, NeutronException {
    Optional<? extends FloatingIPApi> floatingIPApiExtension =
        neutronApi.getFloatingIPApi(getRegionOne());

    if (floatingIPApiExtension.isPresent()) {
      FloatingIPApi floatingIPApi = floatingIPApiExtension.get();

      return floatingIPApi.create(
          CreateFloatingIP.createBuilder(
                  getNetworkIdByName(OpenstackConfiguration.EXTERNAL_NETWORK))
              .build());
    } else {
      throw new NeutronException("FloatingIp is not present");
    }
  }
コード例 #2
0
  /**
   * Create a new Floating IP from the given floating network and assign it to the given port
   *
   * @param port the port to which a Floating IP to be assigned
   * @param floatingNetworkUuid the network uuid of the floating network from which the Floating IP
   *     should be created
   * @return the newly created/assigned {@link FloatingIP}
   */
  private FloatingIP createAndAssignFloatingIP(Port port, String floatingNetworkUuid) {

    assertNotNull(port, "Cannot create floating IP. Invalid port. Port cannot be null");
    assertNotNullAndNotEmpty(
        floatingNetworkUuid,
        "Cannot create floating IP. Invalid floating network uuid. "
            + "Floating network uuid cannot be null");

    if (log.isDebugEnabled()) {
      String msg =
          String.format(
              "Trying to create a floating IP from network %s to assign to the port %s",
              floatingNetworkUuid, port.getId());
      log.debug(msg);
    }

    FloatingIP.CreateFloatingIP createFip;
    try {
      createFip = FloatingIP.createBuilder(floatingNetworkUuid).portId(port.getId()).build();
    } catch (Exception e) {
      String msg =
          String.format(
              "Error while getting floating IP builder for the external network %s and port %s",
              floatingNetworkUuid, port.toString());
      log.error(msg, e);
      throw new CloudControllerException(msg, e);
    }

    FloatingIP floatingIP = null;
    try {
      floatingIP = floatingIPApi.create(createFip);
    } catch (Exception e) {
      String msg =
          String.format(
              "Error while creating floating IP for the port %s, from floating network %s",
              port.toString(), floatingNetworkUuid);
      log.error(msg, e);
      throw new CloudControllerException(msg, e);
    }

    String msg =
        String.format("Unable to create a floating IP from network %s", floatingNetworkUuid);
    assertNotNull(floatingIP, msg);

    return floatingIP;
  }