forked from einride/balena-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deviceconfvar.go
100 lines (92 loc) · 2.88 KB
/
deviceconfvar.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
package balena
import (
"context"
"fmt"
"net/http"
"strconv"
"go.einride.tech/balena/odata"
)
const deviceConfVarBasePath = "v6/device_config_variable"
type DeviceConfVarService service
type DeviceConfVarResponse struct {
ID int64 `json:"id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Device odata.Object `json:"device,omitempty"`
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
// List lists all environment variables given a specific device ID/UUID.
func (s *DeviceConfVarService) List(ctx context.Context, deviceID IDOrUUID) ([]*DeviceConfVarResponse, error) {
query := "%24filter=device+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, deviceConfVarBasePath, query, nil)
if err != nil {
return nil, fmt.Errorf("unable to create request: %v", err)
}
type Response struct {
D []*DeviceConfVarResponse `json:"d,omitempty"`
}
resp := &Response{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("unable to perform request: %v", err)
}
return resp.D, nil
}
// Create creates an environment variable with name=value given a device ID/UUID.
func (s *DeviceConfVarService) Create(
ctx context.Context,
deviceID IDOrUUID,
name string,
value string,
) (*DeviceConfVarResponse, 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"`
Name string `json:"name"`
Value string `json:"value"`
}
req, err := s.client.NewRequest(ctx, http.MethodPost, deviceConfVarBasePath, "", &request{
DeviceID: id,
Name: name,
Value: value,
})
if err != nil {
return nil, fmt.Errorf("unable to create request: %v", err)
}
resp := &DeviceConfVarResponse{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("unable to perform request: %v", err)
}
return resp, nil
}
// DeleteWithName deletes a variable with the given name from the device with given ID/UUID.
// No error is returned if no variable with such name exists.
func (s *DeviceConfVarService) DeleteWithName(ctx context.Context, deviceID IDOrUUID, name string) error {
// Get the variable ID
query := "%24filter=device+eq+%27" + deviceID.id + "%27"
if deviceID.isUUID {
query = "%24filter=device/uuid+eq+%27" + deviceID.id + "%27"
}
query = query + "+and+name+eq+%27" + name + "%27"
req, err := s.client.NewRequest(ctx, http.MethodDelete, deviceConfVarBasePath, query, nil)
if err != nil {
return fmt.Errorf("unable to create request: %v", err)
}
err = s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("unable to perform request: %v", err)
}
return nil
}