Skip to content

Commit

Permalink
Use snake case in the initiate run endpoint, send structured init par…
Browse files Browse the repository at this point in the history
…ams (#41)
  • Loading branch information
kylekthompson authored Dec 18, 2023
1 parent cec0d26 commit af85551
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 22 deletions.
14 changes: 13 additions & 1 deletion cmd/mint/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 26 additions & 11 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
46 changes: 43 additions & 3 deletions internal/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -42,16 +42,56 @@ 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'"},
},
TargetedTaskKeys: []string{},
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"))
})
})

Expand Down
13 changes: 9 additions & 4 deletions internal/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/api/task_definition.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 11 additions & 1 deletion internal/cli/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit af85551

Please sign in to comment.