forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
121 lines (112 loc) · 3.06 KB
/
common.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
package common
import (
"strings"
"github.com/rancher/norman/clientbase"
"github.com/rancher/norman/types"
)
func SortSchema(schemas map[string]types.Schema) []string {
inserted := map[string]bool{}
result := []string{}
for i := 0; i < 100; i++ {
for k, schema := range schemas {
if inserted[k] {
continue
}
ready := true
for fieldName, field := range schema.ResourceFields {
if strings.Contains(field.Type, "reference") {
reference := GetReference(field.Type)
if isNamespaceIDRef(reference, k) {
continue
}
if !inserted[reference] && fieldName != "creatorId" && k != reference {
ready = false
}
}
}
if ready {
inserted[k] = true
result = append(result, k)
}
}
}
return result
}
var (
namespacedSchema = map[string]bool{
"project": true,
}
)
func isNamespaceIDRef(ref, schemaType string) bool {
if ref == "namespace" && namespacedSchema[schemaType] {
return true
}
return false
}
func GetReference(name string) string {
name = strings.TrimSuffix(strings.TrimPrefix(name, "array["), "]")
r := strings.TrimSuffix(strings.TrimPrefix(name, "reference["), "]")
return strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(r, "/v3/schemas/"), "/v3/clusters/schemas/"), "/v3/projects/schemas/")
}
// ReplaceGlobalReference replace name to id
func ReplaceGlobalReference(schema types.Schema, data map[string]interface{}, referenceMap map[string]map[string]string, client *clientbase.APIBaseClient) error {
for key, field := range schema.ResourceFields {
if strings.Contains(field.Type, "reference") {
reference := GetReference(field.Type)
if _, ok := data[key]; !ok {
continue
}
if err := FillInReferenceMap(client, reference, referenceMap, nil); err != nil {
return err
}
if strings.HasPrefix(field.Type, "array") {
r := []string{}
for _, k := range data[key].([]interface{}) {
r = append(r, referenceMap[reference][k.(string)])
}
data[key] = r
} else {
data[key] = referenceMap[reference][data[key].(string)]
}
}
}
return nil
}
func FillInReferenceMap(client *clientbase.APIBaseClient, schemaKey string, referenceMap map[string]map[string]string, filter map[string]string) error {
if _, ok := referenceMap[schemaKey]; ok {
return nil
}
referenceMap[schemaKey] = map[string]string{}
respObj := map[string]interface{}{}
if err := client.List(schemaKey, &types.ListOpts{}, &respObj); err != nil {
return err
}
if data, ok := respObj["data"]; ok {
if collections, ok := data.([]interface{}); ok {
for _, obj := range collections {
if objMap, ok := obj.(map[string]interface{}); ok {
id := GetValue(objMap, "id")
name := GetValue(objMap, "name")
filtered := true
for k, v := range filter {
if GetValue(objMap, k) != v {
filtered = false
}
}
if filtered {
referenceMap[schemaKey][name] = id
}
}
}
}
}
return nil
}
func GetValue(data map[string]interface{}, key string) string {
if v, ok := data[key]; ok {
if _, ok := v.(string); ok {
return v.(string)
}
}
return ""
}