-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_entities.go
102 lines (87 loc) · 2.78 KB
/
list_entities.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
package entity
import (
"context"
"connectrpc.com/connect"
"github.com/common-fate/sdk/eid"
entityv1alpha1 "github.com/common-fate/sdk/gen/commonfate/entity/v1alpha1"
"github.com/patrickmn/go-cache"
"go.opentelemetry.io/otel/attribute"
)
type ListInput struct {
Type string
PageToken string
IncludeArchived bool
OrderDescending bool
}
func (c *Client) List(ctx context.Context, input ListInput) (*ListOutput, error) {
ctx, span := tracer.Start(ctx, "ListEntities")
defer span.End()
req := &entityv1alpha1.ListRequest{
Universe: "default",
Type: input.Type,
PageToken: input.PageToken,
IncludeArchived: input.IncludeArchived,
}
if input.OrderDescending {
req.Order = entityv1alpha1.Order_ORDER_DESCENDING.Enum().Enum()
}
res, err := c.raw.List(ctx, connect.NewRequest(req))
if err != nil {
return nil, err
}
// update the attribute cache
for _, e := range res.Msg.Entities {
id := eid.EID{
Type: e.Eid.Type,
ID: e.Eid.Id,
}
c.cache.Set(id.String(), e, cache.DefaultExpiration)
}
span.SetAttributes(attribute.Int("entities_count", len(res.Msg.Entities)))
return res.Msg, nil
}
type listEntitiesRequestCall struct {
input ListInput
client *Client
}
// FilterEntitiesRequest returns a request with filterEntitiesRequestCall.Pages() that will pull all pages of results, invoking the callback for each page
// I based this pattern off the google cloud SDK, I found it to be pretty neat, not set on the naming
// I think a good API here will have the option to do a single API call or a Pages call
// in the google API it would be filterEntitiesRequestCall.Do() to make a single request
// they also use a chained builder pattern
func (c *Client) ListRequest(input ListInput) *listEntitiesRequestCall {
return &listEntitiesRequestCall{
input: input,
client: c,
}
}
type ListOutput = entityv1alpha1.ListResponse
// Pages invokes f for each page of results.
// A non-nil error returned from f will halt the iteration.
// The provided context supersedes any context provided to the Context method.
func (c *listEntitiesRequestCall) Pages(ctx context.Context, f func(*ListOutput) error) error {
// resets the input back to its original state
originalPageToken := c.input.PageToken
defer func() { c.input.PageToken = originalPageToken }()
for {
x, err := c.client.List(ctx, c.input)
if err != nil {
return err
}
if err := f(x); err != nil {
return err
}
if x.NextPageToken == "" {
return nil
}
c.input.PageToken = x.NextPageToken
}
}
// All returns all results from a paginated API
func (c *Client) All(ctx context.Context, input ListInput) (out []*entityv1alpha1.Entity, err error) {
err = c.ListRequest(input).Pages(ctx, func(lo *ListOutput) error {
out = append(out, lo.Entities...)
return nil
})
return
}