forked from dahendel/awx-go
/
groups.go
127 lines (101 loc) · 2.83 KB
/
groups.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
package awx
import (
"bytes"
"encoding/json"
"fmt"
)
// GroupService implements awx Groups apis.
type GroupService struct {
client *Client
}
// ListGroupsResponse represents `ListGroups` endpoint response.
type ListGroupsResponse struct {
Pagination
Results []*Group `json:"results"`
}
// ListGroups shows list of awx Groups.
func (g *GroupService) ListGroups(params map[string]string) ([]*Group, *ListGroupsResponse, error) {
result := new(ListGroupsResponse)
endpoint := "/api/v2/groups/"
resp, err := g.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
}
// CreateGroup creates an awx Group.
func (g *GroupService) CreateGroup(data map[string]interface{}, params map[string]string) (*Group, 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(Group)
endpoint := "/api/v2/groups/"
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
// Add check if Group exists and return proper error
resp, err := g.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
}
// UpdateGroup update an awx group
func (g *GroupService) UpdateGroup(id int, data map[string]interface{}, params map[string]string) (*Group, error) {
result := new(Group)
endpoint := fmt.Sprintf("/api/v2/groups/%d", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := g.client.Requester.PatchJSON(endpoint, bytes.NewReader(payload), result, params)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// DeleteGroup delete an awx Group.
func (g *GroupService) DeleteGroup(id int) (*Group, error) {
result := new(Group)
endpoint := fmt.Sprintf("/api/v2/groups/%d", id)
resp, err := g.client.Requester.Delete(endpoint, result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
func (g *GroupService) AddChildGroup(groupID, childID int) (*Group, error) {
result := new(Group)
endpoint := fmt.Sprintf("/api/v2/groups/%d/children/", groupID)
payload := map[string]int{
"id": childID,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, err
}
resp, err := g.client.Requester.PostJSON(endpoint, bytes.NewReader(jsonPayload), result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}