github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/terraform/eval_validate.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  )
     8  
     9  // EvalValidateError is the error structure returned if there were
    10  // validation errors.
    11  type EvalValidateError struct {
    12  	Warnings []string
    13  	Errors   []error
    14  }
    15  
    16  func (e *EvalValidateError) Error() string {
    17  	return fmt.Sprintf("Warnings: %s. Errors: %s", e.Warnings, e.Errors)
    18  }
    19  
    20  // EvalValidateCount is an EvalNode implementation that validates
    21  // the count of a resource.
    22  type EvalValidateCount struct {
    23  	Resource *config.Resource
    24  }
    25  
    26  // TODO: test
    27  func (n *EvalValidateCount) Eval(ctx EvalContext) (interface{}, error) {
    28  	var count int
    29  	var errs []error
    30  	var err error
    31  	if _, err := ctx.Interpolate(n.Resource.RawCount, nil); err != nil {
    32  		errs = append(errs, fmt.Errorf(
    33  			"Failed to interpolate count: %s", err))
    34  		goto RETURN
    35  	}
    36  
    37  	count, err = n.Resource.Count()
    38  	if err != nil {
    39  		// If we can't get the count during validation, then
    40  		// just replace it with the number 1.
    41  		c := n.Resource.RawCount.Config()
    42  		c[n.Resource.RawCount.Key] = "1"
    43  		count = 1
    44  	}
    45  
    46  	if count < 0 {
    47  		errs = append(errs, fmt.Errorf(
    48  			"Count is less than zero: %d", count))
    49  	}
    50  
    51  RETURN:
    52  	if len(errs) != 0 {
    53  		err = &EvalValidateError{
    54  			Errors: errs,
    55  		}
    56  	}
    57  	return nil, err
    58  }
    59  
    60  // EvalValidateProvider is an EvalNode implementation that validates
    61  // the configuration of a resource.
    62  type EvalValidateProvider struct {
    63  	Provider *ResourceProvider
    64  	Config   **ResourceConfig
    65  }
    66  
    67  func (n *EvalValidateProvider) Eval(ctx EvalContext) (interface{}, error) {
    68  	provider := *n.Provider
    69  	config := *n.Config
    70  
    71  	warns, errs := provider.Validate(config)
    72  	if len(warns) == 0 && len(errs) == 0 {
    73  		return nil, nil
    74  	}
    75  
    76  	return nil, &EvalValidateError{
    77  		Warnings: warns,
    78  		Errors:   errs,
    79  	}
    80  }
    81  
    82  // EvalValidateProvisioner is an EvalNode implementation that validates
    83  // the configuration of a resource.
    84  type EvalValidateProvisioner struct {
    85  	Provisioner *ResourceProvisioner
    86  	Config      **ResourceConfig
    87  }
    88  
    89  func (n *EvalValidateProvisioner) Eval(ctx EvalContext) (interface{}, error) {
    90  	provisioner := *n.Provisioner
    91  	config := *n.Config
    92  	warns, errs := provisioner.Validate(config)
    93  	if len(warns) == 0 && len(errs) == 0 {
    94  		return nil, nil
    95  	}
    96  
    97  	return nil, &EvalValidateError{
    98  		Warnings: warns,
    99  		Errors:   errs,
   100  	}
   101  }
   102  
   103  // EvalValidateResource is an EvalNode implementation that validates
   104  // the configuration of a resource.
   105  type EvalValidateResource struct {
   106  	Provider     *ResourceProvider
   107  	Config       **ResourceConfig
   108  	ResourceName string
   109  	ResourceType string
   110  }
   111  
   112  func (n *EvalValidateResource) Eval(ctx EvalContext) (interface{}, error) {
   113  	// TODO: test
   114  
   115  	provider := *n.Provider
   116  	cfg := *n.Config
   117  	warns, errs := provider.ValidateResource(n.ResourceType, cfg)
   118  
   119  	// If the resouce name doesn't match the name regular
   120  	// expression, show a warning.
   121  	if !config.NameRegexp.Match([]byte(n.ResourceName)) {
   122  		warns = append(warns, fmt.Sprintf(
   123  			"%s: resource name can only contain letters, numbers, "+
   124  				"dashes, and underscores.\n"+
   125  				"This will be an error in Terraform 0.4",
   126  			n.ResourceName))
   127  	}
   128  
   129  	if len(warns) == 0 && len(errs) == 0 {
   130  		return nil, nil
   131  	}
   132  
   133  	return nil, &EvalValidateError{
   134  		Warnings: warns,
   135  		Errors:   errs,
   136  	}
   137  }