Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/cmd/target/azure-web-app/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type CreateFlags struct {
*shared.CreateTargetRoleFlags
*shared.CreateTargetTenantFlags
*shared.WorkerPoolFlags
*machinescommon.CreateTargetDisabledFlags
*machinescommon.WebFlags
}

Expand Down Expand Up @@ -73,6 +74,7 @@ func NewCreateFlags() *CreateFlags {
CreateTargetEnvironmentFlags: shared.NewCreateTargetEnvironmentFlags(),
CreateTargetTenantFlags: shared.NewCreateTargetTenantFlags(),
WorkerPoolFlags: shared.NewWorkerPoolFlags(),
CreateTargetDisabledFlags: machinescommon.NewCreateTargetDisabledFlags(),
WebFlags: machinescommon.NewWebFlags(),
}
}
Expand Down Expand Up @@ -123,6 +125,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command {
shared.RegisterCreateTargetRoleFlags(cmd, createFlags.CreateTargetRoleFlags)
shared.RegisterCreateTargetTenantFlags(cmd, createFlags.CreateTargetTenantFlags)
shared.RegisterCreateTargetWorkerPoolFlags(cmd, createFlags.WorkerPoolFlags)
machinescommon.RegisterCreateTargetDisabledFlags(cmd, createFlags.CreateTargetDisabledFlags)
machinescommon.RegisterWebFlag(cmd, createFlags.WebFlags)
return cmd
}
Expand Down Expand Up @@ -174,14 +177,16 @@ func createRun(opts *CreateOptions) error {
return err
}

deploymentTarget.IsDisabled = opts.Disabled.Value

createdTarget, err := opts.Client.Machines.Add(deploymentTarget)
if err != nil {
return err
}

fmt.Fprintf(opts.Out, "Successfully created Azure web app '%s'.\n", deploymentTarget.Name)
if !opts.NoPrompt {
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.GetSpaceNameOrEmpty(), opts.Name, opts.Account, opts.WebApp, opts.ResourceGroup, opts.Slot, opts.Environments, opts.Roles, opts.Tags, opts.TenantedDeploymentMode, opts.Tenants, opts.TenantTags)
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.GetSpaceNameOrEmpty(), opts.Name, opts.Account, opts.WebApp, opts.ResourceGroup, opts.Slot, opts.Environments, opts.Roles, opts.Tags, opts.TenantedDeploymentMode, opts.Tenants, opts.TenantTags, opts.Disabled)
fmt.Fprintf(opts.Out, "\nAutomation Command: %s\n", autoCmd)
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/target/cloud-region/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type CreateFlags struct {
*shared.CreateTargetRoleFlags
*shared.WorkerPoolFlags
*shared.CreateTargetTenantFlags
*machinescommon.CreateTargetDisabledFlags
*machinescommon.WebFlags
}

Expand All @@ -47,6 +48,7 @@ func NewCreateFlags() *CreateFlags {
CreateTargetEnvironmentFlags: shared.NewCreateTargetEnvironmentFlags(),
CreateTargetRoleFlags: shared.NewCreateTargetRoleFlags(),
CreateTargetTenantFlags: shared.NewCreateTargetTenantFlags(),
CreateTargetDisabledFlags: machinescommon.NewCreateTargetDisabledFlags(),
WebFlags: machinescommon.NewWebFlags(),
}
}
Expand Down Expand Up @@ -84,6 +86,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command {
shared.RegisterCreateTargetRoleFlags(cmd, createFlags.CreateTargetRoleFlags)
shared.RegisterCreateTargetWorkerPoolFlags(cmd, createFlags.WorkerPoolFlags)
shared.RegisterCreateTargetTenantFlags(cmd, createFlags.CreateTargetTenantFlags)
machinescommon.RegisterCreateTargetDisabledFlags(cmd, createFlags.CreateTargetDisabledFlags)
machinescommon.RegisterWebFlag(cmd, createFlags.WebFlags)

return cmd
Expand Down Expand Up @@ -122,13 +125,15 @@ func createRun(opts *CreateOptions) error {
return err
}

target.IsDisabled = opts.Disabled.Value

createdTarget, err := opts.Client.Machines.Add(target)
if err != nil {
return err
}
fmt.Fprintf(opts.Out, "Successfully created cloud region '%s'.\n", target.Name)
if !opts.NoPrompt {
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.GetSpaceNameOrEmpty(), opts.Name, opts.WorkerPool, opts.Environments, opts.Roles, opts.Tags, opts.TenantedDeploymentMode, opts.Tenants, opts.TenantTags)
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.GetSpaceNameOrEmpty(), opts.Name, opts.WorkerPool, opts.Environments, opts.Roles, opts.Tags, opts.TenantedDeploymentMode, opts.Tenants, opts.TenantTags, opts.Disabled)
fmt.Fprintf(opts.Out, "\nAutomation Command: %s\n", autoCmd)
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/cmd/target/disable/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package disable

