Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions temporalcloudcli/commands.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,7 @@ type CloudNamespaceApplyCommand struct {
AsyncOperationId string
Idempotent bool
Async bool
PollInterval cliext.FlagDuration
ResourceVersion string
}

Expand All @@ -2065,6 +2066,8 @@ func NewCloudNamespaceApplyCommand(cctx *CommandContext, parent *CloudNamespaceC
s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.")
s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if the namespace already matches the specification. Without this flag, the command errors when no changes are needed.")
s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.")
s.PollInterval = cliext.MustParseFlagDuration("1s")
s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).")
s.Command.Flags().StringVarP(&s.ResourceVersion, "resource-version", "v", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.")
s.ClientOptions.BuildFlags(s.Command.Flags())
s.DiffOptions.BuildFlags(s.Command.Flags())
Expand Down Expand Up @@ -2680,6 +2683,7 @@ type CloudNamespaceDeleteCommand struct {
Namespace string
AsyncOperationId string
Async bool
PollInterval cliext.FlagDuration
Idempotent bool
ResourceVersion string
}
Expand All @@ -2700,6 +2704,8 @@ func NewCloudNamespaceDeleteCommand(cctx *CommandContext, parent *CloudNamespace
_ = cobra.MarkFlagRequired(s.Command.Flags(), "namespace")
s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.")
s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.")
s.PollInterval = cliext.MustParseFlagDuration("1s")
s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).")
s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if the namespace does not exist. Without this flag, the command errors if the namespace is not found.")
s.Command.Flags().StringVarP(&s.ResourceVersion, "resource-version", "v", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.")
s.ClientOptions.BuildFlags(s.Command.Flags())
Expand Down Expand Up @@ -2802,6 +2808,7 @@ type CloudNamespaceEditCommand struct {
AsyncOperationId string
Idempotent bool
Async bool
PollInterval cliext.FlagDuration
ResourceVersion string
}

Expand All @@ -2822,6 +2829,8 @@ func NewCloudNamespaceEditCommand(cctx *CommandContext, parent *CloudNamespaceCo
s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.")
s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if no changes were made in the editor. Without this flag, the command errors when the configuration is unchanged.")
s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.")
s.PollInterval = cliext.MustParseFlagDuration("1s")
s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).")
s.Command.Flags().StringVarP(&s.ResourceVersion, "resource-version", "v", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.")
s.ClientOptions.BuildFlags(s.Command.Flags())
s.DiffOptions.BuildFlags(s.Command.Flags())
Expand Down Expand Up @@ -3949,6 +3958,7 @@ type CloudNamespaceLifecycleSetCommand struct {
EnableDeleteProtection bool
AsyncOperationId string
Async bool
PollInterval cliext.FlagDuration
Idempotent bool
ResourceVersion string
}
Expand All @@ -3971,6 +3981,8 @@ func NewCloudNamespaceLifecycleSetCommand(cctx *CommandContext, parent *CloudNam
_ = cobra.MarkFlagRequired(s.Command.Flags(), "enable-delete-protection")
s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.")
s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.")
s.PollInterval = cliext.MustParseFlagDuration("1s")
s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).")
s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if the lifecycle configuration is already set to the specified value. Without this flag, the command errors when no change is needed.")
s.Command.Flags().StringVar(&s.ResourceVersion, "resource-version", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.")
s.ClientOptions.BuildFlags(s.Command.Flags())
Expand Down
6 changes: 3 additions & 3 deletions temporalcloudcli/commands.namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *CloudNamespaceEditCommand) run(cctx *CommandContext, _ []string) error
}

// Poll for completion
poller, err := getPoller(cctx, c.ClientOptions)
poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration())
if err != nil {
return err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (c *CloudNamespaceApplyCommand) run(cctx *CommandContext, _ []string) error
}

// Step 7: Poll for completion
poller, err := getPoller(cctx, c.ClientOptions)
poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration())
if err != nil {
return err
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func (c *CloudNamespaceDeleteCommand) run(cctx *CommandContext, _ []string) erro
}

