forked from einride/balena-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
142 lines (130 loc) · 5.62 KB
/
application.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
package balena
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"strconv"
"go.einride.tech/balena/odata"
)
const applicationBasePath = "v6/application"
// ApplicationService handles communication with the application related methods of the
// Balena Cloud API.
type ApplicationService service
type ApplicationsResponse struct {
ID int64 `json:"id,omitempty"`
ShouldTrackLatestRelease bool `json:"should_track_latest_release,omitempty"`
IsPublic bool `json:"is_public,omitempty"`
IsHost bool `json:"is_host,omitempty"`
IsArchived bool `json:"is_archived,omitempty"`
IsDiscoverable bool `json:"is_discoverable,omitempty"`
UUID string `json:"uuid,omitempty"`
IsStoredAtRepositoryURL string `json:"is_stored_at__repository_url,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
AppName string `json:"app_name,omitempty"`
Actor int64 `json:"actor,omitempty"`
Slug string `json:"slug,omitempty"`
IsOfClass string `json:"is_of__class,omitempty"`
Organization *odata.Object `json:"organization,omitempty"`
ShouldBeRunningRelease *odata.Object `json:"should_be_running__release,omitempty"`
IsForDeviceType *odata.Object `json:"is_for__device_type,omitempty"`
DependsOnApplication interface{} `json:"depends_on__application,omitempty"`
IsAccessibleBySupportUntilDate interface{} `json:"is_accessible_by_support_until__date,omitempty"`
}
func (s *ApplicationService) List(ctx context.Context) ([]*ApplicationsResponse, error) {
return s.GetWithQuery(ctx, "")
}
// GetWithQuery allows querying for devices using a custom open data protocol query.
// The query should be a valid, escaped OData query such as `%24filter=uuid+eq+'12333422'`.
//
// Forward slash in filter keys should not be escaped (So `device/uuid` should not be escaped).
func (s *ApplicationService) GetWithQuery(ctx context.Context, query string) ([]*ApplicationsResponse, error) {
return s.getWithQueryAndPath(ctx, applicationBasePath, query)
}
// Get returns information on a single application given its ID.
// If the application does not exist, both the response and error are nil.
func (s *ApplicationService) Get(ctx context.Context, applicationID int64) (*ApplicationsResponse, error) {
path := odata.EntityURL(applicationBasePath, strconv.FormatInt(applicationID, 10))
resp, err := s.getWithQueryAndPath(ctx, path, "")
if len(resp) > 1 {
return nil, errors.New("received more than 1 application, expected 0 or 1")
}
if len(resp) == 0 {
return nil, nil
}
return resp[0], err
}
// GetByName returns information on a single application given its Name
// If the application does not exist, both the response and error are nil.
func (s *ApplicationService) GetByName(ctx context.Context, applicationName string) (*ApplicationsResponse, error) {
query := "%24filter=app_name%20eq%20%27" + applicationName + "%27"
resp, err := s.getWithQueryAndPath(ctx, applicationBasePath, query)
if len(resp) > 1 {
return nil, errors.New("received more than 1 application, expected 0 or 1")
}
if len(resp) == 0 {
return nil, nil
}
return resp[0], err
}
func (s *ApplicationService) getWithQueryAndPath(
ctx context.Context,
path string,
query string,
) ([]*ApplicationsResponse, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, query, nil)
if err != nil {
return nil, fmt.Errorf("unable to create get request: %v", err)
}
type Response struct {
D []ApplicationsResponse `json:"d,omitempty"`
}
resp := &Response{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("unable to get application list: %v", err)
}
apps := make([]*ApplicationsResponse, 0, len(resp.D))
for _, app := range resp.D {
app := app
apps = append(apps, &app)
}
return apps, nil
}
// EnableTrackLatestRelease sets all devices owned by the application to track the latest available release.
func (s *ApplicationService) EnableTrackLatestRelease(ctx context.Context, applicationID int64) ([]byte, error) {
type request struct {
ShouldTrackLatestRelease bool `json:"should_track_latest_release"`
}
var query string
path := odata.EntityURL(applicationBasePath, strconv.FormatInt(applicationID, 10))
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, query, &request{ShouldTrackLatestRelease: true})
if err != nil {
return nil, fmt.Errorf("unable to create setTrackLatestRelease request: %v", err)
}
var buf bytes.Buffer
err = s.client.Do(req, &buf)
if err != nil {
return nil, fmt.Errorf("unable to path application: %v", err)
}
return buf.Bytes(), nil
}
// DisableTrackLatestRelease sets all devices owned by the application to NOT track the latest available release.
func (s *ApplicationService) DisableTrackLatestRelease(ctx context.Context, applicationID int64) ([]byte, error) {
type request struct {
ShouldTrackLatestRelease bool `json:"should_track_latest_release"`
}
var query string
path := odata.EntityURL(applicationBasePath, strconv.FormatInt(applicationID, 10))
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, query, &request{ShouldTrackLatestRelease: false})
if err != nil {
return nil, fmt.Errorf("unable to create setTrackLatestRelease request: %v", err)
}
var buf bytes.Buffer
err = s.client.Do(req, &buf)
if err != nil {
return nil, fmt.Errorf("unable to path application: %v", err)
}
return buf.Bytes(), nil
}