Skip to content

Commit

Permalink
use view when get branch/cluster (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyangyu authored Oct 1, 2024
1 parent 7983f4f commit 7a9fa68
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 45 deletions.
4 changes: 2 additions & 2 deletions internal/cli/serverless/branch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func CreateAndWaitReady(ctx context.Context, h *internal.Helper, d cloud.TiDBClo
case <-timer:
return errors.New(fmt.Sprintf("Timeout waiting for branch %s to be ready, please check status on dashboard.", newBranchID))
case <-ticker.C:
b, err := d.GetBranch(ctx, clusterId, newBranchID)
b, err := d.GetBranch(ctx, clusterId, newBranchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func CreateAndSpinnerWait(ctx context.Context, h *internal.Helper, d cloud.TiDBC
case <-timer:
return errors.New(fmt.Sprintf("Timeout waiting for branch %s to be ready, please check status on dashboard.", newBranchID))
case <-ticker.C:
b, err := d.GetBranch(ctx, clusterId, newBranchID)
b, err := d.GetBranch(ctx, clusterId, newBranchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/serverless/branch/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (suite *CreateBranchSuite) TestCreateBranchArgs() {
body := &branch.Branch{}
err := json.Unmarshal([]byte(getBranchResultStr), body)
assert.Nil(err)
suite.mockClient.On("GetBranch", ctx, clusterID, branchId).Return(body, nil)
suite.mockClient.On("GetBranch", ctx, clusterID, branchId, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_BASIC).Return(body, nil)

tests := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/serverless/branch/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/tidbcloud/tidbcloud-cli/internal/flag"
"github.com/tidbcloud/tidbcloud-cli/internal/output"
"github.com/tidbcloud/tidbcloud-cli/internal/service/cloud"
"github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/branch"

"github.com/juju/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -124,7 +125,7 @@ func DescribeCmd(h *internal.Helper) *cobra.Command {
}
}

branch, err := d.GetBranch(ctx, clusterID, branchID)
branch, err := d.GetBranch(ctx, clusterID, branchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_FULL)
if err != nil {
return errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/serverless/branch/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (suite *DescribeBranchSuite) TestDescribeBranchArgs() {
assert.Nil(err)
clusterID := "10202848322613926203"
branchID := "bran-fgwdnpasmrahnh5iozqawnmijq"
suite.mockClient.On("GetBranch", ctx, clusterID, branchID).Return(body, nil)
suite.mockClient.On("GetBranch", ctx, clusterID, branchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_FULL).Return(body, nil)

tests := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/serverless/branch/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func ResetAndWaitReady(ctx context.Context, h *internal.Helper, d cloud.TiDBClou
case <-timer:
return errors.New(fmt.Sprintf("Timeout waiting for branch %s to be ready, please check status on dashboard.", branchID))
case <-ticker.C:
b, err := d.GetBranch(ctx, clusterID, branchID)
b, err := d.GetBranch(ctx, clusterID, branchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func ResetAndSpinnerWait(ctx context.Context, h *internal.Helper, d cloud.TiDBCl
case <-timer:
return ui.Result(fmt.Sprintf("Timeout waiting for branch %s to be ready, please check status on dashboard.", branchID))
case <-ticker.C:
b, err := d.GetBranch(ctx, clusterID, branchID)
b, err := d.GetBranch(ctx, clusterID, branchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/serverless/branch/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (suite *ResetBranchSuite) TestResetBranchArgs() {
body := &branch.Branch{}
err := json.Unmarshal([]byte(getBranchResultStr), body)
assert.Nil(err)
suite.mockClient.On("GetBranch", ctx, clusterID, branchID).Return(body, nil)
suite.mockClient.On("GetBranch", ctx, clusterID, branchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_BASIC).Return(body, nil)

tests := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/serverless/branch/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/tidbcloud/tidbcloud-cli/internal/flag"
"github.com/tidbcloud/tidbcloud-cli/internal/service/cloud"
"github.com/tidbcloud/tidbcloud-cli/internal/util"
"github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/branch"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
Expand Down Expand Up @@ -197,7 +198,7 @@ The connection forces the [ANSI SQL mode](https://dev.mysql.com/doc/refman/8.0/e
}

var host, name, port string
branchInfo, err := d.GetBranch(ctx, clusterID, branchID)
branchInfo, err := d.GetBranch(ctx, clusterID, branchID, branch.BRANCHSERVICEGETBRANCHVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/serverless/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func CreateAndWaitReady(ctx context.Context, h *internal.Helper, d cloud.TiDBClo
case <-timer:
return errors.New(fmt.Sprintf("Timeout waiting for cluster %s to be ready, please check status on dashboard.", newClusterID))
case <-ticker.C:
clusterResult, err := d.GetCluster(ctx, newClusterID)
clusterResult, err := d.GetCluster(ctx, newClusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -356,7 +356,7 @@ func CreateAndSpinnerWait(ctx context.Context, h *internal.Helper, d cloud.TiDBC
case <-timer:
return ui.Result(fmt.Sprintf("Timeout waiting for cluster %s to be ready, please check status on dashboard.", newClusterID))
case <-ticker.C:
clusterResult, err := d.GetCluster(ctx, newClusterID)
clusterResult, err := d.GetCluster(ctx, newClusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/serverless/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (suite *CreateClusterSuite) TestCreateClusterArgs() {
Return(&cluster.TidbCloudOpenApiserverlessv1beta1Cluster{
ClusterId: &clusterID,
}, nil)
suite.mockClient.On("GetCluster", ctx, clusterID).Return(body, nil)
suite.mockClient.On("GetCluster", ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC).Return(body, nil)

tests := []struct {
name string
Expand Down Expand Up @@ -140,7 +140,7 @@ func (suite *CreateClusterSuite) TestCreateClusterWithoutProject() {
Return(&cluster.TidbCloudOpenApiserverlessv1beta1Cluster{
ClusterId: &clusterID,
}, nil)
suite.mockClient.On("GetCluster", ctx, clusterID).Return(body, nil)
suite.mockClient.On("GetCluster", ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC).Return(body, nil)

tests := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/serverless/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/tidbcloud/tidbcloud-cli/internal/output"
"github.com/tidbcloud/tidbcloud-cli/internal/service/cloud"
"github.com/tidbcloud/tidbcloud-cli/internal/telemetry"
"github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/cluster"

"github.com/juju/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -110,7 +111,7 @@ func DescribeCmd(h *internal.Helper) *cobra.Command {
clusterID = cID
}

c, err := d.GetCluster(ctx, clusterID)
c, err := d.GetCluster(ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_FULL)
if err != nil {
return errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/serverless/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (suite *DescribeClusterSuite) TestDescribeClusterArgs() {
assert.Nil(err)

clusterID := "12345"
suite.mockClient.On("GetCluster", ctx, clusterID).Return(body, nil)
suite.mockClient.On("GetCluster", ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_FULL).Return(body, nil)

tests := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/serverless/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/tidbcloud/tidbcloud-cli/internal/flag"
"github.com/tidbcloud/tidbcloud-cli/internal/service/cloud"
"github.com/tidbcloud/tidbcloud-cli/internal/util"
"github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/cluster"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
Expand Down Expand Up @@ -167,7 +168,7 @@ The connection forces the [ANSI SQL mode](https://dev.mysql.com/doc/refman/8.0/e
}

var host, name, port string
cluster, err := d.GetCluster(ctx, clusterID)
cluster, err := d.GetCluster(ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC)
if err != nil {
return errors.Trace(err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/serverless/sqluser/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/tidbcloud/tidbcloud-cli/internal/util"

"github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/iam"
"github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/cluster"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -266,7 +267,7 @@ func initialCreateInputModel(userPrefix string) ui.TextInputModel {
}

func getUserPrefix(ctx context.Context, d cloud.TiDBCloudClient, clusterID string) (string, error) {
cluster, err := d.GetCluster(ctx, clusterID)
cluster, err := d.GetCluster(ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC)
if err != nil {
return "", errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/serverless/sqluser/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (suite *CreateSQLUserSuite) TestCreateSQLUserArgs() {
res := &cluster.TidbCloudOpenApiserverlessv1beta1Cluster{}
err = json.Unmarshal([]byte(getClusterResultStr), res)
assert.Nil(err)
suite.mockClient.On("GetCluster", ctx, clusterID).Return(res, nil)
suite.mockClient.On("GetCluster", ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC).Return(res, nil)

tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/serverless/sqluser/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (suite *UpdateSQLUserSuite) TestUpdateSQLUserArgs() {
res := &cluster.TidbCloudOpenApiserverlessv1beta1Cluster{}
err = json.Unmarshal([]byte(getClusterResultStr), res)
assert.Nil(err)
suite.mockClient.On("GetCluster", ctx, clusterID).Return(res, nil)
suite.mockClient.On("GetCluster", ctx, clusterID, cluster.SERVERLESSSERVICEGETCLUSTERVIEWPARAMETER_BASIC).Return(res, nil)

updateBody := &iam.ApiUpdateSqlUserReq{
BuiltinRole: &builtinRole,
Expand Down
38 changes: 19 additions & 19 deletions internal/mock/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/mock/uploader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions internal/service/cloud/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type TiDBCloudClient interface {

DeleteCluster(ctx context.Context, clusterId string) (*cluster.TidbCloudOpenApiserverlessv1beta1Cluster, error)

GetCluster(ctx context.Context, clusterId string) (*cluster.TidbCloudOpenApiserverlessv1beta1Cluster, error)
GetCluster(ctx context.Context, clusterId string, view cluster.ServerlessServiceGetClusterViewParameter) (*cluster.TidbCloudOpenApiserverlessv1beta1Cluster, error)

ListClusters(ctx context.Context, filter *string, pageSize *int32, pageToken *string, orderBy *string, skip *int32) (*cluster.TidbCloudOpenApiserverlessv1beta1ListClustersResponse, error)

Expand All @@ -66,7 +66,7 @@ type TiDBCloudClient interface {

ListImports(ctx context.Context, clusterId string, pageSize *int32, pageToken, orderBy *string) (*imp.ListImportsResp, error)

GetBranch(ctx context.Context, clusterId, branchId string) (*branch.Branch, error)
GetBranch(ctx context.Context, clusterId, branchId string, view branch.BranchServiceGetBranchViewParameter) (*branch.Branch, error)

ListBranches(ctx context.Context, clusterId string, pageSize *int32, pageToken *string) (*branch.ListBranchesResponse, error)

Expand Down Expand Up @@ -174,8 +174,10 @@ func (d *ClientDelegate) DeleteCluster(ctx context.Context, clusterId string) (*
return c, parseError(err, h)
}

func (d *ClientDelegate) GetCluster(ctx context.Context, clusterId string) (*cluster.TidbCloudOpenApiserverlessv1beta1Cluster, error) {
c, h, err := d.sc.ServerlessServiceAPI.ServerlessServiceGetCluster(ctx, clusterId).Execute()
func (d *ClientDelegate) GetCluster(ctx context.Context, clusterId string, view cluster.ServerlessServiceGetClusterViewParameter) (*cluster.TidbCloudOpenApiserverlessv1beta1Cluster, error) {
r := d.sc.ServerlessServiceAPI.ServerlessServiceGetCluster(ctx, clusterId)
r = r.View(view)
c, h, err := r.Execute()
return c, parseError(err, h)
}

Expand Down Expand Up @@ -260,8 +262,10 @@ func (d *ClientDelegate) ListImports(ctx context.Context, clusterId string, page
return is, parseError(err, h)
}

func (d *ClientDelegate) GetBranch(ctx context.Context, clusterId, branchId string) (*branch.Branch, error) {
b, h, err := d.bc.BranchServiceAPI.BranchServiceGetBranch(ctx, clusterId, branchId).Execute()
func (d *ClientDelegate) GetBranch(ctx context.Context, clusterId, branchId string, view branch.BranchServiceGetBranchViewParameter) (*branch.Branch, error) {
r := d.bc.BranchServiceAPI.BranchServiceGetBranch(ctx, clusterId, branchId)
r = r.View(view)
b, h, err := r.Execute()
return b, parseError(err, h)
}

Expand Down

0 comments on commit 7a9fa68

Please sign in to comment.