forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 3
/
graph.go
398 lines (348 loc) · 10.2 KB
/
graph.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
Copyright 2017 WALLIX
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package graph
import (
"bytes"
"fmt"
"io"
"os"
"github.com/thunderbird86/awless/cloud"
"github.com/thunderbird86/awless/cloud/rdf"
tstore "github.com/wallix/triplestore"
)
type Graph struct {
store tstore.Source
}
func NewGraph() *Graph {
return &Graph{tstore.NewSource()}
}
func NewGraphFromFiles(files ...string) (cloud.GraphAPI, error) {
g := NewGraph()
var readers []io.Reader
for _, f := range files {
if reader, err := os.Open(f); err != nil {
return g, err
} else {
readers = append(readers, reader)
}
}
err := g.UnmarshalFromReaders(readers...)
return g, err
}
func (g *Graph) AsRDFGraphSnaphot() tstore.RDFGraph {
return g.store.Snapshot()
}
func (g *Graph) AddResource(resources ...*Resource) error {
for _, res := range resources {
triples, err := res.marshalFullRDF()
if err != nil {
return err
}
for relType, attachedRes := range res.relations {
switch relType {
case rdf.ChildrenOfRel:
for _, attached := range attachedRes {
if err := g.AddParentRelation(attached, res); err != nil {
return err
}
}
case rdf.DependingOnRel:
for _, attached := range attachedRes {
if err := g.AddAppliesOnRelation(attached, res); err != nil {
return err
}
}
}
}
g.store.Add(triples...)
}
return nil
}
func (g *Graph) AddGraph(other *Graph) {
g.store.Add(other.store.CopyTriples()...)
}
func (g *Graph) AddParentRelation(parent, child *Resource) error {
return g.addRelation(parent, child, rdf.ParentOf)
}
func (g *Graph) AddAppliesOnRelation(parent, child *Resource) error {
return g.addRelation(parent, child, rdf.ApplyOn)
}
func (g *Graph) GetResource(t string, id string) (*Resource, error) {
resource := InitResource(t, id)
snap := g.store.Snapshot()
if err := resource.unmarshalFullRdf(snap); err != nil {
return resource, err
}
if err := resource.unmarshalMeta(snap); err != nil {
return resource, err
}
return resource, nil
}
func (g *Graph) FindResource(id string) (*Resource, error) {
byId := &ById{id}
resources, err := byId.Resolve(g.store.Snapshot())
if err != nil {
return nil, err
}
if len(resources) == 1 {
return resources[0], nil
} else if len(resources) > 1 {
return nil, fmt.Errorf("multiple resources with id '%s' found", id)
}
return nil, nil
}
func (g *Graph) FindResourcesByProperty(key string, value interface{}) ([]*Resource, error) {
byProperty := ByProperty{key, value}
return byProperty.Resolve(g.store.Snapshot())
}
func (g *Graph) FindAncestor(res *Resource, resourceType string) *Resource {
var found *Resource
find := func(res *Resource, depth int) error {
if res.Type() == resourceType {
found = res
return nil
}
return nil
}
g.Accept(&ParentsVisitor{From: res, Each: find})
return found
}
func (g *Graph) GetAllResources(typs ...string) ([]*Resource, error) {
byTypes := &ByTypes{typs}
return byTypes.Resolve(g.store.Snapshot())
}
func (g *Graph) ResolveResources(resolvers ...Resolver) ([]*Resource, error) {
var resources []*Resource
snap := g.store.Snapshot()
for _, resolv := range resolvers {
rs, err := resolv.Resolve(snap)
if err != nil {
return resources, err
}
resources = append(resources, rs...)
}
return resources, nil
}
func (g *Graph) FilterGraph(q cloud.Query) (cloud.GraphAPI, error) {
if len(q.ResourceType) != 1 {
return nil, fmt.Errorf("invalid query: must have exactly one resource type, got %d", len(q.ResourceType))
}
resourceType := q.ResourceType[0]
return g.Filter(resourceType, func(r *Resource) bool {
if q.Matcher == nil {
return true
}
return q.Matcher.Match(r)
})
}
func (g *Graph) Find(q cloud.Query) ([]cloud.Resource, error) {
var resources []*Resource
var err error
switch len(q.ResourceType) {
case 0:
return nil, fmt.Errorf("invalid query: need at least one resource type")
case 1:
var filtered cloud.GraphAPI
filtered, err = g.FilterGraph(q)
if err != nil {
return nil, err
}
resources, err = filtered.(*Graph).GetAllResources(q.ResourceType[0])
if err != nil {
return nil, err
}
default:
if q.Matcher != nil {
return nil, fmt.Errorf("invalid query: can not filter whith multiple resource types")
}
resources, err = g.GetAllResources(q.ResourceType...)
if err != nil {
return nil, err
}
}
var res []cloud.Resource
for _, r := range resources {
res = append(res, r)
}
return res, nil
}
func (g *Graph) FindWithProperties(props map[string]interface{}) ([]cloud.Resource, error) {
var resolvers []Resolver
for k, v := range props {
resolvers = append(resolvers, &ByProperty{Key: k, Value: v})
}
resources, err := g.ResolveResources(&And{Resolvers: resolvers})
if err != nil {
return nil, err
}
var res []cloud.Resource
for _, r := range resources {
res = append(res, r)
}
return res, nil
}
func (g *Graph) FindOne(q cloud.Query) (cloud.Resource, error) {
filtered, err := g.FilterGraph(q)
if err != nil {
return nil, err
}
resources, err := filtered.(*Graph).GetAllResources(q.ResourceType[0])
if err != nil {
return nil, err
}
switch len(resources) {
case 0:
return nil, fmt.Errorf("resource not found")
case 1:
return resources[0], nil
default:
return nil, fmt.Errorf("multiple resources found")
}
}
func (g *Graph) Merge(mg cloud.GraphAPI) error {
toMerge, ok := mg.(*Graph)
if !ok {
return fmt.Errorf("can not merge graphs, graph to merge is not a *graph.Graph, but a %T", mg)
}
g.AddGraph(toMerge)
return nil
}
func (g *Graph) ResourceRelations(from cloud.Resource, relation string, recursive bool) (collect []cloud.Resource, err error) {
collectFunc := func(r *Resource, depth int) error {
if depth == 1 || recursive {
collect = append(collect, r)
}
return nil
}
switch relation {
case rdf.ChildrenOfRel:
err = g.Accept(&ChildrenVisitor{From: from.(*Resource), IncludeFrom: false, Relation: rdf.ParentOf, Each: collectFunc})
case rdf.DependingOnRel:
err = g.Accept(&ParentsVisitor{From: from.(*Resource), IncludeFrom: false, Relation: rdf.ApplyOn, Each: collectFunc})
case rdf.ApplyOn:
err = g.Accept(&ChildrenVisitor{From: from.(*Resource), IncludeFrom: false, Relation: rdf.ApplyOn, Each: collectFunc})
default:
err = g.Accept(&ParentsVisitor{From: from.(*Resource), IncludeFrom: false, Relation: relation, Each: collectFunc})
}
return
}
func (g *Graph) VisitRelations(from cloud.Resource, relation string, includeFrom bool, each func(cloud.Resource, int) error) (err error) {
eachFunc := func(r *Resource, depth int) error {
return each(r, depth)
}
switch relation {
case rdf.ChildrenOfRel:
err = g.Accept(&ChildrenVisitor{From: from.(*Resource), IncludeFrom: includeFrom, Relation: rdf.ParentOf, Each: eachFunc})
case rdf.DependingOnRel:
err = g.Accept(&ParentsVisitor{From: from.(*Resource), IncludeFrom: includeFrom, Relation: rdf.ApplyOn, Each: eachFunc})
case rdf.ApplyOn:
err = g.Accept(&ChildrenVisitor{From: from.(*Resource), IncludeFrom: includeFrom, Relation: rdf.ApplyOn, Each: eachFunc})
default:
err = g.Accept(&ParentsVisitor{From: from.(*Resource), IncludeFrom: includeFrom, Relation: relation, Each: eachFunc})
}
return
}
func (g *Graph) ResourceSiblings(res cloud.Resource) (collect []cloud.Resource, err error) {
err = g.Accept(&SiblingsVisitor{From: res.(*Resource), IncludeFrom: false, Each: func(r *Resource, depth int) error {
collect = append(collect, r)
return nil
}})
return collect, err
}
func ResolveResourcesWithProp(snap tstore.RDFGraph, resType, propName, propVal string) ([]*Resource, error) {
resolv := ByTypeAndProperty{
Type: resType,
Key: propName,
Value: propVal,
}
return resolv.Resolve(snap)
}
func (g *Graph) ListResourcesDependingOn(start *Resource) ([]*Resource, error) {
var resources []*Resource
snap := g.store.Snapshot()
for _, tri := range snap.WithPredObj(rdf.ApplyOn, tstore.Resource(start.Id())) {
id := tri.Subject()
rT, err := resolveResourceType(snap, id)
if err != nil {
if err == errTypeNotFound {
resources = append(resources, NotFoundResource(id))
continue
}
return resources, err
}
res, err := g.GetResource(rT, id)
if err != nil {
return resources, err
}
resources = append(resources, res)
}
return resources, nil
}
func (g *Graph) ListResourcesAppliedOn(start *Resource) ([]*Resource, error) {
var resources []*Resource
snap := g.store.Snapshot()
for _, tri := range snap.WithSubjPred(start.Id(), rdf.ApplyOn) {
id, ok := tri.Object().Resource()
if !ok {
return resources, fmt.Errorf("triple %s %s: object is not a resource identifier", start.Id(), rdf.ApplyOn)
}
rT, err := resolveResourceType(snap, id)
if err != nil {
if err == errTypeNotFound {
resources = append(resources, NotFoundResource(id))
continue
}
return resources, err
}
res, err := g.GetResource(rT, id)
if err != nil {
return resources, err
}
resources = append(resources, res)
}
return resources, nil
}
func (g *Graph) Accept(v Visitor) error {
return v.Visit(g)
}
func (g *Graph) Unmarshal(data []byte) error {
ts, err := tstore.NewAutoDecoder(bytes.NewReader(data)).Decode()
if err != nil {
return err
}
g.store.Add(ts...)
return nil
}
func (g *Graph) UnmarshalFromReaders(readers ...io.Reader) error {
dec := tstore.NewDatasetDecoder(tstore.NewLenientNTDecoder, readers...)
ts, err := dec.Decode()
if err != nil {
return err
}
g.store.Add(ts...)
return nil
}
func (g *Graph) MustMarshal() string {
var buff bytes.Buffer
if err := tstore.NewLenientNTEncoder(&buff).Encode(g.store.CopyTriples()...); err != nil {
panic(err)
}
return string(buff.Bytes())
}
func (g *Graph) MarshalTo(w io.Writer) error {
return tstore.NewLenientNTEncoder(w).Encode(g.store.CopyTriples()...)
}
func (g *Graph) addRelation(one, other *Resource, pred string) error {
g.store.Add(tstore.SubjPred(one.Id(), pred).Resource(other.Id()))
return nil
}