github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/accounts/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 accounts
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  
    26  	"github.com/sethvargo/go-password/password"
    27  	"github.com/spf13/cobra"
    28  	"k8s.io/cli-runtime/pkg/genericiooptions"
    29  	"k8s.io/klog/v2"
    30  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    31  
    32  	"github.com/1aal/kubeblocks/pkg/lorry/client"
    33  )
    34  
    35  type CreateUserOptions struct {
    36  	*AccountBaseOptions
    37  	userName string
    38  	password string
    39  }
    40  
    41  func NewCreateUserOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *CreateUserOptions {
    42  	return &CreateUserOptions{
    43  		AccountBaseOptions: NewAccountBaseOptions(f, streams),
    44  	}
    45  }
    46  
    47  func (o *CreateUserOptions) AddFlags(cmd *cobra.Command) {
    48  	o.AccountBaseOptions.AddFlags(cmd)
    49  	cmd.Flags().StringVar(&o.userName, "name", "", "Required. Specify the name of user, which must be unique.")
    50  	cmd.Flags().StringVarP(&o.password, "password", "p", "", "Optional. Specify the password of user. The default value is empty, which means a random password will be generated.")
    51  	_ = cmd.MarkFlagRequired("name")
    52  	// TODO:@shanshan add expire flag if needed
    53  	// cmd.Flags().DurationVar(&o.info.ExpireAt, "expire", 0, "Optional. Specify the expired time of password. The default value is 0, which means the user will never expire.")
    54  }
    55  
    56  func (o CreateUserOptions) Validate(args []string) error {
    57  	if err := o.AccountBaseOptions.Validate(args); err != nil {
    58  		return err
    59  	}
    60  	if len(o.userName) == 0 {
    61  		return errMissingUserName
    62  	}
    63  	return nil
    64  }
    65  
    66  func (o *CreateUserOptions) Complete(f cmdutil.Factory) error {
    67  	var err error
    68  	if err = o.AccountBaseOptions.Complete(f); err != nil {
    69  		return err
    70  	}
    71  	// complete other options
    72  	if len(o.password) == 0 {
    73  		o.password, _ = password.Generate(10, 2, 0, false, false)
    74  	}
    75  	return err
    76  }
    77  
    78  func (o *CreateUserOptions) Run(cmd *cobra.Command, f cmdutil.Factory, streams genericiooptions.IOStreams) error {
    79  	klog.V(1).Info(fmt.Sprintf("connect to cluster %s, component %s, instance %s\n", o.ClusterName, o.ComponentName, o.PodName))
    80  	lorryClient, err := client.NewK8sExecClientWithPod(o.Pod)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	err = lorryClient.CreateUser(context.Background(), o.userName, o.password)
    86  	if err != nil {
    87  		o.printGeneralInfo("fail", err.Error())
    88  		return err
    89  	}
    90  	o.printGeneralInfo("success", "")
    91  	return nil
    92  }