/
template.go
104 lines (85 loc) · 2.16 KB
/
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
package client
import (
"errors"
"fmt"
"os"
)
type TemplateDisk struct {
Bootable bool `json:"bootable"`
Device string `json:"device"`
Size int `json:"size"`
Type string `json:"type"`
SR string `json:"SR"`
}
type TemplateInfo struct {
Arch string `json:"arch"`
Disks []TemplateDisk `json:"disks"`
}
type Template struct {
Id string `json:"id"`
Uuid string `json:"uuid"`
NameLabel string `json:"name_label"`
PoolId string `json:"$poolId"`
TemplateInfo TemplateInfo `json:"template_info"`
}
func (t Template) Compare(obj interface{}) bool {
other := obj.(Template)
if t.Id == other.Id {
return true
}
labelsMatch := false
if t.NameLabel == other.NameLabel {
labelsMatch = true
}
if t.PoolId == "" && labelsMatch {
return true
} else if t.PoolId == other.PoolId && labelsMatch {
return true
}
return false
}
func (t Template) isDiskTemplate() bool {
if len(t.TemplateInfo.Disks) == 0 && t.NameLabel != "Other install media" {
return true
}
return false
}
func (c *Client) GetTemplate(template Template) ([]Template, error) {
obj, err := c.FindFromGetAllObjects(template)
var templates []Template
if err != nil {
return templates, err
}
templates, ok := obj.([]Template)
if !ok {
return templates, errors.New("failed to coerce response into Template slice")
}
return templates, nil
}
func FindTemplateForTests(template *Template, poolId, templateEnvVar string) {
var found bool
templateName, found := os.LookupEnv(templateEnvVar)
if !found {
fmt.Println(fmt.Sprintf("The %s environment variable must be set for the tests", templateEnvVar))
os.Exit(-1)
}
c, err := NewClient(GetConfigFromEnv())
if err != nil {
fmt.Printf("failed to create client with error: %v", err)
os.Exit(-1)
}
templates, err := c.GetTemplate(Template{
NameLabel: templateName,
PoolId: poolId,
})
if err != nil {
fmt.Printf("failed to find templates with error: %v\n", err)
os.Exit(-1)
}
l := len(templates)
if l != 1 {
fmt.Printf("found %d templates when expected to find 1. templates found: %v\n", l, templates)
os.Exit(-1)
}
*template = templates[0]
}