forked from Colstuwjx/awx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awx.go
114 lines (99 loc) · 2.77 KB
/
awx.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
package awx
import (
"fmt"
"net/http"
"testing"
"github.com/kylelemons/godebug/pretty"
)
// 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
PingService *PingService
InventoriesService *InventoriesService
InventoryUpdatesService *InventoryUpdatesService
JobService *JobService
JobTemplateService *JobTemplateService
ProjectService *ProjectService
ProjectUpdatesService *ProjectUpdatesService
UserService *UserService
GroupService *GroupService
HostService *HostService
}
// 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)
}
// CheckAPICallResult compare API calls results
func checkAPICallResult(t *testing.T, expected interface{}, got interface{}) {
if diff := pretty.Compare(expected, got); diff != "" {
t.Fatalf("diff: (-got +want)\n%s", diff)
}
}
// 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 {
r := &Requester{Base: baseURL, BasicAuth: &BasicAuth{Username: userName, Password: passwd}, Client: client}
if r.Client == nil {
r.Client = http.DefaultClient
}
awxClient := &Client{
BaseURL: baseURL,
Requester: r,
}
return &AWX{
client: awxClient,
PingService: &PingService{
client: awxClient,
},
InventoriesService: &InventoriesService{
client: awxClient,
},
InventoryUpdatesService: &InventoryUpdatesService{
client: awxClient,
},
JobService: &JobService{
client: awxClient,
},
JobTemplateService: &JobTemplateService{
client: awxClient,
},
ProjectService: &ProjectService{
client: awxClient,
},
ProjectUpdatesService: &ProjectUpdatesService{
client: awxClient,
},
UserService: &UserService{
client: awxClient,
},
GroupService: &GroupService{
client: awxClient,
},
HostService: &HostService{
client: awxClient,
},
}
}