Skip to content

Commit

Permalink
add create 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 3865614 commit 57c2d5c
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lorry/client/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func (cli *HTTPClient) GetRole(ctx context.Context) (string, error) {
return role.(string), nil
}

func (cli *HTTPClient) CreateUser(ctx context.Context, userName, password string) error {
parameters := map[string]any{
"userName": userName,
"password": password,
}
req := &httpserver.Request{Parameters: parameters}
_, err := cli.Request(ctx, string(CreateUserOp), 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 Expand Up @@ -276,6 +286,7 @@ func GetMapKeyFromRequest(req *http.Request) string {
if err != nil {
return ""
}
req.Body = io.NopCloser(bytes.NewReader(all))
buf.Write(all)
}
keys := make([]string, 0, len(req.Header))
Expand Down
21 changes: 21 additions & 0 deletions lorry/client/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ var _ = Describe("Lorry HTTP Client", func() {
})
})

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

BeforeEach(func() {
lorryClient, _ = NewHTTPClientWithPod(pod)
Expect(lorryClient).ShouldNot(BeNil())
lorryClient.ReconcileTimeout = 100 * time.Second
})

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

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

Context("list users", func() {
var lorryClient *HTTPClient
var users []models.UserInfo
Expand Down
2 changes: 1 addition & 1 deletion lorry/client/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

var (
lorryHTTPPort = 3501
lorryHTTPPort = 35011
dbManager engines.DBManager
mockDBManager *engines.MockDBManager
dcsStore dcs.DCS
Expand Down
66 changes: 66 additions & 0 deletions lorry/ctl/createuser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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"
)

var (
lorryAddr2 string
userName string
password string
)

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

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

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().BoolP("help", "h", false, "Print this help message")

RootCmd.AddCommand(CreateUserCmd)
}
8 changes: 7 additions & 1 deletion lorry/ctl/listsystemaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ lorryctl listsystemaccounts
fmt.Printf("list systemaccounts failed: %v\n", err)
return
}
fmt.Printf("list systemaccounts:\n %v\n", systemaccounts)
fmt.Printf("list systemaccounts:\n")
for _, m := range systemaccounts {
fmt.Println("-------------------------")
for k, v := range m {
fmt.Printf("%s: %v\n", k, v)
}
}
},
}

Expand Down
8 changes: 7 additions & 1 deletion lorry/ctl/listusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ lorryctl listusers
fmt.Printf("list users failed: %v\n", err)
return
}
fmt.Printf("list users : %v\n", users)
fmt.Printf("list users:\n")
for _, m := range users {
fmt.Println("-------------------------")
for k, v := range m {
fmt.Printf("%s: %v\n", k, v)
}
}
},
}

Expand Down
3 changes: 2 additions & 1 deletion lorry/operations/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package user

import (
"context"
"strings"

"github.com/go-logr/logr"
"github.com/pkg/errors"
Expand All @@ -41,7 +42,7 @@ type CreateUser struct {
var createUser operations.Operation = &CreateUser{}

func init() {
err := operations.Register("createuser", createUser)
err := operations.Register(strings.ToLower(string(util.CreateUserOp)), createUser)
if err != nil {
panic(err.Error())
}
Expand Down

0 comments on commit 57c2d5c

Please sign in to comment.