Skip to content

Commit 69dc4c9

Browse files
authored
feat(networkpeering): add delete, disable and list commands (#291)
1 parent a38de77 commit 69dc4c9

15 files changed

Lines changed: 360 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

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

1415
## [3.5.0] - 2024-02-29
1516

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

2728
### Added
2829

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

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

6061
### Added
61-
- Add `objectstorage` commands (`delete`, `list`, `show`) for Managed object storage management
62+
- Add `objectstorage` commands (`delete`, `list`, `show`) for managed object storage management
6263

6364
## [3.1.0] - 2023-11-06
6465

internal/commands/all/all.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/kubernetes/nodegroup"
1515
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/loadbalancer"
1616
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/network"
17+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/networkpeering"
1718
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/objectstorage"
1819
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/root"
1920
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/router"
@@ -94,6 +95,12 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
9495
commands.BuildCommand(network.ModifyCommand(), networkCommand.Cobra(), conf)
9596
commands.BuildCommand(network.DeleteCommand(), networkCommand.Cobra(), conf)
9697

98+
// Network peerings
99+
networkPeeringCommand := commands.BuildCommand(networkpeering.BaseNetworkPeeringCommand(), rootCmd, conf)
100+
commands.BuildCommand(networkpeering.ListCommand(), networkPeeringCommand.Cobra(), conf)
101+
commands.BuildCommand(networkpeering.DeleteCommand(), networkPeeringCommand.Cobra(), conf)
102+
commands.BuildCommand(networkpeering.DisableCommand(), networkPeeringCommand.Cobra(), conf)
103+
97104
// Routers
98105
routerCommand := commands.BuildCommand(router.BaseRouterCommand(), rootCmd, conf)
99106
commands.BuildCommand(router.CreateCommand(), routerCommand.Cobra(), conf)
@@ -191,7 +198,7 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
191198
commands.BuildCommand(objectstorage.ShowCommand(), objectStorageCommand.Cobra(), conf)
192199

193200
// Network Gateway operations
194-
gatewayCommand := commands.BuildCommand(gateway.BasegatewayCommand(), rootCmd, conf)
201+
gatewayCommand := commands.BuildCommand(gateway.BaseGatewayCommand(), rootCmd, conf)
195202
commands.BuildCommand(gateway.DeleteCommand(), gatewayCommand.Cobra(), conf)
196203
commands.BuildCommand(gateway.ListCommand(), gatewayCommand.Cobra(), conf)
197204

internal/commands/gateway/gateway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
55
)
66

