-
Notifications
You must be signed in to change notification settings - Fork 4
/
devicetag.go
181 lines (169 loc) · 5.56 KB
/
devicetag.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
package balena
import (
"bytes"
"context"
"fmt"
"net/http"
"strconv"
"go.einride.tech/balena/odata"
)
const deviceTagBasePath = "v4/device_tag"
// DeviceTagService handles communication with the device tag related methods of the
// Balena Cloud API.
type DeviceTagService service
type DeviceTagResponse struct {
ID int64 `json:"id,omitempty"`
Device odata.Object `json:"device,omitempty"`
TagKey string `json:"tag_key,omitempty"`
Value string `json:"value,omitempty"`
}
// List lists all device tags for a given device ID/UUID.
func (s *DeviceTagService) List(ctx context.Context, deviceID IDOrUUID) ([]*DeviceTagResponse, error) {
query := "%24filter=device/id+eq+%27" + deviceID.id + "%27"
if deviceID.isUUID {
query = "%24filter=device/uuid+eq+%27" + deviceID.id + "%27"
}
req, err := s.client.NewRequest(ctx, http.MethodGet, deviceTagBasePath, query, nil)
if err != nil {
return nil, fmt.Errorf("list device tag NewRequest: %v", err)
}
type Response struct {
D []*DeviceTagResponse `json:"d,omitempty"`
}
resp := &Response{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("list device tag: %v", err)
}
return resp.D, nil
}
// Create creates a device tag with key=value given a device ID/UUID.
// An error is returned if the key already exists.
func (s *DeviceTagService) Create(
ctx context.Context,
deviceID IDOrUUID,
key string,
value string,
) (*DeviceTagResponse, error) {
// If UUID, retrieve device ID
id := deviceID.id
if deviceID.isUUID {
resp, err := s.client.Device.Get(ctx, deviceID)
if err != nil {
return nil, err
}
id = strconv.FormatInt(resp.ID, 10)
}
type request struct {
DeviceID string `json:"device"`
Key string `json:"tag_key"`
Value string `json:"value"`
}
req, err := s.client.NewRequest(ctx, http.MethodPost, deviceTagBasePath, "", &request{
DeviceID: id,
Key: key,
Value: value,
})
if err != nil {
return nil, fmt.Errorf("create device tag NewRequest: %v", err)
}
resp := &DeviceTagResponse{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("create device tag: %v", err)
}
return resp, nil
}
// GetWithKey retrieves a tag with the given key from the given device ID/UUID.
// If no key is found both the response and error returned are nil.
func (s *DeviceTagService) GetWithKey(ctx context.Context, deviceID IDOrUUID, key string) (*DeviceTagResponse, error) {
// Get the variable ID
query := "%24filter=device/id+eq+%27" + deviceID.id + "%27"
if deviceID.isUUID {
query = "%24filter=device/uuid+eq+%27" + deviceID.id + "%27"
}
query = query + "+and+tag_key+eq+%27" + key + "%27"
req, err := s.client.NewRequest(ctx, http.MethodGet, deviceTagBasePath, query, nil)
if err != nil {
return nil, fmt.Errorf("get device tag with key NewRequest: %v", err)
}
type Response struct {
D []*DeviceTagResponse `json:"d,omitempty"`
}
resp := &Response{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("get device tag with key: %v", err)
}
if len(resp.D) > 1 {
return nil, fmt.Errorf("expected 1 tag but got %d", len(resp.D))
}
if len(resp.D) == 0 {
return nil, nil
}
return resp.D[0], nil
}
// UpdateWithKey updates the value of a device tag matching the given key and device ID/UUID.
// No error is returned if the key or device does not exist.
func (s *DeviceTagService) UpdateWithKey(ctx context.Context, deviceID IDOrUUID, key, value string) error {
// Get the variable ID
query := "%24filter=device/id+eq+%27" + deviceID.id + "%27"
if deviceID.isUUID {
query = "%24filter=device/uuid+eq+%27" + deviceID.id + "%27"
}
query = query + "+and+tag_key+eq+%27" + key + "%27"
type request struct {
Value string `json:"value"`
}
req, err := s.client.NewRequest(ctx, http.MethodPatch, deviceTagBasePath, query, &request{
Value: value,
})
if err != nil {
return fmt.Errorf("update device tag with key NewRequest: %v", err)
}
buf := &bytes.Buffer{}
err = s.client.Do(req, buf)
if err != nil {
return fmt.Errorf("update device tag with key: %v", err)
}
return nil
}
// DeleteWithKey deletes a device tag from a given device ID/UUID and key.
// No error is returned if the tag does not exist.
func (s *DeviceTagService) DeleteWithKey(ctx context.Context, deviceID IDOrUUID, key string) error {
// Get the variable ID
query := "%24filter=device/id+eq+%27" + deviceID.id + "%27"
if deviceID.isUUID {
query = "%24filter=device/uuid+eq+%27" + deviceID.id + "%27"
}
query = query + "+and+tag_key+eq+%27" + key + "%27"
req, err := s.client.NewRequest(ctx, http.MethodDelete, deviceTagBasePath, query, nil)
if err != nil {
return fmt.Errorf("delete device tag with key NewRequest: %v", err)
}
buf := &bytes.Buffer{}
err = s.client.Do(req, buf)
if err != nil {
return fmt.Errorf("delete device tag with key: %v", err)
}
return nil
}
// GetWithQuery allows querying for device tags using a custom Open Data Protocol query.
// The query should be a valid, escaped OData query such as `%24filter=uuid+eq+%2712333422%27`.
//
// Forward slash in filter keys should not be escaped (So `device/uuid` should not be escaped).
func (s *DeviceTagService) GetWithQuery(ctx context.Context, query string) ([]*DeviceTagResponse, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, deviceTagBasePath, query, nil)
if err != nil {
return nil, fmt.Errorf("get device tag with query NewRequest: %v", err)
}
type Response struct {
D []*DeviceTagResponse `json:"d,omitempty"`
}
resp := &Response{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("get device tag with query: %v", err)
}
return resp.D, nil
}