-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeviceenvvar.go
122 lines (113 loc) · 3.68 KB
/
deviceenvvar.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
package balena
import (
"context"
"fmt"
"net/http"
"strconv"
"go.einride.tech/balena/odata"
)
const deviceEnvVarBasePath = "v6/device_environment_variable"
type DeviceEnvVarService service
type DeviceEnvVarResponse 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 *DeviceEnvVarService) List(ctx context.Context, deviceID IDOrUUID) ([]*DeviceEnvVarResponse, 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, deviceEnvVarBasePath, query, nil)
if err != nil {
return nil, fmt.Errorf("unable to create request: %v", err)
}
type Response struct {
D []*DeviceEnvVarResponse `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 *DeviceEnvVarService) Create(
ctx context.Context,
deviceID IDOrUUID,
name string,
value string,
) (*DeviceEnvVarResponse, 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, deviceEnvVarBasePath, "", &request{
DeviceID: id,
Name: name,
Value: value,
})
if err != nil {
return nil, fmt.Errorf("unable to create request: %v", err)
}
resp := &DeviceEnvVarResponse{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("unable to perform request: %v", err)
}
return resp, nil
}
// Update a variable with the given name from the device with given ID/UUID to the specified new value.
// No error is returned if no variable with such name exists.
func (s *DeviceEnvVarService) Update(ctx context.Context, deviceID IDOrUUID, name, newValue string) error {
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"
body := struct {
Value string `json:"value"`
}{Value: newValue}
req, err := s.client.NewRequest(ctx, http.MethodPatch, deviceEnvVarBasePath, query, body)
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
}
// 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 *DeviceEnvVarService) 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, deviceEnvVarBasePath, 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
}