/
batch_update_entities.go
89 lines (72 loc) · 2.6 KB
/
batch_update_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
package entity
import (
"context"
"connectrpc.com/connect"
"github.com/common-fate/sdk/eid"
entityv1alpha1 "github.com/common-fate/sdk/gen/commonfate/entity/v1alpha1"
"go.opentelemetry.io/otel/attribute"
)
type BatchUpdateInput struct {
// Put is a slice of entities to be upserted to the entity store.
Put []Entity
// PutChildren is a slice of parent/child relationships to be added to the entity store.
PutChildren []ChildRelation
// Archive is a slice of EIDs of entities to be archived.
// When an entity is archived it will no longer appear in the List API calls by default.
Archive []eid.EID
// Unarchive is a slice of EIDs of entities to be made active again.
Unarchive []eid.EID
// Unarchive is a slice of EIDs of entities to be deleted from the entity store.
Delete []eid.EID
// DeleteChildren is a slice of parent/child relations to be removed from the entity store.
DeleteChildren []ChildRelation
}
type BatchUpdateOutput = entityv1alpha1.BatchUpdateResponse
func (c *Client) BatchUpdate(ctx context.Context, input BatchUpdateInput) (*BatchUpdateOutput, error) {
ctx, span := tracer.Start(ctx, "BatchUpdate")
defer span.End()
span.SetAttributes(
attribute.Int("put_count", len(input.Put)),
attribute.Int("put_children_count", len(input.PutChildren)),
attribute.Int("archive_count", len(input.Archive)),
attribute.Int("unarchive_count", len(input.Unarchive)),
attribute.Int("delete_count", len(input.Delete)),
attribute.Int("delete_children_count", len(input.DeleteChildren)),
)
var req = &entityv1alpha1.BatchUpdateRequest{
Universe: "default",
Put: []*entityv1alpha1.Entity{},
}
if len(input.Put) == 0 && len(input.PutChildren) == 0 && len(input.Archive) == 0 && len(input.Unarchive) == 0 && len(input.Delete) == 0 && len(input.DeleteChildren) == 0 {
// no API call required
return nil, nil
}
for _, e := range input.Put {
parsed, children, err := Marshal(e)
if err != nil {
return nil, err
}
req.Put = append(req.Put, parsed)
req.PutChildren = append(req.PutChildren, children...)
}
for _, c := range input.PutChildren {
req.PutChildren = append(req.PutChildren, c.ToAPI())
}
for _, c := range input.Archive {
req.Archive = append(req.Archive, c.ToAPI())
}
for _, c := range input.Unarchive {
req.Unarchive = append(req.Unarchive, c.ToAPI())
}
for _, c := range input.Delete {
req.Delete = append(req.Delete, c.ToAPI())
}
for _, c := range input.DeleteChildren {
req.DeleteChildren = append(req.DeleteChildren, c.ToAPI())
}
res, err := c.raw.BatchUpdate(ctx, connect.NewRequest(req))
if err != nil {
return nil, err
}
return res.Msg, nil
}