@Override public void attachToVLAN(@Nonnull String providerVpnId, @Nonnull String providerVlanId) throws CloudException, InternalException { APITrace.begin(provider, "attachVPNToVLAN"); try { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was configured"); } Map<String, String> parameters = provider.getStandardParameters(provider.getContext(), ELBMethod.ATTACH_VPN_GATEWAY); EC2Method method; parameters.put("VpcId", providerVlanId); parameters.put("VpnGatewayId", providerVpnId); method = new EC2Method(provider, provider.getEc2Url(), parameters); try { method.invoke(); } catch (EC2Exception e) { logger.error(e.getSummary()); e.printStackTrace(); throw new CloudException(e); } } finally { APITrace.end(); } }
private @Nonnull Iterable<VPNGateway> listGateways( @Nullable String gatewayId, @Nullable String bgpAsn) throws CloudException, InternalException { APITrace.begin(provider, "listVPCGateways"); try { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was configured"); } Map<String, String> parameters = provider.getStandardParameters( provider.getContext(), ELBMethod.DESCRIBE_CUSTOMER_GATEWAYS); EC2Method method; NodeList blocks; Document doc; if (gatewayId != null) { parameters.put("Filter.1.Name", "customer-gateway-id"); parameters.put("Filter.1.Value.1", gatewayId); } else if (bgpAsn != null) { parameters.put("Filter.1.Name", "bgp-asn"); parameters.put("Filter.1.Value.1", bgpAsn); } method = new EC2Method(provider, provider.getEc2Url(), parameters); try { doc = method.invoke(); } catch (EC2Exception e) { String code = e.getCode(); if (code != null) { if (code.startsWith("InvalidCustomer") || code.startsWith("InvalidB")) { return Collections.emptyList(); } } logger.error(e.getSummary()); throw new CloudException(e); } ArrayList<VPNGateway> list = new ArrayList<VPNGateway>(); blocks = doc.getElementsByTagName("item"); for (int i = 0; i < blocks.getLength(); i++) { Node item = blocks.item(i); VPNGateway gw = toGateway(ctx, item); if (gw != null) { list.add(gw); } } return list; } finally { APITrace.end(); } }
@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, provider.getEc2Url(), parameters); try { method.invoke(); } catch (EC2Exception e) { logger.error(e.getSummary()); e.printStackTrace(); throw new CloudException(e); } } finally { APITrace.end(); } }
@Override public @Nonnull VPNGateway createVPNGateway( @Nonnull String endpoint, @Nonnull String name, @Nonnull String description, @Nonnull VPNProtocol protocol, @Nonnull String bgpAsn) throws CloudException, InternalException { APITrace.begin(provider, "createVPNGateway"); try { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was configured"); } Map<String, String> parameters = provider.getStandardParameters(provider.getContext(), ELBMethod.CREATE_CUSTOMER_GATEWAY); EC2Method method; NodeList blocks; Document doc; parameters.put("Type", getAWSProtocol(protocol)); parameters.put("IpAddress", endpoint); parameters.put("BgpAsn", bgpAsn); method = new EC2Method(provider, provider.getEc2Url(), parameters); try { doc = method.invoke(); } catch (EC2Exception e) { logger.error(e.getSummary()); if (logger.isDebugEnabled()) { e.printStackTrace(); } throw new CloudException(e); } blocks = doc.getElementsByTagName("customerGateway"); for (int i = 0; i < blocks.getLength(); i++) { Node item = blocks.item(i); VPNGateway gateway = toGateway(ctx, item); if (gateway != null) { return gateway; } } throw new CloudException("No VPN gateway was created, but no error was reported"); } finally { APITrace.end(); } }
private @Nonnull Iterable<VPN> listVPNs(@Nullable String vpnId) throws CloudException, InternalException { APITrace.begin(provider, "listVPNs"); try { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was configured"); } Map<String, String> parameters = provider.getStandardParameters(provider.getContext(), ELBMethod.DESCRIBE_VPN_GATEWAYS); EC2Method method; NodeList blocks; Document doc; if (vpnId != null) { parameters.put("VpnGatewayId.1", vpnId); } method = new EC2Method(provider, provider.getEc2Url(), parameters); try { doc = method.invoke(); } catch (EC2Exception e) { String code = e.getCode(); if (code != null) { if (code.startsWith("InvalidVpn")) { return Collections.emptyList(); } } logger.error(e.getSummary()); throw new CloudException(e); } ArrayList<VPN> list = new ArrayList<VPN>(); blocks = doc.getElementsByTagName("item"); for (int i = 0; i < blocks.getLength(); i++) { Node item = blocks.item(i); VPN vpn = toVPN(ctx, item); if (vpn != null) { list.add(vpn); } } return list; } finally { APITrace.end(); } }
@Override public @Nonnull Iterable<ResourceStatus> listGatewayStatus() throws CloudException, InternalException { APITrace.begin(provider, "listVPCGatewayStatus"); try { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was configured"); } Map<String, String> parameters = provider.getStandardParameters( provider.getContext(), ELBMethod.DESCRIBE_CUSTOMER_GATEWAYS); EC2Method method; NodeList blocks; Document doc; method = new EC2Method(provider, provider.getEc2Url(), parameters); try { doc = method.invoke(); } catch (EC2Exception e) { String code = e.getCode(); if (code != null) { if (code.startsWith("InvalidCustomer") || code.startsWith("InvalidB")) { return Collections.emptyList(); } } logger.error(e.getSummary()); throw new CloudException(e); } ArrayList<ResourceStatus> list = new ArrayList<ResourceStatus>(); blocks = doc.getElementsByTagName("item"); for (int i = 0; i < blocks.getLength(); i++) { ResourceStatus status = toGatewayStatus(blocks.item(i)); if (status != null) { list.add(status); } } return list; } finally { APITrace.end(); } }
@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, provider.getEc2Url(), parameters); try { method.invoke(); } catch (EC2Exception e) { logger.error(e.getSummary()); e.printStackTrace(); throw new CloudException(e); } } finally { APITrace.end(); } }
@Override public @Nonnull VPN createVPN( @Nullable String dataCenterId, @Nonnull String name, @Nonnull String description, @Nonnull VPNProtocol protocol) throws CloudException, InternalException { APITrace.begin(provider, "createVPN"); try { 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_GATEWAY); EC2Method method; NodeList blocks; Document doc; parameters.put("Type", getAWSProtocol(protocol)); method = new EC2Method(provider, provider.getEc2Url(), parameters); try { doc = method.invoke(); } catch (EC2Exception e) { logger.error(e.getSummary()); e.printStackTrace(); throw new CloudException(e); } blocks = doc.getElementsByTagName("vpnGateway"); for (int i = 0; i < blocks.getLength(); i++) { Node item = blocks.item(i); VPN vpn = toVPN(ctx, item); if (vpn != null) { return vpn; } } throw new CloudException("No VPN was created, but no error was reported"); } finally { APITrace.end(); } }
@Override public void deleteVPNGateway(@Nonnull String gatewayId) throws CloudException, InternalException { APITrace.begin(provider, "deleteVPNGateway"); try { Map<String, String> parameters = provider.getStandardParameters(provider.getContext(), ELBMethod.DELETE_CUSTOMER_GATEWAY); EC2Method method; parameters.put("CustomerGatewayId", gatewayId); method = new EC2Method(provider, provider.getEc2Url(), parameters); try { method.invoke(); } catch (EC2Exception e) { logger.error(e.getSummary()); throw new CloudException(e); } } finally { APITrace.end(); } }
@Override public boolean isSubscribed() throws CloudException, InternalException { APITrace.begin(provider, "isSubscribedVPCGateway"); try { Map<String, String> parameters = provider.getStandardParameters( provider.getContext(), ELBMethod.DESCRIBE_CUSTOMER_GATEWAYS); EC2Method method; method = new EC2Method(provider, provider.getEc2Url(), parameters); try { method.invoke(); return true; } catch (EC2Exception e) { if (e.getStatus() == HttpServletResponse.SC_UNAUTHORIZED || e.getStatus() == HttpServletResponse.SC_FORBIDDEN) { return false; } String code = e.getCode(); if (code != null && (code.equals("SubscriptionCheckFailed") || code.equals("AuthFailure") || code.equals("SignatureDoesNotMatch") || code.equals("UnsupportedOperation") || code.equals("InvalidClientTokenId") || code.equals("OptInRequired"))) { return false; } logger.error(e.getSummary()); e.printStackTrace(); throw new CloudException(e); } } finally { APITrace.end(); } }