diff --git a/app/namespace.go b/app/namespace.go index c2f7ae4a..dd5c041f 100644 --- a/app/namespace.go +++ b/app/namespace.go @@ -326,6 +326,21 @@ func (c *NamespaceClient) listNamespaces() error { } } +func (c *NamespaceClient) getNamespaceCloudApi(namespace string) (*cloudNamespace.Namespace, error) { + res, err := c.cloudAPIClient.GetNamespace(c.ctx, &cloudservice.GetNamespaceRequest{ + Namespace: namespace, + }) + if err != nil { + return nil, err + } + if res.Namespace == nil || res.Namespace.Namespace == "" { + // this should never happen, the server should return an error when the namespace is not found + return nil, fmt.Errorf("invalid namespace returned by server") + } + return res.Namespace, nil +} + +// TODO: deprecate this and use getNamespaceCloudApi everywhere func (c *NamespaceClient) getNamespace(namespace string) (*namespace.Namespace, error) { res, err := c.client.GetNamespace(c.ctx, &namespaceservice.GetNamespaceRequest{ Namespace: namespace, @@ -1812,6 +1827,28 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut, }, }, }, + { + Name: "capacity", + Usage: "Manage namespace capacity settings", + Aliases: []string{"cap"}, + Subcommands: []*cli.Command{ + { + Name: "get", + Usage: "Get namespace capacity settings", + Aliases: []string{"g"}, + Flags: []cli.Flag{ + NamespaceFlag, + }, + Action: func(ctx *cli.Context) error { + n, err := c.getNamespaceCloudApi(ctx.String(NamespaceFlagName)) + if err != nil { + return err + } + return PrintProto(n.GetCapacity()) + }, + }, + }, + }, } // Export Related Command diff --git a/app/namespace_test.go b/app/namespace_test.go index e1ab1339..910b30fa 100644 --- a/app/namespace_test.go +++ b/app/namespace_test.go @@ -19,13 +19,14 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" + "github.com/urfave/cli/v2" + "github.com/temporalio/tcld/protogen/api/namespace/v1" "github.com/temporalio/tcld/protogen/api/namespaceservice/v1" "github.com/temporalio/tcld/protogen/api/request/v1" authservicemock "github.com/temporalio/tcld/protogen/apimock/authservice/v1" apimock "github.com/temporalio/tcld/protogen/apimock/cloudservice/v1" namespaceservicemock "github.com/temporalio/tcld/protogen/apimock/namespaceservice/v1" - "github.com/urfave/cli/v2" ) func TestNamespace(t *testing.T) { @@ -3264,3 +3265,59 @@ func (s *NamespaceTestSuite) TestSetConnectivityRules() { }) } } + +func (s *NamespaceTestSuite) TestGetNamespaceCapacity() { + tests := []struct { + name string + args []string + mock func() + expectErr bool + }{ + { + name: "get namespace capacity - success", + args: []string{"namespace", "capacity", "get", "--namespace", "ns1"}, + mock: func() { + s.mockCloudApiClient.EXPECT(). + GetNamespace(gomock.Any(), &cloudservice.GetNamespaceRequest{ + Namespace: "ns1", + }).Return(&cloudservice.GetNamespaceResponse{ + Namespace: &cloudNamespace.Namespace{ + Namespace: "ns1", + Capacity: &cloudNamespace.Capacity{ + CurrentMode: &cloudNamespace.Capacity_Provisioned_{ + Provisioned: &cloudNamespace.Capacity_Provisioned{ + CurrentValue: 16.0, + }, + }, + }, + }, + }, nil).Times(1) + }, + }, + { + name: "get namespace capacity - fail", + args: []string{"namespace", "capacity", "get", "--namespace", "ns1"}, + expectErr: true, + mock: func() { + s.mockCloudApiClient.EXPECT(). + GetNamespace(gomock.Any(), &cloudservice.GetNamespaceRequest{ + Namespace: "ns1", + }).Return(nil, errors.New("some error")).Times(1) + }, + }, + } + + for _, tc := range tests { + s.Run(tc.name, func() { + if tc.mock != nil { + tc.mock() + } + err := s.RunCmd(tc.args...) + if tc.expectErr { + s.Error(err) + } else { + s.NoError(err) + } + }) + } +}