-
Notifications
You must be signed in to change notification settings - Fork 20
/
template_register.go
124 lines (102 loc) · 3.39 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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// templateRegisterCmd registers a template
var templateRegisterCmd = &cobra.Command{
Use: "register",
Short: "register a custom template",
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
} else if name == "" {
return fmt.Errorf("template name must be specified")
}
description, err := cmd.Flags().GetString("description")
if err != nil {
return err
} else if description == "" {
return fmt.Errorf("template description must be specified")
}
url, err := cmd.Flags().GetString("url")
if err != nil {
return err
} else if url == "" {
return fmt.Errorf("template image URL must be specified")
}
checksum, err := cmd.Flags().GetString("checksum")
if err != nil {
return err
} else if checksum == "" {
return fmt.Errorf("template image file checksum must be specified")
}
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
if zone == "" {
zone = gCurrentAccount.DefaultZone
}
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)
req := egoscale.RegisterCustomTemplate{
Name: name,
URL: url,
Checksum: checksum,
Displaytext: description,
PasswordEnabled: &enablePassword,
SSHKeyEnabled: &enableSSHKey,
}
if username, _ := cmd.Flags().GetString("username"); username != "" {
req.Details = make(map[string]string)
req.Details["username"] = username
}
return templateRegister(req, zone)
},
}
func templateRegister(registerTemplate egoscale.RegisterCustomTemplate, zone string) error {
z, err := getZoneByName(zone)
if err != nil {
return err
}
registerTemplate.ZoneID = z.ID
resp, err := asyncRequest(registerTemplate, fmt.Sprintf("Registering the template"))
if err != nil {
return err
}
templates := resp.(*[]egoscale.Template)
if !gQuiet {
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Zone", "Name", "ID"})
for _, template := range *templates {
table.Append([]string{template.ZoneName, template.Name, template.ID.String()})
}
table.Render()
}
return nil
}
func init() {
templateRegisterCmd.Flags().StringP("checksum", "", "", "the MD5 checksum value of the template")
templateRegisterCmd.Flags().StringP("description", "", "", "the template description")
templateRegisterCmd.Flags().StringP("name", "", "", "the name of the template")
templateRegisterCmd.Flags().StringP("zone", "z", "", "the ID or name of the zone the template is to be hosted on")
templateRegisterCmd.Flags().StringP("url", "", "", "the URL of where the template is hosted")
templateRegisterCmd.Flags().BoolP("disable-password", "", false, "true if the template does not support password authentication; default is false")
templateRegisterCmd.Flags().BoolP("disable-ssh-key", "", false, "true if the template does not support ssh key authentication; default is false")
templateRegisterCmd.Flags().StringP("username", "", "", "The default username of the template")
templateCmd.AddCommand(templateRegisterCmd)
}