Skip to content

Commit

Permalink
Remove input output columns for list workflows (#157)
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Lin <[email protected]>
  • Loading branch information
mayitbeegh authored Aug 9, 2021
1 parent 3e4323a commit 2fae848
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 19 deletions.
4 changes: 3 additions & 1 deletion flytectl/cmd/get/launch_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"testing"

"github.com/flyteorg/flytectl/pkg/printer"

"github.com/flyteorg/flytectl/pkg/filters"

"github.com/flyteorg/flytectl/cmd/config"
Expand Down Expand Up @@ -731,7 +733,7 @@ func TestGetLaunchPlanTableFunc(t *testing.T) {
mockClient.OnListLaunchPlansMatch(ctx, resourceGetRequest).Return(launchPlanListResponse, nil)
mockClient.OnGetLaunchPlanMatch(ctx, objectGetRequest).Return(launchPlan2, nil)
mockClient.OnListLaunchPlanIdsMatch(ctx, namedIDRequest).Return(namedIdentifierList, nil)
config.GetConfig().Output = "table"
config.GetConfig().Output = printer.OutputFormatTABLE.String()
err = getLaunchPlanFunc(ctx, argsLp, cmdCtx)
assert.Nil(t, err)
mockClient.AssertCalled(t, "ListLaunchPlans", ctx, resourceGetRequest)
Expand Down
2 changes: 1 addition & 1 deletion flytectl/cmd/get/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestGetTaskFunc(t *testing.T) {
]`)
}

func TestGetTaskTableFunc(t *testing.T) {
func TestGetTaskFuncWithTable(t *testing.T) {
setup()
getTaskSetup()
taskConfig.DefaultConfig.Filter = filters.Filters{}
Expand Down
34 changes: 22 additions & 12 deletions flytectl/cmd/get/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ var workflowColumns = []printer.Column{
{Header: "Created At", JSONPath: "$.closure.createdAt"},
}

var listWorkflowColumns = []printer.Column{
{Header: "Version", JSONPath: "$.id.version"},
{Header: "Name", JSONPath: "$.id.name"},
{Header: "Created At", JSONPath: "$.closure.createdAt"},
}

func WorkflowToProtoMessages(l []*admin.Workflow) []proto.Message {
messages := make([]proto.Message, 0, len(l))
for _, m := range l {
Expand Down Expand Up @@ -129,14 +135,19 @@ func getWorkflowFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandC
var err error
if len(args) > 0 {
name := args[0]
if workflows, err = FetchWorkflowForName(ctx, cmdCtx.AdminFetcherExt(), name, config.GetConfig().Project, config.GetConfig().Domain); err != nil {
var isList bool
if workflows, isList, err = FetchWorkflowForName(ctx, cmdCtx.AdminFetcherExt(), name, config.GetConfig().Project, config.GetConfig().Domain); err != nil {
return err
}
columns := workflowColumns
if isList {
columns = listWorkflowColumns
}
logger.Debugf(ctx, "Retrieved %v workflow", len(workflows))
if config.GetConfig().MustOutputFormat() == printer.OutputFormatTABLE {
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), workflowColumns, WorkflowToTableProtoMessages(workflows)...)
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), columns, WorkflowToTableProtoMessages(workflows)...)
}
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), workflowColumns, WorkflowToProtoMessages(workflows)...)
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), columns, WorkflowToProtoMessages(workflows)...)
}

workflows, err = cmdCtx.AdminFetcherExt().FetchAllVerOfWorkflow(ctx, "", config.GetConfig().Project, config.GetConfig().Domain, workflowconfig.DefaultConfig.Filter)
Expand All @@ -146,32 +157,31 @@ func getWorkflowFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandC

logger.Debugf(ctx, "Retrieved %v workflows", len(workflows))
if config.GetConfig().MustOutputFormat() == printer.OutputFormatTABLE {
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), workflowColumns, WorkflowToTableProtoMessages(workflows)...)
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), listWorkflowColumns, WorkflowToTableProtoMessages(workflows)...)
}
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), workflowColumns, WorkflowToProtoMessages(workflows)...)
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), listWorkflowColumns, WorkflowToProtoMessages(workflows)...)
}

// FetchWorkflowForName fetches the workflow give it name.
func FetchWorkflowForName(ctx context.Context, fetcher ext.AdminFetcherExtInterface, name, project,
domain string) ([]*admin.Workflow, error) {
var workflows []*admin.Workflow
domain string) (workflows []*admin.Workflow, isList bool, err error) {
var workflow *admin.Workflow
var err error
if workflowconfig.DefaultConfig.Latest {
if workflow, err = fetcher.FetchWorkflowLatestVersion(ctx, name, project, domain, workflowconfig.DefaultConfig.Filter); err != nil {
return nil, err
return nil, false, err
}
workflows = append(workflows, workflow)
} else if workflowconfig.DefaultConfig.Version != "" {
if workflow, err = fetcher.FetchWorkflowVersion(ctx, name, workflowconfig.DefaultConfig.Version, project, domain); err != nil {
return nil, err
return nil, false, err
}
workflows = append(workflows, workflow)
} else {
workflows, err = fetcher.FetchAllVerOfWorkflow(ctx, name, project, domain, workflowconfig.DefaultConfig.Filter)
if err != nil {
return nil, err
return nil, false, err
}
isList = true
}
return workflows, nil
return workflows, isList, nil
}
104 changes: 99 additions & 5 deletions flytectl/cmd/get/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"fmt"
"testing"

"github.com/flyteorg/flytectl/cmd/config"
"github.com/flyteorg/flytectl/pkg/filters"
"github.com/flyteorg/flytectl/pkg/printer"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/flyteorg/flytectl/pkg/ext/mocks"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
Expand All @@ -17,6 +23,9 @@ import (
var (
resourceListRequestWorkflow *admin.ResourceListRequest
workflowListResponse *admin.WorkflowList
argsWf []string
workflow1 *admin.Workflow
workflows []*admin.Workflow
)

func getWorkflowSetup() {
Expand All @@ -30,26 +39,73 @@ func getWorkflowSetup() {
},
}

workflow1 := &admin.Workflow{
variableMap := map[string]*core.Variable{
"var1": {
Type: &core.LiteralType{
Type: &core.LiteralType_CollectionType{
CollectionType: &core.LiteralType{
Type: &core.LiteralType_Simple{
Simple: core.SimpleType_INTEGER,
},
},
},
},
Description: "var1",
},
"var2": {
Type: &core.LiteralType{
Type: &core.LiteralType_CollectionType{
CollectionType: &core.LiteralType{
Type: &core.LiteralType_Simple{
Simple: core.SimpleType_INTEGER,
},
},
},
},
Description: "var2 long descriptions probably needs truncate",
},
}
workflow1 = &admin.Workflow{
Id: &core.Identifier{
Project: projectValue,
Domain: domainValue,
Name: "workflow1",
Version: "v1",
},
Closure: &admin.WorkflowClosure{
CreatedAt: &timestamppb.Timestamp{Seconds: 0, Nanos: 0},
CompiledWorkflow: &core.CompiledWorkflowClosure{
Primary: &core.CompiledWorkflow{
Template: &core.WorkflowTemplate{
Interface: &core.TypedInterface{
Inputs: &core.VariableMap{
Variables: variableMap,
},
},
},
},
},
},
}
workflow2 := &admin.Workflow{
Id: &core.Identifier{
Project: projectValue,
Domain: domainValue,
Name: "workflow2",
Version: "v2",
},
Closure: &admin.WorkflowClosure{
CreatedAt: &timestamppb.Timestamp{Seconds: 0, Nanos: 0},
},
}
workflows := []*admin.Workflow{workflow1, workflow2}
workflows = []*admin.Workflow{workflow1, workflow2}
workflowListResponse = &admin.WorkflowList{
Workflows: workflows,
}
argsWf = []string{"workflow1"}
workflow.DefaultConfig.Latest = false
workflow.DefaultConfig.Version = ""
workflow.DefaultConfig.Filter = filters.DefaultFilter
}

func TestGetWorkflowFuncWithError(t *testing.T) {
Expand All @@ -59,7 +115,7 @@ func TestGetWorkflowFuncWithError(t *testing.T) {
mockFetcher := new(mocks.AdminFetcherExtInterface)
workflow.DefaultConfig.Latest = true
mockFetcher.OnFetchWorkflowLatestVersionMatch(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error fetching latest version"))
_, err = FetchWorkflowForName(ctx, mockFetcher, "workflowName", projectValue, domainValue)
_, _, err = FetchWorkflowForName(ctx, mockFetcher, "workflowName", projectValue, domainValue)
assert.NotNil(t, err)
})

Expand All @@ -70,7 +126,7 @@ func TestGetWorkflowFuncWithError(t *testing.T) {
workflow.DefaultConfig.Version = "v1"
mockFetcher.OnFetchWorkflowVersionMatch(mock.Anything, mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error fetching version"))
_, err = FetchWorkflowForName(ctx, mockFetcher, "workflowName", projectValue, domainValue)
_, _, err = FetchWorkflowForName(ctx, mockFetcher, "workflowName", projectValue, domainValue)
assert.NotNil(t, err)
})

Expand All @@ -80,7 +136,7 @@ func TestGetWorkflowFuncWithError(t *testing.T) {
mockFetcher := new(mocks.AdminFetcherExtInterface)
mockFetcher.OnFetchAllVerOfWorkflowMatch(mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error fetching all version"))
_, err = FetchWorkflowForName(ctx, mockFetcher, "workflowName", projectValue, domainValue)
_, _, err = FetchWorkflowForName(ctx, mockFetcher, "workflowName", projectValue, domainValue)
assert.NotNil(t, err)
})

Expand All @@ -96,3 +152,41 @@ func TestGetWorkflowFuncWithError(t *testing.T) {
})

}

func TestGetWorkflowFuncLatestWithTable(t *testing.T) {
setup()
getWorkflowSetup()
workflow.DefaultConfig.Latest = true
workflow.DefaultConfig.Filter = filters.Filters{}
config.GetConfig().Output = printer.OutputFormatTABLE.String()
u.FetcherExt.OnFetchWorkflowLatestVersionMatch(ctx, "workflow1", projectValue, domainValue, filters.Filters{}).Return(workflow1, nil)
err = getWorkflowFunc(ctx, argsWf, cmdCtx)
assert.Nil(t, err)
tearDownAndVerify(t, `
--------- ----------- --------------------------- --------- ----------------------
| VERSION | NAME | INPUTS | OUTPUTS | CREATED AT |
--------- ----------- --------------------------- --------- ----------------------
| v1 | workflow1 | var1 | | 1970-01-01T00:00:00Z |
| | | var2: var2 long descri... | | |
--------- ----------- --------------------------- --------- ----------------------
1 rows`)
}

func TestListWorkflowFuncWithTable(t *testing.T) {
setup()
getWorkflowSetup()
workflow.DefaultConfig.Filter = filters.Filters{}
config.GetConfig().Output = printer.OutputFormatTABLE.String()
u.FetcherExt.OnFetchAllVerOfWorkflowMatch(ctx, "workflow1", projectValue, domainValue, filters.Filters{}).Return(workflows, nil)
err = getWorkflowFunc(ctx, argsWf, cmdCtx)
assert.Nil(t, err)
tearDownAndVerify(t, `
--------- ----------- ----------------------
| VERSION | NAME | CREATED AT |
--------- ----------- ----------------------
| v1 | workflow1 | 1970-01-01T00:00:00Z |
--------- ----------- ----------------------
| v2 | workflow2 | 1970-01-01T00:00:00Z |
--------- ----------- ----------------------
2 rows`)
}

0 comments on commit 2fae848

Please sign in to comment.