Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vpn #466

Open
wants to merge 66 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
13d3ffc
feat: structure
avirtopeanu-ionos Oct 24, 2024
23ecee7
add client
avirtopeanu-ionos Oct 28, 2024
e15cdd7
feat: improve wireguard gateway create
avirtopeanu-ionos Oct 29, 2024
f4dae6c
fix helptext
avirtopeanu-ionos Oct 29, 2024
87318d3
change default url
avirtopeanu-ionos Oct 29, 2024
a2fa7b4
rm: ipsec. to be added separate pr
avirtopeanu-ionos Oct 29, 2024
6b0e9bd
peer create
avirtopeanu-ionos Oct 29, 2024
148ba77
jsonpaths
avirtopeanu-ionos Oct 30, 2024
c3393f3
vpn wireguard peer
avirtopeanu-ionos Oct 30, 2024
691d914
cols
avirtopeanu-ionos Oct 30, 2024
0098de1
fix: forgot rename also required check
avirtopeanu-ionos Oct 30, 2024
98528b0
fix: check also cidr
avirtopeanu-ionos Oct 30, 2024
d7a141a
tab complete ipblock
avirtopeanu-ionos Oct 30, 2024
1ca382d
fix annoying bug
avirtopeanu-ionos Oct 30, 2024
3856426
gateway list
avirtopeanu-ionos Oct 31, 2024
79b599c
delete
avirtopeanu-ionos Nov 4, 2024
5b1539e
fix completer
avirtopeanu-ionos Nov 4, 2024
cd39aa4
read key from path
avirtopeanu-ionos Nov 4, 2024
974d332
private key path
avirtopeanu-ionos Nov 4, 2024
11aac2c
conv2table func
avirtopeanu-ionos Nov 5, 2024
de9650b
few fixes
avirtopeanu-ionos Nov 5, 2024
cff4a71
peer list
avirtopeanu-ionos Nov 5, 2024
d7daa81
feat: wireguard gateway update
avirtopeanu-ionos Nov 6, 2024
d172522
get
avirtopeanu-ionos Nov 6, 2024
655b559
peer delete
avirtopeanu-ionos Nov 6, 2024
c6f87eb
peer delete
avirtopeanu-ionos Nov 6, 2024
53d936e
peer update
avirtopeanu-ionos Nov 6, 2024
e12d278
peer get
avirtopeanu-ionos Nov 6, 2024
705f1e9
fix: ID not IP
avirtopeanu-ionos Nov 6, 2024
7a18e9d
docs
avirtopeanu-ionos Nov 7, 2024
7b231b7
fix extra helptext
avirtopeanu-ionos Nov 7, 2024
ef917a7
fix broken completions
avirtopeanu-ionos Nov 7, 2024
05a8ea6
bats test
avirtopeanu-ionos Nov 11, 2024
1051cfe
location
avirtopeanu-ionos Nov 11, 2024
8e8a04f
location
avirtopeanu-ionos Nov 11, 2024
00bb279
location
avirtopeanu-ionos Nov 12, 2024
7ecd1b5
initial ipsec
avirtopeanu-ionos Nov 12, 2024
afb839a
temp bats
avirtopeanu-ionos Nov 12, 2024
9d553c7
ipsec
avirtopeanu-ionos Nov 13, 2024
bf7d4c7
fix update, jsonproperties
avirtopeanu-ionos Nov 13, 2024
eddd2e3
json properties ipsec tunnel
avirtopeanu-ionos Nov 14, 2024
25ddd6c
add ipsec to vpn
avirtopeanu-ionos Nov 14, 2024
4585280
ipsec fixes
avirtopeanu-ionos Nov 14, 2024
a16a96b
better completions
avirtopeanu-ionos Nov 14, 2024
4162453
add infos to completions
avirtopeanu-ionos Nov 18, 2024
dd98960
fix nullptr ref
avirtopeanu-ionos Nov 18, 2024
c8fa2d0
fix update making certain fields nil
avirtopeanu-ionos Nov 18, 2024
da7f650
fix flags ipsec create gateway
avirtopeanu-ionos Nov 18, 2024
9778e3d
move completer
avirtopeanu-ionos Nov 18, 2024
a93dcbf
ipsec completer
avirtopeanu-ionos Nov 18, 2024
e5eabed
show DatacenterID
avirtopeanu-ionos Nov 18, 2024
5996d95
temp test
avirtopeanu-ionos Nov 19, 2024
664313d
fix: Private key needs to be provided again on updates
avirtopeanu-ionos Nov 19, 2024
a353d75
cleanup
avirtopeanu-ionos Nov 19, 2024
553e6ff
remove metadata json example
avirtopeanu-ionos Nov 20, 2024
bb09c6a
add ipsec test
avirtopeanu-ionos Nov 20, 2024
ef8abf4
fix: json properties
avirtopeanu-ionos Nov 20, 2024
46c7a93
fix json flag
avirtopeanu-ionos Nov 21, 2024
f2d5884
more ipsec tests
avirtopeanu-ionos Nov 21, 2024
b74fa2b
fix: add missing update tunnel flags
avirtopeanu-ionos Nov 21, 2024
c32745f
fix: required flags update
avirtopeanu-ionos Nov 21, 2024
e2d0055
fix tunnel completion
avirtopeanu-ionos Nov 21, 2024
a980063
fix tunnel viper bug
avirtopeanu-ionos Nov 21, 2024
e9195ce
fix: merge conflicts
avirtopeanu-ionos Nov 21, 2024
8754e30
make docs
avirtopeanu-ionos Nov 21, 2024
dbd61d6
fix examples
avirtopeanu-ionos Nov 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commands/cdn/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package completer

