forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Archive and Update project command (flyteorg#24)
- Loading branch information
1 parent
e8eb060
commit 3ff8060
Showing
8 changed files
with
424 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package update | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/lyft/flytectl/cmd/config" | ||
cmdCore "github.com/lyft/flytectl/cmd/core" | ||
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin" | ||
) | ||
|
||
//go:generate pflags ProjectConfig | ||
|
||
// Config hold configuration for project update flags. | ||
type ProjectConfig struct { | ||
ActivateProject bool `json:"activateProject" pflag:",Activates the project specified as argument."` | ||
ArchiveProject bool `json:"archiveProject" pflag:",Archives the project specified as argument."` | ||
} | ||
|
||
var ( | ||
projectConfig = &ProjectConfig{} | ||
errProjectNotFound = "Project %v not found\n" | ||
errInvalidUpdate = "Invalid state passed. Specify either activate or archive\n" | ||
errFailedUpdate = "Project %v failed to get updated to %v state due to %v\n" | ||
) | ||
|
||
func updateProjectsFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error { | ||
id := config.GetConfig().Project | ||
if id == "" { | ||
fmt.Printf(errProjectNotFound, id) | ||
return nil | ||
} | ||
archiveProject := projectConfig.ArchiveProject | ||
activateProject := projectConfig.ActivateProject | ||
if activateProject == archiveProject { | ||
fmt.Printf(errInvalidUpdate) | ||
return nil | ||
} | ||
projectState := admin.Project_ACTIVE | ||
if archiveProject { | ||
projectState = admin.Project_ARCHIVED | ||
} | ||
_, err := cmdCtx.AdminClient().UpdateProject(ctx, &admin.Project{ | ||
Id: id, | ||
State: projectState, | ||
}) | ||
if err != nil { | ||
fmt.Printf(errFailedUpdate, id, projectState, err) | ||
return nil | ||
} | ||
fmt.Printf("Project %v updated to %v state\n", id, projectState) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package update | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"github.com/lyft/flytectl/cmd/config" | ||
cmdCore "github.com/lyft/flytectl/cmd/core" | ||
"github.com/lyft/flyteidl/clients/go/admin/mocks" | ||
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"log" | ||
"os" | ||
"testing" | ||
) | ||
|
||
const projectValue = "dummyProject" | ||
|
||
var ( | ||
reader *os.File | ||
writer *os.File | ||
err error | ||
ctx context.Context | ||
mockClient *mocks.AdminServiceClient | ||
mockOutStream io.Writer | ||
args []string | ||
cmdCtx cmdCore.CommandContext | ||
projectUpdateRequest *admin.Project | ||
stdOut *os.File | ||
stderr *os.File | ||
) | ||
|
||
func setup() { | ||
reader, writer, err = os.Pipe() | ||
if err != nil { | ||
panic(err) | ||
} | ||
stdOut = os.Stdout | ||
stderr = os.Stderr | ||
os.Stdout = writer | ||
os.Stderr = writer | ||
log.SetOutput(writer) | ||
config.GetConfig().Project = projectValue | ||
mockClient = new(mocks.AdminServiceClient) | ||
mockOutStream = writer | ||
cmdCtx = cmdCore.NewCommandContext(mockClient, mockOutStream) | ||
projectUpdateRequest = &admin.Project{ | ||
Id: projectValue, | ||
State: admin.Project_ACTIVE, | ||
} | ||
} | ||
|
||
func teardownAndVerify(t *testing.T, expectedLog string) { | ||
writer.Close() | ||
os.Stdout = stdOut | ||
os.Stderr = stderr | ||
var buf bytes.Buffer | ||
io.Copy(&buf, reader) | ||
assert.Equal(t, expectedLog, buf.String()) | ||
} | ||
|
||
func modifyProjectFlags(archiveProject *bool, newArchiveVal bool, activateProject *bool, newActivateVal bool) { | ||
*archiveProject = newArchiveVal | ||
*activateProject = newActivateVal | ||
} | ||
|
||
func TestActivateProjectFunc(t *testing.T) { | ||
setup() | ||
defer teardownAndVerify(t, "Project dummyProject updated to ACTIVE state\n") | ||
modifyProjectFlags(&(projectConfig.ArchiveProject), false, &(projectConfig.ActivateProject), true) | ||
mockClient.OnUpdateProjectMatch(ctx, projectUpdateRequest).Return(nil, nil) | ||
updateProjectsFunc(ctx, args, cmdCtx) | ||
mockClient.AssertCalled(t, "UpdateProject", ctx, projectUpdateRequest) | ||
} | ||
|
||
func TestActivateProjectFuncWithError(t *testing.T) { | ||
setup() | ||
defer teardownAndVerify(t, "Project dummyProject failed to get updated to ACTIVE state due to Error Updating Project\n") | ||
modifyProjectFlags(&(projectConfig.ArchiveProject), false, &(projectConfig.ActivateProject), true) | ||
mockClient.OnUpdateProjectMatch(ctx, projectUpdateRequest).Return(nil, errors.New("Error Updating Project")) | ||
updateProjectsFunc(ctx, args, cmdCtx) | ||
mockClient.AssertCalled(t, "UpdateProject", ctx, projectUpdateRequest) | ||
} | ||
|
||
func TestArchiveProjectFunc(t *testing.T) { | ||
setup() | ||
defer teardownAndVerify(t, "Project dummyProject updated to ARCHIVED state\n") | ||
modifyProjectFlags(&(projectConfig.ArchiveProject), true, &(projectConfig.ActivateProject), false) | ||
projectUpdateRequest := &admin.Project{ | ||
Id: projectValue, | ||
State: admin.Project_ARCHIVED, | ||
} | ||
mockClient.OnUpdateProjectMatch(ctx, projectUpdateRequest).Return(nil, nil) | ||
err := updateProjectsFunc(ctx, args, cmdCtx) | ||
assert.Nil(t, err) | ||
mockClient.AssertCalled(t, "UpdateProject", ctx, projectUpdateRequest) | ||
} | ||
|
||
func TestArchiveProjectFuncWithError(t *testing.T) { | ||
setup() | ||
defer teardownAndVerify(t, "Project dummyProject failed to get updated to ARCHIVED state due to Error Updating Project\n") | ||
modifyProjectFlags(&(projectConfig.ArchiveProject), true, &(projectConfig.ActivateProject), false) | ||
projectUpdateRequest := &admin.Project{ | ||
Id: projectValue, | ||
State: admin.Project_ARCHIVED, | ||
} | ||
mockClient.OnUpdateProjectMatch(ctx, projectUpdateRequest).Return(nil, errors.New("Error Updating Project")) | ||
updateProjectsFunc(ctx, args, cmdCtx) | ||
mockClient.AssertCalled(t, "UpdateProject", ctx, projectUpdateRequest) | ||
} | ||
|
||
func TestEmptyProjectInput(t *testing.T) { | ||
setup() | ||
defer teardownAndVerify(t, "Project not found\n") | ||
config.GetConfig().Project = "" | ||
modifyProjectFlags(&(projectConfig.ArchiveProject), false, &(projectConfig.ActivateProject), true) | ||
mockClient.OnUpdateProjectMatch(ctx, projectUpdateRequest).Return(nil, nil) | ||
updateProjectsFunc(ctx, args, cmdCtx) | ||
mockClient.AssertNotCalled(t, "UpdateProject", ctx, projectUpdateRequest) | ||
} | ||
|
||
func TestInvalidInput(t *testing.T) { | ||
setup() | ||
defer teardownAndVerify(t, "Invalid state passed. Specify either activate or archive\n") | ||
modifyProjectFlags(&(projectConfig.ArchiveProject), false, &(projectConfig.ActivateProject), false) | ||
mockClient.OnUpdateProjectMatch(ctx, projectUpdateRequest).Return(nil, nil) | ||
updateProjectsFunc(ctx, args, cmdCtx) | ||
mockClient.AssertNotCalled(t, "UpdateProject", ctx, projectUpdateRequest) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.