Skip to content

Commit

Permalink
Added update for named entity (#54)
Browse files Browse the repository at this point in the history
* Added update for named entity

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

* Added docs

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

* Incorporated the feedback

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

* Fixed unit test

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

* Removed unarchive comment on activate

Signed-off-by: Prafulla Mahindrakar <[email protected]>
  • Loading branch information
pmahindrakar-oss authored Apr 27, 2021
1 parent 89f6e6f commit cf3ffa1
Show file tree
Hide file tree
Showing 23 changed files with 994 additions and 28 deletions.
3 changes: 3 additions & 0 deletions flytectl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ generate:
compile:
go build -o bin/flytectl -ldflags=$(LD_FLAGS) main.go

compile_debug:
go build -gcflags='all=-N -l' -o bin/flytectl main.go

.PHONY: update_boilerplate
update_boilerplate:
@boilerplate/update.sh
Expand Down
17 changes: 17 additions & 0 deletions flytectl/clierrors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package clierrors

var (
ErrInvalidStateUpdate = "Invalid state passed. Specify either activate or archive\n"

ErrProjectNotPassed = "Project not passed\n"
ErrFailedProjectUpdate = "Project %v failed to get updated to %v state due to %v\n"

ErrLPNotPassed = "Launch plan name not passed\n"
ErrFailedLPUpdate = "Launch plan %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"

ErrTaskNotPassed = "Task name not passed\n" // #nosec
ErrFailedTaskUpdate = "Task %v failed to get updated to due to %v\n"
)
1 change: 0 additions & 1 deletion flytectl/cmd/create/executionconfig_flags.go

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

50 changes: 50 additions & 0 deletions flytectl/cmd/update/interfaces/mocks/updater.go

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

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

import (
"context"

cmdCore "github.com/flyteorg/flytectl/cmd/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
)

//go:generate mockery -name=Updater -case=underscore

type Updater interface {
UpdateNamedEntity(ctx context.Context, name, project, domain string, rsType core.ResourceType, cmdCtx cmdCore.CommandContext) error
}
49 changes: 49 additions & 0 deletions flytectl/cmd/update/launch_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package update

import (
"context"
"fmt"

"github.com/flyteorg/flytectl/clierrors"
"github.com/flyteorg/flytectl/cmd/config"
cmdCore "github.com/flyteorg/flytectl/cmd/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
)

const (
updateLPShort = "Updates launch plan metadata"
updateLPLong = `
Following command updates the description on the launchplan.
::
flytectl update launchplan -p flytectldemo -d development core.advanced.run_merge_sort.merge_sort --description "Mergesort example"
Archiving launchplan named entity is not supported and would throw an error.
::
flytectl update launchplan -p flytectldemo -d development core.advanced.run_merge_sort.merge_sort --archive
Activating launchplan named entity would be a noop.
::
flytectl update launchplan -p flytectldemo -d development core.advanced.run_merge_sort.merge_sort --activate
Usage
`
)

func updateLPFunc(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.ErrLPNotPassed)
}
name := args[0]
err := namedEntityConfig.UpdateNamedEntity(ctx, name, project, domain, core.ResourceType_LAUNCH_PLAN, cmdCtx)
if err != nil {
fmt.Printf(clierrors.ErrFailedLPUpdate, name, err)
return err
}
fmt.Printf("updated metadata successfully on %v", name)
return nil
}
44 changes: 44 additions & 0 deletions flytectl/cmd/update/launch_plan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package update

import (
"fmt"
"testing"

"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 UpdateLPSetup() {
ctx = testutils.Ctx
cmdCtx = testutils.CmdCtx
mockClient = testutils.MockClient
}

func TestLPUpdate(t *testing.T) {
testutils.Setup()
UpdateLPSetup()
namedEntityConfig = &NamedEntityConfig{}
args = []string{"task1"}
mockClient.OnUpdateNamedEntityMatch(mock.Anything, mock.Anything).Return(&admin.NamedEntityUpdateResponse{}, nil)
assert.Nil(t, updateLPFunc(ctx, args, cmdCtx))
}

