|
| 1 | +package cloud.stackit.sdk.vpn.examples; |
| 2 | + |
| 3 | +import cloud.stackit.sdk.core.exception.ApiException; |
| 4 | +import cloud.stackit.sdk.vpn.v1api.api.VpnApi; |
| 5 | +import cloud.stackit.sdk.vpn.v1api.model.ConnectionList; |
| 6 | +import cloud.stackit.sdk.vpn.v1api.model.ConnectionResponse; |
| 7 | +import cloud.stackit.sdk.vpn.v1api.model.CreateGatewayConnectionPayload; |
| 8 | +import cloud.stackit.sdk.vpn.v1api.model.CreateGatewayPayload; |
| 9 | +import cloud.stackit.sdk.vpn.v1api.model.CreateGatewayPayloadAvailabilityZones; |
| 10 | +import cloud.stackit.sdk.vpn.v1api.model.GatewayList; |
| 11 | +import cloud.stackit.sdk.vpn.v1api.model.GatewayResponse; |
| 12 | +import cloud.stackit.sdk.vpn.v1api.model.GatewayStatus; |
| 13 | +import cloud.stackit.sdk.vpn.v1api.model.Region; |
| 14 | +import cloud.stackit.sdk.vpn.v1api.model.RoutingType; |
| 15 | +import cloud.stackit.sdk.vpn.v1api.model.TunnelConfiguration; |
| 16 | +import cloud.stackit.sdk.vpn.v1api.model.TunnelConfigurationPhase1; |
| 17 | +import cloud.stackit.sdk.vpn.v1api.model.TunnelConfigurationPhase2; |
| 18 | +import cloud.stackit.sdk.vpn.v1api.model.UpdateGatewayConnectionPayload; |
| 19 | +import cloud.stackit.sdk.vpn.v1api.model.UpdateGatewayPayload; |
| 20 | +import cloud.stackit.sdk.vpn.v1api.model.UpdateGatewayPayloadAvailabilityZones; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.HttpURLConnection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +public final class VpnExample { |
| 29 | + private static final Region REGION = Region.fromValue("eu01"); |
| 30 | + |
| 31 | + private VpnExample() {} |
| 32 | + |
| 33 | + @SuppressWarnings({ |
| 34 | + "PMD.CyclomaticComplexity", |
| 35 | + "PMD.AvoidUsingHardCodedIP", |
| 36 | + "PMD.SystemPrintln", |
| 37 | + "PMD.AvoidThrowingRawExceptionTypes" |
| 38 | + }) |
| 39 | + public static void main(String[] args) throws IOException, ApiException, InterruptedException { |
| 40 | + /* |
| 41 | + * Credentials are read from the credentialsFile in `~/.stackit/credentials.json` or the env |
| 42 | + * STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY |
| 43 | + * */ |
| 44 | + |
| 45 | + VpnApi vpnApi = new VpnApi(); |
| 46 | + |
| 47 | + // the id of your STACKIT project, read from env var for this example |
| 48 | + String projectId = System.getenv("STACKIT_PROJECT_ID"); |
| 49 | + if (projectId == null || projectId.isEmpty()) { |
| 50 | + System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found."); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + /* |
| 56 | + * /////////////////////////////////////////////////////// |
| 57 | + * // G A T E W A Y S // |
| 58 | + * /////////////////////////////////////////////////////// |
| 59 | + * */ |
| 60 | + |
| 61 | + /* create a gateway in the project */ |
| 62 | + GatewayResponse gatewayResponse = |
| 63 | + vpnApi.createGateway( |
| 64 | + projectId, |
| 65 | + REGION, |
| 66 | + new CreateGatewayPayload() |
| 67 | + .displayName("example-vpn-gateway") |
| 68 | + .planId("p500") |
| 69 | + .routingType(RoutingType.ROUTE_BASED) |
| 70 | + .availabilityZones( |
| 71 | + new CreateGatewayPayloadAvailabilityZones() |
| 72 | + .tunnel1("eu01-1") |
| 73 | + .tunnel2("eu01-2"))); |
| 74 | + |
| 75 | + Objects.requireNonNull(gatewayResponse.getId()); |
| 76 | + |
| 77 | + /* wait for the gateway creation to complete */ |
| 78 | + UUID gatewayId = gatewayResponse.getId(); |
| 79 | + Objects.requireNonNull(gatewayId); |
| 80 | + System.out.println(vpnApi.getGateway(projectId, REGION, gatewayId).getState()); |
| 81 | + while (Objects.equals( |
| 82 | + vpnApi.getGateway(projectId, REGION, gatewayId).getState(), |
| 83 | + GatewayStatus.PENDING)) { |
| 84 | + System.out.println("Waiting for gateway creation to complete ..."); |
| 85 | + TimeUnit.SECONDS.sleep(5); |
| 86 | + } |
| 87 | + System.out.println("Gateway created with the ID: " + gatewayId.toString()); |
| 88 | + |
| 89 | + // Delay because of API service: If the Gateway is updated directly after the creation, |
| 90 | + // it will throw an Error 409 "Internal conflict - please retry again later or contact |
| 91 | + // support if the issue persists" |
| 92 | + TimeUnit.SECONDS.sleep(10); |
| 93 | + |
| 94 | + /* update the gateway we just created (send complete resource because of the PUT endpoint) */ |
| 95 | + vpnApi.updateGateway( |
| 96 | + projectId, |
| 97 | + REGION, |
| 98 | + gatewayId, |
| 99 | + new UpdateGatewayPayload() |
| 100 | + .displayName("example-vpn-gateway-updated") |
| 101 | + .planId("p500") |
| 102 | + .routingType(RoutingType.ROUTE_BASED) |
| 103 | + .availabilityZones( |
| 104 | + new UpdateGatewayPayloadAvailabilityZones() |
| 105 | + .tunnel1("eu01-1") |
| 106 | + .tunnel2("eu01-2")) |
| 107 | + .labels(Collections.singletonMap("some-gateway-label", "updated"))); |
| 108 | + |
| 109 | + /* fetch the gateway we just created */ |
| 110 | + GatewayResponse fetchedGateway = vpnApi.getGateway(projectId, REGION, gatewayId); |
| 111 | + System.out.println("\nFetched gateway: "); |
| 112 | + System.out.println("* Gateway name: " + fetchedGateway.getDisplayName()); |
| 113 | + System.out.println("* Id: " + fetchedGateway.getId()); |
| 114 | + |
| 115 | + /* list all available gateways in the project */ |
| 116 | + GatewayList gateways = vpnApi.listGateways(projectId, REGION, null); |
| 117 | + System.out.println("\nAvailable gateways: "); |
| 118 | + for (GatewayResponse gateway : gateways.getGateways()) { |
| 119 | + System.out.println("* " + gateway.getDisplayName()); |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * /////////////////////////////////////////////////////// |
| 124 | + * // C O N N E C T I O N S // |
| 125 | + * /////////////////////////////////////////////////////// |
| 126 | + * */ |
| 127 | + |
| 128 | + /* create a connection in the gateway */ |
| 129 | + TunnelConfiguration tunnelConfiguration = |
| 130 | + new TunnelConfiguration() |
| 131 | + .remoteAddress("198.51.100.10") |
| 132 | + .preSharedKey("Example-super-secret-key-tunnel-0123") |
| 133 | + .phase1( |
| 134 | + new TunnelConfigurationPhase1() |
| 135 | + .dhGroups( |
| 136 | + Collections.singletonList( |
| 137 | + TunnelConfigurationPhase1.DhGroupsEnum |
| 138 | + .ECP384)) |
| 139 | + .encryptionAlgorithms( |
| 140 | + Collections.singletonList( |
| 141 | + TunnelConfigurationPhase1 |
| 142 | + .EncryptionAlgorithmsEnum |
| 143 | + .AES256)) |
| 144 | + .integrityAlgorithms( |
| 145 | + Collections.singletonList( |
| 146 | + TunnelConfigurationPhase1 |
| 147 | + .IntegrityAlgorithmsEnum |
| 148 | + .SHA2_384))) |
| 149 | + .phase2( |
| 150 | + new TunnelConfigurationPhase2() |
| 151 | + .dhGroups( |
| 152 | + Collections.singletonList( |
| 153 | + TunnelConfigurationPhase2.DhGroupsEnum |
| 154 | + .ECP384)) |
| 155 | + .encryptionAlgorithms( |
| 156 | + Collections.singletonList( |
| 157 | + TunnelConfigurationPhase2 |
| 158 | + .EncryptionAlgorithmsEnum |
| 159 | + .AES256)) |
| 160 | + .integrityAlgorithms( |
| 161 | + Collections.singletonList( |
| 162 | + TunnelConfigurationPhase2 |
| 163 | + .IntegrityAlgorithmsEnum |
| 164 | + .SHA2_384))); |
| 165 | + |
| 166 | + ConnectionResponse connectionResponse = |
| 167 | + vpnApi.createGatewayConnection( |
| 168 | + projectId, |
| 169 | + REGION, |
| 170 | + gatewayId, |
| 171 | + new CreateGatewayConnectionPayload() |
| 172 | + .displayName("example-vpn-connection") |
| 173 | + .tunnel1(tunnelConfiguration) |
| 174 | + .tunnel2(tunnelConfiguration) |
| 175 | + .localSubnets(Collections.singletonList("172.16.0.0/24")) |
| 176 | + .remoteSubnets(Collections.singletonList("172.17.0.0/24"))); |
| 177 | + |
| 178 | + Objects.requireNonNull(connectionResponse.getId()); |
| 179 | + |
| 180 | + UUID connectionId = connectionResponse.getId(); |
| 181 | + Objects.requireNonNull(connectionId); |
| 182 | + System.out.println("Gateway created with the ID:" + connectionId.toString()); |
| 183 | + |
| 184 | + /* update the connection we just created */ |
| 185 | + vpnApi.updateGatewayConnection( |
| 186 | + projectId, |
| 187 | + REGION, |
| 188 | + gatewayId, |
| 189 | + connectionId.toString(), |
| 190 | + new UpdateGatewayConnectionPayload() |
| 191 | + .displayName("example-vpn-connection-updated") |
| 192 | + .tunnel1(tunnelConfiguration) |
| 193 | + .tunnel2(tunnelConfiguration) |
| 194 | + .localSubnets(Collections.singletonList("172.16.0.0/24")) |
| 195 | + .remoteSubnets(Collections.singletonList("172.17.0.0/24"))); |
| 196 | + |
| 197 | + /* fetch the connection we just created */ |
| 198 | + ConnectionResponse fetchedConnection = |
| 199 | + vpnApi.getGatewayConnection( |
| 200 | + projectId, REGION, gatewayId, connectionId.toString()); |
| 201 | + System.out.println("\nFetched connection: "); |
| 202 | + System.out.println("* Connection name: " + fetchedConnection.getDisplayName()); |
| 203 | + System.out.println("* Id: " + fetchedConnection.getId()); |
| 204 | + |
| 205 | + /* list all available connection in the project */ |
| 206 | + ConnectionList connections = |
| 207 | + vpnApi.listGatewayConnections(projectId, REGION, gatewayId, null); |
| 208 | + System.out.println("\nAvailable connections: "); |
| 209 | + for (ConnectionResponse connection : connections.getConnections()) { |
| 210 | + System.out.println("* " + connection.getDisplayName()); |
| 211 | + } |
| 212 | + |
| 213 | + /* delete the connection */ |
| 214 | + vpnApi.deleteGatewayConnection(projectId, REGION, gatewayId, connectionId.toString()); |
| 215 | + System.out.println("Deleted connection: " + connectionId.toString()); |
| 216 | + |
| 217 | + /* |
| 218 | + * /////////////////////////////////////////////////////// |
| 219 | + * // D E L E T I O N // |
| 220 | + * /////////////////////////////////////////////////////// |
| 221 | + * */ |
| 222 | + |
| 223 | + /* delete the gateway we created */ |
| 224 | + vpnApi.deleteGateway(projectId, REGION, gatewayId); |
| 225 | + System.out.println("Started gateway deletion: " + gatewayId); |
| 226 | + |
| 227 | + /* wait for gateway deletion to complete */ |
| 228 | + while (true) { |
| 229 | + try { |
| 230 | + vpnApi.getGateway(projectId, REGION, gatewayId); |
| 231 | + System.out.println("Waiting for gateway deletion to complete..."); |
| 232 | + TimeUnit.SECONDS.sleep(5); |
| 233 | + } catch (ApiException e) { |
| 234 | + if (e.getCode() == HttpURLConnection.HTTP_NOT_FOUND) { |
| 235 | + System.out.println("Successfully deleted"); |
| 236 | + break; |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + } catch (ApiException | InterruptedException e) { |
| 242 | + throw new RuntimeException(e); |
| 243 | + } |
| 244 | + } |
| 245 | +} |
0 commit comments