forked from denouche/goawx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawx.go
More file actions
226 lines (205 loc) · 6.88 KB
/
awx.go
File metadata and controls
226 lines (205 loc) · 6.88 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package awx
import (
"fmt"
"net/http"
)
// This variable is mandatory and to be populated for creating services API
var mandatoryFields []string
// AWX represents awx api endpoints with services, and using
// client to communicate with awx server.
type AWX struct {
client *Client
ApplicationService *ApplicationService
ExecutionEnvironmentsService *ExecutionEnvironmentsService
PingService *PingService
InventoriesService *InventoriesService
JobService *JobService
JobTemplateService *JobTemplateService
JobTemplateNotificationTemplatesService *JobTemplateNotificationTemplatesService
ProjectService *ProjectService
ProjectUpdatesService *ProjectUpdatesService
UserService *UserService
GroupService *GroupService
HostService *HostService
CredentialsService *CredentialsService
CredentialTypeService *CredentialTypeService
CredentialInputSourceService *CredentialInputSourceService
InventorySourcesService *InventorySourcesService
InventorySourcesSchedulesService *InventorySourcesSchedulesService
InventoryGroupService *InventoryGroupService
InstanceGroupsService *InstanceGroupsService
NotificationTemplatesService *NotificationTemplatesService
OrganizationsService *OrganizationsService
ScheduleService *SchedulesService
SettingService *SettingService
TeamService *TeamService
WorkflowJobTemplateScheduleService *WorkflowJobTemplateScheduleService
WorkflowJobTemplateService *WorkflowJobTemplateService
WorkflowJobTemplateNodeService *WorkflowJobTemplateNodeService
WorkflowJobTemplateNodeAlwaysService *WorkflowJobTemplateNodeStepService
WorkflowJobTemplateNodeFailureService *WorkflowJobTemplateNodeStepService
WorkflowJobTemplateNodeSuccessService *WorkflowJobTemplateNodeStepService
WorkflowJobTemplateNotificationTemplatesService *WorkflowJobTemplateNotificationTemplatesService
}
// Client implement http client.
type Client struct {
BaseURL string
Requester *Requester
}
// CheckResponse do http response check, and return err if not in [200, 300).
func CheckResponse(resp *http.Response) error {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
return fmt.Errorf("responsed with %d, resp: %v", resp.StatusCode, resp)
}
// ValidateParams is to validate the input to use the services.
func ValidateParams(data map[string]interface{}, mandatoryFields []string) (notfound []string, status bool) {
status = true
for _, key := range mandatoryFields {
_, exists := data[key]
if !exists {
notfound = append(notfound, key)
status = false
}
}
return notfound, status
}
// NewAWX news an awx handler with basic auth support, you could customize the http
// transport by passing custom client.
func NewAWX(baseURL, userName, passwd string, client *http.Client) (*AWX, error) {
r := &Requester{Base: baseURL, Authenticator: &BasicAuth{Username: userName, Password: passwd}, Client: client}
if r.Client == nil {
r.Client = http.DefaultClient
}
awxClient := &Client{
BaseURL: baseURL,
Requester: r,
}
newAWX := newAWX(awxClient)
// test the connection and return and error if there's an issue
_, err := newAWX.PingService.Ping()
if err != nil {
return nil, err
}
return newAWX, nil
}
// NewAWXToken creates an AWX handler with token support.
func NewAWXToken(baseURL, token string, client *http.Client) (*AWX, error) {
r := &Requester{Base: baseURL, Authenticator: &TokenAuth{Token: token}, Client: client}
if r.Client == nil {
r.Client = http.DefaultClient
}
awxClient := &Client{
BaseURL: baseURL,
Requester: r,
}
newAWX := newAWX(awxClient)
// test the connection and return and error if there's an issue
_, err := newAWX.PingService.Ping()
if err != nil {
return nil, err
}
return newAWX, nil
}
func newAWX(c *Client) *AWX {
return &AWX{
client: c,
ApplicationService: &ApplicationService{
client: c,
},
ExecutionEnvironmentsService: &ExecutionEnvironmentsService{
client: c,
},
PingService: &PingService{
client: c,
},
InventoriesService: &InventoriesService{
client: c,
},
JobService: &JobService{
client: c,
},
JobTemplateService: &JobTemplateService{
client: c,
},
JobTemplateNotificationTemplatesService: &JobTemplateNotificationTemplatesService{
client: c,
},
ProjectService: &ProjectService{
client: c,
},
ProjectUpdatesService: &ProjectUpdatesService{
client: c,
},
UserService: &UserService{
client: c,
},
GroupService: &GroupService{
client: c,
},
HostService: &HostService{
client: c,
},
CredentialsService: &CredentialsService{
client: c,
},
CredentialTypeService: &CredentialTypeService{
client: c,
},
CredentialInputSourceService: &CredentialInputSourceService{
client: c,
},
InventorySourcesService: &InventorySourcesService{
client: c,
},
InventorySourcesSchedulesService: &InventorySourcesSchedulesService{
client: c,
},
InventoryGroupService: &InventoryGroupService{
client: c,
},
InstanceGroupsService: &InstanceGroupsService{
client: c,
},
NotificationTemplatesService: &NotificationTemplatesService{
client: c,
},
OrganizationsService: &OrganizationsService{
client: c,
},
ScheduleService: &SchedulesService{
client: c,
},
SettingService: &SettingService{
client: c,
},
TeamService: &TeamService{
client: c,
},
WorkflowJobTemplateScheduleService: &WorkflowJobTemplateScheduleService{
client: c,
},
WorkflowJobTemplateService: &WorkflowJobTemplateService{
client: c,
},
WorkflowJobTemplateNodeService: &WorkflowJobTemplateNodeService{
client: c,
},
WorkflowJobTemplateNodeSuccessService: &WorkflowJobTemplateNodeStepService{
endpoint: fmt.Sprintf("%s%s", workflowJobTemplateNodeAPIEndpoint, "%d/success_nodes/"),
client: c,
},
WorkflowJobTemplateNodeFailureService: &WorkflowJobTemplateNodeStepService{
endpoint: fmt.Sprintf("%s%s", workflowJobTemplateNodeAPIEndpoint, "%d/failure_nodes/"),
client: c,
},
WorkflowJobTemplateNodeAlwaysService: &WorkflowJobTemplateNodeStepService{
endpoint: fmt.Sprintf("%s%s", workflowJobTemplateNodeAPIEndpoint, "%d/always_nodes/"),
client: c,
},
WorkflowJobTemplateNotificationTemplatesService: &WorkflowJobTemplateNotificationTemplatesService{
client: c,
},
}
}