|
| 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