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

refactor: vpn to bundle #484

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions commands/vpn/ipsec/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

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

// -- GATEWAYS
Expand All @@ -16,7 +16,7 @@ func GatewaysProperty[V any](f func(gateway vpn.IPSecGatewayRead) V, fs ...Gatew
if err != nil {
return nil
}
return functional.Map(*recs.Items, f)
return functional.Map(recs.Items, f)
}

// Gateways returns all distributions matching the given filters
Expand Down Expand Up @@ -45,7 +45,7 @@ func TunnelsProperty[V any](gatewayID string, f func(tunnel vpn.IPSecTunnelRead)
if err != nil {
return nil
}
return functional.Map(*recs.Items, f)
return functional.Map(recs.Items, f)
}

// Tunnels returns all distributions matching the given filters
Expand All @@ -70,12 +70,12 @@ type TunnelFilter func(request vpn.ApiIpsecgatewaysTunnelsGetRequest) (vpn.ApiIp

func GatewayIDs() []string {
return GatewaysProperty(func(gateway vpn.IPSecGatewayRead) string {
return *gateway.Id + "\t" + *gateway.Properties.Name + "(" + *gateway.Properties.GatewayIP + ")"
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 + ")"
return p.Id + "\t" + p.Properties.Name + "(" + p.Properties.RemoteHost + ")"
})
}
20 changes: 10 additions & 10 deletions commands/vpn/ipsec/gateway/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
"github.com/ionos-cloud/ionosctl/v6/pkg/pointer"

vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand All @@ -39,28 +39,28 @@ func Create() *core.Command {
)
},
CmdRun: func(c *core.CommandConfig) error {
input := &vpn.IPSecGateway{}
input := vpn.IPSecGateway{}

if fn := core.GetFlagName(c.NS, constants.FlagName); viper.IsSet(fn) {
input.Name = pointer.From(viper.GetString(fn))
input.Name = viper.GetString(fn)
}
if fn := core.GetFlagName(c.NS, constants.FlagDescription); viper.IsSet(fn) {
input.Description = pointer.From(viper.GetString(fn))
}
if fn := core.GetFlagName(c.NS, constants.FlagIp); viper.IsSet(fn) {
input.GatewayIP = pointer.From(viper.GetString(fn))
input.GatewayIP = viper.GetString(fn)
}

input.Connections = pointer.From(make([]vpn.Connection, 1))
input.Connections = make([]vpn.Connection, 1)
if fn := core.GetFlagName(c.NS, constants.FlagDatacenterId); viper.IsSet(fn) {
(*input.Connections)[0].DatacenterId = pointer.From(viper.GetString(fn))
input.Connections[0].DatacenterId = viper.GetString(fn)
}
if fn := core.GetFlagName(c.NS, constants.FlagLanId); viper.IsSet(fn) {
(*input.Connections)[0].LanId = pointer.From(viper.GetString(fn))
input.Connections[0].LanId = viper.GetString(fn)
}

if fn := core.GetFlagName(c.NS, constants.FlagGatewayIP); viper.IsSet(fn) {
input.GatewayIP = pointer.From(viper.GetString(fn))
input.GatewayIP = viper.GetString(fn)
}

// Note: VPN Gateway handles IPv4 and IPv6 addresses separately for both InterfaceIP and Connections.IP
Expand All @@ -76,9 +76,9 @@ func Create() *core.Command {
if fn := core.GetFlagName(c.NS, constants.FlagConnectionIP); viper.IsSet(fn) {
ip := viper.GetString(fn)
if isIPv4(ip) {
(*input.Connections)[0].Ipv4CIDR = pointer.From(ip)
input.Connections[0].Ipv4CIDR = ip
} else {
(*input.Connections)[0].Ipv6CIDR = pointer.From(ip)
input.Connections[0].Ipv6CIDR = pointer.From(ip)
}
}

Expand Down
15 changes: 9 additions & 6 deletions commands/vpn/ipsec/gateway/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/pkg/confirm"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2"
"github.com/spf13/viper"
)

Expand All @@ -38,7 +38,8 @@ func Delete() *core.Command {
if err != nil {
return fmt.Errorf("failed getting gateway by id %s: %w", id, err)
}
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf("Are you sure you want to delete gateway %s (desc: '%s')", *g.Properties.Name, *g.Properties.Description),
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf(
"Are you sure you want to delete gateway %s (desc: '%s')", g.Properties.Name, *g.Properties.Description),
viper.GetBool(constants.ArgForce))
if !yes {
return fmt.Errorf(confirm.UserDenied)
Expand Down Expand Up @@ -71,13 +72,15 @@ func deleteAll(c *core.CommandConfig) error {
return err
}

err = functional.ApplyAndAggregateErrors(*xs.GetItems(), func(g vpn.IPSecGatewayRead) error {
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf("Are you sure you want to delete gateway %s at %s", *g.Properties.Name, *g.Properties.GatewayIP),
err = functional.ApplyAndAggregateErrors(xs.GetItems(), func(g vpn.IPSecGatewayRead) error {
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf(
"Are you sure you want to delete gateway %s at %s",
g.Properties.Name, g.Properties.GatewayIP),
viper.GetBool(constants.ArgForce))
if yes {
_, delErr := client.Must().VPNClient.IPSecGatewaysApi.IpsecgatewaysDelete(context.Background(), *g.Id).Execute()
_, delErr := client.Must().VPNClient.IPSecGatewaysApi.IpsecgatewaysDelete(context.Background(), g.Id).Execute()
if delErr != nil {
return fmt.Errorf("failed deleting %s (name: %s): %w", *g.Id, *g.Properties.Name, delErr)
return fmt.Errorf("failed deleting %s (name: %s): %w", g.Id, g.Properties.Name, delErr)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion commands/vpn/ipsec/gateway/list.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/resource2table"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2"
"github.com/spf13/viper"
)

Expand Down
24 changes: 12 additions & 12 deletions commands/vpn/ipsec/gateway/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/pkg/pointer"

// "github.com/ionos-cloud/ionosctl/v6/pkg/uuidgen"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand All @@ -42,31 +42,31 @@ func Update() *core.Command {
input := original.Properties

if fn := core.GetFlagName(c.NS, constants.FlagName); viper.IsSet(fn) {
input.Name = pointer.From(viper.GetString(fn))
input.Name = viper.GetString(fn)
}
if fn := core.GetFlagName(c.NS, constants.FlagDescription); viper.IsSet(fn) {
input.Description = pointer.From(viper.GetString(fn))
}
if fn := core.GetFlagName(c.NS, constants.FlagIp); viper.IsSet(fn) {
input.GatewayIP = pointer.From(viper.GetString(fn))
input.GatewayIP = viper.GetString(fn)
}

if fn := core.GetFlagName(c.NS, constants.FlagDatacenterId); viper.IsSet(fn) {
// initialize connections if not already set
if input.Connections == nil {
input.Connections = pointer.From(make([]vpn.Connection, 1))
input.Connections = make([]vpn.Connection, 1)
}
(*input.Connections)[0].DatacenterId = pointer.From(viper.GetString(fn))
input.Connections[0].DatacenterId = viper.GetString(fn)
}
if fn := core.GetFlagName(c.NS, constants.FlagLanId); viper.IsSet(fn) {
if input.Connections == nil {
input.Connections = pointer.From(make([]vpn.Connection, 1))
input.Connections = make([]vpn.Connection, 1)
}
(*input.Connections)[0].LanId = pointer.From(viper.GetString(fn))
input.Connections[0].LanId = viper.GetString(fn)
}

if fn := core.GetFlagName(c.NS, constants.FlagGatewayIP); viper.IsSet(fn) {
input.GatewayIP = pointer.From(viper.GetString(fn))
input.GatewayIP = viper.GetString(fn)
}

// Note: VPN Gateway handles IPv4 and IPv6 addresses separately for both InterfaceIP and Connections.IP
Expand All @@ -81,19 +81,19 @@ func Update() *core.Command {

if fn := core.GetFlagName(c.NS, constants.FlagConnectionIP); viper.IsSet(fn) {
if input.Connections == nil {
input.Connections = pointer.From(make([]vpn.Connection, 1))
input.Connections = make([]vpn.Connection, 1)
}
ip := viper.GetString(fn)
if isIPv4(ip) {
(*input.Connections)[0].Ipv4CIDR = pointer.From(ip)
input.Connections[0].Ipv4CIDR = ip
} else {
(*input.Connections)[0].Ipv6CIDR = pointer.From(ip)
input.Connections[0].Ipv6CIDR = pointer.From(ip)
}
}

createdGateway, _, err := client.Must().VPNClient.IPSecGatewaysApi.
IpsecgatewaysPut(context.Background(), id).
IPSecGatewayEnsure(vpn.IPSecGatewayEnsure{Id: &id, Properties: input}).Execute()
IPSecGatewayEnsure(vpn.IPSecGatewayEnsure{Id: id, Properties: input}).Execute()
if err != nil {
return fmt.Errorf("failed updating gateway: %w", err)
}
Expand Down
22 changes: 11 additions & 11 deletions commands/vpn/ipsec/tunnel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
"github.com/ionos-cloud/ionosctl/v6/pkg/pointer"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -105,31 +105,31 @@ func createFromJSON(c *core.CommandConfig, propertiesFromJson vpn.IPSecTunnelCre
}

func createFromProperties(c *core.CommandConfig) error {
input := &vpn.IPSecTunnel{}
input := vpn.IPSecTunnel{}

if fn := core.GetFlagName(c.NS, constants.FlagName); viper.IsSet(fn) {
input.Name = pointer.From(viper.GetString(fn))
input.Name = viper.GetString(fn)
}

if fn := core.GetFlagName(c.NS, constants.FlagDescription); viper.IsSet(fn) {
input.Description = pointer.From(viper.GetString(fn))
}

if fn := core.GetFlagName(c.NS, constants.FlagHost); viper.IsSet(fn) {
input.RemoteHost = pointer.From(viper.GetString(fn))
input.RemoteHost = viper.GetString(fn)
}

if fn := core.GetFlagName(c.NS, constants.FlagAuthMethod); viper.IsSet(fn) {
input.Auth = &vpn.IPSecTunnelAuth{}
input.Auth.Method = pointer.From(viper.GetString(fn))
input.Auth = vpn.IPSecTunnelAuth{}
input.Auth.Method = viper.GetString(fn)
}

if fn := core.GetFlagName(c.NS, constants.FlagPSKKey); viper.IsSet(fn) {
input.Auth.Psk = &vpn.IPSecPSK{}
input.Auth.Psk.Key = pointer.From(viper.GetString(fn))
input.Auth.Psk.Key = viper.GetString(fn)
}

input.Ike = &vpn.IKEEncryption{}
input.Ike = vpn.IKEEncryption{}
if fn := core.GetFlagName(c.NS, constants.FlagIKEDiffieHellmanGroup); viper.IsSet(fn) {
input.Ike.DiffieHellmanGroup = pointer.From(viper.GetString(fn))
}
Expand All @@ -143,7 +143,7 @@ func createFromProperties(c *core.CommandConfig) error {
input.Ike.Lifetime = pointer.From(int32(viper.GetInt(fn)))
}

input.Esp = &vpn.ESPEncryption{}
input.Esp = vpn.ESPEncryption{}
if fn := core.GetFlagName(c.NS, constants.FlagESPDiffieHellmanGroup); viper.IsSet(fn) {
input.Esp.DiffieHellmanGroup = pointer.From(viper.GetString(fn))
}
Expand All @@ -158,10 +158,10 @@ func createFromProperties(c *core.CommandConfig) error {
}

if fn := core.GetFlagName(c.NS, constants.FlagCloudNetworkCIDRs); viper.IsSet(fn) {
input.CloudNetworkCIDRs = pointer.From(viper.GetStringSlice(fn))
input.CloudNetworkCIDRs = viper.GetStringSlice(fn)
}
if fn := core.GetFlagName(c.NS, constants.FlagPeerNetworkCIDRs); viper.IsSet(fn) {
input.PeerNetworkCIDRs = pointer.From(viper.GetStringSlice(fn))
input.PeerNetworkCIDRs = viper.GetStringSlice(fn)
}
tunnel, _, err := client.Must().VPNClient.IPSecTunnelsApi.
IpsecgatewaysTunnelsPost(context.Background(), viper.GetString(core.GetFlagName(c.NS, constants.FlagGatewayID))).
Expand Down
14 changes: 8 additions & 6 deletions commands/vpn/ipsec/tunnel/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/pkg/confirm"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func Delete() *core.Command {
return fmt.Errorf("failed getting tunnel by id %s: %w", id, err)
}
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf("Are you sure you want to delete tunnel %s"+
" (host: '%s')", *p.Properties.Name, *p.Properties.RemoteHost),
" (host: '%s')", p.Properties.Name, p.Properties.RemoteHost),
viper.GetBool(constants.ArgForce))
if !yes {
return fmt.Errorf(confirm.UserDenied)
Expand Down Expand Up @@ -83,13 +83,15 @@ func deleteAll(c *core.CommandConfig) error {
return err
}

err = functional.ApplyAndAggregateErrors(*xs.GetItems(), func(p vpn.IPSecTunnelRead) error {
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf("Are you sure you want to delete tunnel %s at %s", *p.Properties.Name, *p.Properties.RemoteHost),
err = functional.ApplyAndAggregateErrors(xs.GetItems(), func(p vpn.IPSecTunnelRead) error {
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf(
"Are you sure you want to delete tunnel %s at %s",
p.Properties.Name, p.Properties.RemoteHost),
viper.GetBool(constants.ArgForce))
if yes {
_, delErr := client.Must().VPNClient.IPSecGatewaysApi.IpsecgatewaysDelete(context.Background(), *p.Id).Execute()
_, delErr := client.Must().VPNClient.IPSecGatewaysApi.IpsecgatewaysDelete(context.Background(), p.Id).Execute()
if delErr != nil {
return fmt.Errorf("failed deleting %s (name: %s): %w", *p.Id, *p.Properties.Name, delErr)
return fmt.Errorf("failed deleting %s (name: %s): %w", p.Id, p.Properties.Name, delErr)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion commands/vpn/ipsec/tunnel/list.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/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
vpn "github.com/ionos-cloud/sdk-go-vpn"
"github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2"
"github.com/spf13/viper"
)

Expand Down
Loading
Loading