Skip to content

Commit

Permalink
feat(networkpeering): add delete, disable and list commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta committed Mar 6, 2024
1 parent a38de77 commit 87ec62f
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 7 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Support Kubernetes cluster labels: list labels with `show` commands and manage them with `create` and `modify` commands
- Add `networkpeering` commands (`delete`, `disable`, `list`) for network peering management.

## [3.5.0] - 2024-02-29

Expand All @@ -26,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `gateway` commands (`delete`, `list`) for Network gateway management.
- Add `gateway` commands (`delete`, `list`) for network gateway management.
- In machine readable outputs of server list, add support for `--show-ip-addresses` parameter.
- Support for sub-account deletion via `account delete` command

Expand Down Expand Up @@ -58,7 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [3.2.0] - 2023-11-15

### Added
- Add `objectstorage` commands (`delete`, `list`, `show`) for Managed object storage management
- Add `objectstorage` commands (`delete`, `list`, `show`) for managed object storage management

## [3.1.0] - 2023-11-06

Expand Down
9 changes: 8 additions & 1 deletion internal/commands/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/kubernetes/nodegroup"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/loadbalancer"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/network"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/networkpeering"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/objectstorage"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/root"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/router"
Expand Down Expand Up @@ -94,6 +95,12 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
commands.BuildCommand(network.ModifyCommand(), networkCommand.Cobra(), conf)
commands.BuildCommand(network.DeleteCommand(), networkCommand.Cobra(), conf)

// Network peerings
networkPeeringCommand := commands.BuildCommand(networkpeering.BaseNetworkPeeringCommand(), rootCmd, conf)
commands.BuildCommand(networkpeering.ListCommand(), networkPeeringCommand.Cobra(), conf)
commands.BuildCommand(networkpeering.DeleteCommand(), networkPeeringCommand.Cobra(), conf)
commands.BuildCommand(networkpeering.DisableCommand(), networkPeeringCommand.Cobra(), conf)

// Routers
routerCommand := commands.BuildCommand(router.BaseRouterCommand(), rootCmd, conf)
commands.BuildCommand(router.CreateCommand(), routerCommand.Cobra(), conf)
Expand Down Expand Up @@ -191,7 +198,7 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
commands.BuildCommand(objectstorage.ShowCommand(), objectStorageCommand.Cobra(), conf)

// Network Gateway operations
gatewayCommand := commands.BuildCommand(gateway.BasegatewayCommand(), rootCmd, conf)
gatewayCommand := commands.BuildCommand(gateway.BaseGatewayCommand(), rootCmd, conf)
commands.BuildCommand(gateway.DeleteCommand(), gatewayCommand.Cobra(), conf)
commands.BuildCommand(gateway.ListCommand(), gatewayCommand.Cobra(), conf)

Expand Down
4 changes: 2 additions & 2 deletions internal/commands/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
)

