mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #8 from hashicorp/f-destroy-cmd
Destroy support in CLI
This commit is contained in:
commit
29d6a4242e
1
TODO.md
1
TODO.md
@ -1,6 +1,5 @@
|
|||||||
This is just to keep track of what we need to do before 0.1:
|
This is just to keep track of what we need to do before 0.1:
|
||||||
|
|
||||||
* `terraform destroy`
|
|
||||||
* `terraform apply/plan/refresh/destroy` need to be able to take variables as input
|
* `terraform apply/plan/refresh/destroy` need to be able to take variables as input
|
||||||
* Provisioners on top of static resource creation: Shell, Chef, Puppet, etc.
|
* Provisioners on top of static resource creation: Shell, Chef, Puppet, etc.
|
||||||
* `ValidateResource` ResourceProvider API for checking the structure of a resource config
|
* `ValidateResource` ResourceProvider API for checking the structure of a resource config
|
||||||
|
@ -42,6 +42,21 @@ func testPlanFile(t *testing.T, plan *terraform.Plan) string {
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testReadPlan(t *testing.T, path string) *terraform.Plan {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
p, err := terraform.ReadPlan(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
func testStateFile(t *testing.T, s *terraform.State) string {
|
func testStateFile(t *testing.T, s *terraform.State) string {
|
||||||
path := testTempFile(t)
|
path := testTempFile(t)
|
||||||
|
|
||||||
|
@ -20,10 +20,11 @@ type PlanCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *PlanCommand) Run(args []string) int {
|
func (c *PlanCommand) Run(args []string) int {
|
||||||
var refresh bool
|
var destroy, refresh bool
|
||||||
var outPath, statePath string
|
var outPath, statePath string
|
||||||
|
|
||||||
cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError)
|
cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError)
|
||||||
|
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
|
||||||
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
||||||
cmdFlags.StringVar(&outPath, "out", "", "path")
|
cmdFlags.StringVar(&outPath, "out", "", "path")
|
||||||
cmdFlags.StringVar(&statePath, "state", "", "path")
|
cmdFlags.StringVar(&statePath, "state", "", "path")
|
||||||
@ -80,8 +81,9 @@ func (c *PlanCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plan, err := tf.Plan(&terraform.PlanOpts{
|
plan, err := tf.Plan(&terraform.PlanOpts{
|
||||||
Config: b,
|
Config: b,
|
||||||
State: state,
|
Destroy: destroy,
|
||||||
|
State: state,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
|
||||||
@ -120,6 +122,9 @@ Usage: terraform plan [options] [terraform.tf]
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
|
-destroy If set, a plan will be generated to destroy all resources
|
||||||
|
managed by the given configuration and state.
|
||||||
|
|
||||||
-out=path Write a plan file to the given path. This can be used as
|
-out=path Write a plan file to the given path. This can be used as
|
||||||
input to the "apply" command.
|
input to the "apply" command.
|
||||||
|
|
||||||
|
@ -10,6 +10,43 @@ import (
|
|||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestPlan_destroy(t *testing.T) {
|
||||||
|
originalState := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
outPath := testTempFile(t)
|
||||||
|
statePath := testStateFile(t, originalState)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &PlanCommand{
|
||||||
|
TFConfig: testTFConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-destroy",
|
||||||
|
"-out", outPath,
|
||||||
|
"-state", statePath,
|
||||||
|
testFixturePath("plan"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
plan := testReadPlan(t, outPath)
|
||||||
|
for _, r := range plan.Diff.Resources {
|
||||||
|
if !r.Destroy {
|
||||||
|
t.Fatalf("bad: %#v", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
func TestPlan_noState(t *testing.T) {
|
func TestPlan_noState(t *testing.T) {
|
||||||
p := testProvider()
|
p := testProvider()
|
||||||
ui := new(cli.MockUi)
|
ui := new(cli.MockUi)
|
||||||
|
@ -253,6 +253,8 @@ func (t *Terraform) planWalkFn(result *Plan, opts *PlanOpts) depgraph.WalkFunc {
|
|||||||
if r.State.ID != "" {
|
if r.State.ID != "" {
|
||||||
log.Printf("[DEBUG] %s: Making for destroy", r.Id)
|
log.Printf("[DEBUG] %s: Making for destroy", r.Id)
|
||||||
diff = &ResourceDiff{Destroy: true}
|
diff = &ResourceDiff{Destroy: true}
|
||||||
|
} else {
|
||||||
|
log.Printf("[DEBUG] %s: Not marking for destroy, no ID", r.Id)
|
||||||
}
|
}
|
||||||
} else if r.Config == nil {
|
} else if r.Config == nil {
|
||||||
log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id)
|
log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id)
|
||||||
|
Loading…
Reference in New Issue
Block a user