-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
124 lines (110 loc) · 2.68 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/sysward/sysward-agent/logging"
)
type WebApi interface {
JobPostBack(Job)
JobFailure(Job, string)
GetJobs() string
CheckIn(AgentData) error
}
type SyswardApi struct {
httpClient http.Client
}
func (r SyswardApi) CheckIn(agentData AgentData) error {
client := r.httpClient
logging.LogMsg("building json - start")
o, err := agentData.ToJson()
if err != nil {
logging.LogMsg(fmt.Sprintf("[fatal] %s", err))
return nil
}
logging.LogMsg("building json - finish")
logging.LogMsg("posting to api - start")
if os.Getenv("DEBUG") == "true" {
formatted_output, _ := json.MarshalIndent(o, "", "\t")
fmt.Println(string(formatted_output))
}
post_data := strings.NewReader(o)
req, err := http.NewRequest("POST", config.agentCheckinUrl(), post_data)
if err != nil {
logging.LogMsg(fmt.Sprintf("[fatal] %s", err))
return nil
}
str, err := client.Do(req)
if err != nil {
logging.LogMsg(fmt.Sprintf("[fatal] %s", err))
return nil
}
logging.LogMsg(string(str.Status))
logging.LogMsg("posting to api - finish")
return nil
}
func (r SyswardApi) GetJobs() string {
client := r.httpClient
job_url := config.fetchJobUrl(getSystemUID())
jreq, err := client.Get(job_url)
if err != nil || jreq.StatusCode != 200 {
logging.LogMsg(fmt.Sprintf("Error requesting jobs: %s", err))
return ""
}
j, err := ioutil.ReadAll(jreq.Body)
if err != nil {
logging.LogMsg(fmt.Sprintf("Error reading jobs: %s", err))
return ""
}
jreq.Body.Close()
return string(j)
}
func (r SyswardApi) JobFailure(job Job, error_string string) {
logging.LogMsg("Posting job FAIL")
client := r.httpClient
data := struct {
JobId int `json:"job_id"`
Status string `json:"status"`
ErrorMessage string `json:"error_message"`
}{
job.JobId,
"failure",
error_string,
}
o, err := json.Marshal(data)
if err != nil {
panic(err)
}
post_data := strings.NewReader(string(o))
req, err := http.NewRequest("POST", config.fetchJobPostbackUrl(), post_data)
req.Header.Add("X-Sysward-Uid", getSystemUID())
_, err = client.Do(req)
if err != nil {
fmt.Println("Error posting to: " + config.fetchJobPostbackUrl())
panic(err)
}
}
func (r SyswardApi) JobPostBack(job Job) {
client := r.httpClient
data := struct {
JobId int `json:"job_id"`
Status string `json:"status"`
}{
job.JobId,
"success",
}
o, err := json.Marshal(data)
if err != nil {
panic(err)
}
post_data := strings.NewReader(string(o))
req, err := http.NewRequest("POST", config.fetchJobPostbackUrl(), post_data)
req.Header.Add("X-Sysward-Uid", getSystemUID())
_, err = client.Do(req)
if err != nil {
panic(err)
}
}