func TestLPUpdateFail(t *testing.T) {
testutils.Setup()
UpdateLPSetup()
namedEntityConfig = &NamedEntityConfig{}
args = []string{"task1"}
mockClient.OnUpdateNamedEntityMatch(mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed to update"))
assert.NotNil(t, updateTaskFunc(ctx, args, cmdCtx))
}

func TestLPUpdateInvalidArgs(t *testing.T) {
testutils.Setup()
UpdateLPSetup()
namedEntityConfig = &NamedEntityConfig{}
args = []string{}
assert.NotNil(t, updateTaskFunc(ctx, args, cmdCtx))
}
50 changes: 50 additions & 0 deletions flytectl/cmd/update/named_entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package update

import (
"context"
"fmt"

"github.com/flyteorg/flytectl/clierrors"
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"
)

//go:generate pflags NamedEntityConfig --default-var namedEntityConfig

var (
namedEntityConfig = &NamedEntityConfig{}
)

type NamedEntityConfig struct {
Archive bool `json:"archive" pflag:",archive named entity."`
Activate bool `json:"activate" pflag:",activate the named entity."`
Description string `json:"description" pflag:",description of the named entity."`
}

func (n NamedEntityConfig) UpdateNamedEntity(ctx context.Context, name string, project string, domain string, rsType core.ResourceType, cmdCtx cmdCore.CommandContext) error {
archiveProject := n.Archive
activateProject := n.Activate
if activateProject == archiveProject && activateProject {
return fmt.Errorf(clierrors.ErrInvalidStateUpdate)
}
var nameEntityState admin.NamedEntityState
if activateProject {
nameEntityState = admin.NamedEntityState_NAMED_ENTITY_ACTIVE
} else if archiveProject {
nameEntityState = admin.NamedEntityState_NAMED_ENTITY_ARCHIVED
}
_, err := cmdCtx.AdminClient().UpdateNamedEntity(ctx, &admin.NamedEntityUpdateRequest{
ResourceType: rsType,
Id: &admin.NamedEntityIdentifier{
Project: project,
Domain: domain,
Name: name,
},
Metadata: &admin.NamedEntityMetadata{
Description: n.Description,
State: nameEntityState,
},
})
return err
}
48 changes: 48 additions & 0 deletions flytectl/cmd/update/named_entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package update

import (
"fmt"
"testing"

"github.com/flyteorg/flytectl/cmd/testutils"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/stretchr/testify/mock"

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

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

func TestNamedEntity(t *testing.T) {
testutils.Setup()
NamedEntitySetup()
mockClient.OnUpdateNamedEntityMatch(mock.Anything, mock.Anything).Return(&admin.NamedEntityUpdateResponse{}, nil)
namedEntityConfig = &NamedEntityConfig{Archive: false, Activate: true, Description: "named entity description"}
assert.Nil(t, namedEntityConfig.UpdateNamedEntity(ctx, "namedEntity", "project", "domain",
core.ResourceType_WORKFLOW, cmdCtx))
namedEntityConfig = &NamedEntityConfig{Archive: true, Activate: false, Description: "named entity description"}
assert.Nil(t, namedEntityConfig.UpdateNamedEntity(ctx, "namedEntity", "project", "domain",
core.ResourceType_WORKFLOW, cmdCtx))
}

func TestNamedEntityValidationFailure(t *testing.T) {
testutils.Setup()
NamedEntitySetup()
namedEntityConfig = &NamedEntityConfig{Archive: true, Activate: true, Description: "named entity description"}
assert.NotNil(t, namedEntityConfig.UpdateNamedEntity(ctx, "namedEntity", "project", "domain",
core.ResourceType_WORKFLOW, cmdCtx))
}

func TestNamedEntityFailure(t *testing.T) {
testutils.Setup()
NamedEntitySetup()
namedEntityConfig = &NamedEntityConfig{Archive: true, Activate: true, Description: "named entity description"}
mockClient.OnUpdateNamedEntityMatch(mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed to update"))
assert.NotNil(t, namedEntityConfig.UpdateNamedEntity(ctx, "namedEntity", "project", "domain",
core.ResourceType_WORKFLOW, cmdCtx))
}
48 changes: 48 additions & 0 deletions flytectl/cmd/update/namedentityconfig_flags.go

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

Loading

0 comments on commit cf3ffa1

Please sign in to comment.