-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_template_show.go
134 lines (114 loc) · 4.45 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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/dustin/go-humanize"
"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 instanceTemplateShowOutput struct {
ID string `json:"id"`
Zone string `json:"zone"`
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"`
Maintainer string `json:"maintainer"`
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 *instanceTemplateShowOutput) ToJSON() { output.JSON(o) }
func (o *instanceTemplateShowOutput) ToText() { output.Text(o) }
func (o *instanceTemplateShowOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Template"})
defer t.Render()
t.Append([]string{"ID", o.ID})
t.Append([]string{"Zone", o.Zone})
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{"Maintainer", o.Maintainer})
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 instanceTemplateShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Template string `cli-arg:"#" cli-usage:"[FAMILY.]SIZE"`
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 *instanceTemplateShowCmd) cmdAliases() []string { return gShowAlias }
func (c *instanceTemplateShowCmd) cmdShort() string {
return "Show a Compute instance template details"
}
func (c *instanceTemplateShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Compute instance template details.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&instanceTemplateShowOutput{}), ", "))
}
func (c *instanceTemplateShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceTemplateShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
template, err := globalstate.EgoscaleClient.FindTemplate(ctx, c.Zone, c.Template, c.Visibility)
if err != nil {
return fmt.Errorf(
"no template %q found with visibility %s in zone %s",
c.Template,
c.Visibility,
c.Zone,
)
}
return c.outputFunc(&instanceTemplateShowOutput{
ID: *template.ID,
Zone: c.Zone,
Family: utils.DefaultString(template.Family, ""),
Name: *template.Name,
Description: utils.DefaultString(template.Description, ""),
CreationDate: template.CreatedAt.String(),
Visibility: *template.Visibility,
Size: *template.Size,
Version: utils.DefaultString(template.Version, ""),
Build: utils.DefaultString(template.Build, ""),
Maintainer: utils.DefaultString(template.Maintainer, ""),
Checksum: *template.Checksum,
DefaultUser: utils.DefaultString(template.DefaultUser, ""),
SSHKeyEnabled: *template.SSHKeyEnabled,
PasswordEnabled: *template.PasswordEnabled,
BootMode: *template.BootMode,
}, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(instanceTemplateCmd, &instanceTemplateShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
Visibility: defaultTemplateVisibility,
}))
}