diff --git a/lorry/client/httpclient.go b/lorry/client/httpclient.go index d480634baff..3194d29087d 100644 --- a/lorry/client/httpclient.go +++ b/lorry/client/httpclient.go @@ -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) diff --git a/lorry/client/httpclient_test.go b/lorry/client/httpclient_test.go index a0cc740044a..3229d5dc334 100644 --- a/lorry/client/httpclient_test.go +++ b/lorry/client/httpclient_test.go @@ -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() { @@ -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 diff --git a/lorry/ctl/createuser.go b/lorry/ctl/createuser.go index 224b5050ca9..e169f4627f8 100644 --- a/lorry/ctl/createuser.go +++ b/lorry/ctl/createuser.go @@ -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", @@ -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) diff --git a/lorry/ctl/deleteuser.go b/lorry/ctl/deleteuser.go new file mode 100644 index 00000000000..c9d67285482 --- /dev/null +++ b/lorry/ctl/deleteuser.go @@ -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 . +*/ + +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) +}