-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
73 lines (65 loc) · 2.26 KB
/
client.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
package snogo
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
//DefaultServiceNowClient get the client from the environment vars
func DefaultServiceNowClient() *ServiceNowInstance {
return NewInstance(
os.Getenv("SERVICE_NOW_INSTANCE_NAME"),
os.Getenv("SERVICE_NOW_USERNAME"),
os.Getenv("SERVICE_NOW_PASSWORD"))
}
//IncidentCreationPayload Data to create an incident in service-now
type IncidentCreationPayload struct {
AssignmentGroup string `json:"assignment_group"`
CmdbCI string `json:"cmdb_ci"`
ContactType string `json:"contact_type"` //Auto Ticket
Customer string `json:"caller_id"`
Description string `json:"description"`
Impact json.Number `json:"impact"`
ShortDescription string `json:"short_description"`
State json.Number `json:"state"`
Urgency json.Number `json:"urgency"`
}
//ServiceNowInstance should hold the necessary data for a client of the ServiceNow TableAPI
type ServiceNowInstance struct {
name string
baseURL string
authHeader string
client *http.Client
}
// NewInstance - Create new service-now instance
func NewInstance(name, user, pass string) *ServiceNowInstance {
return &ServiceNowInstance{
name: name,
baseURL: fmt.Sprintf("https://%s.service-now.com", name),
authHeader: fmt.Sprintf("Basic %s", base64.URLEncoding.EncodeToString([]byte(user+":"+pass))),
client: http.DefaultClient,
}
}
//Create an incident in service-now based on a post body
func (inst *ServiceNowInstance) Create(table string, body []byte) (string, error) {
fmt.Printf("Creating a service now incident")
req, _ := http.NewRequest("POST", buildPostURL(inst.baseURL, table), bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", inst.authHeader)
resp, reqErr := inst.client.Do(req)
if reqErr != nil {
fmt.Println(reqErr)
return "", reqErr
}
// TODO: ignoring an error on this Close
defer resp.Body.Close()
responseBody, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("responseBody: %s", string(responseBody))
return string(responseBody), nil
}
func buildPostURL(baseURL, table string) string {
return fmt.Sprintf("%s/api/now/table/%s", baseURL, table)
}