-
Notifications
You must be signed in to change notification settings - Fork 20
/
iam.go
122 lines (99 loc) · 2.58 KB
/
iam.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
114
115
116
117
118
119
120
121
122
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
exoscale "github.com/exoscale/egoscale/v2"
)
var iamCmd = &cobra.Command{
Use: "iam",
Short: "Identity and Access Management",
}
func init() {
RootCmd.AddCommand(iamCmd)
}
type iamPolicyOutput struct {
DefaultServiceStrategy string `json:"default-service-strategy"`
Services map[string]iamPolicyServiceOutput `json:"services"`
}
type iamPolicyServiceOutput struct {
Type string `json:"type"`
Rules []iamPolicyServiceRuleOutput `json:"rules"`
}
type iamPolicyServiceRuleOutput struct {
Action string `json:"action"`
Expression string `json:"expression"`
}
func (o *iamPolicyOutput) ToJSON() { output.JSON(o) }
func (o *iamPolicyOutput) ToText() { output.Text(o) }
func (o *iamPolicyOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetAutoMergeCellsByColumnIndex([]int{0, 1})
t.SetHeader([]string{
"Service",
fmt.Sprintf("Type (default strategy \"%s\")", o.DefaultServiceStrategy),
"Rule Action",
"Rule Expression",
})
// use underlying tablewriter.Render to display table even with empty rows
// as default strategy is in header.
defer t.Table.Render()
for name, service := range o.Services {
if len(service.Rules) == 0 {
t.Append([]string{name, service.Type, "", ""})
continue
}
for _, rule := range service.Rules {
t.Append([]string{
name,
service.Type,
rule.Action,
rule.Expression,
})
}
}
}
func iamPolicyFromJSON(data []byte) (*exoscale.IAMPolicy, error) {
var obj iamPolicyOutput
err := json.Unmarshal(data, &obj)
if err != nil {
return nil, fmt.Errorf("failed to parse policy: %w", err)
}
policy := exoscale.IAMPolicy{
DefaultServiceStrategy: obj.DefaultServiceStrategy,
Services: map[string]exoscale.IAMPolicyService{},
}
if len(obj.Services) > 0 {
for name, sv := range obj.Services {
service := exoscale.IAMPolicyService{
Type: func() *string {
t := sv.Type
return &t
}(),
}
if len(sv.Rules) > 0 {
service.Rules = []exoscale.IAMPolicyServiceRule{}
for _, rl := range sv.Rules {
rule := exoscale.IAMPolicyServiceRule{
Action: func() *string {
t := rl.Action
return &t
}(),
}
if rl.Expression != "" {
rule.Expression = func() *string {
t := rl.Expression
return &t
}()
}
service.Rules = append(service.Rules, rule)
}
}
policy.Services[name] = service
}
}
return &policy, nil
}