From af85551d616073bc034ffa5aa8a7e4390eea2c7a Mon Sep 17 00:00:00 2001 From: Kyle Thompson Date: Mon, 18 Dec 2023 17:04:42 -0500 Subject: [PATCH] Use snake case in the initiate run endpoint, send structured init params (#41) --- cmd/mint/run.go | 14 +++++++++- internal/api/client.go | 37 ++++++++++++++++++-------- internal/api/client_test.go | 46 ++++++++++++++++++++++++++++++--- internal/api/config.go | 13 +++++++--- internal/api/task_definition.go | 4 +-- internal/cli/service.go | 12 ++++++++- 6 files changed, 104 insertions(+), 22 deletions(-) diff --git a/cmd/mint/run.go b/cmd/mint/run.go index 3efee9b..8a3e917 100644 --- a/cmd/mint/run.go +++ b/cmd/mint/run.go @@ -68,10 +68,22 @@ var ( } if Json { - runResultJson, err := json.Marshal(runResult) + jsonOutput := struct { + RunId string + RunURL string + TargetedTaskKeys []string + DefinitionPath string + }{ + RunId: runResult.RunId, + RunURL: runResult.RunURL, + TargetedTaskKeys: runResult.TargetedTaskKeys, + DefinitionPath: runResult.DefinitionPath, + } + runResultJson, err := json.Marshal(jsonOutput) if err != nil { return err } + fmt.Println(string(runResultJson)) } else { fmt.Printf("Run is watchable at %s\n", runResult.RunURL) diff --git a/internal/api/client.go b/internal/api/client.go index fed05e7..e2c940b 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -92,7 +92,9 @@ func (c Client) InitiateRun(cfg InitiateRunConfig) (*InitiateRunResult, error) { return nil, errors.Wrap(err, "validation failed") } - encodedBody, err := json.Marshal(struct{ Run InitiateRunConfig }{cfg}) + encodedBody, err := json.Marshal(struct { + Run InitiateRunConfig `json:"run"` + }{cfg}) if err != nil { return nil, errors.Wrap(err, "unable to encode as JSON") } @@ -120,22 +122,35 @@ func (c Client) InitiateRun(cfg InitiateRunConfig) (*InitiateRunResult, error) { } respBody := struct { - RunId string - RunURL string - TargetedTaskKeys []string - DefinitionPath string + SnakeRunId string `json:"run_id"` + SnakeRunURL string `json:"run_url"` + SnakeTargetedTaskKeys []string `json:"targeted_task_keys"` + SnakeDefinitionPath string `json:"definition_path"` + CamelRunId string `json:"runId"` + CamelRunURL string `json:"runURL"` + CamelTargetedTaskKeys []string `json:"targetedTaskKeys"` + CamelDefinitionPath string `json:"definitionPath"` }{} if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil { return nil, errors.Wrap(err, "unable to parse API response") } - return &InitiateRunResult{ - RunId: respBody.RunId, - RunURL: respBody.RunURL, - TargetedTaskKeys: respBody.TargetedTaskKeys, - DefinitionPath: respBody.DefinitionPath, - }, nil + if respBody.CamelRunId != "" { + return &InitiateRunResult{ + RunId: respBody.CamelRunId, + RunURL: respBody.CamelRunURL, + TargetedTaskKeys: respBody.CamelTargetedTaskKeys, + DefinitionPath: respBody.CamelDefinitionPath, + }, nil + } else { + return &InitiateRunResult{ + RunId: respBody.SnakeRunId, + RunURL: respBody.SnakeRunURL, + TargetedTaskKeys: respBody.SnakeTargetedTaskKeys, + DefinitionPath: respBody.SnakeDefinitionPath, + }, nil + } } // ObtainAuthCode requests a new one-time-use code to login on a device diff --git a/internal/api/client_test.go b/internal/api/client_test.go index 5822d87..c239a7b 100644 --- a/internal/api/client_test.go +++ b/internal/api/client_test.go @@ -16,7 +16,7 @@ import ( var _ = Describe("API Client", func() { Describe("InitiateRun", func() { - It("prefixes the endpoint with the base path", func() { + It("prefixes the endpoint with the base path and parses camelcase responses", func() { body := struct { RunID string `json:"runId"` RunURL string `json:"runUrl"` @@ -42,7 +42,7 @@ var _ = Describe("API Client", func() { c := api.Client{roundTrip} initRunConfig := api.InitiateRunConfig{ - InitializationParameters: map[string]string{}, + InitializationParameters: []api.InitializationParameter{}, TaskDefinitions: []api.TaskDefinition{ {Path: "foo", FileContents: "echo 'bar'"}, }, @@ -50,8 +50,48 @@ var _ = Describe("API Client", func() { UseCache: false, } - _, err := c.InitiateRun(initRunConfig) + result, err := c.InitiateRun(initRunConfig) Expect(err).To(BeNil()) + Expect(result.RunId).To(Equal("123")) + }) + + It("prefixes the endpoint with the base path and parses snakecase responses", func() { + body := struct { + RunID string `json:"run_id"` + RunURL string `json:"run_url"` + TargetedTaskKeys []string `json:"targeted_task_keys"` + DefinitionPath string `json:"definition_path"` + }{ + RunID: "123", + RunURL: "https://cloud.rwx.com/mint/org/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 := api.Client{roundTrip} + + initRunConfig := api.InitiateRunConfig{ + InitializationParameters: []api.InitializationParameter{}, + TaskDefinitions: []api.TaskDefinition{ + {Path: "foo", FileContents: "echo 'bar'"}, + }, + TargetedTaskKeys: []string{}, + UseCache: false, + } + + result, err := c.InitiateRun(initRunConfig) + Expect(err).To(BeNil()) + Expect(result.RunId).To(Equal("123")) }) }) diff --git a/internal/api/config.go b/internal/api/config.go index 630c41d..cca9065 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -20,10 +20,15 @@ func (c Config) Validate() error { } type InitiateRunConfig struct { - InitializationParameters map[string]string - TaskDefinitions []TaskDefinition - TargetedTaskKeys []string `json:",omitempty"` - UseCache bool + InitializationParameters []InitializationParameter `json:"initialization_parameters"` + TaskDefinitions []TaskDefinition `json:"task_definitions"` + TargetedTaskKeys []string `json:"targeted_task_keys,omitempty"` + UseCache bool `json:"use_cache"` +} + +type InitializationParameter struct { + Key string `json:"key"` + Value string `json:"value"` } type InitiateRunResult struct { diff --git a/internal/api/task_definition.go b/internal/api/task_definition.go index d97f4ea..a28ef43 100644 --- a/internal/api/task_definition.go +++ b/internal/api/task_definition.go @@ -1,6 +1,6 @@ package api type TaskDefinition struct { - Path string - FileContents string // This type is expected by cloud + Path string `json:"path"` + FileContents string `json:"file_contents"` // This type is expected by cloud } diff --git a/internal/cli/service.go b/internal/cli/service.go index f561a52..6989d68 100644 --- a/internal/cli/service.go +++ b/internal/cli/service.go @@ -133,8 +133,18 @@ func (s Service) InitiateRun(cfg InitiateRunConfig) (*api.InitiateRunResult, err } } + i := 0 + initializationParameters := make([]api.InitializationParameter, len(cfg.InitParameters)) + for key, value := range cfg.InitParameters { + initializationParameters[i] = api.InitializationParameter{ + Key: key, + Value: value, + } + i++ + } + runResult, err := s.APIClient.InitiateRun(api.InitiateRunConfig{ - InitializationParameters: cfg.InitParameters, + InitializationParameters: initializationParameters, TaskDefinitions: taskDefinitions, TargetedTaskKeys: cfg.TargetedTasks, UseCache: !cfg.NoCache,