1/*
2Copyright (c) 2016 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package role
18
19import (
20	"context"
21	"flag"
22
23	"github.com/vmware/govmomi/govc/cli"
24	"github.com/vmware/govmomi/govc/permissions"
25)
26
27type usage struct {
28	*permissions.PermissionFlag
29}
30
31func init() {
32	cli.Register("role.usage", &usage{})
33}
34
35func (cmd *usage) Register(ctx context.Context, f *flag.FlagSet) {
36	cmd.PermissionFlag, ctx = permissions.NewPermissionFlag(ctx)
37	cmd.PermissionFlag.Register(ctx, f)
38}
39
40func (cmd *usage) Process(ctx context.Context) error {
41	if err := cmd.PermissionFlag.Process(ctx); err != nil {
42		return err
43	}
44	return nil
45}
46
47func (cmd *usage) Usage() string {
48	return "NAME..."
49}
50
51func (cmd *usage) Description() string {
52	return `List usage for role NAME.
53
54Examples:
55  govc role.usage
56  govc role.usage Admin`
57}
58
59func (cmd *usage) Run(ctx context.Context, f *flag.FlagSet) error {
60	m, err := cmd.Manager(ctx)
61	if err != nil {
62		return err
63	}
64
65	if f.NArg() == 0 {
66		cmd.List.Permissions, err = m.RetrieveAllPermissions(ctx)
67		if err != nil {
68			return err
69		}
70	} else {
71		for _, name := range f.Args() {
72			role, err := cmd.Role(name)
73			if err != nil {
74				return err
75			}
76
77			perms, err := m.RetrieveRolePermissions(ctx, role.RoleId)
78			if err != nil {
79				return err
80			}
81
82			cmd.List.Add(perms)
83		}
84	}
85
86	return cmd.WriteResult(&cmd.List)
87}
88