-
Notifications
You must be signed in to change notification settings - Fork 19
/
iam_role_update.go
142 lines (113 loc) · 3.41 KB
/
iam_role_update.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cmd
import (
"errors"
"fmt"
"io"
"strings"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
exoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type iamRoleUpdateCmd struct {
cliCommandSettings `cli-cmd:"-"`
Role string `cli-arg:"#" cli-usage:"ID|NAME"`
Description string `cli-flag:"description" cli-usage:"Role description"`
Permissions []string `cli-flag:"permissions" cli-usage:"Role permissions"`
Labels map[string]string `cli-flag:"label" cli-usage:"Role labels (format: key=value)"`
Policy string `cli-flag:"policy" cli-usage:"Role policy (use '-' to read from STDIN)"`
_ bool `cli-cmd:"update"`
}
func (c *iamRoleUpdateCmd) cmdAliases() []string { return nil }
func (c *iamRoleUpdateCmd) cmdShort() string {
return "Update an IAM Role"
}
func (c *iamRoleUpdateCmd) cmdLong() string {
return fmt.Sprintf(`This command updates an IAM Role.
When you supply '-' as a flag argument to '--policy', the new policy will be read from STDIN.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&iamPolicyOutput{}), ", "))
}
func (c *iamRoleUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *iamRoleUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
if c.Role == "" {
return errors.New("Role not provided") // nolint:stylecheck
}
zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone),
)
if _, err := uuid.Parse(c.Role); err != nil {
roles, err := globalstate.EgoscaleClient.ListIAMRoles(ctx, zone)
if err != nil {
return err
}
for _, role := range roles {
if role.Name != nil && *role.Name == c.Role {
c.Role = *role.ID
break
}
}
}
role := &exoscale.IAMRole{
ID: &c.Role,
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Description)) {
role.Description = &c.Description
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Labels)) {
role.Labels = c.Labels
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Permissions)) {
role.Permissions = c.Permissions
}
err := globalstate.EgoscaleClient.UpdateIAMRole(ctx, zone, role)
if err != nil {
return err
}
// If we don't need to update Policy we can exit now
if c.Policy == "" {
if !globalstate.Quiet {
return (&iamRoleShowCmd{
cliCommandSettings: c.cliCommandSettings,
Role: *role.ID,
}).cmdRun(nil, nil)
}
return nil
}
if c.Policy == "-" {
inputReader := cmd.InOrStdin()
b, err := io.ReadAll(inputReader)
if err != nil {
return fmt.Errorf("failed to read policy from stdin: %w", err)
}
c.Policy = string(b)
}
policy, err := iamPolicyFromJSON([]byte(c.Policy))
if err != nil {
return fmt.Errorf("failed to parse IAM policy: %w", err)
}
role.Policy = policy
err = globalstate.EgoscaleClient.UpdateIAMRolePolicy(ctx, zone, role)
if err != nil {
return err
}
if !globalstate.Quiet {
return (&iamRoleShowCmd{
cliCommandSettings: c.cliCommandSettings,
Role: *role.ID,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(iamRoleCmd, &iamRoleUpdateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}