forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_template.go
122 lines (113 loc) · 3.24 KB
/
content_template.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 proxmox
import (
"errors"
)
type ConfigContent_Template struct {
Node string
Storage string
Template string
}
func (content ConfigContent_Template) error(text string) error {
return errors.New("the value of (" + text + ") may not be empty")
}
func (content ConfigContent_Template) mapToApiValues() map[string]interface{} {
return map[string]interface{}{
"storage": content.Storage,
"template": content.Template,
}
}
// Return an error if the one of the values is empty.
func (content ConfigContent_Template) Validate() (err error) {
if content.Node == "" {
return content.error("Node")
}
if content.Storage == "" {
return content.error("Storage")
}
if content.Template == "" {
return content.error("Template")
}
return
}
type TemplateItem struct {
Architecture string
Description string
Headline string
InfoPage string
Location string
ManageURL string
OS string
Package string
Section string
SHA512Checksum string
Source string
Template string
Type string
Version string
}
// Map values from the API to the TemplateItem struct.
func createTemplateList(templateList []interface{}) *[]TemplateItem {
templates := make([]TemplateItem, len(templateList))
for i := range templateList {
itemMap := templateList[i].(map[string]interface{})
tmp := TemplateItem{}
if _, isSet := itemMap["architecture"]; isSet {
tmp.Architecture = itemMap["architecture"].(string)
}
if _, isSet := itemMap["description"]; isSet {
tmp.Description = itemMap["description"].(string)
}
if _, isSet := itemMap["headline"]; isSet {
tmp.Headline = itemMap["headline"].(string)
}
if _, isSet := itemMap["infopage"]; isSet {
tmp.InfoPage = itemMap["infopage"].(string)
}
if _, isSet := itemMap["location"]; isSet {
tmp.Location = itemMap["location"].(string)
}
if _, isSet := itemMap["manageurl"]; isSet {
tmp.ManageURL = itemMap["manageurl"].(string)
}
if _, isSet := itemMap["os"]; isSet {
tmp.OS = itemMap["os"].(string)
}
if _, isSet := itemMap["package"]; isSet {
tmp.Package = itemMap["package"].(string)
}
if _, isSet := itemMap["section"]; isSet {
tmp.Section = itemMap["section"].(string)
}
if _, isSet := itemMap["sha512sum"]; isSet {
tmp.SHA512Checksum = itemMap["sha512sum"].(string)
}
if _, isSet := itemMap["source"]; isSet {
tmp.Source = itemMap["source"].(string)
}
if _, isSet := itemMap["template"]; isSet {
tmp.Template = itemMap["template"].(string)
}
if _, isSet := itemMap["type"]; isSet {
tmp.Type = itemMap["type"].(string)
}
if _, isSet := itemMap["version"]; isSet {
tmp.Version = itemMap["version"].(string)
}
templates[i] = tmp
}
return &templates
}
// Download an LXC template.
func DownloadLxcTemplate(client *Client, content ConfigContent_Template) (err error) {
_, err = client.PostWithTask(content.mapToApiValues(), "/nodes/"+content.Node+"/aplinfo")
return
}
// List all LXC templates available for download.
func ListTemplates(client *Client, node string) (templateList *[]TemplateItem, err error) {
tmpList, err := client.GetItemListInterfaceArray("/nodes/" + node + "/aplinfo")
if err != nil {
return
}
templateList = createTemplateList(tmpList)
return
}