2015-02-04 17:44:23 -06:00
|
|
|
package terraform
|
|
|
|
|
2015-02-08 19:20:46 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
)
|
|
|
|
|
2015-02-04 17:44:23 -06: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 01:32:28 -06:00
|
|
|
return fmt.Sprintf("Warnings: %s. Errors: %s", e.Warnings, e.Errors)
|
2015-02-04 17:44:23 -06:00
|
|
|
}
|
|
|
|
|
2015-02-08 19:20:46 -06:00
|
|
|
// EvalValidateCount is an EvalNode implementation that validates
|
|
|
|
// the count of a resource.
|
|
|
|
type EvalValidateCount struct {
|
|
|
|
Resource *config.Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateCount) Args() ([]EvalNode, []EvalType) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
|
|
|
func (n *EvalValidateCount) Eval(
|
|
|
|
ctx EvalContext, args []interface{}) (interface{}, error) {
|
|
|
|
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 14:16:55 -06: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-08 19:20:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if count < 0 {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Count is less than zero: %d", count))
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN:
|
|
|
|
return nil, &EvalValidateError{
|
|
|
|
Errors: errs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateCount) Type() EvalType {
|
|
|
|
return EvalTypeNull
|
|
|
|
}
|
|
|
|
|
2015-02-04 19:23:26 -06:00
|
|
|
// EvalValidateProvider is an EvalNode implementation that validates
|
|
|
|
// the configuration of a resource.
|
|
|
|
type EvalValidateProvider struct {
|
2015-02-10 01:32:28 -06:00
|
|
|
ProviderName string
|
|
|
|
Provider EvalNode
|
|
|
|
Config EvalNode
|
2015-02-04 19:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateProvider) Args() ([]EvalNode, []EvalType) {
|
|
|
|
return []EvalNode{n.Provider, n.Config},
|
|
|
|
[]EvalType{EvalTypeResourceProvider, EvalTypeConfig}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateProvider) Eval(
|
|
|
|
ctx EvalContext, args []interface{}) (interface{}, error) {
|
|
|
|
provider := args[0].(ResourceProvider)
|
|
|
|
config := args[1].(*ResourceConfig)
|
2015-02-10 01:32:28 -06:00
|
|
|
|
|
|
|
// Get the parent configuration if there is one
|
|
|
|
if parent := ctx.ParentProviderConfig(n.ProviderName); parent != nil {
|
|
|
|
merged := parent.raw.Merge(config.raw)
|
|
|
|
config = NewResourceConfig(merged)
|
|
|
|
}
|
|
|
|
|
2015-02-04 19:23:26 -06:00
|
|
|
warns, errs := provider.Validate(config)
|
|
|
|
if len(warns) == 0 && len(errs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &EvalValidateError{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: errs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateProvider) Type() EvalType {
|
|
|
|
return EvalTypeNull
|
|
|
|
}
|
|
|
|
|
2015-02-09 13:15:54 -06:00
|
|
|
// EvalValidateProvisioner is an EvalNode implementation that validates
|
|
|
|
// the configuration of a resource.
|
|
|
|
type EvalValidateProvisioner struct {
|
|
|
|
Provisioner EvalNode
|
|
|
|
Config EvalNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateProvisioner) Args() ([]EvalNode, []EvalType) {
|
|
|
|
return []EvalNode{n.Provisioner, n.Config},
|
|
|
|
[]EvalType{EvalTypeResourceProvisioner, EvalTypeConfig}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateProvisioner) Eval(
|
|
|
|
ctx EvalContext, args []interface{}) (interface{}, error) {
|
|
|
|
provider := args[0].(ResourceProvisioner)
|
|
|
|
config := args[1].(*ResourceConfig)
|
|
|
|
warns, errs := provider.Validate(config)
|
|
|
|
if len(warns) == 0 && len(errs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &EvalValidateError{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: errs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateProvisioner) Type() EvalType {
|
|
|
|
return EvalTypeNull
|
|
|
|
}
|
|
|
|
|
2015-02-04 17:44:23 -06:00
|
|
|
// EvalValidateResource is an EvalNode implementation that validates
|
|
|
|
// the configuration of a resource.
|
|
|
|
type EvalValidateResource struct {
|
2015-02-04 19:02:18 -06:00
|
|
|
Provider EvalNode
|
2015-02-04 19:23:26 -06:00
|
|
|
Config EvalNode
|
2015-02-09 11:50:20 -06:00
|
|
|
ResourceName string
|
2015-02-08 19:58:02 -06:00
|
|
|
ResourceType string
|
2015-02-04 17:44:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateResource) Args() ([]EvalNode, []EvalType) {
|
2015-02-04 19:23:26 -06:00
|
|
|
return []EvalNode{n.Provider, n.Config},
|
|
|
|
[]EvalType{EvalTypeResourceProvider, EvalTypeConfig}
|
2015-02-04 17:44:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateResource) Eval(
|
|
|
|
ctx EvalContext, args []interface{}) (interface{}, error) {
|
2015-02-04 19:02:18 -06:00
|
|
|
// TODO: test
|
|
|
|
|
2015-02-08 19:58:02 -06:00
|
|
|
provider := args[0].(ResourceProvider)
|
2015-02-09 11:50:20 -06:00
|
|
|
cfg := args[1].(*ResourceConfig)
|
|
|
|
warns, errs := provider.ValidateResource(n.ResourceType, cfg)
|
|
|
|
|
|
|
|
// If the resouce name doesn't match the name regular
|
|
|
|
// expression, show a warning.
|
|
|
|
if !config.NameRegexp.Match([]byte(n.ResourceName)) {
|
|
|
|
warns = append(warns, fmt.Sprintf(
|
|
|
|
"%s: resource name can only contain letters, numbers, "+
|
|
|
|
"dashes, and underscores.\n"+
|
|
|
|
"This will be an error in Terraform 0.4",
|
|
|
|
n.ResourceName))
|
|
|
|
}
|
2015-02-08 19:58:02 -06:00
|
|
|
|
|
|
|
if len(warns) == 0 && len(errs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &EvalValidateError{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: errs,
|
|
|
|
}
|
2015-02-04 17:44:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EvalValidateResource) Type() EvalType {
|
|
|
|
return EvalTypeNull
|
|
|
|
}
|