-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_inventory.go
193 lines (152 loc) · 3.63 KB
/
api_inventory.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package raptor
import (
"encoding/json"
"errors"
"fmt"
"github.com/raptorbox/raptor-sdk-go/models"
)
//CreateInventory instantiate a new API client
func CreateInventory(r *Raptor) *Inventory {
return &Inventory{
Raptor: r,
}
}
//Inventory API client
type Inventory struct {
Raptor *Raptor
devicePermission Permission
}
//Permission return the Permission API
func (i *Inventory) Permission() Permission {
if i.devicePermission == nil {
i.devicePermission = CreateDevicePermission(i.Raptor)
}
return i.devicePermission
}
//NewDevice return a new Device instance
func (i *Inventory) NewDevice() *models.Device {
userID := ""
if i.Raptor.Auth().GetUser() != nil {
userID = i.Raptor.Auth().GetUser().ID
}
dev := models.NewDevice()
dev.UserID = userID
return dev
}
//NewDeviceFromFile load a Device definition from a file
func (i *Inventory) NewDeviceFromFile(src string) (*models.Device, error) {
dev := models.NewDevice()
err := i.Raptor.LoadModelFromFile(src, dev)
dev.EnsureReferences()
return dev, err
}
//NewDeviceQuery return a new DeviceQuery instance
func (i *Inventory) NewDeviceQuery() *models.DeviceQuery {
return models.NewDeviceQuery()
}
//GetConfig return the configuration
func (i *Inventory) GetConfig() models.Config {
return i.Raptor.GetConfig()
}
//GetClient return a client instance
func (i *Inventory) GetClient() models.Client {
return i.Raptor.GetClient()
}
//Load a device by ID
func (i *Inventory) Load(ID string) (*models.Device, error) {
raw, err := i.GetClient().Get(fmt.Sprintf(INVENTORY_LOAD, ID), nil)
if err != nil {
return nil, err
}
dev := models.NewDevice()
err = json.Unmarshal(raw, dev)
if err != nil {
return nil, err
}
dev.EnsureReferences()
return dev, nil
}
//List devices accessible by an user
func (i *Inventory) List() (*models.DevicePager, error) {
raw, err := i.GetClient().Get(INVENTORY_LIST, nil)
if err != nil {
return nil, err
}
pager, err := models.ParseDevicePager(raw)
if err != nil {
return nil, err
}
for i := range pager.Content {
pager.Content[i].EnsureReferences()
}
return pager, nil
}
//Search for devices
func (i *Inventory) Search(q *models.DeviceQuery) (*models.DevicePager, error) {
if q == nil {
return nil, errors.New("Query is missing")
}
raw, err := i.GetClient().Post(INVENTORY_SEARCH, q, nil)
if err != nil {
return nil, err
}
pager, err := models.ParseDevicePager(raw)
if err != nil {
return nil, err
}
for i := range pager.Content {
pager.Content[i].EnsureReferences()
}
return pager, nil
}
//Create a device
func (i *Inventory) Create(dev *models.Device) error {
raw, err := i.GetClient().Post(INVENTORY_CREATE, dev, nil)
if err != nil {
return err
}
res := &models.Device{}
err = i.GetClient().FromJSON(raw, res)
if err != nil {
return err
}
err = dev.Merge(*res)
if err != nil {
return err
}
dev.EnsureReferences()
return nil
}
//Update a device
func (i *Inventory) Update(dev *models.Device) error {
if dev.ID == "" {
return errors.New("Device ID is missing")
}
raw, err := i.GetClient().Put(fmt.Sprintf(INVENTORY_UPDATE, dev.ID), dev, nil)
if err != nil {
return err
}
res := &models.Device{}
err = i.GetClient().FromJSON(raw, res)
if err != nil {
return err
}
err = dev.Merge(*res)
if err != nil {
return err
}
dev.EnsureReferences()
return nil
}
//DeleteByID a device by ID
func (i *Inventory) DeleteByID(id string) error {
err := i.GetClient().Delete(fmt.Sprintf(INVENTORY_DELETE, id), nil)
return err
}
//Delete a device
func (i *Inventory) Delete(dev *models.Device) error {
if dev.ID == "" {
return errors.New("Device ID is missing")
}
return i.DeleteByID(dev.ID)
}