This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
version_set.go
196 lines (172 loc) · 3.67 KB
/
version_set.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package models
import (
"bytes"
"encoding/json"
"log"
)
/*
* VersionSet tracks a versioned thing in the RackN system
*/
type FileData struct {
Path string // Including name e.g. files/raid/jjj
Sha256Sum string // sha256sum of the item
Source string // Location to get file from - self://path is this DRP
Explode bool
}
// VersionSet structure that handles RawModel instead of dealing with
// RawModel which is how DRP is storing it.
//
// An element with Version = ignore means leave it loaded.
type VersionSet struct {
Validation
Access
Meta
Owned
Bundled
Id string `index:",key"`
Documentation string
Description string
Apply bool
DRPVersion string
DRPUXVersion string
Components []*Element
Plugins []*Plugin
Prefs map[string]string
Files []*FileData
Global map[string]interface{}
}
func (vs *VersionSet) Key() string {
return vs.Id
}
func (vs *VersionSet) KeyName() string {
return "Id"
}
func (vs *VersionSet) AuthKey() string {
return vs.Key()
}
func (vs *VersionSet) Prefix() string {
return "version_sets"
}
// GetDocumentaiton returns the object's Documentation
func (vs *VersionSet) GetDocumentation() string {
return vs.Documentation
}
// GetDescription returns the object's Description
func (vs *VersionSet) GetDescription() string {
return vs.Description
}
// Clone the VersionSet
func (vs *VersionSet) Clone() *VersionSet {
ci2 := &VersionSet{}
buf := bytes.Buffer{}
enc, dec := json.NewEncoder(&buf), json.NewDecoder(&buf)
if err := enc.Encode(vs); err != nil {
log.Panicf("Failed to encode endpoint:%s: %v", vs.Id, err)
}
if err := dec.Decode(ci2); err != nil {
log.Panicf("Failed to decode endpoint:%s: %v", vs.Id, err)
}
return ci2
}
func (vs *VersionSet) Fill() {
vs.Validation.fill(vs)
if vs.Meta == nil {
vs.Meta = Meta{}
}
if vs.Errors == nil {
vs.Errors = []string{}
}
if vs.Components == nil {
vs.Components = []*Element{}
}
if vs.Plugins == nil {
vs.Plugins = []*Plugin{}
}
if vs.Prefs == nil {
vs.Prefs = map[string]string{}
}
if vs.Global == nil {
vs.Global = map[string]interface{}{}
}
if vs.Files == nil {
vs.Files = []*FileData{}
}
}
func (vs *VersionSet) Merge(nvs *VersionSet) {
vs.Apply = vs.Apply && nvs.Apply
if nvs.DRPVersion != "" {
vs.DRPVersion = nvs.DRPVersion
}
if nvs.DRPUXVersion != "" {
vs.DRPUXVersion = nvs.DRPUXVersion
}
// Add in components
for _, c := range nvs.Components {
found := false
for i, curc := range vs.Components {
if curc.Name == c.Name {
vs.Components[i] = c
found = true
break
}
}
if !found {
vs.Components = append(vs.Components, c)
}
}
// Add in plugins
for _, c := range nvs.Plugins {
found := false
for i, curc := range vs.Plugins {
if curc.Name == c.Name {
vs.Plugins[i] = c
found = true
break
}
}
if !found {
vs.Plugins = append(vs.Plugins, c)
}
}
// Merge prefs
for k, v := range nvs.Prefs {
vs.Prefs[k] = v
}
// Merge Global
for k, v := range nvs.Global {
vs.Global[k] = v
}
// Files
for _, c := range nvs.Files {
found := false
for i, curc := range vs.Files {
if curc.Path == c.Path {
vs.Files[i] = c
found = true
break
}
}
if !found {
vs.Files = append(vs.Files, c)
}
}
}
func (vs *VersionSet) SliceOf() interface{} {
s := []*VersionSet{}
return &s
}
func (vs *VersionSet) ToModels(obj interface{}) []Model {
items := obj.(*[]*VersionSet)
res := make([]Model, len(*items))
for i, item := range *items {
res[i] = Model(item)
}
return res
}
func (vs *VersionSet) CanHaveActions() bool {
return true
}
// SetName sets the name. In this case, it sets Id.
func (vs *VersionSet) SetName(name string) {
vs.Id = name
}