import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/usage"
"github.com/spf13/cobra"
)

func NewCmdDisable(f factory.Factory) *cobra.Command {
return &cobra.Command{
Args: usage.MaximumNArgs(1),
Use: "disable [<name> | <id>]",
Short: "Disable a deployment target",
Long: "Disable a deployment target in Octopus Deploy",
Example: heredoc.Docf(`
%[1]s deployment-target disable Machines-100
%[1]s deployment-target disable 'web-server'
`, constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
opts := shared.NewSetDisabledStateOptions(args, cmd.NewDependencies(f, c))
return shared.SetDisabledState(opts, true)
},
}
}
119 changes: 119 additions & 0 deletions pkg/cmd/target/disable/disable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package disable_test

import (
"bytes"
"testing"

"github.com/AlecAivazis/survey/v2"
cmdRoot "github.com/OctopusDeploy/cli/pkg/cmd/root"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/test/fixtures"
"github.com/OctopusDeploy/cli/test/testutil"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

var rootResource = testutil.NewRootResource()

const spaceID = "Spaces-1"

func newTarget(id string, name string, isDisabled bool) *machines.DeploymentTarget {
target := machines.NewDeploymentTarget(name, machines.NewCloudRegionEndpoint(), []string{"Environments-1"}, []string{"web"})
target.ID = id
target.SpaceID = spaceID
target.IsDisabled = isDisabled
return target
}

func TestDeploymentTargetDisable(t *testing.T) {
space1 := fixtures.NewSpace(spaceID, "Default Space")

tests := []struct {
name string
run func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer)
}{
{"disables a target identified on the command line", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) {
defer api.Close()
rootCmd.SetArgs([]string{"deployment-target", "disable", "Machines-100", "--no-prompt"})
return rootCmd.ExecuteC()
})

api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1/machines/Machines-100").RespondWith(newTarget("Machines-100", "web-server", false))

updateRequest := api.ExpectRequest(t, "PUT", "/api/Spaces-1/machines/Machines-100")
updated, err := testutil.ReadJson[machines.DeploymentTarget](updateRequest.Request.Body)
assert.Nil(t, err)
assert.True(t, updated.IsDisabled)
updateRequest.RespondWith(newTarget("Machines-100", "web-server", true))

_, err = testutil.ReceivePair(cmdReceiver)
assert.Nil(t, err)
assert.Contains(t, stdOut.String(), "Successfully disabled deployment target 'web-server'")
assert.Equal(t, "", stdErr.String())
}},

{"does not update a target which is already disabled", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) {
defer api.Close()
rootCmd.SetArgs([]string{"deployment-target", "disable", "Machines-100", "--no-prompt"})
return rootCmd.ExecuteC()
})

api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1/machines/Machines-100").RespondWith(newTarget("Machines-100", "web-server", true))

_, err := testutil.ReceivePair(cmdReceiver)
assert.Nil(t, err)
assert.Contains(t, stdOut.String(), "is already disabled")
assert.Equal(t, "", stdErr.String())
}},

{"prompts for the target when none was supplied", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) {
defer api.Close()
rootCmd.SetArgs([]string{"deployment-target", "disable"})
return rootCmd.ExecuteC()
})

api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1/machines?take=2147483647").
RespondWith(resources.Resources[*machines.DeploymentTarget]{Items: []*machines.DeploymentTarget{
newTarget("Machines-100", "web-server", false),
newTarget("Machines-200", "db-server", false),
}})

_ = qa.ExpectQuestion(t, &survey.Select{
Message: "Select the deployment target you wish to disable:",
Options: []string{"web-server", "db-server"},
}).AnswerWith("db-server")

api.ExpectRequest(t, "GET", "/api/Spaces-1/machines/Machines-200").RespondWith(newTarget("Machines-200", "db-server", false))
api.ExpectRequest(t, "PUT", "/api/Spaces-1/machines/Machines-200").RespondWith(newTarget("Machines-200", "db-server", true))

_, err := testutil.ReceivePair(cmdReceiver)
assert.Nil(t, err)
assert.Contains(t, stdOut.String(), "Successfully disabled deployment target 'db-server'")
assert.Equal(t, "", stdErr.String())
}},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
api, qa := testutil.NewMockServerAndAsker()
askProvider := question.NewAskProvider(qa.AsAsker())
fac := testutil.NewMockFactoryWithSpaceAndPrompt(api, space1, askProvider)
rootCmd := cmdRoot.NewCmdRoot(fac, nil, askProvider)
rootCmd.SetOut(stdout)
rootCmd.SetErr(stderr)
test.run(t, api, qa, rootCmd, stdout, stderr)
})
}
}
28 changes: 28 additions & 0 deletions pkg/cmd/target/enable/enable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package enable