// Poll for completion
poller, err := getPoller(cctx, c.ClientOptions)
poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion temporalcloudcli/commands.namespace.lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *CloudNamespaceLifecycleSetCommand) run(cctx *CommandContext, _ []string
}

// Poll for completion
poller, err := getPoller(cctx, c.ClientOptions)
poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration())
if err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions temporalcloudcli/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ commands:
description: |
Return immediately after initiating the operation instead of waiting
for completion. Use the returned operation ID to check status later.
- name: poll-interval
type: duration
default: 1s
description: |
Time to wait between status checks when waiting for operation completion.
Cannot be greater than 10 minutes.
Supports minutes (m) and seconds (s).
- name: resource-version
type: string
short: v
Expand Down Expand Up @@ -289,6 +296,13 @@ commands:
description: |
Return immediately after initiating the operation instead of waiting
for completion. Use the returned operation ID to check status later.
- name: poll-interval
type: duration
default: 1s
description: |
Time to wait between status checks when waiting for operation completion.
Cannot be greater than 10 minutes.
Supports minutes (m) and seconds (s).
- name: resource-version
type: string
short: v
Expand Down Expand Up @@ -328,6 +342,13 @@ commands:
description: |
Return immediately after initiating the operation instead of waiting
for completion. Use the returned operation ID to check status later.
- name: poll-interval
type: duration
default: 1s
description: |
Time to wait between status checks when waiting for operation completion.
Cannot be greater than 10 minutes.
Supports minutes (m) and seconds (s).
- name: idempotent
type: bool
description: |
Expand Down Expand Up @@ -709,6 +730,13 @@ commands:
description: |
Return immediately after initiating the operation instead of waiting
for completion. Use the returned operation ID to check status later.
- name: poll-interval
type: duration
default: 1s
description: |
Time to wait between status checks when waiting for operation completion.
Cannot be greater than 10 minutes.
Supports minutes (m) and seconds (s).
- name: idempotent
type: bool
description: |
Expand Down
6 changes: 5 additions & 1 deletion temporalcloudcli/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math/rand"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -112,7 +113,10 @@ func (s *SharedServerSuite) pollAsyncOperation(
},
}

poller := temporalcloudcli.AsyncOperationPoller{CloudClient: cloudClient.CloudService()}
poller := temporalcloudcli.AsyncOperationPoller{
CloudClient: cloudClient.CloudService(),
PollInterval: time.Second,
}
return poller.PollAsyncOperation(cctx, operationID, "")
}

