@Override public void connectToGateway(@Nonnull String providerVpnId, @Nonnull String toGatewayId) throws CloudException, InternalException { APITrace.begin(provider, "connectVPNToGateway"); try { VPNGateway gateway = getGateway(toGatewayId); VPN vpn = getVPN(providerVpnId); if (gateway == null) { throw new CloudException("No such VPN gateway: " + toGatewayId); } if (vpn == null) { throw new CloudException("No such VPN: " + providerVpnId); } if (!gateway.getProtocol().equals(vpn.getProtocol())) { throw new CloudException( "VPN protocol mismatch between VPN and gateway: " + vpn.getProtocol() + " vs " + gateway.getProtocol()); } ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was configured"); } Map<String, String> parameters = provider.getStandardParameters(provider.getContext(), ELBMethod.CREATE_VPN_CONNECTION); EC2Method method; parameters.put("Type", getAWSProtocol(vpn.getProtocol())); parameters.put("CustomerGatewayId", gateway.getProviderVpnGatewayId()); parameters.put("VpnGatewayId", vpn.getProviderVpnId()); method = new EC2Method(provider, parameters); try { method.invoke(); } catch (EC2Exception e) { logger.error(e.getSummary()); e.printStackTrace(); throw new CloudException(e); } } finally { APITrace.end(); } }
private @Nullable VPNGateway toGateway(@Nonnull ProviderContext ctx, @Nullable Node node) throws CloudException, InternalException { if (node == null) { return null; } NodeList attributes = node.getChildNodes(); VPNGateway gateway = new VPNGateway(); gateway.setProviderOwnerId(ctx.getAccountNumber()); gateway.setProviderRegionId(ctx.getRegionId()); gateway.setCurrentState(VPNGatewayState.PENDING); for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); String nodeName = attr.getNodeName(); if (nodeName.equalsIgnoreCase("customerGatewayId") && attr.hasChildNodes()) { gateway.setProviderVpnGatewayId(attr.getFirstChild().getNodeValue().trim()); } else if (nodeName.equalsIgnoreCase("state") && attr.hasChildNodes()) { String state = attr.getFirstChild().getNodeValue().trim(); if (state.equalsIgnoreCase("available")) { gateway.setCurrentState(VPNGatewayState.AVAILABLE); } else if (state.equalsIgnoreCase("deleting")) { gateway.setCurrentState(VPNGatewayState.DELETING); } else if (state.equalsIgnoreCase("deleted")) { gateway.setCurrentState(VPNGatewayState.DELETED); } else if (state.equalsIgnoreCase("pending")) { gateway.setCurrentState(VPNGatewayState.PENDING); } else { logger.warn("DEBUG: Unknown VPN gateway state: " + state); } } else if (nodeName.equalsIgnoreCase("type") && attr.hasChildNodes()) { String t = attr.getFirstChild().getNodeValue().trim(); if (t.equalsIgnoreCase("ipsec.1")) { gateway.setProtocol(VPNProtocol.IPSEC1); } else if (t.equalsIgnoreCase("openvpn")) { gateway.setProtocol(VPNProtocol.OPEN_VPN); } else { logger.warn("DEBUG: Unknown VPN gateway type: " + t); gateway.setProtocol(VPNProtocol.IPSEC1); } } else if (nodeName.equalsIgnoreCase("ipAddress") && attr.hasChildNodes()) { gateway.setEndpoint(attr.getFirstChild().getNodeValue().trim()); } else if (nodeName.equalsIgnoreCase("bgpAsn") && attr.hasChildNodes()) { gateway.setBgpAsn(attr.getFirstChild().getNodeValue().trim()); } else if (nodeName.equalsIgnoreCase("tagSet") && attr.hasChildNodes()) { provider.setTags(attr, gateway); } } if (gateway.getProviderVpnGatewayId() == null) { return null; } if (gateway.getName() == null) { gateway.setName(gateway.getProviderVpnGatewayId() + " [" + gateway.getEndpoint() + "]"); } if (gateway.getDescription() == null) { gateway.setDescription(gateway.getName()); } return gateway; }