github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/user/create.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package user 21 22 import ( 23 "context" 24 "strings" 25 26 "github.com/go-logr/logr" 27 "github.com/pkg/errors" 28 ctrl "sigs.k8s.io/controller-runtime" 29 30 "github.com/1aal/kubeblocks/pkg/lorry/engines" 31 "github.com/1aal/kubeblocks/pkg/lorry/engines/register" 32 "github.com/1aal/kubeblocks/pkg/lorry/operations" 33 "github.com/1aal/kubeblocks/pkg/lorry/util" 34 ) 35 36 type CreateUser struct { 37 operations.Base 38 dbManager engines.DBManager 39 logger logr.Logger 40 } 41 42 var createUser operations.Operation = &CreateUser{} 43 44 func init() { 45 err := operations.Register(strings.ToLower(string(util.CreateUserOp)), createUser) 46 if err != nil { 47 panic(err.Error()) 48 } 49 } 50 51 func (s *CreateUser) Init(ctx context.Context) error { 52 dbManager, err := register.GetDBManager() 53 if err != nil { 54 return errors.Wrap(err, "get manager failed") 55 } 56 s.dbManager = dbManager 57 s.logger = ctrl.Log.WithName("CreateUser") 58 return nil 59 } 60 61 func (s *CreateUser) IsReadonly(ctx context.Context) bool { 62 return false 63 } 64 65 func (s *CreateUser) PreCheck(ctx context.Context, req *operations.OpsRequest) error { 66 userInfo, err := UserInfoParser(req) 67 if err != nil { 68 return err 69 } 70 71 return userInfo.UserNameAndPasswdValidator() 72 } 73 74 func (s *CreateUser) Do(ctx context.Context, req *operations.OpsRequest) (*operations.OpsResponse, error) { 75 userInfo, _ := UserInfoParser(req) 76 resp := operations.NewOpsResponse(util.CreateUserOp) 77 78 err := s.dbManager.CreateUser(ctx, userInfo.UserName, userInfo.Password) 79 if err != nil { 80 s.logger.Info("executing CreateUser error", "error", err) 81 return resp, err 82 } 83 84 return resp.WithSuccess("") 85 }