Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for optional sync flag to block on async ops till completion #70

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
60 changes: 50 additions & 10 deletions app/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/temporalio/tcld/protogen/api/account/v1"
"github.com/temporalio/tcld/protogen/api/accountservice/v1"
"github.com/temporalio/tcld/protogen/api/request/v1"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ func (c *AccountClient) getAccount() (*account.Account, error) {
return res.Account, nil
}

func (c *AccountClient) updateAccount(ctx *cli.Context, a *account.Account) error {
func (c *AccountClient) updateAccount(ctx *cli.Context, a *account.Account) (*request.RequestStatus, error) {
resourceVersion := a.ResourceVersion
if v := ctx.String(ResourceVersionFlagName); v != "" {
resourceVersion = v
Expand All @@ -59,10 +60,9 @@ func (c *AccountClient) updateAccount(ctx *cli.Context, a *account.Account) erro
Spec: a.Spec,
})
if err != nil {
return err
return nil, err
}

return PrintProto(res)
return res.RequestStatus, nil
}

func (c *AccountClient) parseExistingMetricsCerts(ctx *cli.Context) (account *account.Account, existing caCerts, err error) {
Expand All @@ -82,8 +82,9 @@ func (c *AccountClient) parseExistingMetricsCerts(ctx *cli.Context) (account *ac
return a, existingCerts, nil
}

func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error) {
func NewAccountCommand(getAccountClientFn GetAccountClientFn, getRequestClientFn GetRequestClientFn) (CommandOut, error) {
var c *AccountClient
var r *RequestClient
return CommandOut{
Command: &cli.Command{
Name: "account",
Expand All @@ -92,6 +93,10 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
Before: func(ctx *cli.Context) error {
var err error
c, err = getAccountClientFn(ctx)
if err != nil {
return err
}
r, err = getRequestClientFn(ctx)
return err
},
Subcommands: []*cli.Command{
Expand All @@ -115,6 +120,10 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
{
Name: "enable",
Usage: "Enables the metrics endpoint. CA Certificates *must* be configured prior to enabling the endpoint",
Flags: []cli.Flag{
RequestTimeoutFlag,
WaitForRequestFlag,
},
Action: func(ctx *cli.Context) error {
a, err := c.getAccount()
if err != nil {
Expand All @@ -130,12 +139,20 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
}

a.Spec.Metrics.Enabled = true
return c.updateAccount(ctx, a)
status, err := c.updateAccount(ctx, a)
if err != nil {
return err
}
return r.HandleRequestStatus(ctx, "enable metrics", status)
},
},
{
Name: "disable",
Usage: "Disables the metrics endpoint",
Flags: []cli.Flag{
RequestTimeoutFlag,
WaitForRequestFlag,
},
Action: func(ctx *cli.Context) error {
a, err := c.getAccount()
if err != nil {
Expand All @@ -147,7 +164,12 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
}

a.Spec.Metrics.Enabled = false
return c.updateAccount(ctx, a)
status, err := c.updateAccount(ctx, a)
if err != nil {
return err
}
return r.HandleRequestStatus(ctx, "disable metrics", status)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space

},
},
{
Expand All @@ -164,6 +186,8 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
ResourceVersionFlag,
CaCertificateFlag,
CaCertificateFileFlag,
RequestTimeoutFlag,
WaitForRequestFlag,
},
Action: func(ctx *cli.Context) error {
newCerts, err := readAndParseCACerts(ctx)
Expand Down Expand Up @@ -195,7 +219,11 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
}

a.Spec.Metrics.AcceptedClientCa = bundle
return c.updateAccount(ctx, a)
status, err := c.updateAccount(ctx, a)
if err != nil {
return err
}
return r.HandleRequestStatus(ctx, "add metrics ca certificate", status)
},
},
{
Expand All @@ -208,6 +236,8 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
CaCertificateFlag,
CaCertificateFileFlag,
caCertificateFingerprintFlag,
RequestTimeoutFlag,
WaitForRequestFlag,
},
Action: func(ctx *cli.Context) error {
a, existingCerts, err := c.parseExistingMetricsCerts(ctx)
Expand Down Expand Up @@ -253,7 +283,11 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
if err != nil || !y {
return err
}
return c.updateAccount(ctx, a)
status, err := c.updateAccount(ctx, a)
if err != nil {
return err
}
return r.HandleRequestStatus(ctx, "remove metrics ca certificate", status)
},
},
{
Expand All @@ -265,6 +299,8 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
ResourceVersionFlag,
CaCertificateFlag,
CaCertificateFileFlag,
RequestTimeoutFlag,
WaitForRequestFlag,
},
Action: func(ctx *cli.Context) error {
cert, err := ReadCACerts(ctx)
Expand All @@ -288,7 +324,11 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
a.Spec.Metrics = &account.MetricsSpec{}
}
a.Spec.Metrics.AcceptedClientCa = cert
return c.updateAccount(ctx, a)
status, err := c.updateAccount(ctx, a)
if err != nil {
return err
}
return r.HandleRequestStatus(ctx, "set metrics ca certificates", status)
},
},
{
Expand Down
22 changes: 17 additions & 5 deletions app/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/temporalio/tcld/protogen/api/account/v1"
"github.com/temporalio/tcld/protogen/api/accountservice/v1"
"github.com/temporalio/tcld/protogen/api/request/v1"
requestservicemock "github.com/temporalio/tcld/protogen/apimock/requestservice/v1"
"github.com/urfave/cli/v2"
)

Expand All @@ -25,20 +26,31 @@ func TestAccount(t *testing.T) {

type AccountTestSuite struct {
suite.Suite
cliApp *cli.App
mockCtrl *gomock.Controller
mockService *accountservicemock.MockAccountServiceClient
cliApp *cli.App
mockCtrl *gomock.Controller
mockService *accountservicemock.MockAccountServiceClient
mockReqService *requestservicemock.MockRequestServiceClient
}

func (s *AccountTestSuite) SetupTest() {
s.mockCtrl = gomock.NewController(s.T())
s.mockService = accountservicemock.NewMockAccountServiceClient(s.mockCtrl)
out, err := NewAccountCommand(func(ctx *cli.Context) (*AccountClient, error) {
s.mockReqService = requestservicemock.NewMockRequestServiceClient(s.mockCtrl)

getAccountClientFn := func(ctx *cli.Context) (*AccountClient, error) {
return &AccountClient{
ctx: context.TODO(),
client: s.mockService,
}, nil
})
}
getRequestClientFn := func(ctx *cli.Context) (*RequestClient, error) {
return &RequestClient{
ctx: context.TODO(),
client: s.mockReqService,
}, nil
}

out, err := NewAccountCommand(getAccountClientFn, getRequestClientFn)
s.Require().NoError(err)
AutoConfirmFlag.Value = true
s.cliApp = &cli.App{
Expand Down
6 changes: 5 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"go.uber.org/fx"
)

const (
AppName = "tcld"
)

type AppParams struct {
fx.In
Commands []*cli.Command `group:"commands"`
Expand All @@ -17,7 +21,7 @@ type CommandOut struct {

func NewApp(params AppParams) (*cli.App, error) {
app := &cli.App{
Name: "tcld",
Name: AppName,
Usage: "Temporal Cloud cli",
Flags: []cli.Flag{
ServerFlag,
Expand Down
Loading