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

openapi generator refactor for branch #221

Merged
merged 4 commits into from
Aug 30, 2024
Merged
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ addcopy: ## Add copyright to all files
generate-v1beta1-client: install-openapi-generator## Generate v1beta1 client
go install github.com/go-swagger/go-swagger/cmd/swagger@latest
@echo "==> Generating serverless branch client"
swagger generate client -f pkg/tidbcloud/v1beta1/branch/branch.swagger.json -A tidbcloud-serverless -t pkg/tidbcloud/v1beta1/branch
rm -rf pkg/tidbcloud/v1beta1/serverless/branch
cd tools/openapi-generator && npx openapi-generator-cli generate --additional-properties=withGoMod=false,enumClassPrefix=true --global-property=apiTests=false,apiDocs=false,modelDocs=fasle,modelTests=false -i ../../pkg/tidbcloud/v1beta1/serverless/branch.swagger.json -g go -o ../../pkg/tidbcloud/v1beta1/serverless/branch --package-name branch
@echo "==> Generating serverless client"
swagger generate client -f pkg/tidbcloud/v1beta1/serverless/serverless.swagger.json -A tidbcloud-serverless -t pkg/tidbcloud/v1beta1/serverless
@echo "==> Generating serverless br client"
Expand Down
55 changes: 22 additions & 33 deletions internal/cli/serverless/branch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import (
"tidbcloud-cli/internal/service/cloud"
"tidbcloud-cli/internal/ui"
"tidbcloud-cli/internal/util"
branchApi "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/client/branch_service"
branchModel "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/models"
"tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/branch"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/fatih/color"
"github.com/go-openapi/strfmt"
"github.com/juju/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -119,7 +117,7 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
var clusterId string
var parentID string
var parentTimestampStr string
var parentTimestamp *strfmt.DateTime
var parentTimestamp time.Time
if opts.interactive {
if !h.IOStreams.CanPrompt {
return errors.New("The terminal doesn't support interactive mode, please use non-interactive mode")
Expand Down Expand Up @@ -173,27 +171,26 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
}

if len(parentTimestampStr) != 0 {
t, err := time.Parse(time.RFC3339, parentTimestampStr)
parentTimestamp, err = time.Parse(time.RFC3339, parentTimestampStr)
if err != nil {
return errors.New("Invalid parent timestamp format, please use RFC3339 format")
}
tFormated := strfmt.DateTime(t)
parentTimestamp = &tFormated
}

params := branchApi.NewBranchServiceCreateBranchParams().WithClusterID(clusterId).WithBranch(&branchModel.V1beta1Branch{
DisplayName: &branchName,
ParentID: parentID,
ParentTimestamp: parentTimestamp,
}).WithContext(ctx)

body := branch.NewBranch(branchName)
if parentID != "" {
body.SetParentId(parentID)
}
if !parentTimestamp.IsZero() {
body.SetParentTimestamp(parentTimestamp)
}
if h.IOStreams.CanPrompt {
err := CreateAndSpinnerWait(ctx, d, params, h)
err := CreateAndSpinnerWait(ctx, h, d, clusterId, body)
if err != nil {
return errors.Trace(err)
}
} else {
err := CreateAndWaitReady(ctx, h, d, params)
err := CreateAndWaitReady(ctx, h, d, clusterId, body)
if err != nil {
return err
}
Expand All @@ -210,12 +207,12 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
return createCmd
}

func CreateAndWaitReady(ctx context.Context, h *internal.Helper, d cloud.TiDBCloudClient, params *branchApi.BranchServiceCreateBranchParams) error {
createBranchResult, err := d.CreateBranch(params)
func CreateAndWaitReady(ctx context.Context, h *internal.Helper, d cloud.TiDBCloudClient, clusterId string, body *branch.Branch) error {
b, err := d.CreateBranch(ctx, clusterId, body)
if err != nil {
return errors.Trace(err)
}
newBranchID := createBranchResult.GetPayload().BranchID
newBranchID := *b.BranchId

fmt.Fprintln(h.IOStreams.Out, "... Waiting for branch to be ready")
ticker := time.NewTicker(WaitInterval)
Expand All @@ -226,30 +223,26 @@ 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:
clusterResult, err := d.GetBranch(branchApi.NewBranchServiceGetBranchParams().
WithClusterID(params.ClusterID).
WithBranchID(newBranchID).
WithContext(ctx))
b, err := d.GetBranch(ctx, clusterId, newBranchID)
if err != nil {
return errors.Trace(err)
}
s := clusterResult.GetPayload().State
if s == branchModel.V1beta1BranchStateACTIVE {
if *b.State == branch.BRANCHSTATE_ACTIVE {
fmt.Fprint(h.IOStreams.Out, color.GreenString("Branch %s is ready.", newBranchID))
return nil
}
}
}
}

func CreateAndSpinnerWait(ctx context.Context, d cloud.TiDBCloudClient, params *branchApi.BranchServiceCreateBranchParams, h *internal.Helper) error {
func CreateAndSpinnerWait(ctx context.Context, h *internal.Helper, d cloud.TiDBCloudClient, clusterId string, body *branch.Branch) error {
// use spinner to indicate that the cluster is being created
task := func() tea.Msg {
createBranchResult, err := d.CreateBranch(params)
b, err := d.CreateBranch(ctx, clusterId, body)
if err != nil {
return errors.Trace(err)
}
newBranchID := createBranchResult.GetPayload().BranchID
newBranchID := *b.BranchId

ticker := time.NewTicker(WaitInterval)
defer ticker.Stop()
Expand All @@ -259,15 +252,11 @@ func CreateAndSpinnerWait(ctx context.Context, d cloud.TiDBCloudClient, params *
case <-timer:
return ui.Result(fmt.Sprintf("Timeout waiting for branch %s to be ready, please check status on dashboard.", newBranchID))
case <-ticker.C:
clusterResult, err := d.GetBranch(branchApi.NewBranchServiceGetBranchParams().
WithClusterID(params.ClusterID).
WithBranchID(newBranchID).
WithContext(ctx))
b, err := d.GetBranch(ctx, clusterId, newBranchID)
if err != nil {
return errors.Trace(err)
}
s := clusterResult.GetPayload().State
if s == branchModel.V1beta1BranchStateACTIVE {
if *b.State == branch.BRANCHSTATE_ACTIVE {
return ui.Result(fmt.Sprintf("Branch %s is ready.", newBranchID))
}
case <-ctx.Done():
Expand Down
25 changes: 8 additions & 17 deletions internal/cli/serverless/branch/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
"tidbcloud-cli/internal/iostream"
"tidbcloud-cli/internal/mock"
"tidbcloud-cli/internal/service/cloud"
branchApi "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/client/branch_service"
branchModel "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/models"
"tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/branch"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -63,26 +62,18 @@ func (suite *CreateBranchSuite) TestCreateBranchArgs() {
branchName := "test"
branchId := "12345"

createBranchBody := &branchModel.V1beta1Branch{
DisplayName: &branchName,
createBranchBody := &branch.Branch{
DisplayName: branchName,
}
suite.mockClient.On("CreateBranch", branchApi.NewBranchServiceCreateBranchParams().
WithClusterID(clusterID).WithBranch(createBranchBody).WithContext(ctx)).
Return(&branchApi.BranchServiceCreateBranchOK{
Payload: &branchModel.V1beta1Branch{
BranchID: branchId,
},
suite.mockClient.On("CreateBranch", ctx, clusterID, createBranchBody).
Return(&branch.Branch{
BranchId: &branchId,
}, nil)

body := &branchModel.V1beta1Branch{}
body := &branch.Branch{}
err := json.Unmarshal([]byte(getBranchResultStr), body)
assert.Nil(err)
result := &branchApi.BranchServiceGetBranchOK{
Payload: body,
}
suite.mockClient.On("GetBranch", branchApi.NewBranchServiceGetBranchParams().
WithClusterID(clusterID).WithBranchID(branchId).WithContext(ctx)).
Return(result, nil)
suite.mockClient.On("GetBranch", ctx, clusterID, branchId).Return(body, nil)

tests := []struct {
name string
Expand Down
5 changes: 1 addition & 4 deletions internal/cli/serverless/branch/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"tidbcloud-cli/internal/flag"
"tidbcloud-cli/internal/service/cloud"
"tidbcloud-cli/internal/util"
branchApi "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/client/branch_service"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
Expand Down Expand Up @@ -159,9 +158,7 @@ func DeleteCmd(h *internal.Helper) *cobra.Command {
}
}

params := branchApi.NewBranchServiceDeleteBranchParams().
WithClusterID(clusterID).WithBranchID(branchID).WithContext(ctx)
_, err = d.DeleteBranch(params)
_, err = d.DeleteBranch(ctx, clusterID, branchID)
if err != nil {
return errors.Trace(err)
}
Expand Down
6 changes: 2 additions & 4 deletions internal/cli/serverless/branch/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"tidbcloud-cli/internal/iostream"
"tidbcloud-cli/internal/mock"
"tidbcloud-cli/internal/service/cloud"
branchApi "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/client/branch_service"
"tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/branch"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -59,9 +59,7 @@ func (suite *DeleteBranchSuite) TestDeleteBranchArgs() {

clusterID := "12345"
branchID := "12345"
suite.mockClient.On("DeleteBranch", branchApi.NewBranchServiceDeleteBranchParams().
WithBranchID(branchID).WithClusterID(clusterID).WithContext(ctx)).
Return(&branchApi.BranchServiceDeleteBranchOK{}, nil)
suite.mockClient.On("DeleteBranch", ctx, clusterID, branchID).Return(&branch.Branch{}, nil)

tests := []struct {
name string
Expand Down
7 changes: 2 additions & 5 deletions internal/cli/serverless/branch/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"tidbcloud-cli/internal/config"
"tidbcloud-cli/internal/flag"
"tidbcloud-cli/internal/service/cloud"
branchApi "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/client/branch_service"

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

params := branchApi.NewBranchServiceGetBranchParams().
WithClusterID(clusterID).WithBranchID(branchID).WithContext(ctx)
branch, err := d.GetBranch(params)
branch, err := d.GetBranch(ctx, clusterID, branchID)
if err != nil {
return errors.Trace(err)
}

v, err := json.MarshalIndent(branch.Payload, "", " ")
v, err := json.MarshalIndent(branch, "", " ")
if err != nil {
return errors.Trace(err)
}
Expand Down
16 changes: 5 additions & 11 deletions internal/cli/serverless/branch/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
"tidbcloud-cli/internal/iostream"
"tidbcloud-cli/internal/mock"
"tidbcloud-cli/internal/service/cloud"
branchApi "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/client/branch_service"
branchModel "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/models"
"tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/branch"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand All @@ -39,7 +38,7 @@ const getBranchResultStr = `{
},
"branchId": "bran-fgwdnpasmrahnh5iozqawnmijq",
"clusterId": "10202848322613926203",
"createTime": "2023-12-11T09:41:44.000Z",
"createTime": "2023-12-11T09:41:44Z",
"createdBy": "[email protected]",
"displayName": "t2",
"endpoints": {
Expand All @@ -51,7 +50,7 @@ const getBranchResultStr = `{
"name": "clusters/10202848322613926203/branches/bran-fgwdnpasmrahnh5iozqawnmijq",
"parentId": "10202848322613926203",
"state": "ACTIVE",
"updateTime": "2023-12-11T09:44:05.000Z",
"updateTime": "2023-12-11T09:44:05Z",
"usage": {
"requestUnit": "0",
"rowStorage": 951526
Expand Down Expand Up @@ -86,17 +85,12 @@ func (suite *DescribeBranchSuite) TestDescribeBranchArgs() {
assert := require.New(suite.T())
ctx := context.Background()

body := &branchModel.V1beta1Branch{}
body := &branch.Branch{}
err := json.Unmarshal([]byte(getBranchResultStr), body)
assert.Nil(err)
result := &branchApi.BranchServiceGetBranchOK{
Payload: body,
}
clusterID := "10202848322613926203"
branchID := "bran-fgwdnpasmrahnh5iozqawnmijq"
suite.mockClient.On("GetBranch", branchApi.NewBranchServiceGetBranchParams().
WithBranchID(branchID).WithClusterID(clusterID).WithContext(ctx)).
Return(result, nil)
suite.mockClient.On("GetBranch", ctx, clusterID, branchID).Return(body, nil)

tests := []struct {
name string
Expand Down
19 changes: 10 additions & 9 deletions internal/cli/serverless/branch/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package branch

import (
"fmt"
"time"

"tidbcloud-cli/internal"
"tidbcloud-cli/internal/config"
"tidbcloud-cli/internal/flag"
"tidbcloud-cli/internal/output"
"tidbcloud-cli/internal/service/cloud"
branchModel "tidbcloud-cli/pkg/tidbcloud/v1beta1/branch/models"
"tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/branch"

"github.com/juju/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -126,9 +127,9 @@ func ListCmd(h *internal.Helper) *cobra.Command {
// for terminal which can prompt, humanFormat is the default format.
// for other terminals, json format is the default format.
if format == output.JsonFormat || !h.IOStreams.CanPrompt {
res := &branchModel.V1beta1ListBranchesResponse{
res := &branch.ListBranchesResponse{
Branches: items,
TotalSize: total,
TotalSize: &total,
}
err := output.PrintJson(h.IOStreams.Out, res)
if err != nil {
Expand All @@ -147,12 +148,12 @@ func ListCmd(h *internal.Helper) *cobra.Command {
var rows []output.Row
for _, item := range items {
rows = append(rows, output.Row{
item.BranchID,
*item.DisplayName,
item.ParentID,
item.ParentDisplayName,
string(item.State),
item.CreateTime.String(),
*item.BranchId,
item.DisplayName,
*item.ParentId,
*item.ParentDisplayName,
string(*item.State),
item.CreateTime.Format(time.RFC3339),
})
}

Expand Down
Loading
Loading