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

     1  package edit
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     8  
     9  	"github.com/olli-ai/jx/v2/pkg/users"
    10  
    11  	"github.com/jenkins-x/jx-logging/pkg/log"
    12  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    14  	"github.com/olli-ai/jx/v2/pkg/kube"
    15  	"github.com/olli-ai/jx/v2/pkg/util"
    16  	"github.com/pkg/errors"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  const (
    21  	optionLogin = "login"
    22  )
    23  
    24  var (
    25  	editUserRoleLong = templates.LongDesc(`
    26  		Edits the Roles associated with a User
    27  `)
    28  
    29  	editUserRoleExample = templates.Examples(`
    30  		# Prompt the CLI to pick a User from the list then select which Roles to update for the user
    31  		jx edit userrole
    32  
    33  
    34  		# Update a user to a given set of roles
    35  		jx edit userrole --l mylogin -r foo -r bar
    36  "
    37  	`)
    38  )
    39  
    40  // EditUserRoleOptions the options for the create spring command
    41  type EditUserRoleOptions struct {
    42  	EditOptions
    43  
    44  	Login string
    45  	Roles []string
    46  }
    47  
    48  // NewCmdEditUserRole creates a command object for the "create" command
    49  func NewCmdEditUserRole(commonOpts *opts.CommonOptions) *cobra.Command {
    50  	options := &EditUserRoleOptions{
    51  		EditOptions: EditOptions{
    52  			CommonOptions: commonOpts,
    53  		},
    54  	}
    55  
    56  	cmd := &cobra.Command{
    57  		Use:     "userroles",
    58  		Short:   "Edits the roles associated with a User",
    59  		Aliases: []string{"userrole"},
    60  		Long:    editUserRoleLong,
    61  		Example: editUserRoleExample,
    62  		Run: func(cmd *cobra.Command, args []string) {
    63  			options.Cmd = cmd
    64  			options.Args = args
    65  			err := options.Run()
    66  			helper.CheckErr(err)
    67  		},
    68  	}
    69  
    70  	cmd.Flags().StringVarP(&options.Login, optionLogin, "l", "", "The user login name")
    71  	cmd.Flags().StringArrayVarP(&options.Roles, "role", "r", []string{}, "The roles to set on a user")
    72  
    73  	return cmd
    74  }
    75  
    76  // Run implements the command
    77  func (o *EditUserRoleOptions) Run() error {
    78  	err := o.RegisterUserCRD()
    79  	if err != nil {
    80  		return err
    81  	}
    82  	err = o.RegisterEnvironmentRoleBindingCRD()
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	kubeClient, err := o.KubeClient()
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	jxClient, teamNs, adminNs, err := o.JXClientDevAndAdminNamespace()
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	users, names, err := users.GetUsers(jxClient, adminNs)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	name := o.Login
   103  	if name == "" {
   104  		args := o.Args
   105  		if len(args) > 0 {
   106  			name = args[0]
   107  		}
   108  	}
   109  	if name == "" {
   110  		if o.BatchMode {
   111  			return util.MissingOption(optionLogin)
   112  		}
   113  		name, err = util.PickName(names, "Pick the user to edit", "", o.GetIOFileHandles())
   114  		if err != nil {
   115  			return err
   116  		}
   117  		if name == "" {
   118  			return util.MissingOption(optionLogin)
   119  		}
   120  	}
   121  	user := users[name]
   122  	if user == nil {
   123  		return fmt.Errorf("Could not find user %s", name)
   124  	}
   125  	userKind := user.SubjectKind()
   126  
   127  	roles, roleNames, err := kube.GetTeamRoles(kubeClient, teamNs)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	if len(roleNames) == 0 {
   133  		log.Logger().Warnf("No Team roles for team %s", teamNs)
   134  		return nil
   135  	}
   136  
   137  	userRoles := o.Roles
   138  	if !o.BatchMode && len(userRoles) == 0 {
   139  
   140  		currentRoles, err := kube.GetUserRoles(kubeClient, jxClient, teamNs, userKind, name)
   141  		if err != nil {
   142  			return err
   143  		}
   144  		userRoles, err = util.PickNamesWithDefaults(roleNames, currentRoles, "Roles for user: "+name, "", o.GetIOFileHandles())
   145  		if err != nil {
   146  			return err
   147  		}
   148  	}
   149  
   150  	rolesText := strings.Join(userRoles, ", ")
   151  	log.Logger().Infof("updating user %s for roles %s", name, rolesText)
   152  
   153  	err = kube.UpdateUserRoles(kubeClient, jxClient, teamNs, userKind, name, userRoles, roles)
   154  	if err != nil {
   155  		return errors.Wrapf(err, "Failed to update user roles for user %s kind %s and roles %s", name, userKind, rolesText)
   156  	}
   157  	log.Logger().Infof("Updated roles for user: %s kind: %s roles: %s", util.ColorInfo(name), util.ColorInfo(userKind), util.ColorInfo(rolesText))
   158  	return nil
   159  
   160  }