-
Notifications
You must be signed in to change notification settings - Fork 18
/
templateversion.go
119 lines (99 loc) · 2.98 KB
/
templateversion.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
package model
import (
"encoding/json"
"time"
api "github.com/flightctl/flightctl/api/v1alpha1"
"github.com/flightctl/flightctl/internal/util"
)
var (
TemplateVersionAPI = "v1alpha1"
TemplateVersionKind = "TemplateVersion"
TemplateVersionListKind = "TemplateVersionList"
)
type TemplateVersion struct {
ResourceWithPrimaryKeyOwner
// The desired state, stored as opaque JSON object.
Spec *JSONField[api.TemplateVersionSpec]
// The last reported state, stored as opaque JSON object.
Status *JSONField[api.TemplateVersionStatus]
// The rendered ignition config, exposed in a separate endpoint.
RenderedConfig *string
// An indication if this version is valid. It exposed in a Condition but easier to query here.
Valid *bool
}
type TemplateVersionList []TemplateVersion
func (t TemplateVersion) String() string {
val, _ := json.Marshal(t)
return string(val)
}
func NewTemplateVersionFromApiResource(resource *api.TemplateVersion) *TemplateVersion {
// Shouldn't happen, but just to be safe
if resource == nil || resource.Metadata.Name == nil {
return &TemplateVersion{}
}
var status api.TemplateVersionStatus
if resource.Status != nil {
status = *resource.Status
}
return &TemplateVersion{
ResourceWithPrimaryKeyOwner: ResourceWithPrimaryKeyOwner{
Name: *resource.Metadata.Name,
Generation: resource.Metadata.Generation,
Owner: resource.Metadata.Owner,
Annotations: util.LabelMapToArray(resource.Metadata.Annotations),
},
Spec: MakeJSONField(resource.Spec),
Status: MakeJSONField(status),
}
}
func (t *TemplateVersion) ToApiResource() api.TemplateVersion {
// Shouldn't happen, but just to be safe
if t == nil {
return api.TemplateVersion{}
}
var spec api.TemplateVersionSpec
if t.Spec != nil {
spec = t.Spec.Data
}
var status api.TemplateVersionStatus
if t.Status != nil {
status = t.Status.Data
}
metadataAnnotations := util.LabelArrayToMap(t.ResourceWithPrimaryKeyOwner.Annotations)
return api.TemplateVersion{
ApiVersion: TemplateVersionAPI,
Kind: TemplateVersionKind,
Metadata: api.ObjectMeta{
Name: util.StrToPtr(t.Name),
CreationTimestamp: util.StrToPtr(t.CreatedAt.UTC().Format(time.RFC3339)),
Generation: t.Generation,
Owner: t.Owner,
Annotations: &metadataAnnotations,
},
Spec: spec,
Status: &status,
}
}
func (tl TemplateVersionList) ToApiResource(cont *string, numRemaining *int64) api.TemplateVersionList {
// Shouldn't happen, but just to be safe
if tl == nil {
return api.TemplateVersionList{
ApiVersion: TemplateVersionAPI,
Kind: TemplateVersionListKind,
}
}
deviceList := make([]api.TemplateVersion, len(tl))
for i, device := range tl {
deviceList[i] = device.ToApiResource()
}
ret := api.TemplateVersionList{
ApiVersion: TemplateVersionAPI,
Kind: TemplateVersionListKind,
Items: deviceList,
}
if cont != nil {
ret.Metadata.Continue = cont
ret.Metadata.RemainingItemCount = numRemaining
}
return ret
}