-
Notifications
You must be signed in to change notification settings - Fork 9
/
list.go
83 lines (72 loc) · 2.19 KB
/
list.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
package openshift
import (
"errors"
"github.com/ghodss/yaml"
"github.com/opendevstack/tailor/pkg/cli"
"github.com/opendevstack/tailor/pkg/utils"
"github.com/xeipuuv/gojsonpointer"
)
// ResourceList is a collection of resources that conform to a filter.
type ResourceList struct {
Filter *ResourceFilter
Items []*ResourceItem
}
// NewTemplateBasedResourceList assembles a ResourceList from an input that is
// treated as coming from a local template (desired state).
func NewTemplateBasedResourceList(filter *ResourceFilter, inputs ...[]byte) (*ResourceList, error) {
list := &ResourceList{Filter: filter}
err := list.appendItems("template", "/items", inputs...)
return list, err
}
// NewPlatformBasedResourceList assembles a ResourceList from an input that is
// treated as coming from an OpenShift cluster (current state).
func NewPlatformBasedResourceList(filter *ResourceFilter, inputs ...[]byte) (*ResourceList, error) {
list := &ResourceList{Filter: filter}
err := list.appendItems("platform", "/items", inputs...)
return list, err
}
// Length returns the number of items in the resource list
func (l *ResourceList) Length() int {
return len(l.Items)
}
func (l *ResourceList) getItem(kind string, name string) (*ResourceItem, error) {
for _, item := range l.Items {
if item.Kind == kind && item.Name == name {
return item, nil
}
}
return nil, errors.New("No such item")
}
func (l *ResourceList) appendItems(source, itemsField string, inputs ...[]byte) error {
for _, input := range inputs {
if len(input) == 0 {
cli.DebugMsg("Input config empty")
continue
}
var f interface{}
err := yaml.Unmarshal(input, &f)
if err != nil {
err = utils.DisplaySyntaxError(input, err)
return err
}
m := f.(map[string]interface{})
p, _ := gojsonpointer.NewJsonPointer(itemsField)
items, _, err := p.Get(m)
if err != nil {
return err
}
if items == nil {
return errors.New("Cannot find items to append")
}
for _, v := range items.([]interface{}) {
item, err := NewResourceItem(v.(map[string]interface{}), source)
if err != nil {
return err
}
if item.Comparable && l.Filter.SatisfiedBy(item) {
l.Items = append(l.Items, item)
}
}
}
return nil
}