-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
fake_servicenow_test.go
195 lines (166 loc) · 5.11 KB
/
fake_servicenow_test.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
/*
Copyright 2023 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package servicenow
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"runtime/debug"
"sync"
"sync/atomic"
"github.com/gravitational/trace"
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"github.com/gravitational/teleport/integrations/lib/stringset"
)
type FakeServiceNow struct {
srv *httptest.Server
objects sync.Map
// Incidents
incidentIDCounter uint64
newIncidents chan Incident
incidentUpdates chan Incident
// Incident notes
newIncidentNotes chan string
}
type QueryValues url.Values
func (q QueryValues) GetAsSet(name string) stringset.StringSet {
values := q[name]
result := stringset.NewWithCap(len(values))
for _, v := range values {
if v != "" {
result[v] = struct{}{}
}
}
if len(result) == 0 {
return nil
}
return result
}
type FakeIncident struct {
IncidentID string
Incident
}
func NewFakeServiceNow(concurrency int) *FakeServiceNow {
router := httprouter.New()
serviceNow := &FakeServiceNow{
newIncidents: make(chan Incident, concurrency),
incidentUpdates: make(chan Incident, concurrency),
newIncidentNotes: make(chan string, concurrency*3), // for any incident there could be 1-3 notes
srv: httptest.NewServer(router),
}
router.POST("/api/now/v1/table/incident", func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
var incident Incident
err := json.NewDecoder(r.Body).Decode(&incident)
panicIf(err)
incident.IncidentID = fmt.Sprintf("incident-%v", atomic.AddUint64(&serviceNow.incidentIDCounter, 1))
serviceNow.StoreIncident(incident)
serviceNow.newIncidents <- incident
err = json.NewEncoder(rw).Encode(incidentResult{Result: struct {
IncidentID string `json:"sys_id,omitempty"`
ShortDescription string `json:"short_description,omitempty"`
Description string `json:"description,omitempty"`
CloseCode string `json:"close_code,omitempty"`
CloseNotes string `json:"close_notes,omitempty"`
IncidentState string `json:"incident_state,omitempty"`
WorkNotes string `json:"work_notes,omitempty"`
}{
IncidentID: incident.IncidentID,
ShortDescription: incident.ShortDescription,
Description: incident.Description,
CloseCode: incident.CloseCode,
CloseNotes: incident.CloseNotes,
IncidentState: incident.IncidentState,
WorkNotes: incident.WorkNotes,
}})
panicIf(err)
})
router.PATCH("/api/now/v1/table/incident/:incidentID/", func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
incidentID := ps.ByName("incidentID")
var body Incident
err := json.NewDecoder(r.Body).Decode(&body)
panicIf(err)
incident, found := serviceNow.GetIncident(incidentID)
if !found {
rw.WriteHeader(http.StatusNotFound)
return
}
if body.WorkNotes != "" {
incident.WorkNotes = body.WorkNotes
}
if body.CloseNotes != "" {
incident.CloseNotes = body.CloseNotes
}
if body.CloseCode != "" {
incident.CloseCode = body.CloseCode
}
if body.IncidentState != "" {
incident.IncidentState = body.IncidentState
}
serviceNow.StoreIncident(incident)
serviceNow.incidentUpdates <- incident
})
return serviceNow
}
func (s *FakeServiceNow) URL() string {
return s.srv.URL
}
func (s *FakeServiceNow) Close() {
s.srv.Close()
close(s.newIncidents)
close(s.incidentUpdates)
}
func (s *FakeServiceNow) GetIncident(id string) (Incident, bool) {
if obj, ok := s.objects.Load(id); ok {
incident, ok := obj.(Incident)
return incident, ok
}
return Incident{}, false
}
func (s *FakeServiceNow) StoreIncident(incident Incident) Incident {
if incident.IncidentID == "" {
incident.IncidentID = fmt.Sprintf("incident-%v", atomic.AddUint64(&s.incidentIDCounter, 1))
}
s.objects.Store(incident.IncidentID, incident)
return incident
}
func (s *FakeServiceNow) CheckNewIncident(ctx context.Context) (Incident, error) {
select {
case incident := <-s.newIncidents:
return incident, nil
case <-ctx.Done():
return Incident{}, trace.Wrap(ctx.Err())
}
}
func (s *FakeServiceNow) CheckIncidentUpdate(ctx context.Context) (Incident, error) {
select {
case incident := <-s.incidentUpdates:
return incident, nil
case <-ctx.Done():
return Incident{}, trace.Wrap(ctx.Err())
}
}
func (s *FakeServiceNow) StoreResponder(ctx context.Context, responderID string) string {
s.objects.Store(responderID, responderID)
return responderID
}
func panicIf(err error) {
if err != nil {
log.Panicf("%v at %v", err, string(debug.Stack()))
}
}