-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Add ephemeral environment create command #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c229162
First pass before testing
caitlynstocker d1d0df5
First working draft!
caitlynstocker a6f19c3
Take project name instead of id
caitlynstocker d33865f
Add tests
caitlynstocker 595a06a
First pass at project select
caitlynstocker 211c785
Adjust tests for project select
caitlynstocker b993b7c
Remove additional comments
caitlynstocker 7c9ed99
Change command name
caitlynstocker 9bcbe23
Show only EE configured projects
caitlynstocker 9c7d0f5
Improve code to check for configured projects
caitlynstocker 7be1f45
Improve error messaging when no projects configured
caitlynstocker fbce6c5
Update to latest release of octo go api client
caitlynstocker 2944b31
Update to latest release of octo go api client
caitlynstocker dcc17fb
Improve project prompt text
caitlynstocker 73469ed
Clarify project prompt
caitlynstocker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,126 @@ | ||
| package create | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/MakeNowJust/heredoc/v2" | ||
| "github.com/OctopusDeploy/cli/pkg/cmd" | ||
| "github.com/OctopusDeploy/cli/pkg/cmd/runbook/shared" | ||
| "github.com/OctopusDeploy/cli/pkg/constants" | ||
| "github.com/OctopusDeploy/cli/pkg/factory" | ||
| "github.com/OctopusDeploy/cli/pkg/output" | ||
| "github.com/OctopusDeploy/cli/pkg/question" | ||
| "github.com/OctopusDeploy/cli/pkg/question/selectors" | ||
| "github.com/OctopusDeploy/cli/pkg/util/flag" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments/v2/ephemeralenvironments" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| FlagName = "name" | ||
| FlagProject = "project" | ||
| ) | ||
|
|
||
| type CreateFlags struct { | ||
| Name *flag.Flag[string] | ||
| Project *flag.Flag[string] | ||
| } | ||
|
|
||
| func NewCreateFlags() *CreateFlags { | ||
| return &CreateFlags{ | ||
| Name: flag.New[string](FlagName, false), | ||
| Project: flag.New[string](FlagProject, false), | ||
| } | ||
| } | ||
|
|
||
| type CreateOptions struct { | ||
| *CreateFlags | ||
| *cmd.Dependencies | ||
| GetAllProjectsCallback func() ([]*projects.Project, error) | ||
| } | ||
|
|
||
| func NewCreateOptions(createFlags *CreateFlags, dependencies *cmd.Dependencies) *CreateOptions { | ||
| return &CreateOptions{ | ||
| CreateFlags: createFlags, | ||
| Dependencies: dependencies, | ||
| GetAllProjectsCallback: func() ([]*projects.Project, error) { return shared.GetAllProjects(dependencies.Client) }, | ||
| } | ||
| } | ||
|
|
||
| func NewCmdCreate(f factory.Factory) *cobra.Command { | ||
| createFlags := NewCreateFlags() | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create an ephemeral environment", | ||
| Long: "Create an ephemeral environment in Octopus Deploy", | ||
| Example: heredoc.Docf("$ %s ephemeralenvironment create", constants.ExecutableName), | ||
| Aliases: []string{"new"}, | ||
| RunE: func(c *cobra.Command, _ []string) error { | ||
| opts := NewCreateOptions(createFlags, cmd.NewDependencies(f, c)) | ||
|
|
||
| return createRun(opts) | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringVarP(&createFlags.Name.Value, createFlags.Name.Name, "n", "", "Name of the environment") | ||
| flags.StringVarP(&createFlags.Project.Value, createFlags.Project.Name, "p", "", "Name of the project") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func createRun(opts *CreateOptions) error { | ||
|
|
||
| if !opts.NoPrompt { | ||
| err := PromptMissing(opts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| projectResource, err := projects.GetByName(opts.Client, opts.Space.ID, opts.Project.Value) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to find project '%s': %w", opts.Project.Value, err) | ||
| } | ||
| projectId := projectResource.GetID() | ||
|
|
||
| createEnv, err := ephemeralenvironments.Add(opts.Client, opts.Space.ID, projectId, opts.Name.Value) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = fmt.Fprintf(opts.Out, "\nSuccessfully created ephemeral environment '%s` with id '%s'.\n", opts.Name.Value, createEnv.Id) | ||
caitlynstocker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| link := output.Bluef("%s/app#/%s/projects/%s/ephemeral-environments", opts.Host, opts.Space.GetID(), projectId) | ||
| fmt.Fprintf(opts.Out, "View this ephemeral environments for project `%s` on Octopus Deploy: %s\n", opts.Project.Value, link) | ||
caitlynstocker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if !opts.NoPrompt { | ||
| autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.Name, opts.Project) | ||
| fmt.Fprintf(opts.Out, "%s\n", autoCmd) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func PromptMissing(opts *CreateOptions) error { | ||
|
|
||
| err := question.AskName(opts.Ask, "", "ephemeral environment", &opts.Name.Value) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if opts.Project.Value == "" { | ||
| project, err := selectors.Select(opts.Ask, "Select the project to associate the ephemeral environment with:", opts.GetAllProjectsCallback, func(project *projects.Project) string { return project.GetName() }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| opts.Project.Value = project.GetName() | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,67 @@ | ||
| package create_test | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are based on the test to create a space. |
||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/OctopusDeploy/cli/pkg/cmd" | ||
| "github.com/OctopusDeploy/cli/pkg/cmd/ephemeralenvironment/create" | ||
| "github.com/OctopusDeploy/cli/test/fixtures" | ||
| "github.com/OctopusDeploy/cli/test/testutil" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestPromptMissing_AllOptionsSupplied(t *testing.T) { | ||
|
|
||
| project1 := fixtures.NewProject("Spaces-1", "Projects-1", "Test1", "Lifecycles-1", "ProjectGroups-1", "DeploymentProcesses-1") | ||
| project2 := fixtures.NewProject("Spaces-1", "Projects-2", "Test2", "Lifecycles-1", "ProjectGroups-1", "DeploymentProcesses-2") | ||
|
|
||
| pa := []*testutil.PA{} | ||
|
|
||
| asker, checkRemainingPrompts := testutil.NewMockAsker(t, pa) | ||
|
|
||
| flags := create.NewCreateFlags() | ||
| flags.Name.Value = "Hello Ephemeral Environment" | ||
| flags.Project.Value = "Hello Project" | ||
|
|
||
| opts := &create.CreateOptions{ | ||
| CreateFlags: flags, | ||
| Dependencies: &cmd.Dependencies{Ask: asker}, | ||
| } | ||
| opts.GetAllProjectsCallback = func() ([]*projects.Project, error) { | ||
| return []*projects.Project{project1, project2}, nil | ||
| } | ||
|
|
||
| // Check that no unexpected prompts were triggered | ||
| create.PromptMissing(opts) | ||
| checkRemainingPrompts() | ||
| } | ||
|
|
||
| func TestPromptMissing_NoOptionsSupplied(t *testing.T) { | ||
| project1 := fixtures.NewProject("Spaces-1", "Projects-1", "Hello Project 1", "Lifecycles-1", "ProjectGroups-1", "DeploymentProcesses-1") | ||
| project2 := fixtures.NewProject("Spaces-1", "Projects-2", "Hello Project 2", "Lifecycles-1", "ProjectGroups-1", "DeploymentProcesses-2") | ||
|
|
||
| pa := []*testutil.PA{ | ||
| testutil.NewInputPrompt("Name", "A short, memorable, unique name for this ephemeral environment.", "Hello Ephemeral Environment"), | ||
| testutil.NewSelectPrompt("Select the project to associate the ephemeral environment with:", "", []string{project1.Name, project2.Name}, project1.Name), | ||
| } | ||
|
|
||
| asker, checkRemainingPrompts := testutil.NewMockAsker(t, pa) | ||
|
|
||
| flags := create.NewCreateFlags() | ||
|
|
||
| opts := &create.CreateOptions{ | ||
| CreateFlags: flags, | ||
| Dependencies: &cmd.Dependencies{Ask: asker}, | ||
| GetAllProjectsCallback: func() ([]*projects.Project, error) { | ||
| return []*projects.Project{project1, project2}, nil | ||
| }, | ||
| } | ||
|
|
||
| create.PromptMissing(opts) | ||
|
|
||
| // Check that all expected prompts were called | ||
| checkRemainingPrompts() | ||
| assert.Equal(t, "Hello Ephemeral Environment", flags.Name.Value) | ||
| assert.Equal(t, project1.Name, flags.Project.Value) | ||
| } | ||
This file contains hidden or 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,27 @@ | ||
| package ephemeralenvironment | ||
|
|
||
| import ( | ||
| "github.com/MakeNowJust/heredoc/v2" | ||
| cmdCreate "github.com/OctopusDeploy/cli/pkg/cmd/ephemeralenvironment/create" | ||
| "github.com/OctopusDeploy/cli/pkg/constants" | ||
| "github.com/OctopusDeploy/cli/pkg/constants/annotations" | ||
| "github.com/OctopusDeploy/cli/pkg/factory" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewCmdEphemeralEnvironment(f factory.Factory) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "ephemeralenvironment <command>", | ||
| Short: "Manage ephemeral environments", | ||
| Long: "Manage ephemeral environments in Octopus Deploy", | ||
| Example: heredoc.Docf(` | ||
| $ %[1]s ephemeralenvironment create --name "EE1" --project "MyProject" | ||
| `, constants.ExecutableName), | ||
| Annotations: map[string]string{ | ||
| annotations.IsInfrastructure: "true", | ||
| }, | ||
| } | ||
|
|
||
| cmd.AddCommand(cmdCreate.NewCmdCreate(f)) | ||
| return cmd | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for a
--spaceflag as this is automatically added to the command.