forked from Colstuwjx/awx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
162 lines (135 loc) · 3.92 KB
/
hosts.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
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
package awx
import (
"bytes"
"encoding/json"
"fmt"
)
// HostService implements awx Hosts apis.
type HostService struct {
client *Client
}
// AssociateGroup implement the awx group association request
type AssociateGroup struct {
ID int `json:"id"`
Associate bool `json:"associate"`
}
// ListHostsResponse represents `ListHosts` endpoint response.
type ListHostsResponse struct {
Pagination
Results []*Host `json:"results"`
}
// ListHosts shows list of awx Hosts.
func (h *HostService) ListHosts(params map[string]string) ([]*Host, *ListHostsResponse, error) {
result := new(ListHostsResponse)
endpoint := "/api/v2/hosts/"
resp, err := h.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
}
// CreateHost creates an awx Host.
func (h *HostService) CreateHost(data map[string]interface{}, params map[string]string) (*Host, error) {
mandatoryFields = []string{"name", "inventory"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("Mandatory input arguments are absent: %s", validate)
return nil, err
}
result := new(Host)
endpoint := "/api/v2/hosts/"
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
// Add check if Host exists and return proper error
resp, err := h.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
}
// UpdateHost update an awx Host
func (h *HostService) UpdateHost(id int, data map[string]interface{}, params map[string]string) (*Host, error) {
result := new(Host)
endpoint := fmt.Sprintf("/api/v2/hosts/%d", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := h.client.Requester.PatchJSON(endpoint, bytes.NewReader(payload), result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// AssociateGroup update an awx Host
func (h *HostService) AssociateGroup(id int, data map[string]interface{}, params map[string]string) (*Host, error) {
result := new(Host)
endpoint := fmt.Sprintf("/api/v2/hosts/%d/groups/", id)
data["associate"] = true
mandatoryFields = []string{"id"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("Mandatory input arguments are absent: %s", validate)
return nil, err
}
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := h.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// DisAssociateGroup update an awx Host
func (h *HostService) DisAssociateGroup(id int, data map[string]interface{}, params map[string]string) (*Host, error) {
result := new(Host)
endpoint := fmt.Sprintf("/api/v2/hosts/%d/groups/", id)
data["disassociate"] = true
mandatoryFields = []string{"id"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("Mandatory input arguments are absent: %s", validate)
return nil, err
}
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := h.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// DeleteHost delete an awx Host.
func (h *HostService) DeleteHost(id int) (*Host, error) {
result := new(Host)
endpoint := fmt.Sprintf("/api/v2/hosts/%d", id)
resp, err := h.client.Requester.Delete(endpoint, result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}