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

     1  package create
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/create/options"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     9  
    10  	"github.com/jenkins-x/jx-logging/pkg/log"
    11  	"github.com/olli-ai/jx/v2/pkg/kube"
    12  	"github.com/olli-ai/jx/v2/pkg/util"
    13  	"github.com/spf13/cobra"
    14  
    15  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    16  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    17  )
    18  
    19  var (
    20  	createTeamLong = templates.LongDesc(`
    21  		Creates a Team
    22  `)
    23  
    24  	createTeamExample = templates.Examples(`
    25  		# Create a new pending Team which can then be provisioned
    26  		jx create team myname
    27  "
    28  	`)
    29  )
    30  
    31  // CreateTeamOptions the options for the create spring command
    32  type CreateTeamOptions struct {
    33  	options.CreateOptions
    34  
    35  	Name    string
    36  	Members []string
    37  }
    38  
    39  // NewCmdCreateTeam creates a command object for the "create" command
    40  func NewCmdCreateTeam(commonOpts *opts.CommonOptions) *cobra.Command {
    41  	options := &CreateTeamOptions{
    42  		CreateOptions: options.CreateOptions{
    43  			CommonOptions: commonOpts,
    44  		},
    45  	}
    46  
    47  	cmd := &cobra.Command{
    48  		Use:     "team",
    49  		Short:   "Create a new Team which is then provisioned later on",
    50  		Aliases: []string{"env"},
    51  		Long:    createTeamLong,
    52  		Example: createTeamExample,
    53  		Run: func(cmd *cobra.Command, args []string) {
    54  			options.Cmd = cmd
    55  			options.Args = args
    56  			err := options.Run()
    57  			helper.CheckErr(err)
    58  		},
    59  	}
    60  
    61  	cmd.Flags().StringVarP(&options.Name, optionName, "n", "", "The name of the new Team. Should be all lower case and no special characters other than '-'")
    62  	cmd.Flags().StringArrayVarP(&options.Members, "member", "m", []string{}, "The usernames of the members to add to the Team")
    63  
    64  	return cmd
    65  }
    66  
    67  // Run implements the command
    68  func (o *CreateTeamOptions) Run() error {
    69  	kubeClient, err := o.KubeClient()
    70  	if err != nil {
    71  		return err
    72  	}
    73  	err = o.RegisterTeamCRD()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	jxClient, devNs, err := o.JXClientAndDevNamespace()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	ns, err := kube.GetAdminNamespace(kubeClient, devNs)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	_, names, err := kube.GetPendingTeams(jxClient, ns)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	name := o.Name
    94  	if name == "" {
    95  		args := o.Args
    96  		if len(args) > 0 {
    97  			name = args[0]
    98  		}
    99  	}
   100  	if name == "" {
   101  		return util.MissingOption(optionName)
   102  	}
   103  
   104  	if util.StringArrayIndex(names, name) >= 0 {
   105  		return fmt.Errorf("The Team %s already exists!", name)
   106  	}
   107  
   108  	// TODO configure other properties?
   109  	team := kube.CreateTeam(ns, name, o.Members)
   110  	_, err = jxClient.JenkinsV1().Teams(ns).Create(team)
   111  	if err != nil {
   112  		return fmt.Errorf("Failed to create Team %s: %s", name, err)
   113  	}
   114  	log.Logger().Infof("Created Team: %s", util.ColorInfo(name))
   115  	return nil
   116  }