Skip to content
Open
59 changes: 49 additions & 10 deletions app/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 @@ -78,7 +79,7 @@ func (c *AccountClient) listRegions() ([]regionInfo, error) {
return regions, 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 @@ -90,10 +91,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 @@ -113,8 +113,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 @@ -123,6 +124,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 Down Expand Up @@ -158,6 +163,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 @@ -173,12 +182,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 @@ -190,7 +207,11 @@ 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)
},
},
{
Expand All @@ -207,6 +228,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 @@ -238,7 +261,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 @@ -251,6 +278,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 @@ -296,7 +325,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 @@ -308,6 +341,8 @@ func NewAccountCommand(getAccountClientFn GetAccountClientFn) (CommandOut, error
ResourceVersionFlag,
CaCertificateFlag,
CaCertificateFileFlag,
RequestTimeoutFlag,
WaitForRequestFlag,
},
Action: func(ctx *cli.Context) error {
cert, err := ReadCACerts(ctx)
Expand All @@ -331,7 +366,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 @@ -17,6 +17,7 @@ import (
"github.com/temporalio/tcld/protogen/api/common/v1"
"github.com/temporalio/tcld/protogen/api/request/v1"
accountservicemock "github.com/temporalio/tcld/protogen/apimock/accountservice/v1"
requestservicemock "github.com/temporalio/tcld/protogen/apimock/requestservice/v1"
"github.com/urfave/cli/v2"
)

Expand All @@ -26,20 +27,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