Skip to content

Commit

Permalink
fly: behaviour: add team option to destroy-pipeline command
Browse files Browse the repository at this point in the history
Allow for users to destroying pipelines for different teams
without switching targets

concourse#5215

Signed-off-by: Ka Hin Ng <[email protected]>
  • Loading branch information
Ka Hin Ng committed Jul 30, 2020
1 parent 721bfa4 commit cae001d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
19 changes: 16 additions & 3 deletions fly/commands/destroy_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package commands
import (
"fmt"

"github.com/concourse/concourse/fly/commands/internal/flaghelpers"
"github.com/concourse/concourse/fly/rc"
"github.com/concourse/concourse/go-concourse/concourse"
"github.com/vito/go-interact/interact"

"github.com/concourse/concourse/fly/commands/internal/flaghelpers"
)

type DestroyPipelineCommand struct {
Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" required:"true" description:"Pipeline to destroy"`
SkipInteractive bool `short:"n" long:"non-interactive" description:"Destroy the pipeline without confirmation"`

Team string `long:"team" description:"Name of the team to which the pipeline belongs, if different from the target default"`
}

func (command *DestroyPipelineCommand) Validate() error {
Expand All @@ -35,6 +37,17 @@ func (command *DestroyPipelineCommand) Execute(args []string) error {
return err
}

var team concourse.Team

if command.Team != "" {
team, err = target.FindTeam(command.Team)
if err != nil {
return err
}
} else {
team = target.Team()
}

pipelineName := string(command.Pipeline)
fmt.Printf("!!! this will remove all data for pipeline `%s`\n\n", pipelineName)

Expand All @@ -47,7 +60,7 @@ func (command *DestroyPipelineCommand) Execute(args []string) error {
}
}

found, err := target.Team().DeletePipeline(pipelineName)
found, err := team.DeletePipeline(pipelineName)
if err != nil {
return err
}
Expand Down
61 changes: 61 additions & 0 deletions fly/integration/destroy_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package integration_test

import (
"fmt"
"github.com/concourse/concourse/atc"
"io"
"net/http"
"os/exec"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -149,6 +151,65 @@ var _ = Describe("Fly CLI", func() {
Eventually(sess).Should(gexec.Exit(1))
})
})

Context("with a team specified", func() {
BeforeEach(func() {
args = append(args, "--team", "team-two")

atcServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/api/v1/teams/team-two"),
ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
Name: "team-two",
}),
),
)
})

It("warns that it's about to do bad things", func() {
Eventually(sess).Should(gbytes.Say("!!! this will remove all data for pipeline `some-pipeline`"))
})

It("bails out if the user says no", func() {
no()
Eventually(sess).Should(gbytes.Say(`bailing out`))
Eventually(sess).Should(gexec.Exit(0))
})

Context("when the pipeline exists", func() {
BeforeEach(func() {
atcServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("DELETE", "/api/v1/teams/team-two/pipelines/some-pipeline"),
ghttp.RespondWith(204, ""),
),
)
})

It("succeeds if the user says yes", func() {
yes()
Eventually(sess).Should(gbytes.Say("`some-pipeline` deleted"))
Eventually(sess).Should(gexec.Exit(0))
})
})

Context("and the pipeline does not exist", func() {
BeforeEach(func() {
atcServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("DELETE", "/api/v1/teams/team-two/pipelines/some-pipeline"),
ghttp.RespondWith(404, ""),
),
)
})

It("writes that it did not exist and exits successfully", func() {
yes()
Eventually(sess).Should(gbytes.Say("`some-pipeline` does not exist"))
Eventually(sess).Should(gexec.Exit(0))
})
})
})
})
})
})
4 changes: 4 additions & 0 deletions fly/integration/error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ var _ = Describe("Fly CLI", func() {
exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "pipeline", "--team", "doesnotexist")),
Entry("set-pipeline command returns an error",
exec.Command(flyPath, "-t", targetName, "set-pipeline", "-p", "pipeline", "-c", "fixtures/testConfig.yml", "--team", "doesnotexist")),
Entry("destroy-pipeline command returns an error",
exec.Command(flyPath, "-t", targetName, "destroy-pipeline", "-p", "pipeline", "--team", "doesnotexist")),
)

DescribeTable("and you are NOT authorized to view the team",
Expand Down Expand Up @@ -156,6 +158,8 @@ var _ = Describe("Fly CLI", func() {
exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "pipeline", "--team", "other-team")),
Entry("set-pipeline command returns an error",
exec.Command(flyPath, "-t", targetName, "set-pipeline", "-p", "pipeline", "-c", "fixtures/testConfig.yml", "--team", "other-team")),
Entry("destroy-pipeline command returns an error",
exec.Command(flyPath, "-t", targetName, "destroy-pipeline", "-p", "pipeline", "--team", "other-team")),
)
})
})
4 changes: 4 additions & 0 deletions release-notes/latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
* Reduce the allowed character set for Concourse valid identifiers. Only prints warnings instead of errors as a first step. #5810
* Add `--team` flag for `fly pause-pipeline` command. #5917
* Add `--team` flag for `fly hide-pipeline` command. #5917

#### <sub><sup><a name="5215" href="#5215">:link:</a></sup></sub> feature

* Add `--team` flag for `fly destroy-pipeline` command. #5215

0 comments on commit cae001d

Please sign in to comment.