예제 #1
0
  @Override
  public void disconnectFromGateway(@Nonnull String vpnId, @Nonnull String gatewayId)
      throws CloudException, InternalException {
    APITrace.begin(provider, "disconnectVPNFromGateway");
    try {
      VPNGateway gateway = getGateway(gatewayId);
      VPN vpn = getVPN(vpnId);

      if (gateway == null) {
        throw new CloudException("No such VPN gateway: " + gatewayId);
      }
      if (vpn == null) {
        throw new CloudException("No such VPN: " + vpnId);
      }
      String connectionId = null;

      for (VPNConnection c : listConnections(vpnId, null)) {
        if (gatewayId.equals(c.getProviderGatewayId())) {
          connectionId = c.getProviderVpnConnectionId();
          break;
        }
      }
      if (connectionId == null) {
        logger.warn(
            "Attempt to disconnect a VPN from a gateway when there was no connection in the cloud");
        return;
      }
      ProviderContext ctx = provider.getContext();

      if (ctx == null) {
        throw new CloudException("No context was configured");
      }
      Map<String, String> parameters =
          provider.getStandardParameters(provider.getContext(), ELBMethod.DELETE_VPN_CONNECTION);
      EC2Method method;

      parameters.put("VpnConnectionId", connectionId);
      method = new EC2Method(provider, parameters);
      try {
        method.invoke();
      } catch (EC2Exception e) {
        logger.error(e.getSummary());
        e.printStackTrace();
        throw new CloudException(e);
      }
    } finally {
      APITrace.end();
    }
  }
예제 #2
0
  private @Nullable VPNConnection toConnection(
      @SuppressWarnings("UnusedParameters") @Nonnull ProviderContext ctx, @Nullable Node node)
      throws CloudException, InternalException {
    if (node == null) {
      return null;
    }

    NodeList attributes = node.getChildNodes();
    VPNConnection connection = new VPNConnection();

    connection.setCurrentState(VPNConnectionState.PENDING);
    for (int i = 0; i < attributes.getLength(); i++) {
      Node attr = attributes.item(i);
      String nodeName = attr.getNodeName();

      if (nodeName.equalsIgnoreCase("vpnConnectionId") && attr.hasChildNodes()) {
        connection.setProviderVpnConnectionId(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("customerGatewayId") && attr.hasChildNodes()) {
        connection.setProviderGatewayId(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("vpnGatewayId") && attr.hasChildNodes()) {
        connection.setProviderVpnId(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("customerGatewayConfiguration")
          && attr.hasChildNodes()) {
        connection.setConfigurationXml(attr.getFirstChild().getNodeValue().trim());
      } else if (nodeName.equalsIgnoreCase("state") && attr.hasChildNodes()) {
        String state = attr.getFirstChild().getNodeValue().trim();

        if (state.equalsIgnoreCase("available")) {
          connection.setCurrentState(VPNConnectionState.AVAILABLE);
        } else if (state.equalsIgnoreCase("deleting")) {
          connection.setCurrentState(VPNConnectionState.DELETING);
        } else if (state.equalsIgnoreCase("deleted")) {
          connection.setCurrentState(VPNConnectionState.DELETED);
        } else if (state.equalsIgnoreCase("pending")) {
          connection.setCurrentState(VPNConnectionState.PENDING);
        } else {
          logger.warn("DEBUG: Unknown VPN connection state: " + state);
        }
      } else if (nodeName.equalsIgnoreCase("type") && attr.hasChildNodes()) {
        String t = attr.getFirstChild().getNodeValue().trim();

        if (t.equalsIgnoreCase("ipsec.1")) {
          connection.setProtocol(VPNProtocol.IPSEC1);
        } else if (t.equalsIgnoreCase("openvpn")) {
          connection.setProtocol(VPNProtocol.OPEN_VPN);
        } else {
          logger.warn("DEBUG: Unknown VPN connection type: " + t);
          connection.setProtocol(VPNProtocol.IPSEC1);
        }
      }
    }
    if (connection.getProviderVpnConnectionId() == null) {
      return null;
    }
    return connection;
  }