Skip to content

Commit

Permalink
add delete user cmd and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun committed Oct 20, 2023
1 parent 57c2d5c commit 0abc7ff
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
9 changes: 9 additions & 0 deletions lorry/client/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ func (cli *HTTPClient) CreateUser(ctx context.Context, userName, password string
return err
}

func (cli *HTTPClient) DeleteUser(ctx context.Context, userName string) error {
parameters := map[string]any{
"userName": userName,
}
req := &httpserver.Request{Parameters: parameters}
_, err := cli.Request(ctx, string(DeleteUserOp), http.MethodPost, req)
return err
}

// ListUsers lists all normal users created
func (cli *HTTPClient) ListUsers(ctx context.Context) ([]map[string]any, error) {
resp, err := cli.Request(ctx, string(ListUsersOp), http.MethodGet, nil)
Expand Down
21 changes: 20 additions & 1 deletion lorry/client/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ var _ = Describe("Lorry HTTP Client", func() {
BeforeEach(func() {
lorryClient, _ = NewHTTPClientWithPod(pod)
Expect(lorryClient).ShouldNot(BeNil())
lorryClient.ReconcileTimeout = 100 * time.Second
})

It("success", func() {
Expand All @@ -212,6 +211,26 @@ var _ = Describe("Lorry HTTP Client", func() {
})
})

Context("delete user", func() {
var lorryClient *HTTPClient

BeforeEach(func() {
lorryClient, _ = NewHTTPClientWithPod(pod)
Expect(lorryClient).ShouldNot(BeNil())
})

It("success", func() {
mockDBManager.EXPECT().DeleteUser(gomock.Any(), gomock.Any()).Return(nil)
Expect(lorryClient.DeleteUser(context.TODO(), "user-test")).Should(Succeed())
})

It("not implemented", func() {
msg := "not implemented for test"
mockDBManager.EXPECT().DeleteUser(gomock.Any(), gomock.Any()).Return(fmt.Errorf(msg))
Expect(lorryClient.DeleteUser(context.TODO(), "user-test")).Should(HaveOccurred())
})
})

Context("list users", func() {
var lorryClient *HTTPClient
var users []models.UserInfo
Expand Down
23 changes: 13 additions & 10 deletions lorry/ctl/createuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
"github.com/apecloud/kubeblocks/lorry/client"
)

var (
lorryAddr2 string
userName string
password string
)
type CreateUserOptions struct {
lorryAddr string
userName string
password string
}

var createUserOptions = &CreateUserOptions{}

var CreateUserCmd = &cobra.Command{
Use: "createuser",
Expand All @@ -42,24 +44,25 @@ lorryctl createuser --username xxx --password xxx
`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
lorryClient, err := client.NewHTTPClientWithURL(lorryAddr2)
lorryClient, err := client.NewHTTPClientWithURL(createUserOptions.lorryAddr)
if err != nil {
fmt.Printf("new lorry http client failed: %v\n", err)
return
}

err = lorryClient.CreateUser(context.TODO(), userName, password)
err = lorryClient.CreateUser(context.TODO(), createUserOptions.userName, createUserOptions.password)
if err != nil {
fmt.Printf("create user failed: %v\n", err)
return
}
fmt.Printf("create user success")
},
}

func init() {
CreateUserCmd.Flags().StringVarP(&userName, "username", "", "", "The name of user to create")
CreateUserCmd.Flags().StringVarP(&password, "password", "", "", "The password of user to create")
CreateUserCmd.Flags().StringVarP(&lorryAddr2, "lorry-addr", "", "http://localhost:3501/v1.0/", "The addr of lorry to request")
CreateUserCmd.Flags().StringVarP(&createUserOptions.userName, "username", "", "", "The name of user to create")
CreateUserCmd.Flags().StringVarP(&createUserOptions.password, "password", "", "", "The password of user to create")
CreateUserCmd.Flags().StringVarP(&createUserOptions.lorryAddr, "lorry-addr", "", "http://localhost:3501/v1.0/", "The addr of lorry to request")
CreateUserCmd.Flags().BoolP("help", "h", false, "Print this help message")

RootCmd.AddCommand(CreateUserCmd)
Expand Down
67 changes: 67 additions & 0 deletions lorry/ctl/deleteuser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ctl

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/apecloud/kubeblocks/lorry/client"
)

type DeleteUserOptions struct {
lorryAddr string
userName string
}

var deleteUserOptions = &DeleteUserOptions{}

var DeleteUserCmd = &cobra.Command{
Use: "deleteuser",
Short: "delete user.",
Example: `
lorryctl deleteuser --username xxx --password xxx
`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
lorryClient, err := client.NewHTTPClientWithURL(deleteUserOptions.lorryAddr)
if err != nil {
fmt.Printf("new lorry http client failed: %v\n", err)
return
}

err = lorryClient.DeleteUser(context.TODO(), deleteUserOptions.userName)
if err != nil {
fmt.Printf("delete user failed: %v\n", err)
return
}
fmt.Printf("delete user success")
},
}

func init() {
DeleteUserCmd.Flags().StringVarP(&deleteUserOptions.userName, "username", "", "", "The name of user to delete")
DeleteUserCmd.Flags().StringVarP(&deleteUserOptions.lorryAddr, "lorry-addr", "", "http://localhost:3501/v1.0/", "The addr of lorry to request")
DeleteUserCmd.Flags().BoolP("help", "h", false, "Print this help message")

RootCmd.AddCommand(DeleteUserCmd)
}

0 comments on commit 0abc7ff

Please sign in to comment.