-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(networkpeering): add
delete
, disable
and list
commands
- Loading branch information
Showing
14 changed files
with
334 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.