Skip to content

Commit 84eac4d

Browse files
authored
feat(networkpeering): add --wait flag to disable command (#295)
1 parent 69dc4c9 commit 84eac4d

5 files changed

Lines changed: 54 additions & 4 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/UpCloudLtd/progress v1.0.2
7-
github.com/UpCloudLtd/upcloud-go-api/v8 v8.0.0
7+
github.com/UpCloudLtd/upcloud-go-api/v8 v8.2.0
88
github.com/adrg/xdg v0.3.2
99
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
1010
github.com/gemalto/flume v0.12.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
1717
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
1818
github.com/UpCloudLtd/progress v1.0.2 h1:CTr1bBuFuXop9TEhR1PakbUMPTlUVL7Bgae9JgqXwPg=
1919
github.com/UpCloudLtd/progress v1.0.2/go.mod h1:iGxOnb9HvHW0yrLGUjHr0lxHhn7TehgWwh7a8NqK6iQ=
20-
github.com/UpCloudLtd/upcloud-go-api/v8 v8.0.0 h1:SqSNedBTgVO7GYZb2ixFradeEKKy5NfrWukQ/SMW0V4=
21-
github.com/UpCloudLtd/upcloud-go-api/v8 v8.0.0/go.mod h1:pMuPcIQ5VUC65UETsaEPgHiDymxwX/3xYm4vlC4E9VI=
20+
github.com/UpCloudLtd/upcloud-go-api/v8 v8.2.0 h1:+/vej9U9eSOUFt1MRRtB+GXEXWtLacyfWn9KmHCnLkw=
21+
github.com/UpCloudLtd/upcloud-go-api/v8 v8.2.0/go.mod h1:pMuPcIQ5VUC65UETsaEPgHiDymxwX/3xYm4vlC4E9VI=
2222
github.com/adrg/xdg v0.3.2 h1:GUSGQ5pHdev83AYhDSS1A/CX+0JIsxbiWtow2DSA+RU=
2323
github.com/adrg/xdg v0.3.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
2424
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

internal/commands/networkpeering/disable.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55

66
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
77
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
8+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
89
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
910
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
11+
"github.com/spf13/pflag"
1012

1113
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1214
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
@@ -28,6 +30,15 @@ type disableCommand struct {
2830
*commands.BaseCommand
2931
resolver.CachingNetworkPeering
3032
completion.NetworkPeering
33+
34+
wait config.OptionalBoolean
35+
}
36+
37+
// InitCommand implements Command.InitCommand
38+
func (c *disableCommand) InitCommand() {
39+
flags := &pflag.FlagSet{}
40+
config.AddToggleFlag(flags, &c.wait, "wait", false, "Wait for network peering to be in disabled state before returning.")
41+
c.AddFlags(flags)
3142
}
3243

3344
// Execute implements commands.MultipleArgumentCommand
@@ -46,7 +57,11 @@ func (c *disableCommand) Execute(exec commands.Executor, arg string) (output.Out
4657
return commands.HandleError(exec, msg, err)
4758
}
4859

49-
exec.PushProgressSuccess(msg)
60+
if c.wait.Value() {
61+
waitForNetworkPeeringState(arg, upcloud.NetworkPeeringStateDisabled, exec, msg)
62+
} else {
63+
exec.PushProgressSuccess(msg)
64+
}
5065

5166
return output.OnlyMarshaled{Value: peering}, nil
5267
}

internal/commands/networkpeering/networkpeering.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package networkpeering
22

33
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/UpCloudLtd/progress/messages"
49
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
10+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
11+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
512
)
613

714
// BaseNetworkPeeringCommand creates the base "networkpeering" command
@@ -14,3 +21,27 @@ func BaseNetworkPeeringCommand() commands.Command {
1421
type networkpeeringCommand struct {
1522
*commands.BaseCommand
1623
}
24+
25+
// waitForNetworkPeeringState waits for network peering to reach given state and updates progress message with key matching given msg. Finally, progress message is updated back to given msg and either done state or timeout warning.
26+
func waitForNetworkPeeringState(uuid string, state upcloud.NetworkPeeringState, exec commands.Executor, msg string) {
27+
exec.PushProgressUpdateMessage(msg, fmt.Sprintf("Waiting for network peering %s to be in %s state", uuid, state))
28+
29+
ctx, cancel := context.WithTimeout(exec.Context(), 15*time.Minute)
30+
defer cancel()
31+
32+
if _, err := exec.All().WaitForNetworkPeeringState(ctx, &request.WaitForNetworkPeeringStateRequest{
33+
UUID: uuid,
34+
DesiredState: state,
35+
}); err != nil {
36+
exec.PushProgressUpdate(messages.Update{
37+
Key: msg,
38+
Message: msg,
39+
Status: messages.MessageStatusWarning,
40+
Details: "Error: " + err.Error(),
41+
})
42+
return
43+
}
44+
45+
exec.PushProgressUpdateMessage(msg, msg)
46+
exec.PushProgressSuccess(msg)
47+
}

internal/mock/mock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,3 +1285,7 @@ func (m *Service) DeleteNetworkPeering(ctx context.Context, r *request.DeleteNet
12851285
args := m.Called(r)
12861286
return args.Error(0)
12871287
}
1288+
1289+
func (m *Service) WaitForNetworkPeeringState(ctx context.Context, r *request.WaitForNetworkPeeringStateRequest) (*upcloud.NetworkPeering, error) {
1290+
return nil, nil
1291+
}

0 commit comments

Comments
 (0)