/
newUserCmd.auto.go
177 lines (156 loc) · 4.99 KB
/
newUserCmd.auto.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
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
// Code generated from specification version 1.0.0: DO NOT EDIT
package cmd
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/reubenmiller/go-c8y-cli/pkg/mapbuilder"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
type newUserCmd struct {
*baseCmd
}
func newNewUserCmd() *newUserCmd {
ccmd := &newUserCmd{}
cmd := &cobra.Command{
Use: "create",
Short: "Create a new user within the collection",
Long: `This command can be used to grant a new user to the tenant`,
Example: `
$ c8y users create --userName "testuser1" --password "a0)8k2kld9lm,!"
Create a user
`,
PreRunE: validateCreateMode,
RunE: ccmd.newUser,
}
cmd.SilenceUsage = true
cmd.Flags().String("tenant", "", "Tenant")
cmd.Flags().String("userName", "", "User name, unique for a given domain. Max: 1000 characters")
cmd.Flags().String("firstName", "", "User first name")
cmd.Flags().String("lastName", "", "User last name")
cmd.Flags().String("phone", "", "User phone number. Format: '+[country code][number]', has to be a valid MSISDN")
cmd.Flags().String("email", "", "User email address")
cmd.Flags().Bool("enabled", false, "User activation status (true/false)")
cmd.Flags().String("password", "", "User password. Min: 6, max: 32 characters. Only Latin1 chars allowed")
cmd.Flags().Bool("sendPasswordResetEmail", false, "User activation status (true/false)")
cmd.Flags().String("customProperties", "", "Custom properties to be added to the user")
addProcessingModeFlag(cmd)
addTemplateFlag(cmd)
// Required flags
ccmd.baseCmd = newBaseCmd(cmd)
return ccmd
}
func (n *newUserCmd) newUser(cmd *cobra.Command, args []string) error {
commonOptions, err := getCommonOptions(cmd)
if err != nil {
return newUserError(fmt.Sprintf("Failed to get common options. err=%s", err))
}
// query parameters
queryValue := url.QueryEscape("")
query := url.Values{}
queryValue, err = url.QueryUnescape(query.Encode())
if err != nil {
return newSystemError("Invalid query parameter")
}
// headers
headers := http.Header{}
if cmd.Flags().Changed("processingMode") {
if v, err := cmd.Flags().GetString("processingMode"); err == nil && v != "" {
headers.Add("X-Cumulocity-Processing-Mode", v)
}
}
// form data
formData := make(map[string]io.Reader)
// body
body := mapbuilder.NewMapBuilder()
body.SetMap(getDataFlag(cmd))
if v, err := cmd.Flags().GetString("userName"); err == nil {
if v != "" {
body.Set("userName", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "userName", err))
}
if v, err := cmd.Flags().GetString("firstName"); err == nil {
if v != "" {
body.Set("firstName", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "firstName", err))
}
if v, err := cmd.Flags().GetString("lastName"); err == nil {
if v != "" {
body.Set("lastName", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "lastName", err))
}
if v, err := cmd.Flags().GetString("phone"); err == nil {
if v != "" {
body.Set("phone", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "phone", err))
}
if v, err := cmd.Flags().GetString("email"); err == nil {
if v != "" {
body.Set("email", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "email", err))
}
if cmd.Flags().Changed("enabled") {
if v, err := cmd.Flags().GetBool("enabled"); err == nil {
body.Set("enabled", v)
} else {
return newUserError("Flag does not exist")
}
}
if v, err := cmd.Flags().GetString("password"); err == nil {
if v != "" {
body.Set("password", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "password", err))
}
if cmd.Flags().Changed("sendPasswordResetEmail") {
if v, err := cmd.Flags().GetBool("sendPasswordResetEmail"); err == nil {
body.Set("sendPasswordResetEmail", v)
} else {
return newUserError("Flag does not exist")
}
}
if cmd.Flags().Changed("customProperties") {
if v, err := cmd.Flags().GetString("customProperties"); err == nil {
body.Set("customProperties", MustParseJSON(v))
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "customProperties", err))
}
}
if err := setDataTemplateFromFlags(cmd, body); err != nil {
return newUserError("Template error. ", err)
}
body.SetRequiredKeys("userName")
if err := body.Validate(); err != nil {
return newUserError("Body validation error. ", err)
}
// path parameters
pathParameters := make(map[string]string)
if v := getTenantWithDefaultFlag(cmd, "tenant", client.TenantName); v != "" {
pathParameters["tenant"] = v
}
path := replacePathParameters("user/{tenant}/users", pathParameters)
req := c8y.RequestOptions{
Method: "POST",
Path: path,
Query: queryValue,
Body: body.GetMap(),
FormData: formData,
Header: headers,
IgnoreAccept: false,
DryRun: globalFlagDryRun,
}
return processRequestAndResponse([]c8y.RequestOptions{req}, commonOptions)
}