Skip to content

Commit

Permalink
feat(upsert_pipeline): Prepare plank for adding ff when upserting pip…
Browse files Browse the repository at this point in the history
…eline.
  • Loading branch information
DanielaS12 committed Sep 14, 2023
1 parent 76f541c commit 09fe6e8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
27 changes: 27 additions & 0 deletions pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,34 @@ func (c *Client) GetPipelines(app, traceparent string) ([]Pipeline, error) {
}

// UpsertPipeline creates/updates a pipeline defined in the struct argument.

func (c *Client) UpsertPipeline(p Pipeline, id, traceparent string) error {
var unused interface{}
if id == "" {
if c.UseGate {
if err := c.PostWithRetry(c.gatePipelinesURL(), traceparent, ApplicationJson, p, &unused); err != nil {
return fmt.Errorf("could not create pipeline '%s' in app '%s': %w", p.Name, p.Application, err)
}
} else {
if err := c.PostWithRetry(c.pipelinesURL(), traceparent, ApplicationJson, p, &unused); err != nil {
return fmt.Errorf("could not create pipeline '%s' in app '%s': %w", p.Name, p.Application, err)
}
}
} else {
if c.UseGate {
if err := c.PutWithRetry(fmt.Sprintf("%s/%s", c.gatePipelinesURL(), id), traceparent, ApplicationJson, p, &unused); err != nil {
return fmt.Errorf("could not update pipeline '%s' in app '%s': %w", p.Name, p.Application, err)
}
} else {
if err := c.PutWithRetry(fmt.Sprintf("%s/%s", c.pipelinesURL(), id), traceparent, ApplicationJson, p, &unused); err != nil {
return fmt.Errorf("could not update pipeline '%s' in app '%s': %w", p.Name, p.Application, err)
}
}
}
return nil
}

func (c *Client) UpsertPipelineUsingOrca(p Pipeline, id, traceparent string) error {
var resultMap map[string]interface{}
operation := getOperation(p, id)
if err := c.PostWithRetry(c.URLs["orca"]+"/ops", traceparent, ApplicationContextJson, operation, &resultMap); err != nil {
Expand Down
72 changes: 69 additions & 3 deletions pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,73 @@ func TestGetPipelinesWithGate(t *testing.T) {
assert.Equal(t, len(val), 0) // Should get 0 pipelines back.
}

func TestUpsertPipelinesNoIdWithGate(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.URL.String(), "http://localhost:8084/plank/pipelines")
assert.Equal(t, req.Method, "POST")
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("[]")),
Header: make(http.Header),
}
})

c := New(WithClient(client))
c.UseGateEndpoints()
err := c.UpsertPipeline(Pipeline{}, "", "")
assert.Nil(t, err)
}

func TestUpsertPipelinesWithGate(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.URL.String(), "http://localhost:8084/plank/pipelines/Test")
assert.Equal(t, req.Method, "PUT")
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("[]")),
Header: make(http.Header),
}
})

c := New(WithClient(client))
c.UseGateEndpoints()
err := c.UpsertPipeline(Pipeline{}, "Test", "")
assert.Nil(t, err)
}

func TestUpsertPipelines(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.URL.String(), "http://localhost:8080/pipelines/Test")
assert.Equal(t, req.Method, "PUT")
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("[]")),
Header: make(http.Header),
}
})

c := New(WithClient(client))
err := c.UpsertPipeline(Pipeline{}, "Test", "")
assert.Nil(t, err)
}

func TestUpsertPipelinesNoId(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.URL.String(), "http://localhost:8080/pipelines")
assert.Equal(t, req.Method, "POST")
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("[]")),
Header: make(http.Header),
}
})

c := New(WithClient(client))
err := c.UpsertPipeline(Pipeline{}, "", "")
assert.Nil(t, err)
}

func TestUpsertPipelinesUsingGate(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
responseJSON := `{
"status": "SUCCEEDED",
Expand All @@ -77,11 +143,11 @@ func TestUpsertPipelines(t *testing.T) {
})

c := New(WithClient(client))
err := c.UpsertPipeline(Pipeline{}, "Test", "")
err := c.UpsertPipelineUsingOrca(Pipeline{}, "Test", "")
assert.Nil(t, err)
}

func TestUpsertPipelinesNoId(t *testing.T) {
func TestUpsertPipelinesUsingGateNoId(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
responseJSON := `{
"status": "SUCCEEDED",
Expand All @@ -100,7 +166,7 @@ func TestUpsertPipelinesNoId(t *testing.T) {
})

c := New(WithClient(client))
err := c.UpsertPipeline(Pipeline{}, "", "")
err := c.UpsertPipelineUsingOrca(Pipeline{}, "", "")
assert.Nil(t, err)
}

Expand Down

0 comments on commit 09fe6e8

Please sign in to comment.