-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
95 lines (77 loc) · 2.82 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
package airvantage
import (
"fmt"
"io"
)
// An Application descriptor.
type Application struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Revision string `json:"revision,omitempty"`
Type string `json:"type,omitempty"`
Category string `json:"category,omitempty"`
State string `json:"state,omitempty"`
Released AVTime `json:"released,omitempty"`
Published AVTime `json:"published,omitempty"`
Deprecated AVTime `json:"deprecated,omitempty"`
IsReference bool `json:"isReference,omitempty"`
IsPublic bool `json:"isPublic,omitempty"`
Labels []string `json:"labels,omitempty"`
ApplicationManager string `json:"applicationManager,omitempty"`
Owner map[string]any `json:"owner,omitempty"` // TODO: real impl.
}
// FindAppUID looks for an application using its name and revision,
// checks if it is in the published state, and returns its UID.
func (av *AirVantage) FindAppUID(name, rev string) (string, error) {
resp, err := av.get("applications", "name", name, "revision", rev, "fields", "uid,state", "size", 2)
if err != nil {
return "", err
}
res := struct{ Items []Application }{}
if err = av.parseResponse(resp, &res); err != nil {
return "", err
}
found := len(res.Items)
if found == 0 {
return "", fmt.Errorf("no application matching '%s'", name)
}
// There must be exactly 1 match
if found > 1 {
return "", fmt.Errorf("several applications matching '%s'", name)
}
// This only contains uid and state
app := res.Items[0]
if app.State != "PUBLISHED" {
return "", fmt.Errorf("application '%s' is not PUBLISHED", name)
}
return app.UID, nil
}
// FindAppByTypeRev retrieves an application by type and revision
func (av *AirVantage) FindAppByTypeRev(apptype, apprev string) (*Application, error) {
resp, err := av.get("applications", "type", apptype, "revision", apprev)
if err != nil {
return nil, err
}
res := struct{ Items []Application }{}
if err = av.parseResponse(resp, &res); err != nil {
return nil, err
}
if len(res.Items) == 0 {
return nil, fmt.Errorf("no application with matching type '%s' & revision '%s'", apptype, apprev)
}
return &res.Items[0], nil
}
// ReleaseApplication releases an application
func (av *AirVantage) ReleaseApplication(zipFile io.Reader) (string, error) {
// why do we need /api/v1 prefix here?
url := av.URL("/api/v1/operations/applications/release")
resp, err := av.client.Post(url, "application/zip", zipFile)
if err != nil {
return "", err
}
res := struct{ Operation string }{}
if err = av.parseResponse(resp, &res); err != nil {
return "", err
}
return string(res.Operation), nil
}