import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/usage"
"github.com/spf13/cobra"
)

func NewCmdEnable(f factory.Factory) *cobra.Command {
return &cobra.Command{
Args: usage.MaximumNArgs(1),
Use: "enable [<name> | <id>]",
Short: "Enable a deployment target",
Long: "Enable a deployment target in Octopus Deploy",
Example: heredoc.Docf(`
%[1]s deployment-target enable Machines-100
%[1]s deployment-target enable 'web-server'
`, constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
opts := shared.NewSetDisabledStateOptions(args, cmd.NewDependencies(f, c))
return shared.SetDisabledState(opts, false)
},
}
}
119 changes: 119 additions & 0 deletions pkg/cmd/target/enable/enable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package enable_test

import (
"bytes"
"testing"

"github.com/AlecAivazis/survey/v2"
cmdRoot "github.com/OctopusDeploy/cli/pkg/cmd/root"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/test/fixtures"
"github.com/OctopusDeploy/cli/test/testutil"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

var rootResource = testutil.NewRootResource()

const spaceID = "Spaces-1"

func newTarget(id string, name string, isDisabled bool) *machines.DeploymentTarget {
target := machines.NewDeploymentTarget(name, machines.NewCloudRegionEndpoint(), []string{"Environments-1"}, []string{"web"})
target.ID = id
target.SpaceID = spaceID
target.IsDisabled = isDisabled
return target
}

func TestDeploymentTargetEnable(t *testing.T) {
space1 := fixtures.NewSpace(spaceID, "Default Space")

tests := []struct {
name string
run func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer)
}{
{"enables a target identified on the command line", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) {
defer api.Close()
rootCmd.SetArgs([]string{"deployment-target", "enable", "Machines-100", "--no-prompt"})
return rootCmd.ExecuteC()
})

api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1/machines/Machines-100").RespondWith(newTarget("Machines-100", "web-server", true))

updateRequest := api.ExpectRequest(t, "PUT", "/api/Spaces-1/machines/Machines-100")
updated, err := testutil.ReadJson[machines.DeploymentTarget](updateRequest.Request.Body)
assert.Nil(t, err)
assert.False(t, updated.IsDisabled)
updateRequest.RespondWith(newTarget("Machines-100", "web-server", false))

_, err = testutil.ReceivePair(cmdReceiver)
assert.Nil(t, err)
assert.Contains(t, stdOut.String(), "Successfully enabled deployment target 'web-server'")
assert.Equal(t, "", stdErr.String())
}},

{"does not update a target which is already enabled", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) {
defer api.Close()
rootCmd.SetArgs([]string{"deployment-target", "enable", "Machines-100", "--no-prompt"})
return rootCmd.ExecuteC()
})

api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1/machines/Machines-100").RespondWith(newTarget("Machines-100", "web-server", false))

_, err := testutil.ReceivePair(cmdReceiver)
assert.Nil(t, err)
assert.Contains(t, stdOut.String(), "is already enabled")
assert.Equal(t, "", stdErr.String())
}},

{"prompts for the target when none was supplied", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) {
defer api.Close()
rootCmd.SetArgs([]string{"deployment-target", "enable"})
return rootCmd.ExecuteC()
})

api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource)
api.ExpectRequest(t, "GET", "/api/Spaces-1/machines?take=2147483647").
RespondWith(resources.Resources[*machines.DeploymentTarget]{Items: []*machines.DeploymentTarget{
newTarget("Machines-100", "web-server", true),
newTarget("Machines-200", "db-server", true),
}})

_ = qa.ExpectQuestion(t, &survey.Select{
Message: "Select the deployment target you wish to enable:",
Options: []string{"web-server", "db-server"},
}).AnswerWith("web-server")

api.ExpectRequest(t, "GET", "/api/Spaces-1/machines/Machines-100").RespondWith(newTarget("Machines-100", "web-server", true))
api.ExpectRequest(t, "PUT", "/api/Spaces-1/machines/Machines-100").RespondWith(newTarget("Machines-100", "web-server", false))

_, err := testutil.ReceivePair(cmdReceiver)
assert.Nil(t, err)
assert.Contains(t, stdOut.String(), "Successfully enabled deployment target 'web-server'")
assert.Equal(t, "", stdErr.String())
}},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
api, qa := testutil.NewMockServerAndAsker()
askProvider := question.NewAskProvider(qa.AsAsker())
fac := testutil.NewMockFactoryWithSpaceAndPrompt(api, space1, askProvider)
rootCmd := cmdRoot.NewCmdRoot(fac, nil, askProvider)
rootCmd.SetOut(stdout)
rootCmd.SetErr(stderr)
test.run(t, api, qa, rootCmd, stdout, stderr)
})
}
}
Loading