github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/create/create_user.go (about)

     1  package create
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/create/options"
     8  
     9  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    10  
    11  	"github.com/olli-ai/jx/v2/pkg/users"
    12  
    13  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
    14  	"github.com/jenkins-x/jx-logging/pkg/log"
    15  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    16  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    17  	"github.com/olli-ai/jx/v2/pkg/kube"
    18  	"github.com/olli-ai/jx/v2/pkg/util"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  const (
    23  	optionLogin                = "login"
    24  	optionCreateServiceAccount = "create-service-account"
    25  )
    26  
    27  var (
    28  	createUserLong = templates.LongDesc(`
    29  		Creates a user
    30  `)
    31  
    32  	createUserExample = templates.Examples(`
    33  		# Create a user
    34  		jx create user -e "user@email.com" --login username --name username
    35  	`)
    36  )
    37  
    38  // CreateUserOptions the options for the create spring command
    39  type CreateUserOptions struct {
    40  	options.CreateOptions
    41  	UserSpec v1.UserDetails
    42  }
    43  
    44  // NewCmdCreateUser creates a command object for the "create" command
    45  func NewCmdCreateUser(commonOpts *opts.CommonOptions) *cobra.Command {
    46  	options := &CreateUserOptions{
    47  		CreateOptions: options.CreateOptions{
    48  			CommonOptions: commonOpts,
    49  		},
    50  	}
    51  
    52  	cmd := &cobra.Command{
    53  		Use:     "user",
    54  		Short:   "Create a new User which is then provisioned by the user controller",
    55  		Aliases: []string{"env"},
    56  		Long:    createUserLong,
    57  		Example: createUserExample,
    58  		Run: func(cmd *cobra.Command, args []string) {
    59  			options.Cmd = cmd
    60  			options.Args = args
    61  			err := options.Run()
    62  			helper.CheckErr(err)
    63  		},
    64  	}
    65  
    66  	cmd.Flags().StringVarP(&options.UserSpec.Login, optionLogin, "l", "", "The user login name")
    67  	cmd.Flags().StringVarP(&options.UserSpec.Name, "name", "n", "", "The textual full name of the user")
    68  	cmd.Flags().StringVarP(&options.UserSpec.Email, "email", "e", "", "The users email address")
    69  	cmd.Flags().BoolVarP(&options.UserSpec.ExternalUser, optionCreateServiceAccount, "s", false, "Enable ServiceAccount for this external user")
    70  
    71  	return cmd
    72  }
    73  
    74  // Run implements the command
    75  func (o *CreateUserOptions) Run() error {
    76  	err := o.RegisterUserCRD()
    77  	if err != nil {
    78  		return err
    79  	}
    80  	err = o.RegisterEnvironmentRoleBindingCRD()
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	kubeClient, err := o.KubeClient()
    86  	if err != nil {
    87  		return err
    88  	}
    89  	jxClient, devNs, err := o.JXClientAndDevNamespace()
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	ns, err := kube.GetAdminNamespace(kubeClient, devNs)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	_, names, err := users.GetUsers(jxClient, ns)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	spec := &o.UserSpec
   105  	login := spec.Login
   106  	if login == "" {
   107  		args := o.Args
   108  		if len(args) > 0 {
   109  			login = args[0]
   110  		}
   111  	}
   112  	if login == "" {
   113  		return util.MissingOption(optionLogin)
   114  	}
   115  
   116  	if util.StringArrayIndex(names, login) >= 0 {
   117  		return fmt.Errorf("The User %s already exists!", login)
   118  	}
   119  
   120  	name := spec.Name
   121  	if name == "" {
   122  		name = strings.Title(login)
   123  	}
   124  	user := users.CreateUser(ns, login, name, spec.Email)
   125  	user.Spec.ExternalUser = spec.ExternalUser
   126  	_, err = jxClient.JenkinsV1().Users(ns).Create(user)
   127  	if err != nil {
   128  		return fmt.Errorf("failed to create User %s: %s", login, err)
   129  	}
   130  	log.Logger().Infof("Created User: %s", util.ColorInfo(login))
   131  	log.Logger().Infof("You can configure the roles for the user via: %s", util.ColorInfo(fmt.Sprintf("jx edit userrole %s", login)))
   132  	return nil
   133  
   134  }