Skip to content

Commit

Permalink
feat(update): Pipeline name and metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Raúl Barroso committed Mar 31, 2021
1 parent 2e48e6e commit 5504da9
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 12 deletions.
38 changes: 33 additions & 5 deletions cmd/update_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/meroxa/cli/utils"
Expand All @@ -29,7 +30,7 @@ type UpdatePipelineClient interface {
GetPipelineByName(ctx context.Context, name string) (*meroxa.Pipeline, error)
// TODO: Try to unify UpdatePipelineStatus with UpdatePipeline in meroxa-go
UpdatePipelineStatus(ctx context.Context, pipelineID int, state string) (*meroxa.Pipeline, error)
UpdatePipeline(ctx context.Context, key string, pipeline meroxa.UpdatePipelineInput) (*meroxa.Pipeline, error)
UpdatePipeline(ctx context.Context, pipelineID int, pipeline meroxa.UpdatePipelineInput) (*meroxa.Pipeline, error)
}

type UpdatePipeline struct {
Expand Down Expand Up @@ -69,10 +70,37 @@ func (up *UpdatePipeline) execute(ctx context.Context, c UpdatePipelineClient) (
return p, err
}

// call meroxa-go to update pipeline status with name
p, err = c.UpdatePipelineStatus(ctx, p.ID, up.state)
if err != nil {
return p, err
// call meroxa-go to update pipeline state
if up.state != "" {
p, err = c.UpdatePipelineStatus(ctx, p.ID, up.state)
if err != nil {
return p, err
}
}

// call meroxa-go to update either name or metadata
if up.newName != "" || up.metadata != "" {
var pi meroxa.UpdatePipelineInput

if up.newName != "" {
pi.Name = up.newName
}

if up.metadata != "" {
metadata := map[string]string{}

err := json.Unmarshal([]byte(up.metadata), &metadata)
if err != nil {
return p, err
}

pi.Metadata = metadata
}

p, err = c.UpdatePipeline(ctx, p.ID, pi)
if err != nil {
return p, err
}
}

return p, nil
Expand Down
93 changes: 92 additions & 1 deletion cmd/update_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestUpdatePipelineExecutionNoFlags(t *testing.T) {
}
}

func TestUpdatePipelineExecution(t *testing.T) {
func TestUpdatePipelineExecutionWithNewState(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockUpdatePipelineClient(ctrl)
Expand Down Expand Up @@ -126,6 +126,97 @@ func TestUpdatePipelineExecution(t *testing.T) {
}
}

func TestUpdatePipelineExecutionWithNewName(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockUpdatePipelineClient(ctrl)

p := utils.GeneratePipeline()
var pi meroxa.UpdatePipelineInput

newName := "new-pipeline-name"
pi.Name = newName

client.
EXPECT().
GetPipelineByName(ctx, p.Name).
Return(&p, nil)

client.
EXPECT().
UpdatePipeline(ctx, p.ID, pi).
Return(&p, nil)

output := utils.CaptureOutput(func() {
up := &UpdatePipeline{}
up.name = p.Name

// What we're trying to update
up.newName = newName

got, err := up.execute(ctx, client)

if !reflect.DeepEqual(got, &p) {
t.Fatalf("expected \"%v\", got \"%v\"", &p, got)
}

if err != nil {
t.Fatalf("not expected error, got \"%s\"", err.Error())
}
})

expected := fmt.Sprintf("Updating %s pipeline...", p.Name)

if !strings.Contains(output, expected) {
t.Fatalf("expected output \"%s\" got \"%s\"", expected, output)
}
}

func TestUpdatePipelineExecutionWithNewMetadata(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockUpdatePipelineClient(ctrl)

p := utils.GeneratePipeline()
var pi meroxa.UpdatePipelineInput

pi.Metadata = map[string]string{"key": "value"}

client.
EXPECT().
GetPipelineByName(ctx, p.Name).
Return(&p, nil)

client.
EXPECT().
UpdatePipeline(ctx, p.ID, pi).
Return(&p, nil)

output := utils.CaptureOutput(func() {
up := &UpdatePipeline{}
up.name = p.Name

// What we're trying to update
up.metadata = "{\"key\": \"value\"}"

got, err := up.execute(ctx, client)

if !reflect.DeepEqual(got, &p) {
t.Fatalf("expected \"%v\", got \"%v\"", &p, got)
}

if err != nil {
t.Fatalf("not expected error, got \"%s\"", err.Error())
}
})

expected := fmt.Sprintf("Updating %s pipeline...", p.Name)

if !strings.Contains(output, expected) {
t.Fatalf("expected output \"%s\" got \"%s\"", expected, output)
}
}

func TestUpdatePipelineOutput(t *testing.T) {
p := utils.GeneratePipeline()
flagRootOutputJSON = false
Expand Down
8 changes: 4 additions & 4 deletions mock-cmd/update_pipeline.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/meroxa/meroxa-go/pipeline.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5504da9

Please sign in to comment.