2015-11-05 15:47:08 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2016-10-27 19:42:49 +02:00
|
|
|
"strings"
|
2016-02-08 23:04:24 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2017-07-05 18:32:29 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-11-05 15:47:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateCommand is a Command implementation that validates the terraform files
|
|
|
|
type ValidateCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultPath = "."
|
|
|
|
|
|
|
|
func (c *ValidateCommand) Run(args []string) int {
|
2017-07-05 18:32:29 +02:00
|
|
|
args = c.Meta.process(args, true)
|
|
|
|
var checkVars bool
|
2015-11-05 15:47:08 +01:00
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
cmdFlags := c.Meta.flagSet("validate")
|
|
|
|
cmdFlags.BoolVar(&checkVars, "check-variables", true, "check-variables")
|
|
|
|
cmdFlags.Usage = func() {
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
}
|
2016-10-27 19:42:49 +02:00
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
args = cmdFlags.Args()
|
|
|
|
|
|
|
|
var dirPath string
|
2015-11-05 15:47:08 +01:00
|
|
|
if len(args) == 1 {
|
|
|
|
dirPath = args[0]
|
|
|
|
} else {
|
|
|
|
dirPath = "."
|
|
|
|
}
|
|
|
|
dir, err := filepath.Abs(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Unable to locate directory %v\n", err.Error()))
|
|
|
|
}
|
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
rtnCode := c.validate(dir, checkVars)
|
2015-11-05 15:47:08 +01:00
|
|
|
|
|
|
|
return rtnCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ValidateCommand) Synopsis() string {
|
|
|
|
return "Validates the Terraform files"
|
|
|
|
}
|
|
|
|
|
2016-10-27 19:42:49 +02:00
|
|
|
func (c *ValidateCommand) Help() string {
|
|
|
|
helpText := `
|
2017-07-05 18:32:29 +02:00
|
|
|
Usage: terraform validate [options] [dir]
|
|
|
|
|
|
|
|
Validate the terraform files in a directory. Validation includes a
|
|
|
|
basic check of syntax as well as checking that all variables declared
|
|
|
|
in the configuration are specified in one of the possible ways:
|
2016-10-27 19:42:49 +02:00
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
-var foo=...
|
|
|
|
-var-file=foo.vars
|
|
|
|
TF_VAR_foo environment variable
|
|
|
|
terraform.tfvars
|
|
|
|
default value
|
2016-10-27 19:42:49 +02:00
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
If dir is not specified, then the current directory will be used.
|
2016-10-27 19:42:49 +02:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
-check-variables=true If set to true (default), the command will check
|
|
|
|
whether all required variables have been specified.
|
2016-10-27 19:42:49 +02:00
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
|
|
|
-var 'foo=bar' Set a variable in the Terraform configuration. This
|
|
|
|
flag can be set multiple times.
|
|
|
|
|
|
|
|
-var-file=foo Set variables in the Terraform configuration from
|
|
|
|
a file. If "terraform.tfvars" is present, it will be
|
|
|
|
automatically loaded if this flag is not specified.
|
2016-10-27 19:42:49 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2017-07-05 18:32:29 +02:00
|
|
|
func (c *ValidateCommand) validate(dir string, checkVars bool) int {
|
2015-11-05 15:47:08 +01:00
|
|
|
cfg, err := config.LoadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error loading files %v\n", err.Error()))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
err = cfg.Validate()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error validating: %v\n", err.Error()))
|
|
|
|
return 1
|
|
|
|
}
|
2017-07-05 18:32:29 +02:00
|
|
|
|
|
|
|
if checkVars {
|
|
|
|
mod, err := c.Module(dir)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := c.contextOpts()
|
|
|
|
opts.Module = mod
|
|
|
|
|
|
|
|
tfCtx, err := terraform.NewContext(opts)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error: %v\n", err.Error()))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validateContext(tfCtx, c.Ui) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 15:47:08 +01:00
|
|
|
return 0
|
|
|
|
}
|