2015-02-05 00:44:23 +01:00
|
|
|
package terraform
|
|
|
|
|
2015-02-09 02:20:46 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2017-04-06 00:34:59 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-02-09 02:20:46 +01:00
|
|
|
)
|
|
|
|
|
2015-02-05 00:44:23 +01:00
|
|
|
// EvalValidateError is the error structure returned if there were
|
|
|
|
// validation errors.
|
|
|
|
type EvalValidateError struct {
|
|
|
|
Warnings []string
|
|
|
|
Errors []error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EvalValidateError) Error() string {
|
2015-02-10 08:32:28 +01:00
|
|
|
return fmt.Sprintf("Warnings: %s. Errors: %s", e.Warnings, e.Errors)
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|
|
|
|
|
2015-02-09 02:20:46 +01:00
|
|
|
// EvalValidateCount is an EvalNode implementation that validates
|
|
|
|
// the count of a resource.
|
|
|
|
type EvalValidateCount struct {
|
|
|
|
Resource *config.Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
2015-02-14 07:58:41 +01:00
|
|
|
func (n *EvalValidateCount) Eval(ctx EvalContext) (interface{}, error) {
|
2015-02-09 02:20:46 +01:00
|
|
|
var count int
|
|
|
|
var errs []error
|
|
|
|
var err error
|
|
|
|
if _, err := ctx.Interpolate(n.Resource.RawCount, nil); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Failed to interpolate count: %s", err))
|
|
|
|
goto RETURN
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err = n.Resource.Count()
|
|
|
|
if err != nil {
|
2015-02-10 21:16:55 +01:00
|
|
|
// If we can't get the count during validation, then
|
|
|
|
// just replace it with the number 1.
|
|
|
|
c := n.Resource.RawCount.Config()
|
|
|
|
c[n.Resource.RawCount.Key] = "1"
|
|
|
|
count = 1
|
2015-02-09 02:20:46 +01:00
|
|
|
}
|
2017-01-28 06:15:43 +01:00
|
|
|
err = nil
|
2015-02-09 02:20:46 +01:00
|
|
|
|
|
|
|
if count < 0 {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Count is less than zero: %d", count))
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN:
|
2015-10-11 19:45:33 +02:00
|
|
|
if len(errs) != 0 {
|
|
|
|
err = &EvalValidateError{
|
|
|
|
Errors: errs,
|
|
|
|
}
|
2015-02-09 02:20:46 +01:00
|
|
|
}
|
2015-10-11 19:45:33 +02:00
|
|
|
return nil, err
|
2015-02-09 02:20:46 +01:00
|
|
|
}
|
|
|
|
|
2015-02-05 02:23:26 +01:00
|
|
|
// EvalValidateProvider is an EvalNode implementation that validates
|
|
|
|
// the configuration of a resource.
|
|
|
|
type EvalValidateProvider struct {
|
2015-03-26 00:28:52 +01:00
|
|
|
Provider *ResourceProvider
|
|
|
|
Config **ResourceConfig
|
2015-02-05 02:23:26 +01:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:58:41 +01:00
|
|
|
func (n *EvalValidateProvider) Eval(ctx EvalContext) (interface{}, error) {
|
|
|
|
provider := *n.Provider
|
|
|
|
config := *n.Config
|
2015-02-10 08:32:28 +01:00
|
|
|
|
2015-02-05 02:23:26 +01:00
|
|
|
warns, errs := provider.Validate(config)
|
|
|
|
if len(warns) == 0 && len(errs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &EvalValidateError{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: errs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:15:54 +01:00
|
|
|
// EvalValidateProvisioner is an EvalNode implementation that validates
|
|
|
|
// the configuration of a resource.
|
|
|
|
type EvalValidateProvisioner struct {
|
2015-02-14 07:58:41 +01:00
|
|
|
Provisioner *ResourceProvisioner
|
|
|
|
Config **ResourceConfig
|
2017-04-06 00:34:59 +02:00
|
|
|
ConnConfig **ResourceConfig
|
2015-02-09 20:15:54 +01:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:58:41 +01:00
|
|
|
func (n *EvalValidateProvisioner) Eval(ctx EvalContext) (interface{}, error) {
|
|
|
|
provisioner := *n.Provisioner
|
|
|
|
config := *n.Config
|
2017-04-06 00:34:59 +02:00
|
|
|
var warns []string
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
{
|
|
|
|
// Validate the provisioner's own config first
|
|
|
|
w, e := provisioner.Validate(config)
|
|
|
|
warns = append(warns, w...)
|
|
|
|
errs = append(errs, e...)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Now validate the connection config, which might either be from
|
|
|
|
// the provisioner block itself or inherited from the resource's
|
|
|
|
// shared connection info.
|
|
|
|
w, e := n.validateConnConfig(*n.ConnConfig)
|
|
|
|
warns = append(warns, w...)
|
|
|
|
errs = append(errs, e...)
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:15:54 +01:00
|
|
|
if len(warns) == 0 && len(errs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &EvalValidateError{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: errs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 00:34:59 +02:00
|
|
|
func (n *EvalValidateProvisioner) validateConnConfig(connConfig *ResourceConfig) (warns []string, errs []error) {
|
|
|
|
// We can't comprehensively validate the connection config since its
|
|
|
|
// final structure is decided by the communicator and we can't instantiate
|
|
|
|
// that until we have a complete instance state. However, we *can* catch
|
|
|
|
// configuration keys that are not valid for *any* communicator, catching
|
|
|
|
// typos early rather than waiting until we actually try to run one of
|
|
|
|
// the resource's provisioners.
|
|
|
|
|
|
|
|
type connConfigSuperset struct {
|
|
|
|
// All attribute types are interface{} here because at this point we
|
|
|
|
// may still have unresolved interpolation expressions, which will
|
|
|
|
// appear as strings regardless of the final goal type.
|
|
|
|
|
|
|
|
Type interface{} `mapstructure:"type"`
|
|
|
|
User interface{} `mapstructure:"user"`
|
|
|
|
Password interface{} `mapstructure:"password"`
|
|
|
|
Host interface{} `mapstructure:"host"`
|
|
|
|
Port interface{} `mapstructure:"port"`
|
|
|
|
Timeout interface{} `mapstructure:"timeout"`
|
|
|
|
ScriptPath interface{} `mapstructure:"script_path"`
|
|
|
|
|
|
|
|
// For type=ssh only (enforced in ssh communicator)
|
|
|
|
PrivateKey interface{} `mapstructure:"private_key"`
|
|
|
|
Agent interface{} `mapstructure:"agent"`
|
|
|
|
BastionHost interface{} `mapstructure:"bastion_host"`
|
|
|
|
BastionPort interface{} `mapstructure:"bastion_port"`
|
|
|
|
BastionUser interface{} `mapstructure:"bastion_user"`
|
|
|
|
BastionPassword interface{} `mapstructure:"bastion_password"`
|
|
|
|
BastionPrivateKey interface{} `mapstructure:"bastion_private_key"`
|
|
|
|
|
|
|
|
// For type=winrm only (enforced in winrm communicator)
|
|
|
|
HTTPS interface{} `mapstructure:"https"`
|
|
|
|
Insecure interface{} `mapstructure:"insecure"`
|
|
|
|
CACert interface{} `mapstructure:"cacert"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var metadata mapstructure.Metadata
|
|
|
|
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
Metadata: &metadata,
|
|
|
|
Result: &connConfigSuperset{}, // result is disregarded; we only care about unused keys
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// should never happen
|
|
|
|
errs = append(errs, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(connConfig.Config); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attrName := range metadata.Unused {
|
|
|
|
errs = append(errs, fmt.Errorf("unknown 'connection' argument %q", attrName))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-05 00:44:23 +01:00
|
|
|
// EvalValidateResource is an EvalNode implementation that validates
|
|
|
|
// the configuration of a resource.
|
|
|
|
type EvalValidateResource struct {
|
2015-02-14 07:58:41 +01:00
|
|
|
Provider *ResourceProvider
|
|
|
|
Config **ResourceConfig
|
2015-02-09 18:50:20 +01:00
|
|
|
ResourceName string
|
2015-02-09 02:58:02 +01:00
|
|
|
ResourceType string
|
2016-05-02 04:01:48 +02:00
|
|
|
ResourceMode config.ResourceMode
|
2016-07-01 01:22:20 +02:00
|
|
|
|
|
|
|
// IgnoreWarnings means that warnings will not be passed through. This allows
|
|
|
|
// "just-in-time" passes of validation to continue execution through warnings.
|
|
|
|
IgnoreWarnings bool
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:58:41 +01:00
|
|
|
func (n *EvalValidateResource) Eval(ctx EvalContext) (interface{}, error) {
|
|
|
|
provider := *n.Provider
|
|
|
|
cfg := *n.Config
|
2016-05-02 04:01:48 +02:00
|
|
|
var warns []string
|
|
|
|
var errs []error
|
|
|
|
// Provider entry point varies depending on resource mode, because
|
|
|
|
// managed resources and data resources are two distinct concepts
|
|
|
|
// in the provider abstraction.
|
|
|
|
switch n.ResourceMode {
|
|
|
|
case config.ManagedResourceMode:
|
|
|
|
warns, errs = provider.ValidateResource(n.ResourceType, cfg)
|
|
|
|
case config.DataResourceMode:
|
|
|
|
warns, errs = provider.ValidateDataSource(n.ResourceType, cfg)
|
|
|
|
}
|
2015-02-09 18:50:20 +01:00
|
|
|
|
2016-07-01 01:22:20 +02:00
|
|
|
// If the resource name doesn't match the name regular
|
|
|
|
// expression, show an error.
|
2015-02-09 18:50:20 +01:00
|
|
|
if !config.NameRegexp.Match([]byte(n.ResourceName)) {
|
2016-02-23 17:32:02 +01:00
|
|
|
errs = append(errs, fmt.Errorf(
|
2015-02-09 18:50:20 +01:00
|
|
|
"%s: resource name can only contain letters, numbers, "+
|
2016-07-01 01:22:20 +02:00
|
|
|
"dashes, and underscores.", n.ResourceName))
|
2015-02-09 18:50:20 +01:00
|
|
|
}
|
2015-02-09 02:58:02 +01:00
|
|
|
|
2016-07-01 01:22:20 +02:00
|
|
|
if (len(warns) == 0 || n.IgnoreWarnings) && len(errs) == 0 {
|
2015-02-09 02:58:02 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &EvalValidateError{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: errs,
|
|
|
|
}
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|