Skip to content

Commit 7f957ff

Browse files
committed
feat(vpn): add example for VPN Gateway and Connection management
1 parent f1ca97a commit 7f957ff

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

examples/vpn/go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/stackitcloud/stackit-sdk-go/examples/vpn
2+
3+
go 1.26
4+
5+
// This is not needed in production. This is only here to point the golangci linter to the local version instead of the last release on GitHub.
6+
replace github.com/stackitcloud/stackit-sdk-go/services/vpn => ../../services/vpn
7+
8+
require (
9+
github.com/stackitcloud/stackit-sdk-go/core v0.26.0
10+
github.com/stackitcloud/stackit-sdk-go/services/vpn v0.8.0
11+
)
12+
13+
require (
14+
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
15+
github.com/google/uuid v1.6.0 // indirect
16+
)

examples/vpn/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
2+
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
3+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
5+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7+
github.com/stackitcloud/stackit-sdk-go/core v0.26.0 h1:jQEb9gkehfp6VCP6TcYk7BI10cz4l0KM2L6hqYBH2QA=
8+
github.com/stackitcloud/stackit-sdk-go/core v0.26.0/go.mod h1:WU1hhxnjXw2EV7CYa1nlEvNpMiRY6CvmIOaHuL3pOaA=

examples/vpn/vpn.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/stackitcloud/stackit-sdk-go/core/config"
9+
vpn "github.com/stackitcloud/stackit-sdk-go/services/vpn/v1api"
10+
)
11+
12+
func main() {
13+
region := "eu01" // Region where the resources will be created
14+
projectId := "PROJECT_ID" // the uuid of your STACKIT project
15+
planId := "PLAN_ID" // the id of the plan you want to use for the VPN Gateway. You can get the available plans with `ListPlans`.
16+
17+
// STACKIT VPN enforces the following requirements for a secure PSK:
18+
// - must be at least 20 characters long
19+
// - must be at least 16 different characters
20+
// - must have at least one upper case letter
21+
// - must have at least one lower case letter
22+
// - must have at least one number
23+
psk := "Super.$ecret_Shared3Key12345"
24+
25+
// Create a new API client, that uses default authentication and configuration
26+
vpnClient, err := vpn.NewAPIClient(
27+
config.WithRegion(region),
28+
)
29+
if err != nil {
30+
fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err)
31+
os.Exit(1)
32+
}
33+
34+
// Create a VPN Gateway
35+
createVpnGatewayPayload := vpn.CreateGatewayPayload{
36+
DisplayName: "exampleVpnGateway",
37+
PlanId: planId,
38+
RoutingType: vpn.ROUTINGTYPE_ROUTE_BASED,
39+
AvailabilityZones: vpn.CreateGatewayPayloadAvailabilityZones{
40+
Tunnel1: "eu01-1",
41+
Tunnel2: "eu01-2",
42+
},
43+
}
44+
45+
gatewayResp, err := vpnClient.DefaultAPI.CreateGateway(context.Background(), projectId, vpn.Region(region)).CreateGatewayPayload(createVpnGatewayPayload).Execute()
46+
if err != nil {
47+
fmt.Fprintf(os.Stderr, "Error when calling `CreateVpnGateway`: %v\n", err)
48+
os.Exit(1)
49+
}
50+
fmt.Printf("Created VPN Gateway with id \"%s\".\n", *gatewayResp.Id)
51+
52+
// Create a VPN Connection
53+
phase1 := vpn.TunnelConfigurationPhase1{
54+
DhGroups: []string{"ecp384"},
55+
EncryptionAlgorithms: []string{"aes256"},
56+
IntegrityAlgorithms: []string{"sha2_384"},
57+
}
58+
phase2 := vpn.TunnelConfigurationPhase2{
59+
DhGroups: []string{"ecp384"},
60+
EncryptionAlgorithms: []string{"aes256"},
61+
IntegrityAlgorithms: []string{"sha2_384"},
62+
}
63+
tunnel := vpn.TunnelConfiguration{
64+
Phase1: phase1,
65+
Phase2: phase2,
66+
PreSharedKey: &psk,
67+
RemoteAddress: "0.0.0.0",
68+
}
69+
70+
createGatewayConnectionPayload := vpn.CreateGatewayConnectionPayload{
71+
DisplayName: "exampleVpnConnection",
72+
Tunnel1: tunnel,
73+
Tunnel2: tunnel,
74+
}
75+
connectionResp, err := vpnClient.DefaultAPI.CreateGatewayConnection(context.Background(), projectId, vpn.Region(region), *gatewayResp.Id).CreateGatewayConnectionPayload(createGatewayConnectionPayload).Execute()
76+
if err != nil {
77+
fmt.Fprintf(os.Stderr, "Error when calling `CreateVpnConnection`: %v\n", err)
78+
os.Exit(1)
79+
}
80+
fmt.Printf("Created VPN Connection with id \"%s\".\n", *connectionResp.Id)
81+
82+
// Delete the VPN Connection
83+
err = vpnClient.DefaultAPI.DeleteGatewayConnection(context.Background(), projectId, vpn.Region(region), *gatewayResp.Id, *connectionResp.Id).Execute()
84+
if err != nil {
85+
fmt.Fprintf(os.Stderr, "Error when calling `DeleteVpnConnection`: %v\n", err)
86+
os.Exit(1)
87+
}
88+
fmt.Printf("Deleted VPN Connection with id \"%s\".\n", *connectionResp.Id)
89+
90+
// Delete the VPN Gateway
91+
err = vpnClient.DefaultAPI.DeleteGateway(context.Background(), projectId, vpn.Region(region), *gatewayResp.Id).Execute()
92+
if err != nil {
93+
fmt.Fprintf(os.Stderr, "Error when calling `DeleteVpnGateway`: %v\n", err)
94+
os.Exit(1)
95+
}
96+
fmt.Printf("Deleted VPN Gateway with id \"%s\".\n", *gatewayResp.Id)
97+
98+
}

0 commit comments

Comments
 (0)