-
Notifications
You must be signed in to change notification settings - Fork 2
/
cric.go
218 lines (204 loc) · 5.92 KB
/
cric.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
package services
// DAS service module
// CRIC module
//
// Copyright (c) 2015-2016 - Valentin Kuznetsov <vkuznet AT gmail dot com>
//
import (
"bytes"
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
"github.com/dmwm/das2go/dasql"
"github.com/dmwm/das2go/mongo"
"github.com/dmwm/das2go/utils"
)
// helper function to load CRIC data stream
func loadCRICData(api string, data []byte) []mongo.DASRecord {
var out []mongo.DASRecord
var rec mongo.DASRecord
// to prevent json.Unmarshal behavior to convert all numbers to float
// we'll use json decode method with instructions to use numbers as is
buf := bytes.NewBuffer(data)
dec := json.NewDecoder(buf)
dec.UseNumber()
err := dec.Decode(&rec)
// original way to decode data
// err := json.Unmarshal(data, &rec)
if err != nil {
msg := fmt.Sprintf("CRIC unable to unmarshal the data into DAS record, api=%s, data=%s, error=%v", api, string(data), err)
if utils.VERBOSE > 0 {
log.Printf("ERROR: CRIC unable to unmarshal, data %+v, api %v, error %v\n", string(data), api, err)
}
out = append(out, mongo.DASErrorRecord(msg, utils.CRICErrorName, utils.CRICError))
return out
}
desc := rec["desc"].(map[string]interface{})
headers := desc["columns"].([]interface{})
values := rec["result"].([]interface{})
for _, item := range values {
row := make(mongo.DASRecord)
val := item.([]interface{})
for i, h := range headers {
key := h.(string)
row[key] = val[i]
if key == "username" {
row["name"] = row[key]
}
}
out = append(out, row)
}
return out
}
// CRICUnmarshal unmarshals CRIC data stream and return DAS records based on api
func CRICUnmarshal(api string, data []byte) []mongo.DASRecord {
records := loadCRICData(api, data)
return records
}
func getCRICData(api string) []mongo.DASRecord {
furl := CricUrl(api)
if strings.Contains(api, "site") {
furl = fmt.Sprintf("%s?json&preset=site-names&rcsite_state=ANY", furl)
} else if strings.Contains(api, "group") {
furl = fmt.Sprintf("%s?json&preset=group-responsibilities", furl)
} else if strings.Contains(api, "role") {
furl = fmt.Sprintf("%s?json&preset=roles", furl)
} else {
furl = fmt.Sprintf("%s?json&preset=people", furl)
}
client := utils.HttpClient()
response := utils.FetchResponse(client, furl, "")
if response.Error == nil {
records := loadCRICData(api, response.Data)
return records
}
log.Printf("ERROR: cric, api %v, error %v\n", api, response.Error)
var out []mongo.DASRecord
return out
}
// CricSiteNames local API returns site-names
func (LocalAPIs) CricSiteNames(dasquery dasql.DASQuery) []mongo.DASRecord {
spec := dasquery.Spec
var out []mongo.DASRecord
api := "site-names"
site := spec["site"].(string)
sitePattern := ""
if strings.Contains(site, "*") {
sitePattern = strings.Replace(site, "*", "", -1)
}
records := getCRICData(api)
for _, r := range records {
siteName := r["alias"].(string)
r["name"] = r["alias"]
if siteName == site {
out = append(out, r)
} else if len(sitePattern) > 0 {
matched, _ := regexp.MatchString(fmt.Sprintf("^%s", sitePattern), siteName)
if matched {
out = append(out, r)
}
}
}
return out
}
// CricGroups local API returns group names
func (LocalAPIs) CricGroups(dasquery dasql.DASQuery) []mongo.DASRecord {
spec := dasquery.Spec
var out []mongo.DASRecord
api := "groups"
group := spec["group"].(string)
groupPattern := ""
if strings.Contains(group, "*") {
groupPattern = strings.Replace(group, "*", "", -1)
}
records := getCRICData(api)
for _, r := range records {
groupName := r["name"].(string)
if groupName == group {
out = append(out, r)
} else if len(groupPattern) > 0 && strings.Contains(groupName, groupPattern) {
out = append(out, r)
}
}
return out
}
// CricGroupResponsibilities return group responsibilities
func (LocalAPIs) CricGroupResponsibilities(dasquery dasql.DASQuery) []mongo.DASRecord {
spec := dasquery.Spec
var out []mongo.DASRecord
api := "group-responsibilities"
group := spec["group"].(string)
groupPattern := ""
if strings.Contains(group, "*") {
groupPattern = strings.Replace(group, "*", "", -1)
}
records := getCRICData(api)
for _, r := range records {
val := r["user_name"]
if val != nil {
groupName := val.(string)
r["name"] = r["user_group"]
if groupName == group {
out = append(out, r)
} else if len(groupPattern) > 0 && strings.Contains(groupName, groupPattern) {
out = append(out, r)
}
}
}
return out
}
// CricPeopleEmail returns CRIC people via email
func (LocalAPIs) CricPeopleEmail(dasquery dasql.DASQuery) []mongo.DASRecord {
spec := dasquery.Spec
var out []mongo.DASRecord
api := "people"
user := spec["user"].(string)
records := getCRICData(api)
for _, r := range records {
if r["email"].(string) == user {
out = append(out, r)
}
}
return out
}
// CricPeopleName returns CRIC people via names
func (LocalAPIs) CricPeopleName(dasquery dasql.DASQuery) []mongo.DASRecord {
spec := dasquery.Spec
var out []mongo.DASRecord
api := "people"
user := strings.ToLower(spec["user"].(string))
records := getCRICData(api)
for _, r := range records {
username := strings.ToLower(r["username"].(string))
forename := strings.ToLower(r["forename"].(string))
surname := strings.ToLower(r["surname"].(string))
email := strings.ToLower(r["email"].(string))
if username == user || forename == user || surname == user || email == user {
out = append(out, r)
}
}
return out
}
// CricRoles returns CRIC roles
func (LocalAPIs) CricRoles(dasquery dasql.DASQuery) []mongo.DASRecord {
spec := dasquery.Spec
var out []mongo.DASRecord
api := "roles"
role := spec["role"].(string)
rolePattern := ""
if strings.Contains(role, "*") {
rolePattern = strings.Replace(role, "*", "", -1)
}
records := getCRICData(api)
for _, r := range records {
roleTitle := r["title"].(string)
if roleTitle == role {
out = append(out, r)
} else if len(rolePattern) > 0 && strings.Contains(roleTitle, rolePattern) {
out = append(out, r)
}
}
return out
}