forked from Colstuwjx/awx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventories.go
135 lines (107 loc) · 3.36 KB
/
inventories.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
package awx
import (
"bytes"
"encoding/json"
"fmt"
)
// InventoriesService implements awx inventories apis.
type InventoriesService struct {
client *Client
}
// ListInventoriesResponse represents `ListInventories` endpoint response.
type ListInventoriesResponse struct {
Pagination
Results []*Inventory `json:"results"`
}
// ListInventories shows list of awx inventories.
func (i *InventoriesService) ListInventories(params map[string]string) ([]*Inventory, *ListInventoriesResponse, error) {
result := new(ListInventoriesResponse)
endpoint := "/api/v2/inventories/"
resp, err := i.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
}
// CreateInventory creates an awx inventory.
func (i *InventoriesService) CreateInventory(data map[string]interface{}, params map[string]string) (*Inventory, error) {
mandatoryFields = []string{"name", "organization"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("Mandatory input arguments are absent: %s", validate)
return nil, err
}
result := new(Inventory)
endpoint := "/api/v2/inventories/"
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
// Add check if inventory exists and return proper error
resp, err := i.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
}
// UpdateInventory update an awx inventory
func (i *InventoriesService) UpdateInventory(id int, data map[string]interface{}, params map[string]string) (*Inventory, error) {
result := new(Inventory)
endpoint := fmt.Sprintf("/api/v2/inventories/%d", id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := i.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
}
// GetInventory retrives the inventory information from its ID or Name
func (i *InventoriesService) GetInventory(id int, params map[string]string) (*Inventory, error) {
endpoint := fmt.Sprintf("/api/v2/inventories/%d", id)
result := new(Inventory)
resp, err := i.client.Requester.GetJSON(endpoint, result, map[string]string{})
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return result, nil
}
// DeleteInventory delete an inventory from AWX
func (i *InventoriesService) DeleteInventory(id int) (*Inventory, error) {
result := new(Inventory)
endpoint := fmt.Sprintf("/api/v2/inventories/%d", id)
resp, err := i.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 (i *InventoriesService) SyncInventorySourcesByInventoryID(id int) ([]*InventoryUpdate, error) {
result := new([]*InventoryUpdate)
endpoint := fmt.Sprintf("/api/v2/inventories/%d/update_inventory_sources/", id)
resp, err := i.client.Requester.PostJSON(endpoint, nil, result, nil)
if err != nil {
return nil, err
}
if err := CheckResponse(resp); err != nil {
return nil, err
}
return *result, nil
}