import (
"context"

"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
cdn "github.com/ionos-cloud/sdk-go-cdn"
Expand Down
3 changes: 2 additions & 1 deletion commands/cdn/distribution/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/pkg/pointer"
"github.com/ionos-cloud/ionosctl/v6/pkg/uuidgen"
cdn "github.com/ionos-cloud/sdk-go-cdn"
"github.com/spf13/viper"
"os"
)

func Create() *core.Command {
Expand Down
1 change: 1 addition & 0 deletions commands/cdn/distribution/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package distribution
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
Expand Down
1 change: 1 addition & 0 deletions commands/cdn/distribution/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package distribution
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
cdn "github.com/ionos-cloud/sdk-go-cdn"
"github.com/spf13/cobra"
Expand Down
1 change: 1 addition & 0 deletions commands/cdn/distribution/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package distribution
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
Expand Down
1 change: 1 addition & 0 deletions commands/cdn/distribution/routingrules/routing_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routingrules
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
Expand Down
1 change: 1 addition & 0 deletions commands/cdn/distribution/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package distribution
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/spf13/cobra"

Expand Down
31 changes: 31 additions & 0 deletions commands/cloudapi-v6/completer/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ func AttachedCdromsIds(datacenterId, serverId string) []string {
return attachedCdromsIds
}

func DatacenterIdsFilterLocation(loc string) []string {
req := client.Must().CloudClient.DataCentersApi.DatacentersGet(context.Background())
if loc != "" {
req = req.Filter("location", loc)
}
dcs, _, err := req.Execute()
if err != nil {
return nil
}

var dcIds []string
for _, item := range *dcs.Items {
var completion string
if item.Id == nil {
continue
}
completion = *item.Id
if props, ok := item.GetPropertiesOk(); ok {
if name, ok := props.GetNameOk(); ok {
completion = fmt.Sprintf("%s\t%s", completion, *name)
}
if location, ok := props.GetLocationOk(); ok {
completion = fmt.Sprintf("%s - %s", completion, *location)
}
}

dcIds = append(dcIds, completion)
}
return dcIds
}

func DataCentersIds() []string {
datacenterSvc := resources.NewDataCenterService(client.Must(), context.Background())
datacenters, _, err := datacenterSvc.List(resources.ListQueryParams{})
Expand Down
76 changes: 41 additions & 35 deletions commands/dbaas/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,55 @@ import (
)

func GetCidrCompletionFunc(cmd *core.Command) func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
getNicIp := func() (string, error) {
ls, _, err := client.Must().CloudClient.ServersApi.DatacentersServersGet(context.Background(),
viper.GetString(core.GetFlagName(cmd.NS, constants.FlagDatacenterId))).Execute()
if err != nil || ls.Items == nil || len(*ls.Items) == 0 {
return "", fmt.Errorf("failed getting servers %w", err)
return func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
databaseIp := "192.168.1.128" // fallback in case of no servers / errs
ip, err := getNicIp(cmd)
if err != nil || ip == "" {
ip = databaseIp
}
cidrs := generateCidrs(cmd, ip)
return []string{strings.Join(cidrs, ",")}, cobra.ShellCompDirectiveNoFileComp
}
}

for _, server := range *ls.Items {
if server.Id == nil {
return "", fmt.Errorf("failed getting ID")
}
func getNicIp(cmd *core.Command) (string, error) {
ls, _, err := client.Must().CloudClient.ServersApi.DatacentersServersGet(context.Background(),
viper.GetString(core.GetFlagName(cmd.NS, constants.FlagDatacenterId))).Execute()
if err != nil || ls.Items == nil || len(*ls.Items) == 0 {
return "", fmt.Errorf("failed getting servers %w", err)
}

nics, _, err := client.Must().CloudClient.NetworkInterfacesApi.DatacentersServersNicsGet(context.Background(),
viper.GetString(core.GetFlagName(cmd.NS, constants.FlagDatacenterId)), *server.Id).Execute()
if err != nil || nics.Items == nil || len(*nics.Items) == 0 {
return "", fmt.Errorf("failed getting nics %w", err)
}
// Find the first nic with IPs not empty and return it
for _, nic := range *nics.Items {
if nic.Properties != nil && nic.Properties.Ips != nil && len(*nic.Properties.Ips) > 0 {
return (*nic.Properties.Ips)[0], nil
}
for _, server := range *ls.Items {
if server.Id == nil {
return "", fmt.Errorf("failed getting ID")
}

nics, _, err := client.Must().CloudClient.NetworkInterfacesApi.DatacentersServersNicsGet(context.Background(),
viper.GetString(core.GetFlagName(cmd.NS, constants.FlagDatacenterId)), *server.Id).Execute()
if err != nil || nics.Items == nil || len(*nics.Items) == 0 {
return "", fmt.Errorf("failed getting nics %w", err)
}
// Find the first nic with IPs not empty and return it
for _, nic := range *nics.Items {
if nic.Properties != nil && nic.Properties.Ips != nil && len(*nic.Properties.Ips) > 0 {
return (*nic.Properties.Ips)[0], nil
}
}
return "", fmt.Errorf("no NIC with IP")
}
return "", fmt.Errorf("no NIC with IP")
}

generateCidrs := func(ip string, instances int) []string {
var cidrs []string
for i := 0; i < instances; i++ {
cidrs = append(cidrs, fake.IP(fake.WithIPv4(), fake.WithIPCIDR(ip+"/24"))+"/24")
}
return cidrs
func generateCidrs(cmd *core.Command, ip string) []string {
var cidrs []string

instances := viper.GetInt(core.GetFlagName(cmd.NS, constants.FlagInstances))
if instances == 0 {
instances = 1 // flag might not be set
}

return func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
databaseIp := "192.168.1.128" // fallback in case of no servers / errs
ip, err := getNicIp()
if err != nil || ip == "" {
ip = databaseIp
}
instances := viper.GetInt(core.GetFlagName(cmd.NS, constants.FlagInstances))
cidrs := generateCidrs(ip, instances)
return []string{strings.Join(cidrs, ",")}, cobra.ShellCompDirectiveNoFileComp
for i := 0; i < instances; i++ {
cidrs = append(cidrs, fake.IP(fake.WithIPv4(), fake.WithIPCIDR(ip+"/24"))+"/24")
}

return cidrs
}
2 changes: 1 addition & 1 deletion commands/dns/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"

"github.com/ionos-cloud/sdk-go-dns"
ionoscloud "github.com/ionos-cloud/sdk-go-dns"
"github.com/spf13/viper"
)

Expand Down
7 changes: 5 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package commands

import (
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cdn"
"os"
"strings"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn"

certificates "github.com/ionos-cloud/ionosctl/v6/commands/certmanager"
"github.com/ionos-cloud/ionosctl/v6/commands/cfg"
cloudapiv6 "github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6"
Expand All @@ -16,6 +17,7 @@ import (
logging_service "github.com/ionos-cloud/ionosctl/v6/commands/logging-service"
"github.com/ionos-cloud/ionosctl/v6/commands/token"
vm_autoscaling "github.com/ionos-cloud/ionosctl/v6/commands/vm-autoscaling"
"github.com/ionos-cloud/ionosctl/v6/commands/vpn"
"github.com/ionos-cloud/ionosctl/v6/internal/config"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
Expand Down Expand Up @@ -251,8 +253,9 @@ func addCommands() {
),
)

// CDN
rootCmd.AddCommand(cdn.Command())

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

const (
Expand Down
92 changes: 92 additions & 0 deletions commands/vpn/ipsec/completer/completer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package completer

import (
"context"

"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/config"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/spf13/viper"
)

// -- GATEWAYS

// GatewaysProperty returns a list of properties of all gateways matching the given filters
func GatewaysProperty[V any](f func(gateway vpn.IPSecGatewayRead) V, fs ...GatewayFilter) []V {
recs, err := Gateways(fs...)
if err != nil {
return nil
}
return functional.Map(*recs.Items, f)
}

// Gateways returns all distributions matching the given filters
func Gateways(fs ...GatewayFilter) (vpn.IPSecGatewayReadList, error) {
if url := config.GetServerUrl(); url == constants.DefaultApiURL || url == "" {
viper.Set(constants.ArgServerUrl, constants.DefaultVPNApiURL)
}

req := client.Must().VPNClient.IPSecGatewaysApi.IpsecgatewaysGet(context.Background())
for _, f := range fs {
var err error
req, err = f(req)
if err != nil {
return vpn.IPSecGatewayReadList{}, err
}
}

ls, _, err := req.Execute()
if err != nil {
return vpn.IPSecGatewayReadList{}, err
}
return ls, nil
}

type GatewayFilter func(request vpn.ApiIpsecgatewaysGetRequest) (vpn.ApiIpsecgatewaysGetRequest, error)

// TunnelsProperty returns a list of properties of all tunnels matching the given filters
func TunnelsProperty[V any](gatewayID string, f func(tunnel vpn.IPSecTunnelRead) V, fs ...TunnelFilter) []V {
recs, err := Tunnels(gatewayID, fs...)
if err != nil {
return nil
}
return functional.Map(*recs.Items, f)
}

// Tunnels returns all distributions matching the given filters
func Tunnels(gatewayID string, fs ...TunnelFilter) (vpn.IPSecTunnelReadList, error) {
if url := config.GetServerUrl(); url == constants.DefaultApiURL || url == "" {
viper.Set(constants.ArgServerUrl, constants.DefaultVPNApiURL)
}

req := client.Must().VPNClient.IPSecTunnelsApi.IpsecgatewaysTunnelsGet(context.Background(), gatewayID)
for _, f := range fs {
var err error
req, err = f(req)
if err != nil {
return vpn.IPSecTunnelReadList{}, err
}
}

ls, _, err := req.Execute()
if err != nil {
return vpn.IPSecTunnelReadList{}, err
}
return ls, nil
}

type TunnelFilter func(request vpn.ApiIpsecgatewaysTunnelsGetRequest) (vpn.ApiIpsecgatewaysTunnelsGetRequest, error)

func GatewayIDs() []string {
return GatewaysProperty(func(gateway vpn.IPSecGatewayRead) string {
return *gateway.Id + "\t" + *gateway.Properties.Name + "(" + *gateway.Properties.GatewayIP + ")"
})
}

func TunnelIDs(gatewayID string) []string {
return TunnelsProperty(gatewayID, func(p vpn.IPSecTunnelRead) string {
return *p.Id + "\t" + *p.Properties.Name + "(" + *p.Properties.RemoteHost + ")"
})
}
Loading
Loading