Skip to content

Commit

Permalink
change: use new funcs for location based
Browse files Browse the repository at this point in the history
  • Loading branch information
avirtopeanu-ionos committed Dec 6, 2024
1 parent 40a5304 commit 109f92b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 37 deletions.
2 changes: 1 addition & 1 deletion commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func addCommands() {

rootCmd.AddCommand(cdn.Command())

rootCmd.AddCommand(funcChangeDefaultApiUrl(vpn.Root(), constants.DefaultVPNApiURL))
rootCmd.AddCommand(vpn.Root())
}

const (
Expand Down
37 changes: 1 addition & 36 deletions commands/vpn/vpn.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package vpn

import (
"fmt"
"maps"
"slices"

"github.com/ionos-cloud/ionosctl/v6/commands/vpn/ipsec"

"github.com/ionos-cloud/ionosctl/v6/commands/vpn/wireguard"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/spf13/cobra"
)

Expand All @@ -21,40 +15,11 @@ func Root() *core.Command {
Use: "vpn",
Short: "VPN Operations",
TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
location, _ := cmd.Flags().GetString(constants.FlagLocation)
changeLocation(client.Must().VPNClient, location)
},
},
}

cmd.AddCommand(wireguard.Root())
cmd.AddCommand(ipsec.Root())

cmd.Command.PersistentFlags().String(constants.FlagLocation, "de/txl", fmt.Sprintf("The location your resources are hosted in. Possible values: %s", slices.Collect(maps.Keys(locationToURL))))
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagLocation, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var locations []string
for k := range locationToURL {
locations = append(locations, k)
}

return locations, cobra.ShellCompDirectiveNoFileComp
})

return cmd
}

var locationToURL = map[string]string{
"de/fra": "https://vpn.de-fra.ionos.com",
"de/txl": "https://vpn.de-txl.ionos.com",
}

func changeLocation(client *vpn.APIClient, location string) {
cfg := client.GetConfig()
cfg.Servers = vpn.ServerConfigurations{
{
URL: locationToURL[location],
},
}
return
return core.WithRegionalFlags(cmd, constants.VPNApiRegionalURL, constants.VPNLocations)
}
2 changes: 2 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ const (
LoggingApiRegionalURL = "https://logging.%s.ionos.com"
CDNApiRegionalURL = "https://cdn.%s.ionos.com"
MariaDBApiRegionalURL = "https://mariadb.%s.ionos.com"
VPNApiRegionalURL = "https://vpn.%s.ionos.com"
)

var (
DNSLocations = []string{"de/fra"}
LoggingAPILocations = []string{"de/txl", "de/fra", "gb/lhr", "fr/par", "es/vit"}
CDNLocations = []string{"de/fra"}
MariaDBLocations = []string{"de/txl", "de/fra", "es/vit", "fr/par", "gb/lhr", "us/ewr", "us/las", "us/mci"}
VPNLocations = []string{"de/txl", "de/fra", "es/vit", "fr/par", "gb/lhr", "gb/bhx", "us/ewr", "us/las", "us/mci"}
)

// enum values. TODO: ideally i'd like these handled by the SDK
Expand Down

0 comments on commit 109f92b

Please sign in to comment.