pkg/cmd/admin/groups/new.go
0e9076a3
 package groups
 
 import (
 	"errors"
 	"fmt"
 	"io"
 
3dc55b2d
 	"k8s.io/kubernetes/pkg/kubectl"
83c702b4
 	kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
3dc55b2d
 	"k8s.io/kubernetes/pkg/runtime"
3dd75654
 	"k8s.io/kubernetes/pkg/util/sets"
0e9076a3
 
 	"github.com/spf13/cobra"
 
 	"github.com/openshift/origin/pkg/client"
6267dded
 	"github.com/openshift/origin/pkg/cmd/templates"
0e9076a3
 	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
 	userapi "github.com/openshift/origin/pkg/user/api"
 )
 
6267dded
 const NewGroupRecommendedName = "new"
0e9076a3
 
6267dded
 var (
 	newLong = templates.LongDesc(`
 		Create a new group.
0e9076a3
 
6267dded
 		This command will create a new group with an optional list of users.`)
0e9076a3
 
6267dded
 	newExample = templates.Examples(`
 		# Add a group with no users
 	  %[1]s my-group
3dc55b2d
 
6267dded
 	  # Add a group with two users
 	  %[1]s my-group user1 user2
 
 	  # Add a group with one user and shorter output
 	  %[1]s my-group user1 -o name`)
0e9076a3
 )
 
 type NewGroupOptions struct {
 	GroupClient client.GroupInterface
 
 	Group string
 	Users []string
3dc55b2d
 
 	Out     io.Writer
 	Printer kubectl.ResourcePrinterFunc
0e9076a3
 }
 
 func NewCmdNewGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
3dc55b2d
 	options := &NewGroupOptions{Out: out}
0e9076a3
 
 	cmd := &cobra.Command{
 		Use:     name + " GROUP [USER ...]",
 		Short:   "Create a new group",
 		Long:    newLong,
 		Example: fmt.Sprintf(newExample, fullName),
 		Run: func(cmd *cobra.Command, args []string) {
3dc55b2d
 			if err := options.Complete(f, cmd, args); err != nil {
0e9076a3
 				kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
 			}
3dc55b2d
 			kcmdutil.CheckErr(options.Validate())
0e9076a3
 			kcmdutil.CheckErr(options.AddGroup())
 		},
 	}
 
3dc55b2d
 	kcmdutil.AddPrinterFlags(cmd)
 
0e9076a3
 	return cmd
 }
 
3dc55b2d
 func (o *NewGroupOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
0e9076a3
 	if len(args) < 1 {
 		return errors.New("You must specify at least one argument: GROUP [USER ...]")
 	}
 
 	o.Group = args[0]
 	if len(args) > 1 {
 		o.Users = append(o.Users, args[1:]...)
 	}
 
 	osClient, _, err := f.Clients()
 	if err != nil {
 		return err
 	}
 
 	o.GroupClient = osClient.Groups()
3dc55b2d
 
 	outputFormat := kcmdutil.GetFlagString(cmd, "output")
 	templateFile := kcmdutil.GetFlagString(cmd, "template")
 	noHeaders := kcmdutil.GetFlagBool(cmd, "no-headers")
 	printer, _, err := kubectl.GetPrinter(outputFormat, templateFile, noHeaders)
 	if err != nil {
 		return err
 	}
 
 	if printer != nil {
 		o.Printer = printer.PrintObj
 	} else {
 		o.Printer = func(obj runtime.Object, out io.Writer) error {
 			mapper, _ := f.Object(false)
 			return f.PrintObject(cmd, mapper, obj, out)
 		}
 	}
 
 	return nil
 }
 
 func (o *NewGroupOptions) Validate() error {
 	if len(o.Group) == 0 {
 		return fmt.Errorf("Group is required")
 	}
 	if o.GroupClient == nil {
 		return fmt.Errorf("GroupClient is required")
 	}
 	if o.Out == nil {
 		return fmt.Errorf("Out is required")
 	}
 	if o.Printer == nil {
 		return fmt.Errorf("Printer is required")
 	}
 
0e9076a3
 	return nil
 }
 
 func (o *NewGroupOptions) AddGroup() error {
 	group := &userapi.Group{}
 	group.Name = o.Group
 
3dd75654
 	usedNames := sets.String{}
0e9076a3
 	for _, user := range o.Users {
 		if usedNames.Has(user) {
 			continue
 		}
 		usedNames.Insert(user)
 
 		group.Users = append(group.Users, user)
 	}
 
3dc55b2d
 	actualGroup, err := o.GroupClient.Create(group)
 	if err != nil {
 		return err
 	}
 
 	return o.Printer(actualGroup, o.Out)
0e9076a3
 }