github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/user/describe.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 "encoding/json" 25 "fmt" 26 27 "github.com/go-logr/logr" 28 "github.com/pkg/errors" 29 ctrl "sigs.k8s.io/controller-runtime" 30 31 "github.com/1aal/kubeblocks/pkg/lorry/engines" 32 "github.com/1aal/kubeblocks/pkg/lorry/engines/models" 33 "github.com/1aal/kubeblocks/pkg/lorry/engines/register" 34 "github.com/1aal/kubeblocks/pkg/lorry/operations" 35 "github.com/1aal/kubeblocks/pkg/lorry/util" 36 ) 37 38 type DescribeUser struct { 39 operations.Base 40 dbManager engines.DBManager 41 logger logr.Logger 42 } 43 44 var describeUser operations.Operation = &DescribeUser{} 45 46 func init() { 47 err := operations.Register("describeuser", describeUser) 48 if err != nil { 49 panic(err.Error()) 50 } 51 } 52 53 func (s *DescribeUser) Init(ctx context.Context) error { 54 dbManager, err := register.GetDBManager() 55 if err != nil { 56 return errors.Wrap(err, "get manager failed") 57 } 58 s.dbManager = dbManager 59 s.logger = ctrl.Log.WithName("describeUser") 60 return nil 61 } 62 63 func (s *DescribeUser) IsReadonly(ctx context.Context) bool { 64 return true 65 } 66 67 func (s *DescribeUser) PreCheck(ctx context.Context, req *operations.OpsRequest) error { 68 userInfo, err := UserInfoParser(req) 69 if err != nil { 70 return err 71 } 72 73 return userInfo.UserNameValidator() 74 } 75 76 func (s *DescribeUser) Do(ctx context.Context, req *operations.OpsRequest) (*operations.OpsResponse, error) { 77 userInfo, _ := UserInfoParser(req) 78 resp := operations.NewOpsResponse(util.DescribeUserOp) 79 80 result, err := s.dbManager.DescribeUser(ctx, userInfo.UserName) 81 if err != nil { 82 s.logger.Info("executing describeUser error", "error", err) 83 return resp, err 84 } 85 86 resp.Data["user"] = result 87 return resp.WithSuccess("") 88 } 89 90 func UserInfoParser(req *operations.OpsRequest) (*models.UserInfo, error) { 91 user := &models.UserInfo{} 92 if req == nil || req.Parameters == nil { 93 return nil, fmt.Errorf("no Parameters provided") 94 } else if jsonData, err := json.Marshal(req.Parameters); err != nil { 95 return nil, err 96 } else if err = json.Unmarshal(jsonData, user); err != nil { 97 return nil, err 98 } 99 return user, nil 100 }