From 1ea27c5a47e009044971a87458dd47b434baaa36 Mon Sep 17 00:00:00 2001 From: Eron Wright Date: Fri, 16 Feb 2024 13:59:27 -0800 Subject: [PATCH 1/5] FindByUrn method for grpclog package --- grpclog/grpclog.go | 27 +++++++++++++++++++++++++++ grpclog/grpclog_test.go | 12 ++++++++++++ 2 files changed, 39 insertions(+) diff --git a/grpclog/grpclog.go b/grpclog/grpclog.go index 157e563..2431c9f 100644 --- a/grpclog/grpclog.go +++ b/grpclog/grpclog.go @@ -45,6 +45,14 @@ const ( Update Method = "/pulumirpc.ResourceProvider/Update" ) +type resourceRequest interface { + rpc.CheckRequest | rpc.DiffRequest | rpc.ReadRequest | rpc.CreateRequest | rpc.UpdateRequest | rpc.DeleteRequest +} + +type resourceResponse interface { + rpc.CheckResponse | rpc.DiffResponse | rpc.ReadResponse | rpc.CreateResponse | rpc.UpdateResponse | emptypb.Empty +} + func (l *GrpcLog) Attaches() ([]TypedEntry[rpc.PluginAttach, emptypb.Empty], error) { return unmarshalTypedEntries[rpc.PluginAttach, emptypb.Empty](l.WhereMethod(Attach)) } @@ -218,3 +226,22 @@ func (l *GrpcLog) Marshal() ([]byte, error) { } return bytes, nil } + +type hasURN interface { + GetUrn() string +} + +// FindByUrn finds the first entry with the given resource URN, or nil if none is found. +func FindByUrn[TRequest resourceRequest, TResponse resourceResponse](entries []TypedEntry[TRequest, TResponse], + urn string) *TypedEntry[TRequest, TResponse] { + // nolint:copylocks + for _, e := range entries { + var eI any = &e.Request + if hasUrn, ok := eI.(hasURN); ok { + if hasUrn.GetUrn() == urn { + return &e + } + } + } + return nil +} diff --git a/grpclog/grpclog_test.go b/grpclog/grpclog_test.go index 1d03f64..7cc4322 100644 --- a/grpclog/grpclog_test.go +++ b/grpclog/grpclog_test.go @@ -35,3 +35,15 @@ func TestGrpcLog(t *testing.T) { assert.Equal(t, log, unmarshalled) }) } + +func TestFindByUrn(t *testing.T) { + t.Parallel() + log, err := grpclog.LoadLog(filepath.Join("testdata", "aws_bucket_grpc.json")) + assert.NoError(t, err) + creates, err := log.Creates() + assert.NoError(t, err) + resource := grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::tested-resource") + assert.NotNil(t, resource) + resource = grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::other") + assert.Nil(t, resource) +} From 5dbf8d71df4ade8a828e04abd9bbe5c1c0705af0 Mon Sep 17 00:00:00 2001 From: Eron Wright Date: Tue, 20 Feb 2024 13:46:08 -0800 Subject: [PATCH 2/5] feedback --- grpclog/grpclog.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/grpclog/grpclog.go b/grpclog/grpclog.go index 2431c9f..c136713 100644 --- a/grpclog/grpclog.go +++ b/grpclog/grpclog.go @@ -237,8 +237,9 @@ func FindByUrn[TRequest resourceRequest, TResponse resourceResponse](entries []T // nolint:copylocks for _, e := range entries { var eI any = &e.Request - if hasUrn, ok := eI.(hasURN); ok { - if hasUrn.GetUrn() == urn { + switch r := eI.(type) { + case hasURN: + if r.GetUrn() == urn { return &e } } From db2365c5f588e95f1337830cc47ea9468f5d560e Mon Sep 17 00:00:00 2001 From: Eron Wright Date: Tue, 20 Feb 2024 13:49:45 -0800 Subject: [PATCH 3/5] feedback --- grpclog/grpclog.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grpclog/grpclog.go b/grpclog/grpclog.go index c136713..b09a568 100644 --- a/grpclog/grpclog.go +++ b/grpclog/grpclog.go @@ -236,8 +236,8 @@ func FindByUrn[TRequest resourceRequest, TResponse resourceResponse](entries []T urn string) *TypedEntry[TRequest, TResponse] { // nolint:copylocks for _, e := range entries { - var eI any = &e.Request - switch r := eI.(type) { + var rI any = &e.Request + switch r := rI.(type) { case hasURN: if r.GetUrn() == urn { return &e From dbb20ad2d2ca923ae7246dd7b218f007222284b3 Mon Sep 17 00:00:00 2001 From: Eron Wright Date: Tue, 20 Feb 2024 16:23:08 -0800 Subject: [PATCH 4/5] feedback --- grpclog/grpclog.go | 12 ++++++------ grpclog/grpclog_test.go | 6 ++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/grpclog/grpclog.go b/grpclog/grpclog.go index b09a568..c9a0f85 100644 --- a/grpclog/grpclog.go +++ b/grpclog/grpclog.go @@ -232,17 +232,17 @@ type hasURN interface { } // FindByUrn finds the first entry with the given resource URN, or nil if none is found. +// Returns the index of the entry, if found. func FindByUrn[TRequest resourceRequest, TResponse resourceResponse](entries []TypedEntry[TRequest, TResponse], - urn string) *TypedEntry[TRequest, TResponse] { - // nolint:copylocks - for _, e := range entries { - var rI any = &e.Request + urn string) (*TypedEntry[TRequest, TResponse], int) { + for i := range entries { + var rI any = &entries[i].Request switch r := rI.(type) { case hasURN: if r.GetUrn() == urn { - return &e + return &entries[i], i } } } - return nil + return nil, -1 } diff --git a/grpclog/grpclog_test.go b/grpclog/grpclog_test.go index 7cc4322..627ea53 100644 --- a/grpclog/grpclog_test.go +++ b/grpclog/grpclog_test.go @@ -42,8 +42,10 @@ func TestFindByUrn(t *testing.T) { assert.NoError(t, err) creates, err := log.Creates() assert.NoError(t, err) - resource := grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::tested-resource") + resource, i := grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::tested-resource") assert.NotNil(t, resource) - resource = grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::other") + assert.Equal(t, 0, i) + resource, i = grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::other") assert.Nil(t, resource) + assert.Equal(t, -1, i) } From f97b518b7f90cdd1e929040fb307150f7ba56f9e Mon Sep 17 00:00:00 2001 From: Eron Wright Date: Tue, 20 Feb 2024 17:11:56 -0800 Subject: [PATCH 5/5] feedback --- grpclog/grpclog.go | 4 ++-- grpclog/grpclog_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/grpclog/grpclog.go b/grpclog/grpclog.go index c9a0f85..c0b9143 100644 --- a/grpclog/grpclog.go +++ b/grpclog/grpclog.go @@ -231,9 +231,9 @@ type hasURN interface { GetUrn() string } -// FindByUrn finds the first entry with the given resource URN, or nil if none is found. +// FindByURN finds the first entry with the given resource URN, or nil if none is found. // Returns the index of the entry, if found. -func FindByUrn[TRequest resourceRequest, TResponse resourceResponse](entries []TypedEntry[TRequest, TResponse], +func FindByURN[TRequest resourceRequest, TResponse resourceResponse](entries []TypedEntry[TRequest, TResponse], urn string) (*TypedEntry[TRequest, TResponse], int) { for i := range entries { var rI any = &entries[i].Request diff --git a/grpclog/grpclog_test.go b/grpclog/grpclog_test.go index 627ea53..14bc489 100644 --- a/grpclog/grpclog_test.go +++ b/grpclog/grpclog_test.go @@ -36,16 +36,16 @@ func TestGrpcLog(t *testing.T) { }) } -func TestFindByUrn(t *testing.T) { +func TestFindByURN(t *testing.T) { t.Parallel() log, err := grpclog.LoadLog(filepath.Join("testdata", "aws_bucket_grpc.json")) assert.NoError(t, err) creates, err := log.Creates() assert.NoError(t, err) - resource, i := grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::tested-resource") + resource, i := grpclog.FindByURN(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::tested-resource") assert.NotNil(t, resource) assert.Equal(t, 0, i) - resource, i = grpclog.FindByUrn(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::other") + resource, i = grpclog.FindByURN(creates, "urn:pulumi:p-it-antons-mac-bucket-9f59db4a::test::aws:s3/bucket:Bucket::other") assert.Nil(t, resource) assert.Equal(t, -1, i) }