/
uiPluginFetcher.go
150 lines (123 loc) · 3.89 KB
/
uiPluginFetcher.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
package c8yfetcher
import (
"context"
"fmt"
"regexp"
"github.com/pkg/errors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y/pkg/c8y"
)
type UIPluginFetcher struct {
*CumulocityFetcher
// Look for shared plugins if no local ones are found
EnableSharedPlugins bool
}
func NewUIPluginFetcher(factory *cmdutil.Factory) *UIPluginFetcher {
return &UIPluginFetcher{
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
}
}
func (f *UIPluginFetcher) getByID(id string) ([]fetcherResultSet, error) {
app, resp, err := f.Client().Application.GetApplication(
c8y.WithDisabledDryRunContext(context.Background()),
id,
)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch by id")
}
results := make([]fetcherResultSet, 1)
results[0] = fetcherResultSet{
ID: app.ID,
Name: app.Name,
Value: resp.JSON(),
}
return results, nil
}
// getByName returns applications matching a given using regular expression
func (f *UIPluginFetcher) getByName(name string) ([]fetcherResultSet, error) {
serverOptions := &c8y.ApplicationOptions{
PaginationOptions: *c8y.NewPaginationOptions(2000),
Type: c8y.ApplicationTypeHosted,
}
serverOptions.WithHasVersions(true)
if f.EnableSharedPlugins && f.Client().TenantName != "" {
// Ignore microservices which don't match the owner
// so that microservices of sub tenants don't get returned.
serverOptions.Owner = f.Client().TenantName
}
col, _, err := f.Client().Application.GetApplications(
c8y.WithDisabledDryRunContext(context.Background()),
serverOptions,
)
if err != nil {
return nil, errors.Wrap(err, "could not fetch ui plugin")
}
pattern, err := regexp.Compile("^" + regexp.QuoteMeta(name) + "$")
if err != nil {
return nil, errors.Wrap(err, "invalid regex")
}
results := make([]fetcherResultSet, 0)
// Note: Match against both name and contextPath
// as the contextPath is used by UI plugins as a reference
for i, app := range col.Applications {
if pattern.MatchString(app.Name) || pattern.MatchString(app.ContextPath) {
results = append(results, fetcherResultSet{
ID: app.ID,
Name: app.Name,
Value: col.Items[i],
})
}
}
// If not results are found, then also include any matches (not just in the current tenant)
if len(results) == 0 && f.EnableSharedPlugins {
// Run request against, but without the tenant filter
serverOptions.Availability = c8y.ApplicationAvailabilityShared
serverOptions.Owner = ""
col, _, err := f.Client().Application.GetApplications(
c8y.WithDisabledDryRunContext(context.Background()),
serverOptions,
)
if err != nil {
return nil, errors.Wrap(err, "could not fetch applications")
}
for i, app := range col.Applications {
if pattern.MatchString(app.Name) || pattern.MatchString(app.ContextPath) {
results = append(results, fetcherResultSet{
ID: app.ID,
Name: app.Name,
Value: col.Items[i],
})
}
}
}
return results, nil
}
// Find UI Plugins returns plugins given either an id or search text
// @values: An array of ids, or names (with wildcards)
// @lookupID: Lookup the data if an id is given. If a non-id text is given, the result will always be looked up.
func FindUIPlugins(factory *cmdutil.Factory, values []string, lookupID bool, format string, resolveSharedPlugins bool) ([]entityReference, error) {
f := NewUIPluginFetcher(factory)
f.EnableSharedPlugins = resolveSharedPlugins
formattedValues, err := lookupEntity(f, values, lookupID, format)
if err != nil {
return nil, err
}
results := []entityReference{}
invalidLookups := []string{}
for _, item := range formattedValues {
if item.ID != "" {
results = append(results, item)
} else {
if item.Name != "" {
invalidLookups = append(invalidLookups, item.Name)
}
}
}
var errors error
if len(invalidLookups) > 0 {
errors = fmt.Errorf("no results %v", invalidLookups)
}
return results, errors
}