7-
// BasegatewayCommand creates the base "gateway" command
8-
func BasegatewayCommand() commands.Command {
7+
// BaseGatewayCommand creates the base "gateway" command
8+
func BaseGatewayCommand() commands.Command {
99
return &gatewayCommand{
1010
commands.New("gateway", "Manage gateways"),
1111
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package networkpeering
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
8+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
12+
)
13+
14+
// DeleteCommand creates the "networkpeering delete" command
15+
func DeleteCommand() commands.Command {
16+
return &deleteCommand{
17+
BaseCommand: commands.New(
18+
"delete",
19+
"Delete a network peering",
20+
"upctl networkpeering delete 8abc8009-4325-4b23-4321-b1232cd81231",
21+
"upctl networkpeering delete my-network-peering",
22+
),
23+
}
24+
}
25+
26+
type deleteCommand struct {
27+
*commands.BaseCommand
28+
resolver.CachingNetworkPeering
29+
completion.NetworkPeering
30+
}
31+
32+
// Execute implements commands.MultipleArgumentCommand
33+
func (c *deleteCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
34+
svc := exec.All()
35+
msg := fmt.Sprintf("Deleting network peering %v", arg)
36+
exec.PushProgressStarted(msg)
37+
38+
err := svc.DeleteNetworkPeering(exec.Context(), &request.DeleteNetworkPeeringRequest{
39+
UUID: arg,
40+
})
41+
if err != nil {
42+
return commands.HandleError(exec, msg, err)
43+
}
44+
45+
exec.PushProgressSuccess(msg)
46+
47+
return output.None{}, nil
48+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package networkpeering
2+
3+
import (
4+
"testing"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
8+
smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock"
9+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
12+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestDeleteCommand(t *testing.T) {
17+
targetMethod := "DeleteNetworkPeering"
18+
19+
peering := upcloud.NetworkPeering{
20+
Name: "test-peering",
21+
UUID: "9cb62e7d-e95f-4eaa-9c8b-9c6f5e2a66db",
22+
}
23+
24+
for _, test := range []struct {
25+
name string
26+
arg string
27+
error string
28+
req request.DeleteNetworkPeeringRequest
29+
}{
30+
{
31+
name: "delete with UUID",
32+
arg: peering.UUID,
33+
req: request.DeleteNetworkPeeringRequest{UUID: peering.UUID},
34+
},
35+
} {
36+
t.Run(test.name, func(t *testing.T) {
37+
mService := smock.Service{}
38+
mService.On(targetMethod, &test.req).Return(nil)
39+
40+
conf := config.New()
41+
command := commands.BuildCommand(DeleteCommand(), nil, conf)
42+
43+
command.Cobra().SetArgs([]string{test.arg})
44+
_, err := mockexecute.MockExecute(command, &mService, conf)
45+
46+
if test.error != "" {
47+
assert.EqualError(t, err, test.error)
48+
} else {
49+
assert.NoError(t, err)
50+
mService.AssertNumberOfCalls(t, targetMethod, 1)
51+
}
52+
})
53+
}
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package networkpeering
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
8+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
12+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
13+
)
14+
15+
// DisableCommand creates the "networkpeering disable" command
16+
func DisableCommand() commands.Command {
17+
return &disableCommand{
18+
BaseCommand: commands.New(
19+
"disable",
20+
"Disable a network peering",
21+
"upctl networkpeering disable 8abc8009-4325-4b23-4321-b1232cd81231",
22+
"upctl networkpeering disable my-network-peering",
23+
),
24+
}
25+
}
26+
27+
type disableCommand struct {
28+
*commands.BaseCommand
29+
resolver.CachingNetworkPeering
30+
completion.NetworkPeering
31+
}
32+
33+
// Execute implements commands.MultipleArgumentCommand
34+
func (c *disableCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
35+
svc := exec.All()
36+
msg := fmt.Sprintf("Disabling network peering %v", arg)
37+
exec.PushProgressStarted(msg)
38+
39+
peering, err := svc.ModifyNetworkPeering(exec.Context(), &request.ModifyNetworkPeeringRequest{
40+
UUID: arg,
41+
NetworkPeering: request.ModifyNetworkPeering{
42+
ConfiguredStatus: upcloud.NetworkPeeringConfiguredStatusDisabled,
43+
},
44+
})
45+
if err != nil {
46+
return commands.HandleError(exec, msg, err)
47+
}
48+
49+
exec.PushProgressSuccess(msg)
50+
51+
return output.OnlyMarshaled{Value: peering}, nil
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package networkpeering
2+
3+
import (
4+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
5+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
6+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
7+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
8+
)
9+
10+
// ListCommand creates the "networkpeering list" command
11+
func ListCommand() commands.Command {
12+
return &listCommand{
13+
BaseCommand: commands.New("list", "List network peerings", "upctl networkpeering list"),
14+
}
15+
}
16+
17+
type listCommand struct {
18+
*commands.BaseCommand
19+
}
20+
21+
// ExecuteWithoutArguments implements commands.NoArgumentCommand
22+
func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
23+
svc := exec.All()
24+
peerings, err := svc.GetNetworkPeerings(exec.Context())
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
rows := []output.TableRow{}
30+
for _, peering := range peerings {
31+
peerNetwork := ""
32+
if len(peering.PeerNetwork.IPNetworks) > 0 {
33+
peerNetwork = peering.PeerNetwork.IPNetworks[0].Address
34+
}
35+
36+
rows = append(rows, output.TableRow{
37+
peering.UUID,
38+
peering.Name,
39+
peering.Network.IPNetworks[0].Address,
40+
peerNetwork,
41+
peering.State,
42+
})
43+
}
44+
45+
// For JSON and YAML output, passthrough API response
46+
return output.MarshaledWithHumanOutput{
47+
Value: peerings,
48+
Output: output.Table{
49+
Columns: []output.TableColumn{
50+
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
51+
{Key: "name", Header: "Name"},
52+
{Key: "network", Header: "Network", Colour: ui.DefaultAddressColours},
53+
{Key: "peer_network", Header: "Peer Network", Colour: ui.DefaultAddressColours},
54+
{Key: "status", Header: "Status", Format: format.NetworkPeeringState},
55+
},
56+
Rows: rows,
57+
},
58+
}, nil
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package networkpeering
2+
3+
import (
4+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
5+
)
6+
7+
// BaseNetworkPeeringCommand creates the base "networkpeering" command
8+
func BaseNetworkPeeringCommand() commands.Command {
9+
return &networkpeeringCommand{
10+
commands.New("networkpeering", "Manage network peerings"),
11+
}
12+
}
13+
14+
type networkpeeringCommand struct {
15+
*commands.BaseCommand
16+
}

internal/commands/objectstorage/objectstorage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
// BaseobjectstorageCommand creates the base "objectstorage" command
88
func BaseobjectstorageCommand() commands.Command {
99
return &objectstorageCommand{
10-
commands.New("objectstorage", "Manage Managed object storage services"),
10+
commands.New("objectstorage", "Manage managed object storage services"),
1111
}
1212
}
1313

internal/completion/gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// Gateway implements argument completion for gateways, by uuid or name.
1111
type Gateway struct{}
1212

13-
// make sure LoadBalancer implements the interface
13+
// make sure Gateway implements the interface
1414
var _ Provider = Gateway{}
1515

1616
// CompleteArgument implements completion.Provider

0 commit comments

Comments
 (0)