forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
97 lines (78 loc) · 2.77 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package create
import (
"fmt"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
userv1 "github.com/openshift/api/user/v1"
userv1client "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
)
const UserRecommendedName = "user"
var (
userLong = templates.LongDesc(`
This command can be used to create a user object.
Typically, users are created automatically during login. If automatic
creation is disabled (by using the "lookup" mapping method), users must
be created manually.
Corresponding identity and useridentitymapping objects must also be created
to allow logging in as the created user.`)
userExample = templates.Examples(`
# Create a user with the username "ajones" and the display name "Adam Jones"
%[1]s ajones --full-name="Adam Jones"`)
)
type CreateUserOptions struct {
CreateSubcommandOptions *CreateSubcommandOptions
FullName string
UserClient userv1client.UsersGetter
}
// NewCmdCreateUser is a macro command to create a new user
func NewCmdCreateUser(name, fullName string, f genericclioptions.RESTClientGetter, streams genericclioptions.IOStreams) *cobra.Command {
o := &CreateUserOptions{
CreateSubcommandOptions: NewCreateSubcommandOptions(streams),
}
cmd := &cobra.Command{
Use: name + " USERNAME",
Short: "Manually create a user (only needed if automatic creation is disabled).",
Long: userLong,
Example: fmt.Sprintf(userExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(cmd, f, args))
cmdutil.CheckErr(o.Run())
},
}
cmd.Flags().StringVar(&o.FullName, "full-name", o.FullName, "Display name of the user")
o.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
cmdutil.AddDryRunFlag(cmd)
return cmd
}
func (o *CreateUserOptions) Complete(cmd *cobra.Command, f genericclioptions.RESTClientGetter, args []string) error {
clientConfig, err := f.ToRESTConfig()
if err != nil {
return err
}
o.UserClient, err = userv1client.NewForConfig(clientConfig)
if err != nil {
return err
}
return o.CreateSubcommandOptions.Complete(f, cmd, args)
}
func (o *CreateUserOptions) Run() error {
user := &userv1.User{
// this is ok because we know exactly how we want to be serialized
TypeMeta: metav1.TypeMeta{APIVersion: userv1.SchemeGroupVersion.String(), Kind: "User"},
ObjectMeta: metav1.ObjectMeta{
Name: o.CreateSubcommandOptions.Name,
},
FullName: o.FullName,
}
var err error
if !o.CreateSubcommandOptions.DryRun {
user, err = o.UserClient.Users().Create(user)
if err != nil {
return err
}
}
return o.CreateSubcommandOptions.Printer.PrintObj(user, o.CreateSubcommandOptions.Out)
}