/
resource.go
75 lines (63 loc) · 1.98 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
package v1
import (
"encoding/json"
)
type Resources []Resource
// Resource is the representation of a resource in the state.
type Resource struct {
// ID is the unique key of this resource in the whole State.
// ApiVersion:Kind:Namespace:Name is an idiomatic way for Kubernetes resources.
// providerNamespace:providerName:resourceType:resourceName for Terraform resources
ID string `json:"id" yaml:"id"`
// Type represents all Runtimes we supported like Kubernetes and Terraform
Type Type `json:"type" yaml:"type"`
// Attributes represents all specified attributes of this resource
Attributes map[string]interface{} `json:"attributes" yaml:"attributes"`
// DependsOn contains all resources this resource depends on
DependsOn []string `json:"dependsOn,omitempty" yaml:"dependsOn,omitempty"`
// Extensions specifies arbitrary metadata of this resource
Extensions map[string]interface{} `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
func (r *Resource) ResourceKey() string {
return r.ID
}
// DeepCopy return a copy of resource
func (r *Resource) DeepCopy() *Resource {
var out Resource
data, err := json.Marshal(r)
if err != nil {
panic(err)
}
_ = json.Unmarshal(data, &out)
return &out
}
func (rs Resources) Index() map[string]*Resource {
m := make(map[string]*Resource)
for i := range rs {
m[rs[i].ResourceKey()] = &rs[i]
}
return m
}
// GVKIndex returns a map of GVK to resources, for now, only Kubernetes resources.
func (rs Resources) GVKIndex() map[string][]*Resource {
m := make(map[string][]*Resource)
for i := range rs {
resource := &rs[i]
if resource.Type != Kubernetes {
continue
}
gvk := resource.Extensions[ResourceExtensionGVK].(string)
m[gvk] = append(m[gvk], resource)
}
return m
}
func (rs Resources) Len() int { return len(rs) }
func (rs Resources) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs Resources) Less(i, j int) bool {
switch {
case rs[i].ID != rs[j].ID:
return rs[i].ID < rs[j].ID
default:
return false
}
}