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
|
|
|
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
|
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-08-28 21:01:11 +02:00
|
|
|
args, err := c.Meta.process(args, true)
|
2017-03-08 05:09:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2017-07-05 18:32:29 +02:00
|
|
|
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-10-21 00:42:51 +02:00
|
|
|
// Check for user-supplied plugin path
|
|
|
|
if c.pluginPath, err = c.loadPluginPath(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
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 {
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
2015-11-05 15:47:08 +01:00
|
|
|
cfg, err := config.LoadDir(dir)
|
|
|
|
if err != nil {
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
diags = diags.Append(err)
|
2017-10-05 21:00:45 +02:00
|
|
|
c.showDiagnostics(err)
|
2015-11-05 15:47:08 +01:00
|
|
|
return 1
|
|
|
|
}
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
|
|
|
|
diags = diags.Append(cfg.Validate())
|
|
|
|
|
|
|
|
if diags.HasErrors() {
|
2017-11-22 00:08:00 +01:00
|
|
|
c.showDiagnostics(diags)
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
return 1
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
2017-07-05 18:32:29 +02:00
|
|
|
|
|
|
|
if checkVars {
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
mod, modDiags := c.Module(dir)
|
|
|
|
diags = diags.Append(modDiags)
|
|
|
|
if modDiags.HasErrors() {
|
|
|
|
c.showDiagnostics(diags)
|
2017-07-05 18:32:29 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := c.contextOpts()
|
|
|
|
opts.Module = mod
|
|
|
|
|
|
|
|
tfCtx, err := terraform.NewContext(opts)
|
|
|
|
if err != nil {
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
diags = diags.Append(err)
|
|
|
|
c.showDiagnostics(diags)
|
2017-07-05 18:32:29 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
diags = diags.Append(tfCtx.Validate())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.showDiagnostics(diags)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return 1
|
2017-07-05 18:32:29 +02:00
|
|
|
}
|
|
|
|
|
2015-11-05 15:47:08 +01:00
|
|
|
return 0
|
|
|
|
}
|