-
Notifications
You must be signed in to change notification settings - Fork 327
/
compose_chart.go
118 lines (99 loc) · 2.39 KB
/
compose_chart.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
package store
import (
"context"
"github.com/cortezaproject/corteza/server/compose/types"
"github.com/cortezaproject/corteza/server/pkg/envoy/resource"
"github.com/cortezaproject/corteza/server/pkg/filter"
"github.com/cortezaproject/corteza/server/store"
)
type (
composeChart struct {
cfg *EncoderConfig
res *resource.ComposeChart
chr *types.Chart
relNS *types.Namespace
relMods types.ModuleSet
}
)
// mergeComposeChart merges b into a, prioritising a
func mergeComposeChart(a, b *types.Chart) *types.Chart {
c := *a
if c.Handle == "" {
c.Handle = b.Handle
}
if c.Name == "" {
c.Name = b.Name
}
c.NamespaceID = b.NamespaceID
if c.CreatedAt.IsZero() {
c.CreatedAt = b.CreatedAt
}
if c.UpdatedAt == nil {
c.UpdatedAt = b.UpdatedAt
}
if c.DeletedAt == nil {
c.DeletedAt = b.DeletedAt
}
if len(c.Config.Reports) <= 0 {
c.Config.Reports = b.Config.Reports
}
if c.Config.ColorScheme == "" {
c.Config.ColorScheme = b.Config.ColorScheme
}
return &c
}
// findComposeChart looks for the chart in the resources & the store
//
// Provided resources are prioritized.
func findComposeChart(ctx context.Context, s store.Storer, nsID uint64, rr resource.InterfaceSet, ii resource.Identifiers) (ch *types.Chart, err error) {
ch = resource.FindComposeChart(rr, ii)
if ch != nil {
return ch, nil
}
if nsID <= 0 {
return nil, nil
}
// Go in the store
return findComposeChartStore(ctx, s, nsID, makeGenericFilter(ii))
}
// findComposeChartStore looks for the chart in the store
func findComposeChartStore(ctx context.Context, s store.Storer, nsID uint64, gf genericFilter) (ch *types.Chart, err error) {
if nsID == 0 {
return nil, nil
}
if gf.id > 0 {
ch, err = store.LookupComposeChartByID(ctx, s, gf.id)
if err != nil && err != store.ErrNotFound {
return nil, err
}
if ch != nil {
return
}
}
for _, i := range gf.identifiers {
ch, err = store.LookupComposeChartByNamespaceIDHandle(ctx, s, nsID, i)
if err == store.ErrNotFound {
var cc types.ChartSet
cc, _, err = store.SearchComposeCharts(ctx, s, types.ChartFilter{
NamespaceID: nsID,
Name: i,
Paging: filter.Paging{
Limit: 2,
},
})
if len(cc) > 1 {
return nil, resourceErrIdentifierNotUnique(i)
}
if len(cc) == 1 {
ch = cc[0]
}
}
if err != nil && err != store.ErrNotFound {
return nil, err
}
if ch != nil {
return
}
}
return nil, nil
}