Skip to content

Commit

Permalink
Added Update Execution cli (#261)
Browse files Browse the repository at this point in the history
* Added Update Execution cli

Signed-off-by: Prafulla Mahindrakar <[email protected]>

* Added unit tests

Signed-off-by: Prafulla Mahindrakar <[email protected]>

* reset

Signed-off-by: Prafulla Mahindrakar <[email protected]>

* nit

Signed-off-by: Prafulla Mahindrakar <[email protected]>
  • Loading branch information
pmahindrakar-oss authored Jan 12, 2022
1 parent f079e1d commit a101cd6
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 7 deletions.
3 changes: 3 additions & 0 deletions flytectl/clierrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var (
ErrLPVersionNotPassed = "Launch plan version not passed\n" //nolint
ErrFailedLPUpdate = "Launch plan %v failed to get updated due to %v\n"

ErrExecutionNotPassed = "Execution name not passed\n"
ErrFailedExecutionUpdate = "Execution %v failed to get updated due to %v\n"

ErrWorkflowNotPassed = "Workflow name not passed\n"
ErrFailedWorkflowUpdate = "Workflow %v failed to get updated to due to %v\n"

Expand Down
13 changes: 13 additions & 0 deletions flytectl/cmd/config/subcommand/execution/update_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package execution

//go:generate pflags UpdateConfig --default-var UConfig --bind-default-var
var (
UConfig = &UpdateConfig{}
)

// UpdateConfig
type UpdateConfig struct {
Archive bool `json:"archive" pflag:",archive execution."`
Activate bool `json:"activate" pflag:",activate execution."`
DryRun bool `json:"dryRun" pflag:",execute command without making any modifications."`
}
57 changes: 57 additions & 0 deletions flytectl/cmd/config/subcommand/execution/updateconfig_flags.go

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

144 changes: 144 additions & 0 deletions flytectl/cmd/config/subcommand/execution/updateconfig_flags_test.go

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

73 changes: 73 additions & 0 deletions flytectl/cmd/update/execution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package update

import (
"context"
"fmt"

"github.com/flyteorg/flytectl/clierrors"
"github.com/flyteorg/flytectl/cmd/config"
"github.com/flyteorg/flytectl/cmd/config/subcommand/execution"
cmdCore "github.com/flyteorg/flytectl/cmd/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/logger"
)

const (
updateExecutionShort = "Update execution status"
updateExecutionLong = `
Activating an execution shows it in the cli and UI:
::
flytectl update execution -p flytectldemo -d development oeh94k9r2r --activate
Archiving execution hides it from cli and UI:
::
flytectl update execution -p flytectldemo -d development oeh94k9r2r --archive
Usage
`
)

func updateExecutionFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
project := config.GetConfig().Project
domain := config.GetConfig().Domain
if len(args) != 1 {
return fmt.Errorf(clierrors.ErrExecutionNotPassed)
}
executionName := args[0]
activateExec := execution.UConfig.Activate
archiveExec := execution.UConfig.Archive
if activateExec && archiveExec {
return fmt.Errorf(clierrors.ErrInvalidStateUpdate)
}

var executionState admin.ExecutionStatus_ExecutionState
if activateExec {
executionState = admin.ExecutionStatus_EXECUTION_ACTIVE
} else if archiveExec {
executionState = admin.ExecutionStatus_EXECUTION_ARCHIVED
}

if execution.UConfig.DryRun {
logger.Debugf(ctx, "skipping UpdateExecution request (DryRun)")
} else {
_, err := cmdCtx.AdminClient().UpdateExecution(ctx, &admin.ExecutionUpdateRequest{
Id: &core.WorkflowExecutionIdentifier{
Project: project,
Domain: domain,
Name: executionName,
},
Status: &admin.ExecutionStatus{State: executionState},
})
if err != nil {
fmt.Printf(clierrors.ErrFailedExecutionUpdate, executionName, err)
return err
}
}
fmt.Printf("updated execution %s successfully to state %s\n", executionName, executionState.String())

return nil
}
71 changes: 71 additions & 0 deletions flytectl/cmd/update/execution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package update

import (
"fmt"
"testing"

"github.com/flyteorg/flytectl/cmd/config/subcommand/execution"
"github.com/flyteorg/flytectl/cmd/testutils"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func UpdateExecutionSetup() {
ctx = testutils.Ctx
cmdCtx = testutils.CmdCtx
mockClient = testutils.MockClient
}

func TestExecutionUpdate(t *testing.T) {
testutils.Setup()
UpdateExecutionSetup()
args = []string{"execution1"}
// Activate
execution.UConfig.Activate = true
mockClient.OnUpdateExecutionMatch(mock.Anything, mock.Anything).Return(&admin.ExecutionUpdateResponse{}, nil)
assert.Nil(t, updateExecutionFunc(ctx, args, cmdCtx))
// Archive
execution.UConfig.Activate = false
execution.UConfig.Archive = true
assert.Nil(t, updateExecutionFunc(ctx, args, cmdCtx))
// Reset
execution.UConfig.Activate = false
execution.UConfig.Archive = false

// Dry run
execution.UConfig.DryRun = true
assert.Nil(t, updateExecutionFunc(ctx, args, cmdCtx))
mockClient.AssertNotCalled(t, "UpdateExecution", mock.Anything)

// Reset
execution.UConfig.DryRun = false
}

func TestExecutionUpdateValidationFailure(t *testing.T) {
testutils.Setup()
UpdateExecutionSetup()
args = []string{"execution1"}
execution.UConfig.Activate = true
execution.UConfig.Archive = true
assert.NotNil(t, updateExecutionFunc(ctx, args, cmdCtx))
// Reset
execution.UConfig.Activate = false
execution.UConfig.Archive = false
}

func TestExecutionUpdateFail(t *testing.T) {
testutils.Setup()
UpdateExecutionSetup()
args = []string{"execution1"}
mockClient.OnUpdateExecutionMatch(mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed to update"))
assert.NotNil(t, updateExecutionFunc(ctx, args, cmdCtx))
}

func TestExecutionUpdateInvalidArgs(t *testing.T) {
testutils.Setup()
UpdateExecutionSetup()
args = []string{}
assert.NotNil(t, updateExecutionFunc(ctx, args, cmdCtx))
}
3 changes: 3 additions & 0 deletions flytectl/cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package update

import (
"github.com/flyteorg/flytectl/cmd/config/subcommand/clusterresourceattribute"
"github.com/flyteorg/flytectl/cmd/config/subcommand/execution"
"github.com/flyteorg/flytectl/cmd/config/subcommand/executionclusterlabel"
"github.com/flyteorg/flytectl/cmd/config/subcommand/executionqueueattribute"
"github.com/flyteorg/flytectl/cmd/config/subcommand/launchplan"
Expand Down Expand Up @@ -41,6 +42,8 @@ func CreateUpdateCommand() *cobra.Command {
Short: updateLPMetaShort, Long: updateLPMetaLong},
"project": {CmdFunc: updateProjectsFunc, Aliases: []string{}, ProjectDomainNotRequired: true, PFlagProvider: DefaultProjectConfig,
Short: projectShort, Long: projectLong},
"execution": {CmdFunc: updateExecutionFunc, Aliases: []string{}, ProjectDomainNotRequired: false, PFlagProvider: execution.UConfig,
Short: updateExecutionShort, Long: updateExecutionLong},
"task-meta": {CmdFunc: updateTaskFunc, Aliases: []string{}, ProjectDomainNotRequired: false, PFlagProvider: namedEntityConfig,
Short: updateTaskShort, Long: updateTaskLong},
"workflow-meta": {CmdFunc: updateWorkflowFunc, Aliases: []string{}, ProjectDomainNotRequired: false, PFlagProvider: namedEntityConfig,
Expand Down
Loading

0 comments on commit a101cd6

Please sign in to comment.