-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept_event.go
executable file
·167 lines (142 loc) · 5.13 KB
/
accept_event.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
/*
Copyright © 2022 ITRS Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package snow
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/labstack/echo/v4"
)
func AcceptEvent(c echo.Context) (err error) {
cc := c.(*RouterContext)
vc := cc.Conf
var incident Incident
var ok bool
var cmdb_ci_id, sys_class_name string
defer c.Request().Body.Close()
err = json.NewDecoder(c.Request().Body).Decode(&incident)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
s := InitializeConnection(vc)
if cmdb_ci_id, ok = incident["sys_id"]; !ok {
var search string
if search, ok = incident["search"]; !ok {
if cmdb_ci_id, ok = incident["default_cmdb_ci"]; !ok {
return echo.NewHTTPError(http.StatusNotFound, "must supply either a default_cmdb_ci or a sys_id search parameter")
}
} else {
if vc.GetString("servicenow.searchtype") == "simple" {
if cmdb_ci_id, sys_class_name, err = LookupSysIDSimple(vc, "cmdb_ci", search, incident["default_cmdb_ci"]); err != nil {
return
}
} else {
if cmdb_ci_id, sys_class_name, err = LookupSysID(vc, "cmdb_ci", search, incident["default_cmdb_ci"]); err != nil {
return
}
}
}
}
_ = sys_class_name // unused for now
if cmdb_ci_id == "" {
incident["action"] = "Failed"
return echo.NewHTTPError(http.StatusNotFound, "cmdb_ci sys_id is empty or search resulted in no matches")
}
// save for re-user after defaults
correlation_id := incident["correlation_id"]
response := make(map[string]string)
incident_id, short_description, state, err := LookupIncident(vc, cmdb_ci_id, correlation_id)
if err != nil {
return
}
// always set a short description for an existing incident, even if it's just the one
// we got back from the lookup. if user wants to remove it, they use the config
if state > 0 {
if _, ok := incident["short_description"]; !ok {
incident["short_description"] = short_description
}
}
// look up state mappings for defaults for 'state' - this is a list of strings
for sk, sv := range vc.GetStringMapStringSlice("servicenow.incidentstates") {
if fmt.Sprint(state) == sk {
for _, si := range sv {
// trim spaces as YAML comma lists leave spaces in
si = strings.ToLower(strings.TrimSpace(si))
configDefaults(incident, vc.GetStringMapString("servicenow.incidentstatedefaults."+si))
}
}
}
user := vc.GetString("servicenow.username")
if _, ok := incident["caller_id"]; ok {
user = incident["caller_id"]
}
// real basic validation of user
if !userRE.MatchString(user) {
return echo.NewHTTPError(http.StatusBadRequest, "username supplied is invalid")
}
// only lookup user after all defaults applied
u, err := s.GET("1", "sys_id", "", "user_name="+user, "").QueryTableDetail("sys_user")
if err != nil || len(u) == 0 {
return echo.NewHTTPError(http.StatusNotFound, "User not found")
}
incident["caller_id"] = u["sys_id"]
if incident_id != "" {
incidentID, err := UpdateIncident(vc, incident_id, incident)
if err != nil {
return err
}
response["host"] = incident["host"]
response["action"] = "Updated"
response["number"] = incidentID
response["event_type"] = "Incident"
} else if incident["update_only"] == "true" {
response["host"] = incident["host"]
response["action"] = "Ignored"
response["event_type"] = "Incident"
return echo.NewHTTPError(http.StatusAccepted, "No Incident Created. 'update only' option set.")
} else {
incident["correlation_id"] = correlation_id
incidentID, err := CreateIncident(vc, cmdb_ci_id, incident)
if err != nil {
return err
}
response["host"] = incident["host"]
response["action"] = "Created"
response["number"] = incidentID
response["event_type"] = "Incident"
}
return c.JSON(201, response)
}
// an empty value means delete any value passed - e.g. short_description in an update
func configDefaults(incident Incident, defaults map[string]string) {
for k, v := range defaults {
if _, ok := incident[k]; !ok {
// trim spaces and surrounding quotes before unquoting embedded escapes
str, err := strconv.Unquote(`"` + strings.Trim(v, `"`) + `"`)
if err == nil {
v = str
}
incident[k] = v
} else if v == "" {
delete(incident, k)
}
}
}