-
Notifications
You must be signed in to change notification settings - Fork 1
/
uiExtension.go
271 lines (234 loc) · 7.23 KB
/
uiExtension.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package c8y
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
)
// UIExtensionService to managed UI extensions
// WARNING: THE UI Extension Service API is not yet finalized so expect changes in the future!
type UIExtensionService service
var ApplicationTagLatest = "latest"
type UIExtension struct {
Application
Manifest *UIManifest `json:"manifest,omitempty"`
ManifestFile *UIManifestFile `json:"-"`
}
type UIManifest struct {
Package string `json:"package,omitempty"`
IsPackage *bool `json:"isPackage,omitempty"`
}
func (m *UIManifest) WithIsPackage(v bool) *UIManifest {
m.IsPackage = &v
return m
}
func (m *UIManifest) WithPackage(v string) *UIManifest {
m.Package = v
return m
}
type UIManifestFile struct {
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
ContextPath string `json:"contextPath,omitempty"`
Package string `json:"package,omitempty"`
IsPackage bool `json:"isPackage,omitempty"`
Version string `json:"version,omitempty"`
Author string `json:"author"`
Description string `json:"description,omitempty"`
License string `json:"license"`
Remotes map[string][]string `json:"remotes"`
RequiredPlatformVersion string `json:"requiredPlatformVersion"`
}
const CumulocityUIManifestFile = "cumulocity.json"
func NewUIExtension(name string) *UIExtension {
ext := &UIExtension{
Application: Application{
Name: name,
Key: name + "-key",
ContextPath: name,
Type: "HOSTED",
},
Manifest: &UIManifest{},
}
ext.Manifest.
WithIsPackage(true).
WithPackage("plugin")
return ext
}
func GetUIExtensionManifestContents(zipFilename string, contents interface{}) error {
reader, err := zip.OpenReader(zipFilename)
if err != nil {
return err
}
defer reader.Close()
for _, file := range reader.File {
// check if the file matches the name for application portfolio xml
if strings.EqualFold(file.Name, CumulocityUIManifestFile) {
rc, err := file.Open()
if err != nil {
return err
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(rc); err != nil {
return err
}
defer rc.Close()
// Unmarshal bytes
if err := json.Unmarshal(buf.Bytes(), &contents); err != nil {
return err
}
}
}
return nil
}
func (s *UIExtensionService) NewUIExtensionFromFile(filename string) (*UIExtension, error) {
app := &UIExtension{
Manifest: &UIManifest{},
ManifestFile: &UIManifestFile{},
}
err := GetUIExtensionManifestContents(filename, app.ManifestFile)
app.Name = app.ManifestFile.Name
app.Key = app.ManifestFile.Key
app.Type = ApplicationTypeHosted
app.ContextPath = app.ManifestFile.ContextPath
app.Manifest.WithIsPackage(app.ManifestFile.IsPackage)
app.Manifest.WithPackage(app.ManifestFile.Package)
return app, err
}
func NewApplicationExtension(name string) *UIExtension {
app := &UIExtension{
Application: Application{
Name: name,
Key: name + "-key",
ContextPath: name,
Type: ApplicationTypeHosted,
},
Manifest: &UIManifest{},
}
app.Manifest.WithIsPackage(true)
app.Manifest.WithPackage("package")
return app
}
type UpsertOptions struct {
SkipActivation bool
Version *ApplicationVersion
}
func HasTag(tags []string, tag string) bool {
for _, v := range tags {
if strings.EqualFold(v, tag) {
return true
}
}
return false
}
// CreateVersion creates a new version of an application from a given file
// The filename can either be a string or a io.Reader
func (s *UIExtensionService) CreateExtension(ctx context.Context, application *Application, filename any, opt UpsertOptions) (*ApplicationVersion, *Response, error) {
var app *Application
var resp *Response
var err error
// Check if application already exits
if application.ID != "" {
// No need to look it up
app = &Application{
ID: application.ID,
}
} else if application.Name != "" {
// Lookup via name
opts := &ApplicationOptions{}
matches, listResp, listErr := s.client.Application.GetApplicationsByName(ctx, application.Name, opts.WithHasVersions(true))
if listErr != nil {
return nil, listResp, listErr
}
if len(matches.Applications) > 0 {
app = &matches.Applications[0]
}
} else {
return nil, nil, fmt.Errorf("application must have either the .ID or .Name set")
}
if app == nil {
// Create the new application
app, resp, err = s.client.Application.Create(ctx, application)
// New applications must have the first binary be activated, so ignore the existing SkipActivation option
opt.SkipActivation = false
// Append latest tag if not already defined
if opt.Version != nil {
if !HasTag(opt.Version.Tags, ApplicationTagLatest) {
opt.Version.Tags = append(opt.Version.Tags, ApplicationTagLatest)
}
}
} else {
// Update the existing application
if application.Availability != "" {
props := &Application{}
props.Availability = application.Availability
app, resp, err = s.client.Application.Update(ctx, app.ID, props)
}
}
if err != nil {
return nil, resp, err
}
var binaryVersion *ApplicationVersion
var binaryVersionResponse *Response
// Upload binary
switch v := filename.(type) {
case string:
binaryVersion, binaryVersionResponse, err = s.client.ApplicationVersions.CreateVersion(ctx, app.ID, v, *opt.Version)
case io.Reader:
binaryVersion, binaryVersionResponse, err = s.client.ApplicationVersions.CreateVersionFromReader(ctx, app.ID, v, *opt.Version)
default:
return nil, nil, fmt.Errorf("invalid file type. Only string or reader is accepted")
}
if err != nil {
return binaryVersion, binaryVersionResponse, err
}
if binaryVersion != nil {
// Store a reference to the related application
binaryVersion.Application = app
}
// Activate the version
if !opt.SkipActivation {
_, resp, err = s.client.Application.Update(ctx, app.ID, &Application{
ActiveVersionID: binaryVersion.BinaryID,
})
if err != nil {
return binaryVersion, resp, err
}
}
return binaryVersion, binaryVersionResponse, err
}
func (s *UIExtensionService) SetActive(ctx context.Context, appID string, binaryID string) (*Application, *Response, error) {
return s.client.Application.Update(ctx, appID, &Application{
ActiveVersionID: binaryID,
})
}
type ExtensionOptions struct {
PaginationOptions
Name string `url:"name,omitempty"`
Owner string `url:"owner,omitempty"`
Availability string `url:"availability,omitempty"`
ProviderFor string `url:"providerFor,omitempty"`
Subscriber string `url:"subscriber,omitempty"`
Tenant string `url:"tenant,omitempty"`
Type string `url:"type,omitempty"`
User string `url:"user,omitempty"`
HasVersions bool `url:"hasVersions,omitempty"`
}
// GetVersions returns a list of versions for a given application
func (s *UIExtensionService) GetExtensions(ctx context.Context, opt *ExtensionOptions) (*ApplicationCollection, *Response, error) {
data := new(ApplicationCollection)
if opt == nil {
opt = &ExtensionOptions{}
}
opt.HasVersions = true
resp, err := s.client.SendRequest(ctx, RequestOptions{
Method: "GET",
Path: "application/applications",
Query: opt,
ResponseData: data,
})
return data, resp, err
}