forked from Colstuwjx/awx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
103 lines (82 loc) · 2.33 KB
/
users.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
package awx
import (
"bytes"
"encoding/json"
"fmt"
)
// UserService implements awx Users apis.
type UserService struct {
client *Client
}
// ListUsersResponse represents `ListUsers` endpoint response.
type ListUsersResponse struct {
Pagination
Results []*User `json:"results"`
}
// ListUsers shows list of awx Users.
func (u *UserService) ListUsers(params map[string]string) ([]*User, *ListUsersResponse, error) {
result := new(ListUsersResponse)
endpoint := "/api/v2/users/"
resp, err := u.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
}
// CreateUser creates an awx User.
func (u *UserService) CreateUser(data map[string]interface{}, params map[string]string) (*User, error) {
mandatoryFields = []string{"username", "password", "first_name", "last_name", "email"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("Mandatory input arguments are absent: %s", validate)
return nil, err
}
result := new(User)
endpoint := "/api/v2/users/"
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
// Add check if User exists and return proper error
resp, err := u.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
}
// UpdateUser update an awx user.
func (u *UserService) UpdateUser(id int, data map[string]interface{}, params map[string]string) (*User, error) {
result := new(User)
endpoint := fmt.Sprintf("/api/v2/users/%d", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := u.client.Requester.PutJSON(endpoint, bytes.NewReader(payload), result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// DeleteUser delete an awx User.
func (u *UserService) DeleteUser(id int) (*User, error) {
result := new(User)
endpoint := fmt.Sprintf("/api/v2/users/%d", id)
resp, err := u.client.Requester.Delete(endpoint, result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}