-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_template_show.go
145 lines (123 loc) · 4.72 KB
/
instance_template_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cmd
import (
"fmt"
"os"
"strings"
"github.com/dustin/go-humanize"
"github.com/exoscale/cli/table"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type computeInstanceTemplateShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Family string `json:"family"`
CreationDate string `json:"creation_date"`
Visibility string `json:"visibility"`
Size int64 `json:"size"`
Version string `json:"version"`
Build string `json:"build"`
DefaultUser string `json:"default_user"`
SSHKeyEnabled bool `json:"ssh_key_enabled"`
PasswordEnabled bool `json:"password_enabled"`
BootMode string `json:"boot_mode"`
Checksum string `json:"checksum"`
}
func (o *computeInstanceTemplateShowOutput) toJSON() { outputJSON(o) }
func (o *computeInstanceTemplateShowOutput) toText() { outputText(o) }
func (o *computeInstanceTemplateShowOutput) toTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Template"})
defer t.Render()
t.Append([]string{"ID", o.ID})
t.Append([]string{"Name", o.Name})
t.Append([]string{"Description", o.Description})
t.Append([]string{"Family", o.Family})
t.Append([]string{"Creation Date", o.CreationDate})
t.Append([]string{"Visibility", o.Visibility})
t.Append([]string{"Size", humanize.IBytes(uint64(o.Size))})
t.Append([]string{"Version", o.Version})
t.Append([]string{"Build", o.Build})
t.Append([]string{"Default User", o.DefaultUser})
t.Append([]string{"SSH key enabled", fmt.Sprint(o.SSHKeyEnabled)})
t.Append([]string{"Password enabled", fmt.Sprint(o.PasswordEnabled)})
t.Append([]string{"Boot Mode", o.BootMode})
t.Append([]string{"Checksum", o.Checksum})
}
type computeInstanceTemplateShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Template string `cli-arg:"#" cli-usage:"[FAMILY.]SIZE"`
Family string `cli-short:"f" cli-usage:"template family to filter results to"`
Visibility string `cli-short:"v" cli-usage:"template visibility (public|private)"`
Zone string `cli-short:"z" cli-usage:"zone to filter results to (default: current account's default zone)"`
}
func (c *computeInstanceTemplateShowCmd) cmdAliases() []string { return gShowAlias }
func (c *computeInstanceTemplateShowCmd) cmdShort() string {
return "Show a Compute instance template details"
}
func (c *computeInstanceTemplateShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Compute instance template details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&computeInstanceTemplateShowOutput{}), ", "))
}
func (c *computeInstanceTemplateShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *computeInstanceTemplateShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(gCurrentAccount.Environment, gCurrentAccount.DefaultZone),
)
// Opportunistic shortcut in case the template is referenced by ID.
template, _ := cs.Client.GetTemplate(ctx, c.Zone, c.Template)
if template == nil {
var templateID string
templates, err := cs.ListTemplates(ctx, c.Zone, c.Visibility, c.Family)
if err != nil {
return fmt.Errorf("error retrieving templates: %s", err)
}
for _, template := range templates {
if *template.ID == c.Template || *template.Name == c.Template {
templateID = *template.ID
break
}
}
if templateID == "" {
return fmt.Errorf(
"no template %q found with visibility %s in zone %s",
c.Template,
c.Visibility,
c.Zone,
)
}
template, err = cs.Client.GetTemplate(ctx, c.Zone, templateID)
if err != nil {
return fmt.Errorf("error retrieving template: %s", err)
}
}
return output(&computeInstanceTemplateShowOutput{
ID: *template.ID,
Family: defaultString(template.Family, ""),
Name: *template.Name,
Description: defaultString(template.Description, ""),
CreationDate: template.CreatedAt.String(),
Visibility: *template.Visibility,
Size: *template.Size,
Version: defaultString(template.Version, ""),
Build: defaultString(template.Build, ""),
Checksum: *template.Checksum,
DefaultUser: defaultString(template.DefaultUser, ""),
SSHKeyEnabled: *template.SSHKeyEnabled,
PasswordEnabled: *template.PasswordEnabled,
BootMode: *template.BootMode,
}, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(computeInstanceTemplateCmd, &computeInstanceTemplateShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
Visibility: defaultTemplateVisibility,
}))
}