Skip to content

Commit

Permalink
fix: fix orchestration ledger v1/v2 compatibility (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Jun 9, 2023
1 parent eb0d739 commit 24dfccc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
2 changes: 1 addition & 1 deletion components/orchestration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
go.temporal.io/sdk v1.21.1
go.temporal.io/sdk/contrib/opentelemetry v0.2.0
go.uber.org/fx v1.19.1
golang.org/x/mod v0.8.0
golang.org/x/oauth2 v0.5.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -112,7 +113,6 @@ require (
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package activities

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
"github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/collectionutils"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
"golang.org/x/mod/semver"
)

type CreateTransactionRequest struct {
Expand All @@ -17,6 +21,18 @@ type CreateTransactionRequest struct {
}

func (a Activities) CreateTransaction(ctx context.Context, request CreateTransactionRequest) (*shared.CreateTransactionResponse, error) {
versionsResponse, err := a.client.GetVersions(ctx)
if err != nil {
return nil, err
}
if versionsResponse.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d when getting versions", versionsResponse.StatusCode)
}

version := collectionutils.Filter(versionsResponse.GetVersionsResponse.Versions, func(version shared.Version) bool {
return version.Name == "ledger"
})[0].Version

response, err := a.client.Transactions.
CreateTransaction(
ctx,
Expand All @@ -25,22 +41,32 @@ func (a Activities) CreateTransaction(ctx context.Context, request CreateTransac
Ledger: request.Ledger,
},
)
if err != nil {
return nil, err
}

switch response.StatusCode {
case http.StatusOK:
return response.CreateTransactionResponse, nil
default:
if response.ErrorResponse != nil {
return nil, temporal.NewApplicationError(
response.ErrorResponse.ErrorMessage,
string(response.ErrorResponse.ErrorCode),
response.ErrorResponse.Details)
if semver.IsValid(version) && semver.Compare(version, "v2.0.0") < 0 {
baseResponse := api.BaseResponse[[]shared.Transaction]{}
if err := json.NewDecoder(response.RawResponse.Body).Decode(&baseResponse); err != nil {
return nil, err
}
return &shared.CreateTransactionResponse{
Data: (*baseResponse.Data)[0],
}, nil
} else {
if err != nil {
return nil, err
}

switch response.StatusCode {
case http.StatusOK:
return response.CreateTransactionResponse, nil
default:
if response.ErrorResponse != nil {
return nil, temporal.NewApplicationError(
response.ErrorResponse.ErrorMessage,
string(response.ErrorResponse.ErrorCode),
response.ErrorResponse.Details)
}

return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/integration/internal/gateway.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand All @@ -27,8 +28,36 @@ func registerService(s string, url *url.URL) {
}
}

type serviceInfo struct {
Name string `json:"name"`
// We do not want to omit empty values in the json response
Version string `json:"version"`
Health bool `json:"health"`
}

type versionsResponse struct {
Versions []*serviceInfo `json:"versions"`
}

func startFakeGateway() {
gatewayServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/versions" {
res := versionsResponse{
Versions: []*serviceInfo{
{
Name: "ledger",
Version: "v2.0.0",
},
// If needed, add other services version
},
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(res); err != nil {
panic(err)
}
w.WriteHeader(http.StatusOK)
return
}
for name, proxy := range proxies {
if strings.HasPrefix(r.URL.Path, "/api/"+name) {
ginkgo.GinkgoWriter.Printf("Proxying %s: %s\r\n", name, proxy.url.String())
Expand Down

0 comments on commit 24dfccc

Please sign in to comment.