-
Notifications
You must be signed in to change notification settings - Fork 7
/
smartGroupFetcher.manual.go
68 lines (56 loc) · 1.49 KB
/
smartGroupFetcher.manual.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
package c8yfetcher
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y/pkg/c8y"
)
type SmartGroupFetcher struct {
*CumulocityFetcher
}
func NewSmartGroupFetcher(factory *cmdutil.Factory) *SmartGroupFetcher {
return &SmartGroupFetcher{
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
}
}
func (f *SmartGroupFetcher) getByID(id string) ([]fetcherResultSet, error) {
mo, resp, err := f.Client().Inventory.GetManagedObject(
c8y.WithDisabledDryRunContext(context.Background()),
id,
nil,
)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch by id")
}
results := make([]fetcherResultSet, 1)
results[0] = fetcherResultSet{
ID: mo.ID,
Name: mo.Name,
Value: resp.JSON(),
}
return results, nil
}
func (f *SmartGroupFetcher) getByName(name string) ([]fetcherResultSet, error) {
mcol, _, err := f.Client().Inventory.GetManagedObjects(
c8y.WithDisabledDryRunContext(context.Background()),
&c8y.ManagedObjectOptions{
Query: fmt.Sprintf("type eq 'c8y_DynamicGroup' and name eq '%s'", name),
PaginationOptions: *c8y.NewPaginationOptions(5),
},
)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch by id")
}
results := make([]fetcherResultSet, len(mcol.ManagedObjects))
for i, device := range mcol.ManagedObjects {
results[i] = fetcherResultSet{
ID: device.ID,
Name: device.Name,
Value: mcol.Items[i],
}
}
return results, nil
}