-
Notifications
You must be signed in to change notification settings - Fork 7
/
microserviceFetcher.go
129 lines (103 loc) · 3.28 KB
/
microserviceFetcher.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
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 MicroserviceFetcher struct {
*CumulocityFetcher
// Enable the owner check which will only return microservices owned by the current tenant
// This option was added to ensure that it will not break the existing behaviour,
// though this option could be enabled by default in the future if it proves to be more useful
EnableOwnerCheck bool
}
func NewMicroserviceFetcher(factory *cmdutil.Factory) *MicroserviceFetcher {
return &MicroserviceFetcher{
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
}
}
func (f *MicroserviceFetcher) 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 *MicroserviceFetcher) getByName(name string) ([]fetcherResultSet, error) {
serverOptions := &c8y.ApplicationOptions{
PaginationOptions: *c8y.NewPaginationOptions(2000),
Type: c8y.ApplicationTypeMicroservice,
}
if f.EnableOwnerCheck && 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 microservices")
}
pattern, err := regexp.Compile("^" + regexp.QuoteMeta(name) + "$")
if err != nil {
return nil, errors.Wrap(err, "invalid regex")
}
if err != nil {
return nil, errors.Wrap(err, "Could not fetch by id")
}
results := make([]fetcherResultSet, 0)
for i, app := range col.Applications {
if pattern.MatchString(app.Name) {
results = append(results, fetcherResultSet{
ID: app.ID,
Name: app.Name,
Value: col.Items[i],
})
}
}
return results, nil
}
// findMicroservices returns microservices 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 FindMicroservices(factory *cmdutil.Factory, values []string, lookupID bool, format string, enableOwnerCheck bool) ([]entityReference, error) {
f := NewMicroserviceFetcher(factory)
f.EnableOwnerCheck = enableOwnerCheck
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
}