// BasegatewayCommand creates the base "gateway" command
func BasegatewayCommand() commands.Command {
// BaseGatewayCommand creates the base "gateway" command
func BaseGatewayCommand() commands.Command {
return &gatewayCommand{
commands.New("gateway", "Manage gateways"),
}
Expand Down
48 changes: 48 additions & 0 deletions internal/commands/networkpeering/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package networkpeering

import (
"fmt"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)

// DeleteCommand creates the "networkpeering delete" command
func DeleteCommand() commands.Command {
return &deleteCommand{
BaseCommand: commands.New(
"delete",
"Delete a network peering",
"upctl networkpeering delete 8abc8009-4325-4b23-4321-b1232cd81231",
"upctl networkpeering delete my-network-peering",
),
}
}

type deleteCommand struct {
*commands.BaseCommand
resolver.CachingNetworkPeering
completion.NetworkPeering
}

// Execute implements commands.MultipleArgumentCommand
func (c *deleteCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
svc := exec.All()
msg := fmt.Sprintf("Deleting network peering %v", arg)
exec.PushProgressStarted(msg)

err := svc.DeleteNetworkPeering(exec.Context(), &request.DeleteNetworkPeeringRequest{
UUID: arg,
})
if err != nil {
return commands.HandleError(exec, msg, err)
}

exec.PushProgressSuccess(msg)

return output.None{}, nil
}
54 changes: 54 additions & 0 deletions internal/commands/networkpeering/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package networkpeering

import (
"testing"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/stretchr/testify/assert"
)

func TestDeleteCommand(t *testing.T) {
targetMethod := "DeleteNetworkPeering"

peering := upcloud.NetworkPeering{
Name: "test-peering",
UUID: "9cb62e7d-e95f-4eaa-9c8b-9c6f5e2a66db",
}

for _, test := range []struct {
name string
arg string
error string
req request.DeleteNetworkPeeringRequest
}{
{
name: "delete with UUID",
arg: peering.UUID,
req: request.DeleteNetworkPeeringRequest{UUID: peering.UUID},
},
} {
t.Run(test.name, func(t *testing.T) {
mService := smock.Service{}
mService.On(targetMethod, &test.req).Return(nil)

conf := config.New()
command := commands.BuildCommand(DeleteCommand(), nil, conf)

command.Cobra().SetArgs([]string{test.arg})
_, err := mockexecute.MockExecute(command, &mService, conf)

if test.error != "" {
assert.EqualError(t, err, test.error)
} else {
assert.NoError(t, err)
mService.AssertNumberOfCalls(t, targetMethod, 1)
}
})
}
}
52 changes: 52 additions & 0 deletions internal/commands/networkpeering/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package networkpeering

import (
"fmt"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)

// DisableCommand creates the "networkpeering disable" command
func DisableCommand() commands.Command {
return &disableCommand{
BaseCommand: commands.New(
"disable",
"Disable a network peering",
"upctl networkpeering disable 8abc8009-4325-4b23-4321-b1232cd81231",
"upctl networkpeering disable my-network-peering",
),
}
}

type disableCommand struct {
*commands.BaseCommand
resolver.CachingNetworkPeering
completion.NetworkPeering
}

// Execute implements commands.MultipleArgumentCommand
func (c *disableCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
svc := exec.All()
msg := fmt.Sprintf("Disabling network peering %v", arg)
exec.PushProgressStarted(msg)

peering, err := svc.ModifyNetworkPeering(exec.Context(), &request.ModifyNetworkPeeringRequest{
UUID: arg,
NetworkPeering: request.ModifyNetworkPeering{
ConfiguredStatus: upcloud.NetworkPeeringConfiguredStatusDisabled,
},
})
if err != nil {
return commands.HandleError(exec, msg, err)
}

exec.PushProgressSuccess(msg)

return output.OnlyMarshaled{Value: peering}, nil
}
58 changes: 58 additions & 0 deletions internal/commands/networkpeering/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package networkpeering

import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
)

// ListCommand creates the "networkpeering list" command
func ListCommand() commands.Command {
return &listCommand{
BaseCommand: commands.New("list", "List network peerings", "upctl networkpeering list"),
}
}

type listCommand struct {
*commands.BaseCommand
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
peerings, err := svc.GetNetworkPeerings(exec.Context())
if err != nil {
return nil, err
}

rows := []output.TableRow{}
for _, peering := range peerings {
peerNetwork := ""
if len(peering.PeerNetwork.IPNetworks) > 0 {
peerNetwork = peering.PeerNetwork.IPNetworks[0].Address
}

rows = append(rows, output.TableRow{
peering.UUID,
peering.Name,
peering.Network.IPNetworks[0].Address,
peerNetwork,
peering.State,
})
}

// For JSON and YAML output, passthrough API response
return output.MarshaledWithHumanOutput{
Value: peerings,
Output: output.Table{
Columns: []output.TableColumn{
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
{Key: "name", Header: "Name"},
{Key: "network", Header: "Network", Colour: ui.DefaultAddressColours},
{Key: "peer_network", Header: "Peer Network", Colour: ui.DefaultAddressColours},
{Key: "status", Header: "Status"},
},
Rows: rows,
},
}, nil
}
16 changes: 16 additions & 0 deletions internal/commands/networkpeering/networkpeering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package networkpeering

import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
)

// BaseNetworkPeeringCommand creates the base "networkpeering" command
func BaseNetworkPeeringCommand() commands.Command {
return &networkpeeringCommand{
commands.New("networkpeering", "Manage network peerings"),
}
}

type networkpeeringCommand struct {
*commands.BaseCommand
}
2 changes: 1 addition & 1 deletion internal/commands/objectstorage/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// BaseobjectstorageCommand creates the base "objectstorage" command
func BaseobjectstorageCommand() commands.Command {
return &objectstorageCommand{
commands.New("objectstorage", "Manage Managed object storage services"),
commands.New("objectstorage", "Manage managed object storage services"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/completion/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// Gateway implements argument completion for gateways, by uuid or name.
type Gateway struct{}

// make sure LoadBalancer implements the interface
// make sure Gateway implements the interface
var _ Provider = Gateway{}

// CompleteArgument implements completion.Provider
Expand Down
28 changes: 28 additions & 0 deletions internal/completion/networkpeering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package completion

import (
"context"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
"github.com/spf13/cobra"
)

// NetworkPeering implements argument completion for network peerings, by uuid or name.
type NetworkPeering struct{}

// make sure NetworkPeering implements the interface
var _ Provider = NetworkPeering{}

// CompleteArgument implements completion.Provider
func (s NetworkPeering) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
peerings, err := svc.GetNetworkPeerings(ctx)
if err != nil {
return None(toComplete)
}
var vals []string
for _, peering := range peerings {
vals = append(vals, peering.UUID, peering.Name)
}

return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
}
21 changes: 21 additions & 0 deletions internal/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,3 +1264,24 @@ func (m *Service) ModifyGateway(_ context.Context, r *request.ModifyGatewayReque
}
return args[0].(*upcloud.Gateway), args.Error(1)
}

func (m *Service) GetNetworkPeerings(ctx context.Context, f ...request.QueryFilter) (upcloud.NetworkPeerings, error) {
return nil, nil
}

func (m *Service) GetNetworkPeering(ctx context.Context, r *request.GetNetworkPeeringRequest) (*upcloud.NetworkPeering, error) {
return nil, nil
}

func (m *Service) CreateNetworkPeering(ctx context.Context, r *request.CreateNetworkPeeringRequest) (*upcloud.NetworkPeering, error) {
return nil, nil
}

func (m *Service) ModifyNetworkPeering(ctx context.Context, r *request.ModifyNetworkPeeringRequest) (*upcloud.NetworkPeering, error) {
return nil, nil
}

func (m *Service) DeleteNetworkPeering(ctx context.Context, r *request.DeleteNetworkPeeringRequest) error {
args := m.Called(r)
return args.Error(0)
}
Loading

0 comments on commit 87ec62f

Please sign in to comment.