Skip to content

Commit

Permalink
generate instanceID in grpc workflow start API (#7503)
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Kumar <shivamkm07@gmail.com>
Co-authored-by: Artur Souza <asouza.pro@gmail.com>
  • Loading branch information
shivamkm07 and artursouza committed Feb 6, 2024
1 parent e207377 commit 2bcb802
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
10 changes: 0 additions & 10 deletions pkg/api/http/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/dapr/dapr/pkg/api/http/endpoints"
Expand Down Expand Up @@ -200,16 +199,7 @@ func (a *api) onStartWorkflowHandler() http.HandlerFunc {
InModifier: func(r *http.Request, in *runtimev1pb.StartWorkflowRequest) (*runtimev1pb.StartWorkflowRequest, error) {
in.WorkflowName = chi.URLParam(r, workflowName)
in.WorkflowComponent = chi.URLParam(r, workflowComponent)

// The instance ID is optional. If not specified, we generate a random one.
in.InstanceId = r.URL.Query().Get(instanceID)
if in.GetInstanceId() == "" {
randomID, err := uuid.NewRandom()
if err != nil {
return nil, err
}
in.InstanceId = randomID.String()
}

// We accept the HTTP request body as the input to the workflow
// without making any assumptions about its format.
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/universal/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"unicode"

"github.com/google/uuid"
"github.com/microsoft/durabletask-go/api"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -69,6 +70,14 @@ func (a *Universal) GetWorkflowBeta1(ctx context.Context, in *runtimev1pb.GetWor

// StartWorkflowBeta1 is the API handler for starting a workflow
func (a *Universal) StartWorkflowBeta1(ctx context.Context, in *runtimev1pb.StartWorkflowRequest) (*runtimev1pb.StartWorkflowResponse, error) {
// The instance ID is optional. If not specified, we generate a random one.
if in.GetInstanceId() == "" {
randomID, err := uuid.NewRandom()
if err != nil {
return nil, err
}
in.InstanceId = randomID.String()
}
if err := a.validateInstanceID(in.GetInstanceId(), true /* isCreate */); err != nil {
a.logger.Debug(err)
return &runtimev1pb.StartWorkflowResponse{}, err
Expand Down
13 changes: 6 additions & 7 deletions pkg/api/universal/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ func TestStartWorkflowBeta1API(t *testing.T) {
instanceID: fakeInstanceID,
expectedError: messages.ErrWorkflowNameMissing,
},
{
testName: "No instance ID provided in start request",
workflowComponent: fakeComponentName,
workflowName: fakeWorkflowName,
instanceID: "",
expectedError: messages.ErrMissingOrEmptyInstance,
},
{
testName: "Invalid instance ID provided in start request",
workflowComponent: fakeComponentName,
Expand All @@ -98,6 +91,12 @@ func TestStartWorkflowBeta1API(t *testing.T) {
instanceID: daprt.ErrorInstanceID,
expectedError: messages.ErrStartWorkflow.WithFormat(fakeWorkflowName, daprt.ErrFakeWorkflowComponentError),
},
{
testName: "No instance ID provided in start request",
workflowComponent: fakeComponentName,
workflowName: fakeWorkflowName,
instanceID: "",
},
{
testName: "All is well in start request",
workflowComponent: fakeComponentName,
Expand Down

0 comments on commit 2bcb802

Please sign in to comment.