forked from Colstuwjx/awx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
131 lines (108 loc) · 3.23 KB
/
job.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
package awx
import (
"bytes"
"encoding/json"
"fmt"
)
// Enum of job statuses.
const (
JobStatusNew = "new"
JobStatusPending = "pending"
JobStatusWaiting = "waiting"
JobStatusRunning = "running"
JobStatusSuccessful = "successful"
JobStatusFailed = "failed"
JobStatusError = "error"
JobStatusCanceled = "canceled"
)
// JobService implements awx job apis.
type JobService struct {
client *Client
}
// HostSummariesResponse represents `JobHostSummaries` endpoint response.
type HostSummariesResponse struct {
Pagination
Results []HostSummary `json:"results"`
}
// JobEventsResponse represents `JobEvents` endpoint response.
type JobEventsResponse struct {
Pagination
Results []JobEvent `json:"results"`
}
// CancelJobResponse represents `CancelJob` endpoint response.
type CancelJobResponse struct {
Detail string `json:"detail"`
}
// GetJob shows the details of a job.
func (j *JobService) GetJob(id int, params map[string]string) (*Job, error) {
result := new(Job)
endpoint := fmt.Sprintf("/api/v2/jobs/%d/", id)
resp, err := j.client.Requester.GetJSON(endpoint, result, params)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// CancelJob cancels a job.
func (j *JobService) CancelJob(id int, data map[string]interface{}, params map[string]string) (*CancelJobResponse, error) {
result := new(CancelJobResponse)
endpoint := fmt.Sprintf("/api/v2/jobs/%d/cancel/", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := j.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, params)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// RelaunchJob relaunch a job.
func (j *JobService) RelaunchJob(id int, data map[string]interface{}, params map[string]string) (*JobLaunch, error) {
result := new(JobLaunch)
endpoint := fmt.Sprintf("/api/v2/jobs/%d/relaunch/", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := j.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, params)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// GetHostSummaries get a job hosts summaries.
func (j *JobService) GetHostSummaries(id int, params map[string]string) ([]HostSummary, *HostSummariesResponse, error) {
result := new(HostSummariesResponse)
endpoint := fmt.Sprintf("/api/v2/jobs/%d/job_host_summaries/", id)
resp, err := j.client.Requester.GetJSON(endpoint, result, params)
if err != nil {
return nil, result, err
}
if err := CheckResponse(resp); err != nil {
return nil, result, err
}
return result.Results, result, nil
}
// GetJobEvents get a list of job events.
func (j *JobService) GetJobEvents(id int, params map[string]string) ([]JobEvent, *JobEventsResponse, error) {
result := new(JobEventsResponse)
endpoint := fmt.Sprintf("/api/v2/jobs/%d/job_events/", id)
resp, err := j.client.Requester.GetJSON(endpoint, result, params)
if err != nil {
return nil, result, err
}
if err := CheckResponse(resp); err != nil {
return nil, result, err
}
return result.Results, result, nil
}