/
storage_repository.go
117 lines (93 loc) · 2.59 KB
/
storage_repository.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
package client
import (
"errors"
"fmt"
"os"
)
type StorageRepository struct {
Id string `json:"id"`
Uuid string `json:"uuid"`
NameLabel string `json:"name_label"`
PoolId string `json:"$poolId"`
SRType string `json:"SR_type"`
Container string `json:"$container"`
PhysicalUsage int `json:"physical_usage"`
Size int `json:"size"`
Usage int `json:"usage"`
Tags []string `json:"tags,omitempty"`
}
func (s StorageRepository) Compare(obj interface{}) bool {
otherSr := obj.(StorageRepository)
if s.Id != "" && s.Id == otherSr.Id {
return true
}
if len(s.Tags) > 0 {
for _, tag := range s.Tags {
if !stringInSlice(tag, otherSr.Tags) {
return false
}
}
}
labelsMatch := false
if s.NameLabel == otherSr.NameLabel {
labelsMatch = true
}
if s.PoolId == "" && labelsMatch {
return true
} else if s.PoolId == otherSr.PoolId && labelsMatch {
return true
}
return false
}
func stringInSlice(needle string, haystack []string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
func (c *Client) GetStorageRepositoryById(id string) (StorageRepository, error) {
obj, err := c.FindFromGetAllObjects(StorageRepository{Id: id})
var sr StorageRepository
if err != nil {
return sr, err
}
srs, ok := obj.([]StorageRepository)
if !ok {
return sr, errors.New("failed to coerce response into StorageRepository slice")
}
if len(srs) != 1 {
return sr, errors.New(fmt.Sprintf("expected a single storage respository to be returned, instead received: %d in the response: %v", len(srs), obj))
}
return srs[0], nil
}
func (c *Client) GetStorageRepository(sr StorageRepository) ([]StorageRepository, error) {
obj, err := c.FindFromGetAllObjects(sr)
if err != nil {
return nil, err
}
srs, ok := obj.([]StorageRepository)
if !ok {
return nil, errors.New("failed to coerce response into StorageRepository slice")
}
return srs, nil
}
func FindStorageRepositoryForTests(pool Pool, sr *StorageRepository, tag string) {
c, err := NewClient(GetConfigFromEnv())
if err != nil {
fmt.Printf("failed to create client with error: %v", err)
os.Exit(-1)
}
defaultSr, err := c.GetStorageRepositoryById(pool.DefaultSR)
if err != nil {
fmt.Printf("failed to find the default storage repository with id: %s with error: %v\n", pool.DefaultSR, err)
os.Exit(-1)
}
*sr = defaultSr
err = c.AddTag(defaultSr.Id, tag)
if err != nil {
fmt.Printf("failed to set tag on default storage repository with id: %s with error: %v\n", pool.DefaultSR, err)
os.Exit(-1)
}
}