Debian Codegraph @

govc/sso/group/update.go

raw
1 /*
2 Copyright (c) 2019 VMware, Inc. All Rights Reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package group
18
19 import (
20 "context"
21 "flag"
22 "fmt"
23
24 "github.com/vmware/govmomi/govc/cli"
25 "github.com/vmware/govmomi/govc/flags"
26 "github.com/vmware/govmomi/govc/sso"
27 "github.com/vmware/govmomi/ssoadmin"
28 "github.com/vmware/govmomi/ssoadmin/types"
29 )
30
31 type update struct {
32 *flags.ClientFlag
33
34 d string
35 a string
36 r string
37 g bool
38 }
39
40 func init() {
41 cli.Register("sso.group.update", &update{})
42 }
43
44 func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) {
45 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
46 cmd.ClientFlag.Register(ctx, f)
47
48 f.StringVar(&cmd.d, "d", "", "Group description")
49 f.StringVar(&cmd.a, "a", "", "Add user/group to group")
50 f.StringVar(&cmd.r, "r", "", "Remove user/group from group")
51 f.BoolVar(&cmd.g, "g", false, "Add/Remove group from group")
52 }
53
54 func (cmd *update) Description() string {
55 return `Update SSO group.
56
57 Examples:
58 govc sso.group.update -d "Group description" NAME
59 govc sso.group.update -a user1 NAME
60 govc sso.group.update -r user2 NAME
61 govc sso.group.update -g -a group1 NAME
62 govc sso.group.update -g -r group2 NAME`
63 }
64
65 func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
66 if f.NArg() != 1 {
67 return flag.ErrHelp
68 }
69 id := f.Arg(0)
70
71 return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
72 if cmd.d != "" {
73 err := c.UpdateGroup(ctx, id, types.AdminGroupDetails{Description: cmd.d})
74 if err != nil {
75 return err
76 }
77 }
78
79 if cmd.a != "" {
80 if cmd.g {
81 group, err := c.FindGroup(ctx, cmd.a)
82 if err != nil {
83 return err
84 }
85 if group == nil {
86 return fmt.Errorf("group %q not found", cmd.a)
87 }
88 if err = c.AddGroupsToGroup(ctx, id, group.Id); err != nil {
89 return err
90 }
91 } else {
92 user, err := c.FindUser(ctx, cmd.a)
93 if err != nil {
94 return err
95 }
96 if user == nil {
97 return fmt.Errorf("user %q not found", cmd.a)
98 }
99 if err = c.AddUsersToGroup(ctx, id, user.Id); err != nil {
100 return err
101 }
102 }
103 }
104
105 if cmd.r != "" {
106 var pid types.PrincipalId
107 if cmd.g {
108 group, err := c.FindGroup(ctx, cmd.r)
109 if err != nil {
110 return err
111 }
112 if group == nil {
113 return fmt.Errorf("group %q not found", cmd.r)
114 }
115 pid = group.Id
116 } else {
117 user, err := c.FindUser(ctx, cmd.r)
118 if err != nil {
119 return err
120 }
121 if user == nil {
122 return fmt.Errorf("user %q not found", cmd.r)
123 }
124 pid = user.Id
125 }
126
127 if err := c.RemoveUsersFromGroup(ctx, id, pid); err != nil {
128 return err
129 }
130 }
131
132 return nil
133 })
134 }