Skip to content

Commit

Permalink
feat: add device config variables resource
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmr1 committed Jun 16, 2021
1 parent ca31666 commit 649eea0
Show file tree
Hide file tree
Showing 3 changed files with 442 additions and 0 deletions.
2 changes: 2 additions & 0 deletions balena.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Client struct {
ReleaseTag *ReleaseTagService
DeviceEnvVar *DeviceEnvVarService
DeviceServVar *DeviceServVarService
DeviceConfVar *DeviceConfVarService
DeviceTag *DeviceTagService
}

Expand Down Expand Up @@ -67,6 +68,7 @@ func New(httpClient *http.Client, authToken string) *Client {
c.Release = (*ReleaseService)(&c.common)
c.DeviceEnvVar = (*DeviceEnvVarService)(&c.common)
c.DeviceServVar = (*DeviceServVarService)(&c.common)
c.DeviceConfVar = (*DeviceConfVarService)(&c.common)
c.DeviceTag = (*DeviceTagService)(&c.common)
c.ReleaseTag = (*ReleaseTagService)(&c.common)
return c
Expand Down
100 changes: 100 additions & 0 deletions deviceconfvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
Loading

0 comments on commit 649eea0

Please sign in to comment.