mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 02:16:28 +02:00
databroker: preserve data type when deleting changeset (#5540)
* databroker: preserve data type when deleting changeset * use cs.now
This commit is contained in:
parent
a96ab2fe93
commit
e7675a5b2a
2 changed files with 58 additions and 9 deletions
|
@ -4,7 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"google.golang.org/protobuf/types/known/anypb"
|
"google.golang.org/protobuf/proto"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ func GetChangeSet(current, target RecordSetBundle, cmpFn RecordCompareFn) []*Rec
|
||||||
cs := &changeSet{now: timestamppb.Now()}
|
cs := &changeSet{now: timestamppb.Now()}
|
||||||
|
|
||||||
for _, rec := range current.GetRemoved(target).Flatten() {
|
for _, rec := range current.GetRemoved(target).Flatten() {
|
||||||
cs.Remove(rec.GetType(), rec.GetId())
|
cs.Remove(rec)
|
||||||
}
|
}
|
||||||
for _, rec := range current.GetModified(target, cmpFn).Flatten() {
|
for _, rec := range current.GetModified(target, cmpFn).Flatten() {
|
||||||
cs.Upsert(rec)
|
cs.Upsert(rec)
|
||||||
|
@ -33,13 +33,10 @@ type changeSet struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove adds a record to the change set.
|
// Remove adds a record to the change set.
|
||||||
func (cs *changeSet) Remove(typ string, id string) {
|
func (cs *changeSet) Remove(record *Record) {
|
||||||
cs.updates = append(cs.updates, &Record{
|
record = proto.Clone(record).(*Record)
|
||||||
Type: typ,
|
record.DeletedAt = cs.now
|
||||||
Id: id,
|
cs.updates = append(cs.updates, record)
|
||||||
DeletedAt: cs.now,
|
|
||||||
Data: &anypb.Any{TypeUrl: typ},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upsert adds a record to the change set.
|
// Upsert adds a record to the change set.
|
||||||
|
|
52
pkg/grpc/databroker/changeset_test.go
Normal file
52
pkg/grpc/databroker/changeset_test.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package databroker_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"google.golang.org/protobuf/testing/protocmp"
|
||||||
|
"google.golang.org/protobuf/types/known/structpb"
|
||||||
|
|
||||||
|
"github.com/pomerium/datasource/pkg/directory"
|
||||||
|
"github.com/pomerium/pomerium/pkg/grpc/databroker"
|
||||||
|
"github.com/pomerium/pomerium/pkg/protoutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetChangeset(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
rsb1 := databroker.RecordSetBundle{}
|
||||||
|
rsb2 := databroker.RecordSetBundle{}
|
||||||
|
updates := databroker.GetChangeSet(rsb1, rsb2, func(record1, record2 *databroker.Record) bool {
|
||||||
|
return cmp.Equal(record1, record2, protocmp.Transform())
|
||||||
|
})
|
||||||
|
assert.Len(t, updates, 0)
|
||||||
|
|
||||||
|
rsb1 = databroker.RecordSetBundle{}
|
||||||
|
rsb1.Add(&databroker.Record{
|
||||||
|
Type: directory.UserRecordType,
|
||||||
|
Id: "user-1",
|
||||||
|
Data: protoutil.NewAny(mustNewStruct(map[string]any{
|
||||||
|
"email": "user-1@example.com",
|
||||||
|
})),
|
||||||
|
})
|
||||||
|
rsb2 = databroker.RecordSetBundle{}
|
||||||
|
updates = databroker.GetChangeSet(rsb1, rsb2, func(record1, record2 *databroker.Record) bool {
|
||||||
|
return cmp.Equal(record1, record2, protocmp.Transform())
|
||||||
|
})
|
||||||
|
if assert.Len(t, updates, 1) {
|
||||||
|
assert.Equal(t, directory.UserRecordType, updates[0].GetType())
|
||||||
|
assert.Equal(t, "type.googleapis.com/google.protobuf.Struct", updates[0].GetData().GetTypeUrl(),
|
||||||
|
"should preserve data type")
|
||||||
|
assert.NotNil(t, updates[0].GetDeletedAt())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustNewStruct(m map[string]any) *structpb.Struct {
|
||||||
|
s, err := structpb.NewStruct(m)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue