Skip to content

Commit 1a4ec00

Browse files
CLOUDP-277610: [AtlasCLI] Clusters watch is broken when using atlas upgrade (#3314)
1 parent 3eafd86 commit 1a4ec00

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

internal/cli/clusters/watch.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package clusters
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
21+
"net/http"
2022

2123
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
2224
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
@@ -31,6 +33,7 @@ import (
3133
type WatchOpts struct {
3234
cli.GlobalOpts
3335
cli.WatchOpts
36+
cli.RefresherOpts
3437
name string
3538
store store.ClusterDescriber
3639
}
@@ -45,24 +48,34 @@ func (opts *WatchOpts) initStore(ctx context.Context) func() error {
4548
}
4649
}
4750

48-
func isRetryable(err error) bool {
49-
atlasErr, ok := atlasClustersPinned.AsError(err)
50-
return ok && atlasErr.GetErrorCode() == "CLUSTER_NOT_FOUND"
51-
}
51+
func (opts *WatchOpts) watcher(ctx context.Context) func() (any, bool, error) {
52+
return func() (any, bool, error) {
53+
result, err := opts.store.AtlasCluster(opts.ConfigProjectID(), opts.name)
54+
if err != nil {
55+
var atlasClustersPinnedErr *atlasClustersPinned.GenericOpenAPIError
5256

53-
func (opts *WatchOpts) watcher() (any, bool, error) {
54-
result, err := opts.store.AtlasCluster(opts.ConfigProjectID(), opts.name)
55-
if err != nil {
56-
return nil, false, err
57-
}
58-
if result.GetStateName() == "UPDATING" {
59-
opts.IsRetryableErr = isRetryable
57+
if errors.As(err, &atlasClustersPinnedErr) {
58+
if *atlasClustersPinnedErr.Model().Error == http.StatusUnauthorized {
59+
// Refresh the access token
60+
// Note: this only updates the config, so we have to re-initialize the store
61+
if err := opts.RefreshAccessToken(ctx); err != nil {
62+
return nil, false, err
63+
}
64+
65+
// Re-initialize store, refreshAccessToken only refreshes the config
66+
return nil, false, opts.initStore(ctx)()
67+
}
68+
}
69+
}
70+
if err != nil {
71+
return nil, false, err
72+
}
73+
return nil, result.GetStateName() == "IDLE", nil
6074
}
61-
return nil, result.GetStateName() == "IDLE", nil
6275
}
6376

64-
func (opts *WatchOpts) Run() error {
65-
if _, err := opts.Watch(opts.watcher); err != nil {
77+
func (opts *WatchOpts) Run(ctx context.Context) error {
78+
if _, err := opts.Watch(opts.watcher(ctx)); err != nil {
6679
return err
6780
}
6881

@@ -93,11 +106,12 @@ You can interrupt the command's polling at any time with CTRL-C.
93106
opts.ValidateProjectID,
94107
opts.initStore(cmd.Context()),
95108
opts.InitOutput(cmd.OutOrStdout(), watchTemplate),
109+
opts.InitFlow(config.Default()),
96110
)
97111
},
98-
RunE: func(_ *cobra.Command, args []string) error {
112+
RunE: func(cmd *cobra.Command, args []string) error {
99113
opts.name = args[0]
100-
return opts.Run()
114+
return opts.Run(cmd.Context())
101115
},
102116
}
103117

internal/cli/clusters/watch_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package clusters
1818

1919
import (
20+
"context"
2021
"testing"
2122

2223
"github.com/golang/mock/gomock"
@@ -44,7 +45,7 @@ func TestWatch_Run(t *testing.T) {
4445
Return(expected, nil).
4546
Times(1)
4647

47-
if err := opts.Run(); err != nil {
48+
if err := opts.Run(context.Background()); err != nil {
4849
t.Fatalf("Run() unexpected error: %v", err)
4950
}
5051
}

internal/decryption/log_record.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ func (logLine *AuditLogLine) logAdditionalAuthData() []byte {
106106
const AADByteSize = 8
107107

108108
additionalAuthData := make([]byte, AADByteSize)
109-
binary.LittleEndian.PutUint64(additionalAuthData, uint64(logLine.TS.UnixMilli())) //nolint:gosec
109+
//nolint:gosec
110+
binary.LittleEndian.PutUint64(additionalAuthData, uint64(logLine.TS.UnixMilli()))
110111
return additionalAuthData
111112
}
112113

0 commit comments

Comments
 (0)