-
Notifications
You must be signed in to change notification settings - Fork 20
/
template_register.go
155 lines (126 loc) · 3.79 KB
/
template_register.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
146
147
148
149
150
151
152
153
154
155
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// templateRegisterCmd registers a template
var templateRegisterCmd = &cobra.Command{
Use: "register NAME",
Short: "register a custom template",
Long: fmt.Sprintf(`This command registers a new Compute instance template.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&templateShowOutput{}), ", ")),
Aliases: gCreateAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
if cmd.Flags().Changed("from-snapshot") {
return cmdCheckRequiredFlags(cmd, []string{
"zone",
})
}
return cmdCheckRequiredFlags(cmd, []string{
"zone",
"url",
"checksum",
})
},
RunE: func(cmd *cobra.Command, args []string) error {
description, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
url, err := cmd.Flags().GetString("url")
if err != nil {
return err
}
checksum, err := cmd.Flags().GetString("checksum")
if err != nil {
return err
}
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
username, err := cmd.Flags().GetString("username")
if err != nil {
return err
}
disablePassword, err := cmd.Flags().GetBool("disable-password")
if err != nil {
return err
}
enablePassword := !(disablePassword)
disableSSHKey, err := cmd.Flags().GetBool("disable-ssh-key")
if err != nil {
return err
}
enableSSHKey := !(disableSSHKey)
bootmode, err := cmd.Flags().GetString("boot-mode")
if err != nil {
return err
}
snapshotID, err := cmd.Flags().GetString("from-snapshot")
if err != nil {
return err
}
req := egoscale.RegisterCustomTemplate{
Name: args[0],
URL: url,
Checksum: checksum,
Displaytext: description,
PasswordEnabled: &enablePassword,
SSHKeyEnabled: &enableSSHKey,
BootMode: bootmode,
}
if username != "" {
req.Details = make(map[string]string)
req.Details["username"] = username
}
if snapshotID != "" {
snapshot, err := exportSnapshot(snapshotID)
if err != nil {
return err
}
req.Checksum = snapshot.MD5sum
req.URL = snapshot.PresignedURL
}
return output(templateRegister(req, zone))
},
}
func templateRegister(registerTemplate egoscale.RegisterCustomTemplate, zone string) (outputter, error) {
z, err := getZoneByNameOrID(zone)
if err != nil {
return nil, err
}
registerTemplate.ZoneID = z.ID
resp, err := asyncRequest(registerTemplate, "Registering the template")
if err != nil {
return nil, err
}
templates := resp.(*[]egoscale.Template)
if len(*templates) != 1 {
return nil, nil
}
template := (*templates)[0]
if !gQuiet {
return showTemplate(&template)
}
return nil, nil
}
func init() {
templateRegisterCmd.Flags().StringP("checksum", "c", "", "MD5 checksum value of the template")
templateRegisterCmd.Flags().StringP("description", "d", "", "template description")
templateRegisterCmd.Flags().StringP("zone", "z", "", "ID or name of the zone the template is to be hosted on")
templateRegisterCmd.Flags().StringP("username", "u", "", "default username of the template")
templateRegisterCmd.Flags().String("url", "", "URL of where the template is hosted")
templateRegisterCmd.Flags().String("boot-mode", "legacy", "template boot mode (legacy|uefi)")
templateRegisterCmd.Flags().String("from-snapshot", "", "ID of a Compute instance snapshot to register as a new template")
templateRegisterCmd.Flags().Bool("disable-password", false, "disable password-based authentication")
templateRegisterCmd.Flags().Bool("disable-ssh-key", false, "disable SSH key-based authentication")
templateCmd.AddCommand(templateRegisterCmd)
}