2014-06-09 20:53:41 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-06-19 22:51:05 +02:00
|
|
|
"os"
|
2014-06-09 20:53:41 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
// PlanCommand is a Command implementation that compares a Terraform
|
2014-06-09 20:53:41 +02:00
|
|
|
// configuration to an actual infrastructure and shows the differences.
|
2014-06-20 20:47:02 +02:00
|
|
|
type PlanCommand struct {
|
2014-06-09 20:53:41 +02:00
|
|
|
TFConfig *terraform.Config
|
|
|
|
Ui cli.Ui
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Run(args []string) int {
|
2014-06-19 22:51:05 +02:00
|
|
|
var statePath string
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError)
|
2014-06-19 22:51:05 +02:00
|
|
|
cmdFlags.StringVar(&statePath, "state", "", "path")
|
2014-06-09 20:53:41 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error(
|
2014-06-20 20:47:02 +02:00
|
|
|
"The plan command expects only one argument with the path\n" +
|
2014-06-09 20:53:41 +02:00
|
|
|
"to a Terraform configuration.\n")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-19 22:51:05 +02:00
|
|
|
// Load up the state
|
|
|
|
var state *terraform.State
|
|
|
|
if statePath != "" {
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err = terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:53:41 +02:00
|
|
|
b, err := config.Load(args[0])
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
tfconfig := c.TFConfig
|
|
|
|
tfconfig.Config = b
|
|
|
|
|
2014-06-10 20:34:08 +02:00
|
|
|
tf, err := terraform.New(tfconfig)
|
2014-06-09 20:53:41 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
plan, err := tf.Plan(state)
|
2014-06-10 20:34:08 +02:00
|
|
|
if err != nil {
|
2014-06-20 20:47:02 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
|
2014-06-10 20:34:08 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
if plan.Diff.Empty() {
|
2014-06-19 23:58:30 +02:00
|
|
|
c.Ui.Output("No changes. Infrastructure is up-to-date.")
|
|
|
|
} else {
|
2014-06-20 20:47:02 +02:00
|
|
|
c.Ui.Output(strings.TrimSpace(plan.String()))
|
2014-06-19 23:58:30 +02:00
|
|
|
}
|
2014-06-10 20:34:08 +02:00
|
|
|
|
2014-06-09 20:53:41 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Help() string {
|
2014-06-09 20:53:41 +02:00
|
|
|
helpText := `
|
2014-06-20 20:47:02 +02:00
|
|
|
Usage: terraform plan [options] [terraform.tf]
|
2014-06-09 20:53:41 +02:00
|
|
|
|
|
|
|
Shows the differences between the Terraform configuration and
|
|
|
|
the actual state of an infrastructure.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-state=statefile Path to a Terraform state file to use to look
|
|
|
|
up Terraform-managed resources.
|
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Synopsis() string {
|
2014-06-09 20:53:41 +02:00
|
|
|
return "Show changes between Terraform config and infrastructure"
|
|
|
|
}
|