From 9a44e71445863208b23eade3389c9ceec6a5e589 Mon Sep 17 00:00:00 2001 From: Miguel Prieto Date: Thu, 2 Jan 2025 19:43:45 -0300 Subject: [PATCH 1/2] Unify environment variable usage. --- .github/workflows/integration-tests.yml | 4 +-- Dockerfile | 8 ++--- README.md | 37 ++++------------------- examples/hello_world/main.go | 30 +------------------ sdk/client/api_client.go | 22 +++++++++++--- test/testdata/util.go | 40 ++++--------------------- 6 files changed, 36 insertions(+), 105 deletions(-) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 0a6b4921..7ce4b732 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -36,8 +36,8 @@ jobs: gotestsum --format testname --junitfile report.xml -- -v ./test/integration_tests/... env: CONDUCTOR_SERVER_URL: ${{ secrets.CONDUCTOR_SERVER_URL }} - KEY: ${{ secrets.KEY }} - SECRET: ${{ secrets.SECRET }} + CONDUCTOR_AUTH_KEY: ${{ secrets.KEY }} + CONDUCTOR_AUTH_SECRET: ${{ secrets.SECRET }} - name: Set PR Status to Failure if: ${{ failure() }} diff --git a/Dockerfile b/Dockerfile index 27f68be6..cfdcfb71 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,10 +12,10 @@ RUN go test -v $(go list ./... | grep -v /test/integration_tests) FROM build as inttest COPY /test /package/test -ARG KEY -ARG SECRET +ARG CONDUCTOR_AUTH_KEY +ARG CONDUCTOR_AUTH_SECRET ARG CONDUCTOR_SERVER_URL -ENV KEY=${KEY} -ENV SECRET=${SECRET} +ENV CONDUCTOR_AUTH_KEY=${CONDUCTOR_AUTH_KEY} +ENV CONDUCTOR_AUTH_SECRET=${CONDUCTOR_AUTH_SECRET} ENV CONDUCTOR_SERVER_URL=${CONDUCTOR_SERVER_URL} RUN go test -v ./test/integration_tests/... diff --git a/README.md b/README.md index aba8a14a..3a2197ab 100644 --- a/README.md +++ b/README.md @@ -173,41 +173,16 @@ To begin with, let's take a look at the variable declaration in [examples/hello_ ```go var ( - apiClient = client.NewAPIClient( - authSettings(), - httpSettings(), - ) + apiClient = client.NewAPIClientFromEnv() taskRunner = worker.NewTaskRunnerWithApiClient(apiClient) workflowExecutor = executor.NewWorkflowExecutor(apiClient) ) -func authSettings() *settings.AuthenticationSettings { - key := os.Getenv("KEY") - secret := os.Getenv("SECRET") - if key != "" && secret != "" { - return settings.NewAuthenticationSettings( - key, - secret, - ) - } - - return nil -} - -func httpSettings() *settings.HttpSettings { - url := os.Getenv("CONDUCTOR_SERVER_URL") - if url == "" { - fmt.Fprintf(os.Stderr, "Error: CONDUCTOR_SERVER_URL env variable is not set\n") - os.Exit(1) - } - - return settings.NewHttpSettings(url) -} ``` First we create an `APIClient` instance. This is a REST client. -We need to pass on the proper settings to our client. For convenience to run the example you can set the following environment variables: `CONDUCTOR_SERVER_URL`, `KEY`, `SECRET`. +We need to provide the correct settings to our client. In this example, `client.NewAPIClientFromEnv()` is used, which initializes a new client by reading the settings from the following environment variables: `CONDUCTOR_SERVER_URL`, `CONDUCTOR_AUTH_KEY`, and `CONDUCTOR_AUTH_SECRET`. Now let's take a look at the `main` function: @@ -265,11 +240,11 @@ cd examples go run hello_world/main.go ``` -#### Running the example in Orkes playground. +#### Running the example with an [Orkes developer account](https://developer.orkescloud.com). ```shell -export CONDUCTOR_SERVER_URL="https://play.orkes.io/api" -export KEY="..." -export SECRET="..." +export CONDUCTOR_SERVER_URL="https://developer.orkescloud.com/api" +export CONDUCTOR_AUTH_KEY="..." +export CONDUCTOR_AUTH_SECRET="..." cd examples go run hello_world/main.go ``` diff --git a/examples/hello_world/main.go b/examples/hello_world/main.go index e6450f04..395fc38e 100644 --- a/examples/hello_world/main.go +++ b/examples/hello_world/main.go @@ -2,51 +2,23 @@ package main import ( hello_world "examples/hello_world/src" - "os" "time" log "github.com/sirupsen/logrus" "github.com/conductor-sdk/conductor-go/sdk/client" "github.com/conductor-sdk/conductor-go/sdk/model" - "github.com/conductor-sdk/conductor-go/sdk/settings" "github.com/conductor-sdk/conductor-go/sdk/worker" "github.com/conductor-sdk/conductor-go/sdk/workflow/executor" ) var ( - apiClient = client.NewAPIClient( - authSettings(), - httpSettings(), - ) + apiClient = client.NewAPIClientFromEnv() taskRunner = worker.NewTaskRunnerWithApiClient(apiClient) workflowExecutor = executor.NewWorkflowExecutor(apiClient) ) -func authSettings() *settings.AuthenticationSettings { - key := os.Getenv("KEY") - secret := os.Getenv("SECRET") - if key != "" && secret != "" { - return settings.NewAuthenticationSettings( - key, - secret, - ) - } - - return nil -} - -func httpSettings() *settings.HttpSettings { - url := os.Getenv("CONDUCTOR_SERVER_URL") - if url == "" { - log.Error("Error: CONDUCTOR_SERVER_URL env variable is not set") - os.Exit(1) - } - - return settings.NewHttpSettings(url) -} - func main() { // Start the Greet Worker. This worker will process "greet" tasks. taskRunner.StartWorker("greet", hello_world.Greet, 1, time.Millisecond*100) diff --git a/sdk/client/api_client.go b/sdk/client/api_client.go index 5d50f33e..6320b62d 100644 --- a/sdk/client/api_client.go +++ b/sdk/client/api_client.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "io" + "log" "mime/multipart" "net" "net/http" @@ -47,7 +48,6 @@ var ( type APIClient struct { httpRequester *HttpRequester - tokenManager authentication.TokenManager } func NewAPIClient( @@ -62,9 +62,23 @@ func NewAPIClient( ) } func NewAPIClientFromEnv() *APIClient { - authenticationSettings := settings.NewAuthenticationSettings(os.Getenv(CONDUCTOR_AUTH_KEY), os.Getenv(CONDUCTOR_AUTH_SECRET)) - httpSettings := settings.NewHttpSettings(os.Getenv(CONDUCTOR_SERVER_URL)) - return NewAPIClient(authenticationSettings, httpSettings) + return NewAPIClient(NewAuthenticationSettingsFromEnv(), NewHttpSettingsFromEnv()) +} + +func NewAuthenticationSettingsFromEnv() *settings.AuthenticationSettings { + return settings.NewAuthenticationSettings( + os.Getenv(CONDUCTOR_AUTH_KEY), + os.Getenv(CONDUCTOR_AUTH_SECRET), + ) +} + +func NewHttpSettingsFromEnv() *settings.HttpSettings { + url := os.Getenv(CONDUCTOR_SERVER_URL) + if url == "" { + log.Fatalf("Error: %s env variable is not set", CONDUCTOR_SERVER_URL) + } + + return settings.NewHttpSettings(url) } func NewAPIClientWithTokenExpiration( diff --git a/test/testdata/util.go b/test/testdata/util.go index 51b3295d..fea681ed 100644 --- a/test/testdata/util.go +++ b/test/testdata/util.go @@ -19,7 +19,6 @@ import ( "github.com/conductor-sdk/conductor-go/sdk/authentication" "github.com/conductor-sdk/conductor-go/sdk/client" "github.com/conductor-sdk/conductor-go/sdk/model" - "github.com/conductor-sdk/conductor-go/sdk/settings" "github.com/conductor-sdk/conductor-go/sdk/worker" "github.com/conductor-sdk/conductor-go/sdk/workflow" "github.com/conductor-sdk/conductor-go/sdk/workflow/executor" @@ -27,17 +26,12 @@ import ( log "github.com/sirupsen/logrus" ) -const ( - AUTHENTICATION_KEY_ID = "KEY" - AUTHENTICATION_KEY_SECRET = "SECRET" - BASE_URL = "CONDUCTOR_SERVER_URL" -) - -var ( - apiClient = getApiClientWithAuthentication() -) - var ( + apiClient = client.NewAPIClientWithTokenExpiration( + client.NewAuthenticationSettingsFromEnv(), + client.NewHttpSettingsFromEnv(), + authentication.NewTokenExpiration(3*time.Second, 30*time.Second), + ) MetadataClient = client.MetadataResourceApiService{ APIClient: apiClient, } @@ -98,30 +92,6 @@ func ValidateWorkflowDaemon(waitTime time.Duration, outputChannel chan error, wo outputChannel <- nil } -func getApiClientWithAuthentication() *client.APIClient { - return client.NewAPIClientWithTokenExpiration( - getAuthenticationSettings(), - getHttpSettingsWithAuth(), - authentication.NewTokenExpiration( - 3*time.Second, - 30*time.Second, - ), - ) -} - -func getAuthenticationSettings() *settings.AuthenticationSettings { - return settings.NewAuthenticationSettings( - os.Getenv(AUTHENTICATION_KEY_ID), - os.Getenv(AUTHENTICATION_KEY_SECRET), - ) -} - -func getHttpSettingsWithAuth() *settings.HttpSettings { - return settings.NewHttpSettings( - os.Getenv(BASE_URL), - ) -} - func StartWorkflows(workflowQty int, workflowName string) ([]string, error) { workflowIdList := make([]string, workflowQty) for i := 0; i < workflowQty; i += 1 { From a76d1f94d25a98c89743215ba967cefb4c26ee7e Mon Sep 17 00:00:00 2001 From: Miguel Prieto Date: Fri, 3 Jan 2025 01:20:21 -0300 Subject: [PATCH 2/2] Fix unit test. These tests shouldn't require a workflow executor connected to a Conductor instance. --- test/unit_tests/workflow_def_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/unit_tests/workflow_def_test.go b/test/unit_tests/workflow_def_test.go index a99110be..ab1d8c11 100644 --- a/test/unit_tests/workflow_def_test.go +++ b/test/unit_tests/workflow_def_test.go @@ -8,7 +8,7 @@ import ( "github.com/conductor-sdk/conductor-go/sdk/model" "github.com/conductor-sdk/conductor-go/sdk/workflow" - "github.com/conductor-sdk/conductor-go/test/testdata" + "github.com/stretchr/testify/assert" ) @@ -17,7 +17,7 @@ func TestRetrySettings(t *testing.T) { simpleTask.RetryPolicy(2, workflow.FixedRetry, 10, 1) simpleTask.Input("url", "${workflow.input.url}") simpleTask.CacheConfig("${url}", 120) - wf := workflow.NewConductorWorkflow(testdata.WorkflowExecutor). + wf := workflow.NewConductorWorkflow(nil). Name("workflow_with_task_retries"). Version(1). Add(simpleTask) @@ -41,7 +41,7 @@ func TestHttpTask(t *testing.T) { httpTask.RetryPolicy(2, workflow.FixedRetry, 10, 1) httpTask.Input("url", "${workflow.input.url}") httpTask.CacheConfig("${url}", 120) - wf := workflow.NewConductorWorkflow(testdata.WorkflowExecutor). + wf := workflow.NewConductorWorkflow(nil). Name("workflow_with_http_task_retries"). Version(1). Add(httpTask) @@ -62,7 +62,7 @@ func TestUpdateTaskWithTaskId(t *testing.T) { updateTask.MergeOutput(true) updateTask.TaskOutput(map[string]interface{}{"key": map[string]interface{}{"nestedKey": "nestedValue"}}) - wf := workflow.NewConductorWorkflow(testdata.WorkflowExecutor). + wf := workflow.NewConductorWorkflow(nil). Name("workflow_with_update_task"). Version(1). Add(updateTask) @@ -82,12 +82,11 @@ func TestUpdateTaskWithTaskId(t *testing.T) { } func TestUpdateTaskWithWorkflowIdAndTaskRef(t *testing.T) { - updateTask := workflow.NewUpdateTask("update_task_ref", model.CompletedTask, "target_workflow", "target_task_ref") updateTask.MergeOutput(true) integers := []int{2, 3, 5, 7, 11, 13} updateTask.TaskOutput(map[string]interface{}{"key": integers}) - wf := workflow.NewConductorWorkflow(testdata.WorkflowExecutor). + wf := workflow.NewConductorWorkflow(nil). Name("workflow_with_update_task"). Version(1). Add(updateTask)