Skip to content

Commit

Permalink
feat: add a long-running operation
Browse files Browse the repository at this point in the history
Make DeleteShipper a long-running operation to enable POC:ing with how
to best support authorization for the implicit LRO mixins.
  • Loading branch information
odsod committed May 19, 2021
1 parent 15d9b83 commit 1bfd571
Show file tree
Hide file tree
Showing 10 changed files with 1,341 additions and 914 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ all: \
spanner-generate \
go-lint \
go-test \
go-mod-tidy
go-mod-tidy \
go-install-iamctl

include tools/buf/rules.mk
include tools/golangci-lint/rules.mk
Expand Down Expand Up @@ -61,3 +62,8 @@ go-mod-tidy:
go-test:
$(info [$@] running Go test suites...)
go test -count=1 -race ./...

.PHONY: go-install-iamctl
go-install-iamctl:
$(info [$@] installing iamctl...)
@cd ./cmd/iamctl && go install .
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func runDeleteShipperCommand(
client iamexamplev1.FreightServiceClient,
flags *deleteShipperFlags,
) error {
shipper, err := client.DeleteShipper(ctx, &iamexamplev1.DeleteShipperRequest{
operation, err := client.DeleteShipper(ctx, &iamexamplev1.DeleteShipperRequest{
Name: flags.Name,
})
if err != nil {
return err
}
log.Println(protojson.Format(shipper))
log.Println(protojson.Format(operation))
return nil
}
31 changes: 28 additions & 3 deletions iamexample/server_shippers_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import (
"context"

"cloud.google.com/go/spanner"
"go.einride.tech/aip/resourceid"
"go.einride.tech/aip/resourcename"
"go.einride.tech/aip/validation"
"go.einride.tech/iam/iamexample/iamexampledb"
iamexamplev1 "go.einride.tech/iam/proto/gen/einride/iam/example/v1"
"google.golang.org/genproto/googleapis/longrunning"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
)

// DeleteShipper implements iamexamplev1.FreightServiceServer.
func (s *Server) DeleteShipper(
ctx context.Context,
request *iamexamplev1.DeleteShipperRequest,
) (*iamexamplev1.Shipper, error) {
) (*longrunning.Operation, error) {
var parsedRequest deleteShipperRequest
if err := parsedRequest.parse(request); err != nil {
return nil, err
Expand All @@ -28,7 +31,7 @@ func (s *Server) DeleteShipper(
func (s *Server) deleteShipper(
ctx context.Context,
request *deleteShipperRequest,
) (*iamexamplev1.Shipper, error) {
) (*longrunning.Operation, error) {
var result *iamexamplev1.Shipper
commitTime, err := s.Spanner.ReadWriteTransaction(
ctx,
Expand Down Expand Up @@ -66,7 +69,29 @@ func (s *Server) deleteShipper(
}
result.UpdateTime = timestamppb.New(commitTime)
result.DeleteTime = result.UpdateTime
return result, nil
operationResponse, err := anypb.New(result)
if err != nil {
s.errorHook(ctx, err)
return nil, status.Error(codes.Internal, "error marshaling operation response")
}
now := timestamppb.Now()
operationMetadata, err := anypb.New(&iamexamplev1.DeleteShipperOperationMetadata{
StartTime: now,
EndTime: now,
})
if err != nil {
s.errorHook(ctx, err)
return nil, status.Error(codes.Internal, "error marshaling operation metadata")
}
operation := &longrunning.Operation{
Name: resourcename.Sprint("operations/{operation}", resourceid.NewSystemGeneratedBase32()),
Done: true,
Metadata: operationMetadata,
Result: &longrunning.Operation_Response{
Response: operationResponse,
},
}
return operation, nil
}

type deleteShipperRequest struct {
Expand Down
11 changes: 8 additions & 3 deletions iamexample/server_shippers_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package iamexample
import (
"context"
"testing"
"time"

iamexamplev1 "go.einride.tech/iam/proto/gen/einride/iam/example/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -35,15 +36,19 @@ func (ts *serverTestSuite) testDeleteShipper(t *testing.T) {
)
assert.NilError(t, err)
assert.Equal(t, input.DisplayName, created.DisplayName)
deleted, err := fx.client.DeleteShipper(
operation, err := fx.client.DeleteShipper(
WithOutgoingMembers(ctx, member),
&iamexamplev1.DeleteShipperRequest{
Name: created.Name,
},
)
assert.NilError(t, err)
assert.Assert(t, operation.Done)
response, err := anypb.UnmarshalNew(operation.GetResponse(), proto.UnmarshalOptions{})
assert.NilError(t, err)
deleted, ok := response.(*iamexamplev1.Shipper)
assert.Assert(t, ok)
assert.Equal(t, created.Name, deleted.Name)
assert.Assert(t, time.Since(deleted.DeleteTime.AsTime()) < time.Second)
})
})

Expand Down
1,627 changes: 870 additions & 757 deletions proto/gen/einride/iam/example/v1/freight_service.pb.go

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions proto/gen/einride/iam/example/v1/freight_service_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proto/gen/einride/iam/example/v1/freight_service_iam.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1bfd571

Please sign in to comment.