-
Notifications
You must be signed in to change notification settings - Fork 18
/
iam_access_key_show.go
122 lines (98 loc) · 3.33 KB
/
iam_access_key_show.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 (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type iamAccessKeyShowOutput struct {
Name string `json:"name"`
Type string `json:"type"`
APIKey string `json:"api_key"`
APISecret *string `json:"api_secret,omitempty"`
Operations *[]string `json:"operations,omitempty"`
Tags *[]string `json:"tags,omitempty"`
Resources *[]string `json:"resources,omitempty"`
}
func (o *iamAccessKeyShowOutput) ToJSON() { output.JSON(o) }
func (o *iamAccessKeyShowOutput) ToText() { output.Text(o) }
func (o *iamAccessKeyShowOutput) ToTable() {
if o.APISecret != nil {
defer fmt.Fprint(os.Stderr, `
/!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\
/!\ Ensure to save your API Secret somewhere, /!\
/!\ as there is no way to recover it afterwards /!\
/!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\
`)
}
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"IAM Access Key"})
defer t.Render()
t.Append([]string{"Name", o.Name})
t.Append([]string{"Type", o.Type})
t.Append([]string{"API Key", o.APIKey})
t.Append([]string{"API Secret", utils.DefaultString(o.APISecret, strings.Repeat("*", 43))})
if o.Operations != nil {
t.Append([]string{"Operations", strings.Join(*o.Operations, "\n")})
}
if o.Tags != nil {
t.Append([]string{"Tags", strings.Join(*o.Tags, "\n")})
}
if o.Resources != nil {
t.Append([]string{"Resources", strings.Join(*o.Resources, "\n")})
}
}
type iamAccessKeyShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
APIKey string `cli-arg:"#" cli-usage:"API-KEY"`
}
func (c *iamAccessKeyShowCmd) cmdAliases() []string { return gShowAlias }
func (c *iamAccessKeyShowCmd) cmdShort() string {
return "Show an IAM access key details"
}
func (c *iamAccessKeyShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows an IAM access key details.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&iamAccessKeyShowOutput{}), ", "))
}
func (c *iamAccessKeyShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *iamAccessKeyShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
iamAccessKey, err := globalstate.EgoscaleClient.GetIAMAccessKey(ctx, zone, c.APIKey)
if err != nil {
return err
}
out := iamAccessKeyShowOutput{
Name: *iamAccessKey.Name,
APIKey: *iamAccessKey.Key,
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,
}
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(iamAccessKeyCmd, &iamAccessKeyShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}