| 1 |
/* |
| 2 |
Copyright (c) 2018 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 user |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"text/tabwriter" |
| 25 |
|
| 26 |
"github.com/vmware/govmomi/govc/cli" |
| 27 |
"github.com/vmware/govmomi/govc/flags" |
| 28 |
"github.com/vmware/govmomi/govc/sso" |
| 29 |
"github.com/vmware/govmomi/ssoadmin" |
| 30 |
"github.com/vmware/govmomi/ssoadmin/types" |
| 31 |
) |
| 32 |
|
| 33 |
type ls struct { |
| 34 |
*flags.ClientFlag |
| 35 |
*flags.OutputFlag |
| 36 |
|
| 37 |
solution bool |
| 38 |
} |
| 39 |
|
| 40 |
func init() { |
| 41 |
cli.Register("sso.user.ls", &ls{}) |
| 42 |
} |
| 43 |
|
| 44 |
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { |
| 45 |
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) |
| 46 |
cmd.ClientFlag.Register(ctx, f) |
| 47 |
|
| 48 |
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) |
| 49 |
cmd.OutputFlag.Register(ctx, f) |
| 50 |
|
| 51 |
f.BoolVar(&cmd.solution, "s", false, "List solution users") |
| 52 |
} |
| 53 |
|
| 54 |
func (cmd *ls) Description() string { |
| 55 |
return `List SSO users. |
| 56 |
|
| 57 |
Examples: |
| 58 |
govc sso.user.ls -s` |
| 59 |
} |
| 60 |
|
| 61 |
func (cmd *ls) Process(ctx context.Context) error { |
| 62 |
if err := cmd.ClientFlag.Process(ctx); err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
return cmd.OutputFlag.Process(ctx) |
| 66 |
} |
| 67 |
|
| 68 |
type solutionResult []types.AdminSolutionUser |
| 69 |
|
| 70 |
func (r solutionResult) Dump() interface{} { |
| 71 |
return []types.AdminSolutionUser(r) |
| 72 |
} |
| 73 |
|
| 74 |
func (r solutionResult) Write(w io.Writer) error { |
| 75 |
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) |
| 76 |
for _, info := range r { |
| 77 |
fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Details.Description) |
| 78 |
} |
| 79 |
return tw.Flush() |
| 80 |
} |
| 81 |
|
| 82 |
type personResult []types.AdminPersonUser |
| 83 |
|
| 84 |
func (r personResult) Dump() interface{} { |
| 85 |
return []types.AdminPersonUser(r) |
| 86 |
} |
| 87 |
|
| 88 |
func (r personResult) Write(w io.Writer) error { |
| 89 |
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) |
| 90 |
for _, info := range r { |
| 91 |
fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Details.Description) |
| 92 |
} |
| 93 |
return tw.Flush() |
| 94 |
} |
| 95 |
|
| 96 |
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { |
| 97 |
arg := f.Arg(0) |
| 98 |
|
| 99 |
return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error { |
| 100 |
if cmd.solution { |
| 101 |
info, err := c.FindSolutionUsers(ctx, arg) |
| 102 |
if err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
|
| 106 |
return cmd.WriteResult(solutionResult(info)) |
| 107 |
} |
| 108 |
|
| 109 |
info, err := c.FindPersonUsers(ctx, arg) |
| 110 |
if err != nil { |
| 111 |
return err |
| 112 |
} |
| 113 |
|
| 114 |
return cmd.WriteResult(personResult(info)) |
| 115 |
}) |
| 116 |
} |