-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuser.go
More file actions
196 lines (153 loc) · 4.88 KB
/
user.go
File metadata and controls
196 lines (153 loc) · 4.88 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package create
import (
"encoding/json"
"io"
"os"
"github.com/ibm-verify/verifyctl/pkg/cmd/resource"
"github.com/ibm-verify/verifyctl/pkg/config"
"gopkg.in/yaml.v3"
"github.com/ibm-verify/verify-sdk-go/pkg/config/directory"
cmdutil "github.com/ibm-verify/verifyctl/pkg/util/cmd"
"github.com/ibm-verify/verifyctl/pkg/util/templates"
"github.com/spf13/cobra"
contextx "github.com/ibm-verify/verify-sdk-go/pkg/core/context"
errorsx "github.com/ibm-verify/verify-sdk-go/pkg/core/errors"
)
const (
userUsage = "user [options]"
userMessagePrefix = "CreateUser"
userEntitlements = "Manage users"
userResourceName = "user"
)
var (
userShortDesc = cmdutil.TranslateShortDesc(userMessagePrefix, "Additional options to create a user.")
userLongDesc = templates.LongDesc(cmdutil.TranslateLongDesc(userMessagePrefix, `
Additional options to create a user.
Resources managed on Verify have specific entitlements, so ensure that the application or API client used
with the 'auth' command is configured with the appropriate entitlements.
An empty resource file can be generated using:
verifyctl create user --boilerplate
You can identify the entitlement required by running:
verifyctl create user --entitlements`))
userExamples = templates.Examples(cmdutil.TranslateExamples(userMessagePrefix, `
# Create an empty user resource. This can be piped into a file.
verifyctl create user --boilerplate
# Create a user using a YAML file.
verifyctl create -f "user.yaml"
# Create a user using a JSON file.
verifyctl create -f "user.json"`))
)
type userOptions struct {
options
config *config.CLIConfig
}
func newUserCommand(config *config.CLIConfig, streams io.ReadWriter) *cobra.Command {
o := &userOptions{
config: config,
}
cmd := &cobra.Command{
Use: userUsage,
Short: userShortDesc,
Long: userLongDesc,
Example: userExamples,
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.ExitOnError(cmd, o.Complete(cmd, args))
cmdutil.ExitOnError(cmd, o.Validate(cmd, args))
cmdutil.ExitOnError(cmd, o.Run(cmd, args))
},
}
cmd.SetOut(streams)
cmd.SetErr(streams)
cmd.SetIn(streams)
o.AddFlags(cmd)
return cmd
}
func (o *userOptions) AddFlags(cmd *cobra.Command) {
o.addCommonFlags(cmd, userResourceName)
cmd.Flags().StringVarP(&o.file, "file", "f", "", "Path to the JSON file containing user data.")
}
func (o *userOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
func (o *userOptions) Validate(cmd *cobra.Command, args []string) error {
if o.entitlements || o.boilerplate {
return nil
}
if len(o.file) == 0 {
return errorsx.G11NError("The 'file' option is required if no other options are used.")
}
return nil
}
func (o *userOptions) Run(cmd *cobra.Command, args []string) error {
if o.entitlements {
cmdutil.WriteString(cmd, entitlementsMessage+" "+userEntitlements)
return nil
}
if o.boilerplate {
resourceObj := &resource.ResourceObject{
Kind: resource.ResourceTypePrefix + "User",
APIVersion: "2.0",
Data: &directory.User{},
}
cmdutil.WriteAsYAML(cmd, resourceObj, cmd.OutOrStdout())
return nil
}
_, err := o.config.SetAuthToContext(cmd.Context())
if err != nil {
return err
}
return o.createUser(cmd)
}
func (o *userOptions) createUser(cmd *cobra.Command) error {
ctx := cmd.Context()
vc := contextx.GetVerifyContext(ctx)
// get the contents of the file
b, err := os.ReadFile(o.file)
if err != nil {
vc.Logger.Errorf("Unable to read file; filename=%s, err=%v", o.file, err)
return err
}
// create user with data
return o.createUserWithData(cmd, b)
}
func (o *userOptions) createUserWithData(cmd *cobra.Command, data []byte) error {
ctx := cmd.Context()
vc := contextx.GetVerifyContext(ctx)
// unmarshal to user
user := &directory.User{}
if err := yaml.Unmarshal(data, &user); err != nil {
vc.Logger.Errorf("unable to unmarshal the user; err=%v", err)
return err
}
client := directory.NewUserClient()
resourceURI, err := client.CreateUser(ctx, user)
if err != nil {
return err
}
cmdutil.WriteString(cmd, "Resource created: "+resourceURI)
return nil
}
func (o *userOptions) createUserFromDataMap(cmd *cobra.Command, data map[string]interface{}) error {
ctx := cmd.Context()
vc := contextx.GetVerifyContext(ctx)
// unmarshal to user
user := &directory.User{}
b, err := json.Marshal(data)
if err != nil {
vc.Logger.Errorf("failed to marshal the data map; err=%v", err)
return err
}
if err := json.Unmarshal(b, user); err != nil {
vc.Logger.Errorf("unable to unmarshal to an user; err=%v", err)
return err
}
client := directory.NewUserClient()
resourceURI, err := client.CreateUser(ctx, user)
if err != nil {
vc.Logger.Errorf("unable to create the user; err=%v, user=%+v", err, user)
return err
}
cmdutil.WriteString(cmd, "Resource created: "+resourceURI)
return nil
}