-
Notifications
You must be signed in to change notification settings - Fork 19
/
iam_access_key_create.go
103 lines (81 loc) · 2.92 KB
/
iam_access_key_create.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
package cmd
import (
"fmt"
"strings"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type iamAccessKeyCreateCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"create"`
Name string `cli-arg:"#"`
Operations []string `cli-flag:"operation" cli-usage:"API operation to restrict the access key to. Can be repeated multiple times."`
Resources []string `cli-flag:"resource" cli-usage:"API resource to restrict the access key to (format: DOMAIN/TYPE:NAME). Can be repeated multiple times."`
Tags []string `cli-flag:"tag" cli-usage:"API operations tag to restrict the access key to. Can be repeated multiple times."`
}
func (c *iamAccessKeyCreateCmd) cmdAliases() []string { return gCreateAlias }
func (c *iamAccessKeyCreateCmd) cmdShort() string {
return "Create an IAM access key"
}
func (c *iamAccessKeyCreateCmd) cmdLong() string {
return fmt.Sprintf(`This command creates an IAM access key.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&iamAccessKeyShowOutput{}), ", "))
}
func (c *iamAccessKeyCreateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *iamAccessKeyCreateCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := gCurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
opts := make([]egoscale.CreateIAMAccessKeyOpt, 0)
if len(c.Operations) > 0 {
opts = append(opts, egoscale.CreateIAMAccessKeyWithOperations(c.Operations))
}
if len(c.Resources) > 0 {
resources := make([]egoscale.IAMAccessKeyResource, len(c.Resources))
for i, rs := range c.Resources {
r, err := parseIAMAccessKeyResource(rs)
if err != nil {
return fmt.Errorf("invalid API resource %q", rs)
}
resources[i] = *r
}
opts = append(opts, egoscale.CreateIAMAccessKeyWithResources(resources))
}
if len(c.Tags) > 0 {
opts = append(opts, egoscale.CreateIAMAccessKeyWithTags(c.Tags))
}
iamAccessKey, err := cs.CreateIAMAccessKey(ctx, zone, c.Name, opts...)
if err != nil {
return fmt.Errorf("unable to create a new IAM access key: %w", err)
}
out := iamAccessKeyShowOutput{
Name: *iamAccessKey.Name,
APIKey: *iamAccessKey.Key,
APISecret: iamAccessKey.Secret,
Operations: iamAccessKey.Operations,
Resources: func() *[]string {
if iamAccessKey.Resources != nil {
list := make([]string, len(*iamAccessKey.Resources))
for i, r := range *iamAccessKey.Resources {
list[i] = fmt.Sprintf("%s/%s:%s", r.Domain, r.ResourceType, r.ResourceName)
}
return &list
}
return nil
}(),
Tags: iamAccessKey.Tags,
Type: *iamAccessKey.Type,
}
if !gQuiet {
return c.outputFunc(&out, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(iamAccessKeyCmd, &iamAccessKeyCreateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}