/
describe_group.go
73 lines (63 loc) · 1.59 KB
/
describe_group.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
package cmd
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/spf13/cobra"
"log"
"sort"
)
// groupCmd represents the group command
var groupCmd = &cobra.Command{
Use: "group NAME",
Aliases: []string{"g"},
Short: "Describe group",
Long: `Describe given group.`,
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 1:
describeGroup(args[0])
default:
log.Fatal("wrong number of arguments")
}
},
}
func init() {
describeCmd.AddCommand(groupCmd)
}
// describe group and policies it uses
func describeGroup(name string) {
describeGroupDetails(&name)
describeGroupAttachedPolicies(&name)
}
// describe details for iam group
func describeGroupDetails(name *string) {
output, err := client.GetGroup(context.TODO(), &iam.GetGroupInput{GroupName: name})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Arn: %s\n", *output.Group.Arn)
fmt.Printf("CreateDate: %s\n", *output.Group.CreateDate)
fmt.Printf("GroupId: %s\n", *output.Group.GroupId)
fmt.Printf("Users:\n")
// sort and print users
names := []string{}
for _, u := range output.Users {
names = append(names, *u.UserName)
}
sort.Strings(names)
for _, name := range names {
fmt.Printf(" %s\n", name)
}
}
// describe names of attached policies
func describeGroupAttachedPolicies(name *string) {
fmt.Println("Attached policies:")
output, err := client.ListAttachedGroupPolicies(context.TODO(), &iam.ListAttachedGroupPoliciesInput{GroupName: name})
if err != nil {
log.Fatal(err)
}
for _, p := range output.AttachedPolicies {
fmt.Printf(" %s\n", *p.PolicyName)
}
}