From 5d8e13869585e5063cdf9f3e5a1df11f1600aaa7 Mon Sep 17 00:00:00 2001 From: Tony Schneider Date: Tue, 5 Dec 2023 08:59:41 -0500 Subject: [PATCH] Add mint api base path (#21) --- internal/client/client.go | 16 ++++- internal/client/client_suite_test.go | 13 ++++ internal/client/client_test.go | 98 ++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 internal/client/client_suite_test.go create mode 100644 internal/client/client_test.go diff --git a/internal/client/client.go b/internal/client/client.go index 5c74cee..4024a45 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "strings" "github.com/pkg/errors" @@ -15,6 +16,7 @@ import ( // Client is an API Client for Mint type Client struct { RoundTrip func(*http.Request) (*http.Response, error) + Host string } func New(cfg Config) (Client, error) { @@ -31,12 +33,12 @@ func New(cfg Config) (Client, error) { return http.DefaultClient.Do(req) } - return Client{roundTrip}, nil + return Client{roundTrip, cfg.Host}, nil } -// InitiateRun sends a request to Mint for starting a new runn +// InitiateRun sends a request to Mint for starting a new run func (c Client) InitiateRun(cfg InitiateRunConfig) (*InitiateRunResult, error) { - endpoint := "/api/runs" + endpoint := c.mintEndpoint("/api/runs") if err := cfg.Validate(); err != nil { return nil, errors.Wrap(err, "validation failed") @@ -104,3 +106,11 @@ func extractErrorMessage(reader io.Reader) string { return errorStruct.Result.Data.Error } + +// TODO(TS): Remove this once we're fully transitioned +func (c Client) mintEndpoint(path string) string { + if !strings.Contains(c.Host, "cloud") { + return path + } + return "/mint" + path +} diff --git a/internal/client/client_suite_test.go b/internal/client/client_suite_test.go new file mode 100644 index 0000000..79e004c --- /dev/null +++ b/internal/client/client_suite_test.go @@ -0,0 +1,13 @@ +package client_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestClient(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Client Suite") +} diff --git a/internal/client/client_test.go b/internal/client/client_test.go new file mode 100644 index 0000000..fc526e7 --- /dev/null +++ b/internal/client/client_test.go @@ -0,0 +1,98 @@ +package client_test + +import ( + "bytes" + "encoding/json" + "io" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "net/http" + + "github.com/rwx-research/mint-cli/internal/client" +) + +var _ = Describe("Client", func() { + Describe("InitiateRun", func() { + Context("without a mint base path", func() { + It("uses an mint.rwx.com style endpoint", func() { + body := struct { + RunID string `json:"runId"` + RunURL string `json:"runUrl"` + TargetedTaskKeys []string `json:"targetedTaskKeys"` + DefinitionPath string `json:"definitionPath"` + }{ + RunID: "123", + RunURL: "https://mint.rwx.com/runs/123", + TargetedTaskKeys: []string{}, + DefinitionPath: "foo", + } + bodyBytes, _ := json.Marshal(body) + + roundTrip := func(req *http.Request) (*http.Response, error) { + Expect(req.URL.Path).To(Equal("/api/runs")) + return &http.Response{ + Status: "201 Created", + StatusCode: 201, + Body: io.NopCloser(bytes.NewReader(bodyBytes)), + }, nil + } + + c := client.Client{roundTrip, "mint.rwx.com"} + + initRunConfig := client.InitiateRunConfig{ + InitializationParameters: map[string]string{}, + TaskDefinitions: []client.TaskDefinition{ + {Path: "foo", FileContents: "echo 'bar'"}, + }, + TargetedTaskKeys: []string{}, + UseCache: false, + } + + _, err := c.InitiateRun(initRunConfig) + Expect(err).To(BeNil()) + }) + }) + + Context("with a mint base path", func() { + It("prefixes the endpoint with the base path", func() { + body := struct { + RunID string `json:"runId"` + RunURL string `json:"runUrl"` + TargetedTaskKeys []string `json:"targetedTaskKeys"` + DefinitionPath string `json:"definitionPath"` + }{ + RunID: "123", + RunURL: "https://mint.rwx.com/runs/123", + TargetedTaskKeys: []string{}, + DefinitionPath: "foo", + } + bodyBytes, _ := json.Marshal(body) + + roundTrip := func(req *http.Request) (*http.Response, error) { + Expect(req.URL.Path).To(Equal("/mint/api/runs")) + return &http.Response{ + Status: "201 Created", + StatusCode: 201, + Body: io.NopCloser(bytes.NewReader(bodyBytes)), + }, nil + } + + c := client.Client{roundTrip, "cloud.rwx.com"} + + initRunConfig := client.InitiateRunConfig{ + InitializationParameters: map[string]string{}, + TaskDefinitions: []client.TaskDefinition{ + {Path: "foo", FileContents: "echo 'bar'"}, + }, + TargetedTaskKeys: []string{}, + UseCache: false, + } + + _, err := c.InitiateRun(initRunConfig) + Expect(err).To(BeNil()) + }) + }) + }) +})