Expand Down
20 changes: 13 additions & 7 deletions temporalcloudcli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package temporalcloudcli

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -208,7 +209,7 @@ func (r *operationHandler) HandleOperation(op *operation.AsyncOperation, resourc
if r.asyncOpts.Async {
return r.cctx.Printer.PrintStructured(MutationResult{AsyncOp: op, ID: resourceID}, printer.StructuredOptions{})
}
poller, pollerErr := getPoller(r.cctx, r.clientOpts)
poller, pollerErr := getPoller(r.cctx, r.clientOpts, r.asyncOpts.PollInterval.Duration())
if pollerErr != nil {
return pollerErr
}
Expand Down Expand Up @@ -306,11 +307,12 @@ func wrapDeleteOperation[Req any, Res AsyncOperationResponse](
}

type AsyncOperationPoller struct {
CloudClient cloudservice.CloudServiceClient
CloudClient cloudservice.CloudServiceClient
PollInterval time.Duration
}

// pollAsyncOperation polls an async operation until it reaches a terminal state.
// It prints status updates every second and returns the final AsyncOperation.
// It prints status updates every PollInterval and returns the final AsyncOperation.
//
// The cloudClient should be pre-built using cctx.BuildCloudClient().
//
Expand All @@ -321,7 +323,10 @@ func (p *AsyncOperationPoller) PollAsyncOperation(
operationID string,
id string,
) error {
ticker := time.NewTicker(1 * time.Second)
if p.PollInterval <= 0 {
return errors.New("--poll-interval must be greater than 0")
}
ticker := time.NewTicker(p.PollInterval)
defer ticker.Stop()

for {
Expand Down Expand Up @@ -410,7 +415,7 @@ func newUnchangedResult() Result {
}
}

func getPoller(cctx *CommandContext, opts ClientOptions) (Poller, error) {
func getPoller(cctx *CommandContext, opts ClientOptions, pollInterval time.Duration) (Poller, error) {
if cctx.Poller != nil {
return cctx.Poller, nil
}
Expand All @@ -419,7 +424,8 @@ func getPoller(cctx *CommandContext, opts ClientOptions) (Poller, error) {
return nil, err
}
return &AsyncOperationPoller{
CloudClient: cloudClient.CloudService(),
CloudClient: cloudClient.CloudService(),
PollInterval: pollInterval,
}, nil
}

Expand Down Expand Up @@ -453,7 +459,7 @@ func wrapAsyncOperation[P any](
}, printer.StructuredOptions{})
}

poller, err := getPoller(cctx, clientOpts)
poller, err := getPoller(cctx, clientOpts, asyncOpts.PollInterval.Duration())
if err != nil {
return err
}
Expand Down
55 changes: 55 additions & 0 deletions temporalcloudcli/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
cloudmock "github.com/temporalio/cloud-cli/internal/cloudservice/mock"
"github.com/temporalio/cloud-cli/temporalcloudcli"
"github.com/temporalio/cloud-cli/temporalcloudcli/internal/printer"
cmdmock "github.com/temporalio/cloud-cli/temporalcloudcli/mock"
"go.temporal.io/api/temporalproto"
cloudservice "go.temporal.io/cloud-sdk/api/cloudservice/v1"
operation "go.temporal.io/cloud-sdk/api/operation/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -219,6 +222,58 @@ func TestAsyncOperationHandler_PollingError(t *testing.T) {
require.ErrorIs(t, err, pollingErr)
}

func TestAsyncOperationPoller_PollInterval(t *testing.T) {
const interval = 20 * time.Millisecond
states := []operation.AsyncOperation_State{
operation.AsyncOperation_STATE_PENDING,
operation.AsyncOperation_STATE_IN_PROGRESS,
operation.AsyncOperation_STATE_FULFILLED,
}
mockCloud := cloudmock.NewMockCloudServiceClient(t)
for _, state := range states {
mockCloud.EXPECT().
GetAsyncOperation(mock.Anything, &cloudservice.GetAsyncOperationRequest{AsyncOperationId: "op-1"}, mock.Anything).
Return(&cloudservice.GetAsyncOperationResponse{
AsyncOperation: &operation.AsyncOperation{Id: "op-1", State: state},
}, nil).
Once()
}
var buf bytes.Buffer
cctx := newTestCommandContext(t, &buf)
poller := temporalcloudcli.AsyncOperationPoller{CloudClient: mockCloud, PollInterval: interval}

start := time.Now()
require.NoError(t, poller.PollAsyncOperation(cctx, "op-1", "my-namespace"))
elapsed := time.Since(start)

assert.GreaterOrEqual(t, elapsed, time.Duration(len(states))*interval)
assert.Less(t, elapsed, 2*time.Second, "status checks did not follow the configured interval")
}

func TestAsyncOperationPoller_InvalidPollInterval(t *testing.T) {
tests := []struct {
name string
interval time.Duration
}{
{name: "Zero", interval: 0},
{name: "Negative", interval: -time.Second},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
cctx := newTestCommandContext(t, &buf)
poller := temporalcloudcli.AsyncOperationPoller{
CloudClient: cloudmock.NewMockCloudServiceClient(t),
PollInterval: tt.interval,
}

err := poller.PollAsyncOperation(cctx, "op-1", "my-namespace")
require.EqualError(t, err, "--poll-interval must be greater than 0")
assert.Empty(t, buf.String())
})
}
}

func TestAsyncOperationHandler_HandleUpdateErr_NothingToChange_Idempotent(t *testing.T) {
var buf bytes.Buffer
cctx := newTestCommandContext(t, &buf)
Expand Down