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 93 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
6c2a4be
change: remove old functionality
avirtopeanu-ionos Nov 29, 2024
b0e467f
change: remove old GetServerURL workarounds
avirtopeanu-ionos Nov 29, 2024
cc8fcf4
feat: withRegionalFlags decorator
avirtopeanu-ionos Nov 29, 2024
6eebebb
feat: withCompletions option
avirtopeanu-ionos Nov 29, 2024
92a9af2
change: remove old dns completer workarounds
avirtopeanu-ionos Nov 29, 2024
298a0ca
revert: don't apply the new funcs yet
avirtopeanu-ionos Nov 29, 2024
21b7adf
add comments, remove print statements
avirtopeanu-ionos Nov 29, 2024
4749f72
change: allow users to force a location for completions
avirtopeanu-ionos Nov 29, 2024
114a153
WithRegionalFlags allow force custom location but warn
avirtopeanu-ionos Nov 29, 2024
f2c8dfe
replace: dns zone completions
avirtopeanu-ionos Dec 4, 2024
20aa7a2
replace: dns record completions
avirtopeanu-ionos Dec 4, 2024
72ef089
clouadpi ipblock completion in a dns command
avirtopeanu-ionos Dec 4, 2024
f7152ef
replace: dnssec completions
avirtopeanu-ionos Dec 4, 2024
71d40fd
fix: use regional API flags for Logging API
avirtopeanu-ionos Dec 4, 2024
6cac438
fix: stop testing credentials if the URL is overriden
avirtopeanu-ionos Dec 4, 2024
e7df74e
replace: logging pipeline completions
avirtopeanu-ionos Dec 4, 2024
58e0ed8
replace: logging logs completions
avirtopeanu-ionos Dec 4, 2024
2ca6844
make cdn a regional command
avirtopeanu-ionos Dec 4, 2024
86b89f0
feat: cdn completions
avirtopeanu-ionos Dec 4, 2024
98a706a
better constant names
avirtopeanu-ionos Dec 4, 2024
9a91d66
replace: MariaDB add regional support
avirtopeanu-ionos Dec 4, 2024
c444bd7
goimports
avirtopeanu-ionos Dec 4, 2024
92d8614
test: fix config tests
avirtopeanu-ionos Dec 4, 2024
25439df
feat: structure
avirtopeanu-ionos Oct 24, 2024
054578c
add client
avirtopeanu-ionos Oct 28, 2024
db01811
feat: improve wireguard gateway create
avirtopeanu-ionos Oct 29, 2024
dadf225
fix helptext
avirtopeanu-ionos Oct 29, 2024
bbad69a
change default url
avirtopeanu-ionos Oct 29, 2024
be90f35
rm: ipsec. to be added separate pr
avirtopeanu-ionos Oct 29, 2024
79faede
peer create
avirtopeanu-ionos Oct 29, 2024
3c24600
jsonpaths
avirtopeanu-ionos Oct 30, 2024
9cff6a4
vpn wireguard peer
avirtopeanu-ionos Oct 30, 2024
6d08286
cols
avirtopeanu-ionos Oct 30, 2024
bcd91e4
fix: forgot rename also required check
avirtopeanu-ionos Oct 30, 2024
d74758f
fix: check also cidr
avirtopeanu-ionos Oct 30, 2024
5bb8782
tab complete ipblock
avirtopeanu-ionos Oct 30, 2024
5fd74c3
fix annoying bug
avirtopeanu-ionos Oct 30, 2024
517d1e5
gateway list
avirtopeanu-ionos Oct 31, 2024
53b9627
delete
avirtopeanu-ionos Nov 4, 2024
a3d480e
fix completer
avirtopeanu-ionos Nov 4, 2024
aa53f63
read key from path
avirtopeanu-ionos Nov 4, 2024
3bcf5dc
private key path
avirtopeanu-ionos Nov 4, 2024
725c5a2
conv2table func
avirtopeanu-ionos Nov 5, 2024
d9b0fbc
few fixes
avirtopeanu-ionos Nov 5, 2024
6e2978d
peer list
avirtopeanu-ionos Nov 5, 2024
0d3bd9b
feat: wireguard gateway update
avirtopeanu-ionos Nov 6, 2024
d84ceb6
get
avirtopeanu-ionos Nov 6, 2024
1e84a27
peer delete
avirtopeanu-ionos Nov 6, 2024
565ddb2
peer delete
avirtopeanu-ionos Nov 6, 2024
5b52eaa
peer update
avirtopeanu-ionos Nov 6, 2024
baec901
peer get
avirtopeanu-ionos Nov 6, 2024
b7c725d
fix: ID not IP
avirtopeanu-ionos Nov 6, 2024
120a750
docs
avirtopeanu-ionos Nov 7, 2024
bb14a55
fix extra helptext
avirtopeanu-ionos Nov 7, 2024
dfbb934
fix broken completions
avirtopeanu-ionos Nov 7, 2024
60e2bda
bats test
avirtopeanu-ionos Nov 11, 2024
f00bb93
location
avirtopeanu-ionos Nov 11, 2024
141320d
location
avirtopeanu-ionos Nov 11, 2024
d6a2c78
location
avirtopeanu-ionos Nov 12, 2024
f891f21
initial ipsec
avirtopeanu-ionos Nov 12, 2024
b0be0d8
temp bats
avirtopeanu-ionos Nov 12, 2024
e1b2e53
ipsec
avirtopeanu-ionos Nov 13, 2024
2cf7829
fix update, jsonproperties
avirtopeanu-ionos Nov 13, 2024
09f4a00
json properties ipsec tunnel
avirtopeanu-ionos Nov 14, 2024
54437d6
add ipsec to vpn
avirtopeanu-ionos Nov 14, 2024
84a63a8
ipsec fixes
avirtopeanu-ionos Nov 14, 2024
2425c0c
better completions
avirtopeanu-ionos Nov 14, 2024
efd88e3
add infos to completions
avirtopeanu-ionos Nov 18, 2024
d424b0e
fix nullptr ref
avirtopeanu-ionos Nov 18, 2024
a699e9d
fix update making certain fields nil
avirtopeanu-ionos Nov 18, 2024
8e98f70
fix flags ipsec create gateway
avirtopeanu-ionos Nov 18, 2024
da09fae
move completer
avirtopeanu-ionos Nov 18, 2024
81e4466
ipsec completer
avirtopeanu-ionos Nov 18, 2024
a403a3f
show DatacenterID
avirtopeanu-ionos Nov 18, 2024
abc1409
temp test
avirtopeanu-ionos Nov 19, 2024
4015d76
fix: Private key needs to be provided again on updates
avirtopeanu-ionos Nov 19, 2024
9f56fef
cleanup
avirtopeanu-ionos Nov 19, 2024
0f5014b
remove metadata json example
avirtopeanu-ionos Nov 20, 2024
be9fd1a
add ipsec test
avirtopeanu-ionos Nov 20, 2024
ce4aa3c
fix: json properties
avirtopeanu-ionos Nov 20, 2024
86056c0
fix json flag
avirtopeanu-ionos Nov 21, 2024
d4748fb
more ipsec tests
avirtopeanu-ionos Nov 21, 2024
69f39cc
fix: add missing update tunnel flags
avirtopeanu-ionos Nov 21, 2024
317e3df
fix: required flags update
avirtopeanu-ionos Nov 21, 2024
f9156e9
fix tunnel completion
avirtopeanu-ionos Nov 21, 2024
4175380
fix tunnel viper bug
avirtopeanu-ionos Nov 21, 2024
349360f
fix: merge conflicts
avirtopeanu-ionos Nov 21, 2024
16b92b2
make docs
avirtopeanu-ionos Nov 21, 2024
0aa8a7a
fix examples
avirtopeanu-ionos Nov 21, 2024
c9ba112
change: use new funcs for location based
avirtopeanu-ionos Dec 6, 2024
0af8221
replace: wireguard completions with location based completions
avirtopeanu-ionos Dec 6, 2024
43f085b
replace ipsec completions to work with regional urls
avirtopeanu-ionos Dec 6, 2024
ec78470
fix: merge conflicts
avirtopeanu-ionos Jan 9, 2025
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/distribution/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"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"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
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(filters ...func(datacenter ionoscloud.Datacenter) bool) []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
}
6 changes: 2 additions & 4 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

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

certificates "github.com/ionos-cloud/ionosctl/v6/commands/certmanager"
"github.com/ionos-cloud/ionosctl/v6/commands/cfg"
Expand All @@ -18,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 @@ -225,11 +225,9 @@ func addCommands() {
rootCmd.AddCommand(dns.Root())
rootCmd.AddCommand(logging_service.Root())

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

// Kafka
rootCmd.AddCommand(kafka.Command())
rootCmd.AddCommand(vpn.Root())
}

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

import (
"context"

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

// -- 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) {
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) {
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