forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
249 lines (215 loc) · 5.86 KB
/
resource.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
/*
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 (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/google/badwolf/triple"
"github.com/google/badwolf/triple/literal"
"github.com/google/badwolf/triple/node"
"github.com/wallix/awless/graph/internal/rdf"
)
type Resource struct {
kind ResourceType
id string
Properties Properties
Meta Properties
}
func InitResource(id string, kind ResourceType) *Resource {
return &Resource{id: id, kind: kind, Properties: make(Properties), Meta: make(Properties)}
}
func newResourceType(n *node.Node) ResourceType {
typ := strings.TrimPrefix(n.Type().String(), "/")
return ResourceType(typ)
}
func (res *Resource) String() string {
var identifier string
if res == nil || (res.Id() == "" && res.Type() == "") {
return "[none]"
}
if name, ok := res.Properties["Name"]; ok && name != "" {
identifier = fmt.Sprintf("@%v", name)
} else {
identifier = res.Id()
}
return fmt.Sprintf("%s[%s]", identifier, res.Type())
}
func (res *Resource) Type() ResourceType {
return res.kind
}
func (res *Resource) Id() string {
return res.id
}
// Compare only the id and type of the resources (no properties nor meta)
func (res *Resource) Same(other *Resource) bool {
if res == nil && other == nil {
return true
}
if res == nil || other == nil {
return false
}
return res.Id() == other.Id() && res.Type() == other.Type()
}
func (res *Resource) toRDFNode() (*node.Node, error) {
return node.NewNodeFromStrings(res.kind.ToRDFString(), res.id)
}
func (res *Resource) marshalRDF() ([]*triple.Triple, error) {
var triples []*triple.Triple
n, err := res.toRDFNode()
if err != nil {
return triples, err
}
var lit *literal.Literal
if lit, err = literal.DefaultBuilder().Build(literal.Text, res.kind.ToRDFString()); err != nil {
return triples, err
}
t, err := triple.New(n, rdf.HasTypePredicate, triple.NewLiteralObject(lit))
if err != nil {
return triples, err
}
triples = append(triples, t)
for propKey, propValue := range res.Properties {
prop := Property{Key: propKey, Value: propValue}
propL, err := prop.marshalRDF()
if err != nil {
return nil, err
}
if propT, err := triple.New(n, rdf.PropertyPredicate, propL); err != nil {
return nil, err
} else {
triples = append(triples, propT)
}
}
for metaKey, metaValue := range res.Meta {
prop := Property{Key: metaKey, Value: metaValue}
propL, err := prop.marshalRDF()
if err != nil {
return nil, err
}
if propT, err := triple.New(n, rdf.MetaPredicate, propL); err != nil {
return nil, err
} else {
triples = append(triples, propT)
}
}
return triples, nil
}
type Resources []*Resource
func (res Resources) Map(f func(*Resource) string) (out []string) {
for _, r := range res {
out = append(out, f(r))
}
return
}
type Properties map[string]interface{}
func (props Properties) Subtract(other Properties) Properties {
sub := make(Properties)
for propK, propV := range props {
var found bool
if otherV, ok := other[propK]; ok {
if reflect.DeepEqual(propV, otherV) {
found = true
}
}
if !found {
sub[propK] = propV
}
}
return sub
}
func (props Properties) unmarshalRDF(triples []*triple.Triple) error {
for _, tr := range triples {
prop := &Property{}
if err := prop.unmarshalRDF(tr); err != nil {
return err
}
props[prop.Key] = prop.Value
}
return nil
}
type Property struct {
Key string
Value interface{}
}
func (prop *Property) marshalRDF() (*triple.Object, error) {
json, err := json.Marshal(prop)
if err != nil {
return nil, err
}
var propL *literal.Literal
if propL, err = literal.DefaultBuilder().Build(literal.Text, string(json)); err != nil {
return nil, err
}
return triple.NewLiteralObject(propL), nil
}
func (prop *Property) unmarshalRDF(t *triple.Triple) error {
oL, err := t.Object().Literal()
if err != nil {
return err
}
propStr, err := oL.Text()
if err != nil {
return err
}
if err = json.Unmarshal([]byte(propStr), prop); err != nil {
fmt.Printf("cannot unmarshal %s: %s\n", propStr, err)
}
switch {
case strings.HasSuffix(strings.ToLower(prop.Key), "time"), strings.HasSuffix(strings.ToLower(prop.Key), "date"):
t, err := time.Parse(time.RFC3339, fmt.Sprint(prop.Value))
if err == nil {
prop.Value = t
}
case strings.HasSuffix(strings.ToLower(prop.Key), "timestamp"):
tmstp, err := strconv.Atoi(fmt.Sprint(prop.Value))
if err == nil {
prop.Value = time.Unix(int64(tmstp), 0)
}
case strings.HasSuffix(strings.ToLower(prop.Key), "rules"):
var propRules struct {
Key string
Value []*FirewallRule
}
err = json.Unmarshal([]byte(propStr), &propRules)
if err == nil {
prop.Value = propRules.Value
}
case strings.HasSuffix(strings.ToLower(prop.Key), "routes"):
var propRoutes struct {
Key string
Value []*Route
}
err = json.Unmarshal([]byte(propStr), &propRoutes)
if err == nil {
prop.Value = propRoutes.Value
}
case strings.HasSuffix(strings.ToLower(prop.Key), "grants"):
var propGrants struct {
Key string
Value []*Grant
}
err = json.Unmarshal([]byte(propStr), &propGrants)
if err == nil {
prop.Value = propGrants.Value
}
}
return nil
}
type ResourceById []*Resource
func (r ResourceById) Len() int { return len(r) }
func (r ResourceById) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ResourceById) Less(i, j int) bool { return r[i].Id() < r[j].Id() }