-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_versions.go
72 lines (66 loc) · 1.55 KB
/
resource_versions.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
package vervet
import (
"fmt"
"sort"
"time"
"github.com/getkin/kin-openapi/openapi3"
)
type resourceVersionsSlice []*ResourceVersions
func (s resourceVersionsSlice) validate() error {
for _, v := range s.versions() {
resourcePaths := map[string]string{}
for _, eps := range s {
ep, err := eps.At(v.String())
if err == ErrNoMatchingVersion {
continue
} else if err != nil {
return fmt.Errorf("validation failed: %w", err)
}
for _, path := range ep.Paths.InMatchingOrder() {
if conflict, ok := resourcePaths[path]; ok {
return fmt.Errorf("conflict: %q %q", conflict, ep.sourcePrefix)
}
resourcePaths[path] = ep.sourcePrefix
}
}
}
return nil
}
func (s resourceVersionsSlice) versions() VersionSlice {
vset := map[Version]bool{}
for _, eps := range s {
for i := range eps.versions {
vset[eps.versions[i].Version] = true
}
}
var versions VersionSlice
for v := range vset {
versions = append(versions, v)
}
sort.Sort(versions)
return versions
}
func (s resourceVersionsSlice) at(v Version) (*openapi3.T, error) {
coll := NewCollator()
for _, eps := range s {
ep, err := eps.At(v.String())
if err == ErrNoMatchingVersion {
continue
} else if err != nil {
return nil, err
}
err = coll.Collate(ep)
if err != nil {
return nil, err
}
}
result := coll.Result()
if result == nil {
return nil, ErrNoMatchingVersion
}
if result.Extensions == nil {
result.Extensions = map[string]any{}
}
result.Extensions[ExtSnykApiLifecycle] = v.LifecycleAt(time.Time{}).String()
return result, nil
}