-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
141 lines (126 loc) · 3.76 KB
/
search.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
package controller
import (
"fmt"
web "github.com/JIexa24/chef-webapi"
mergeSort "github.com/JIexa24/go-mergeSort"
"github.com/go-chef/chef"
)
// GetResourceBySearch return list what contains list of Resource on the server
// with specified search.
func GetResourceBySearch(index, val string, dontCutParseResult bool) ([]map[string]interface{}, error) {
if val == "*:*" {
return nil, fmt.Errorf("not allowed pattern *:*")
}
if val == "" {
return nil, nil
}
client := web.App.GetChefClientConfig()
if client == nil {
return make([]map[string]interface{}, 0),
fmt.Errorf("client not configured")
}
query, err := client.Search.NewQuery(index, val)
if err != nil {
return nil, err
}
// Run the query
list, err := query.Do(client)
if err != nil {
return nil, err
}
var result []map[string]interface{}
// Processing search
switch index {
case "node":
result = parseNodes(&list, dontCutParseResult)
case "role":
result = parseRoles(&list)
case "client":
result = parseClients(&list)
case "environment":
result = parseEnvs(&list)
}
return result, nil
}
func parseNodes(list *chef.SearchResult, dontCutParseResult bool) []map[string]interface{} {
var sliceToSort = make([]mergeSort.Interface, 0)
for _, v := range list.Rows {
el := make(map[string]interface{})
el["name"] = v.(map[string]interface{})["name"]
el["selected"] = false
runList := v.(map[string]interface{})["run_list"]
resultRunList := make([]map[string]interface{}, 0)
if runList != nil {
runListArray := runList.([]interface{})
for i := range runListArray {
runListElement := make(map[string]interface{})
runListElement["selected"] = false
runListElement["object"] = runListArray[i].(string)
resultRunList = append(resultRunList, runListElement)
}
}
el["run_list"] = resultRunList
el["ipaddress"] =
v.(map[string]interface{})["automatic"].(map[string]interface{})["ipaddress"]
el["fqdn"] = v.(map[string]interface{})["automatic"].(map[string]interface{})["fqdn"]
el["ohai_time"] = v.(map[string]interface{})["automatic"].(map[string]interface{})["ohai_time"]
sliceToSort = append(sliceToSort, el)
if dontCutParseResult {
el["data"] = v
}
}
compareNameFunction := func(a, b mergeSort.Interface) bool {
return a.(map[string]interface{})["name"].(string) <
b.(map[string]interface{})["name"].(string)
}
sliceToSort = mergeSort.Sort(sliceToSort, compareNameFunction, false)
result := make([]map[string]interface{}, len(sliceToSort))
for i, v := range sliceToSort {
result[i] = v.(map[string]interface{})
}
return result
}
func parseRoles(list *chef.SearchResult) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
for _, v := range list.Rows {
el := make(map[string]interface{})
el["res"] = v.(map[string]interface{})
result = append(result, el)
}
return result
}
func parseClients(list *chef.SearchResult) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
for _, v := range list.Rows {
el := make(map[string]interface{})
el["res"] = v.(map[string]interface{})
result = append(result, el)
}
return result
}
func parseEnvs(list *chef.SearchResult) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
for _, v := range list.Rows {
el := make(map[string]interface{})
el["res"] = v.(map[string]interface{})
result = append(result, el)
}
return result
}
// ConstructSearchORFilter constructing string like
// 'names[0] OR names[1] OR ... OR names[n]'
func ConstructSearchORFilter(sign string, names []string) string {
var result string
if sign == "" {
return result
}
var length = len(names)
var orstring = " OR "
for i, v := range names {
result = result + fmt.Sprintf("%s:%s", sign, v)
if i < length-1 {
result = result + orstring
}
}
return result
}