forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
113 lines (91 loc) · 2.74 KB
/
update.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
/*
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package user
import (
"context"
"flag"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/ssoadmin"
"github.com/vmware/govmomi/ssoadmin/types"
)
type update struct {
userDetails
}
func init() {
cli.Register("sso.user.update", &update{})
}
func (cmd *update) Description() string {
return `Update SSO users.
Examples:
govc sso.user.update -C "$(cat cert.pem)" NAME
govc sso.user.update -p password NAME`
}
// merge uses the current value of a field if the corresponding flag was not set.
// Otherwise the API call would set the current fields to empty strings.
func merge(src *string, current string) {
if *src == "" {
*src = current
}
}
func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}
id := f.Arg(0)
return withClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
user, err := c.FindUser(ctx, id)
if err != nil {
return err
}
if user.Kind == "person" {
current, cerr := c.FindPersonUser(ctx, id)
if cerr != nil {
return cerr
}
merge(&cmd.AdminPersonDetails.Description, current.Details.Description)
merge(&cmd.FirstName, current.Details.FirstName)
merge(&cmd.LastName, current.Details.LastName)
merge(&cmd.EmailAddress, current.Details.EmailAddress)
if err := c.UpdatePersonUser(ctx, id, cmd.AdminPersonDetails); err != nil {
return err
}
if cmd.password != "" {
return c.ResetPersonPassword(ctx, id, cmd.password)
}
return nil
}
if cmd.actas != nil {
action := c.GrantWSTrustRole
if *cmd.actas == false {
action = c.RevokeWSTrustRole
}
if _, err := action(ctx, user.Id, types.RoleActAsUser); err != nil {
return err
}
}
if cmd.role != "" {
if _, err := c.SetRole(ctx, user.Id, cmd.role); err != nil {
return err
}
}
cmd.solution.Certificate = cmd.Certificate()
cmd.solution.Description = cmd.AdminPersonDetails.Description
current, cerr := c.FindSolutionUser(ctx, id)
if cerr != nil {
return cerr
}
merge(&cmd.solution.Certificate, current.Details.Certificate)
merge(&cmd.solution.Description, current.Details.Description)
return c.UpdateSolutionUser(ctx, id, cmd.solution)
})
}