-
Notifications
You must be signed in to change notification settings - Fork 7
/
childAdditionFetcher.go
72 lines (61 loc) · 1.56 KB
/
childAdditionFetcher.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
package c8yfetcher
import (
"context"
"github.com/pkg/errors"
"github.com/reubenmiller/go-c8y/pkg/c8y"
)
type ChildAdditionFetcher struct {
client *c8y.Client
parentID string
Query QueryFilter
*DefaultFetcher
}
func NewChildAdditionFetcher(client *c8y.Client, parentID string) *ChildAdditionFetcher {
return &ChildAdditionFetcher{
client: client,
parentID: parentID,
}
}
func (f *ChildAdditionFetcher) 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 *ChildAdditionFetcher) getByName(name string) ([]fetcherResultSet, error) {
query := "name eq '" + name + "'"
if f.Query != nil {
query = f.Query(name)
}
mcol, _, err := f.client.Inventory.GetChildAdditions(
c8y.WithDisabledDryRunContext(context.Background()),
f.parentID,
&c8y.ManagedObjectOptions{
Query: query,
PaginationOptions: *c8y.NewPaginationOptions(5),
},
)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch by id")
}
results := make([]fetcherResultSet, len(mcol.References))
for i, item := range mcol.References {
results[i] = fetcherResultSet{
ID: item.ManagedObject.ID,
Name: item.ManagedObject.Name,
Value: mcol.References[i].ManagedObject.Item,
}
}
return results, nil
}