Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify environment variable usage #164

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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() }}
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/...
37 changes: 6 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
```
Expand Down
30 changes: 1 addition & 29 deletions examples/hello_world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 18 additions & 4 deletions sdk/client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net"
"net/http"
Expand Down Expand Up @@ -47,7 +48,6 @@ var (

type APIClient struct {
httpRequester *HttpRequester
tokenManager authentication.TokenManager
}

func NewAPIClient(
Expand All @@ -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(
Expand Down
40 changes: 5 additions & 35 deletions test/testdata/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@ 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"

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,
}
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions test/unit_tests/workflow_def_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading