github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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  	return nil, &EvalValidateError{
    53  		Errors: errs,
    54  	}
    55  }
    56  
    57  // EvalValidateProvider is an EvalNode implementation that validates
    58  // the configuration of a resource.
    59  type EvalValidateProvider struct {
    60  	Provider *ResourceProvider
    61  	Config   **ResourceConfig
    62  }
    63  
    64  func (n *EvalValidateProvider) Eval(ctx EvalContext) (interface{}, error) {
    65  	provider := *n.Provider
    66  	config := *n.Config
    67  
    68  	warns, errs := provider.Validate(config)
    69  	if len(warns) == 0 && len(errs) == 0 {
    70  		return nil, nil
    71  	}
    72  
    73  	return nil, &EvalValidateError{
    74  		Warnings: warns,
    75  		Errors:   errs,
    76  	}
    77  }
    78  
    79  // EvalValidateProvisioner is an EvalNode implementation that validates
    80  // the configuration of a resource.
    81  type EvalValidateProvisioner struct {
    82  	Provisioner *ResourceProvisioner
    83  	Config      **ResourceConfig
    84  }
    85  
    86  func (n *EvalValidateProvisioner) Eval(ctx EvalContext) (interface{}, error) {
    87  	provisioner := *n.Provisioner
    88  	config := *n.Config
    89  	warns, errs := provisioner.Validate(config)
    90  	if len(warns) == 0 && len(errs) == 0 {
    91  		return nil, nil
    92  	}
    93  
    94  	return nil, &EvalValidateError{
    95  		Warnings: warns,
    96  		Errors:   errs,
    97  	}
    98  }
    99  
   100  // EvalValidateResource is an EvalNode implementation that validates
   101  // the configuration of a resource.
   102  type EvalValidateResource struct {
   103  	Provider     *ResourceProvider
   104  	Config       **ResourceConfig
   105  	ResourceName string
   106  	ResourceType string
   107  }
   108  
   109  func (n *EvalValidateResource) Eval(ctx EvalContext) (interface{}, error) {
   110  	// TODO: test
   111  
   112  	provider := *n.Provider
   113  	cfg := *n.Config
   114  	warns, errs := provider.ValidateResource(n.ResourceType, cfg)
   115  
   116  	// If the resouce name doesn't match the name regular
   117  	// expression, show a warning.
   118  	if !config.NameRegexp.Match([]byte(n.ResourceName)) {
   119  		warns = append(warns, fmt.Sprintf(
   120  			"%s: resource name can only contain letters, numbers, "+
   121  				"dashes, and underscores.\n"+
   122  				"This will be an error in Terraform 0.4",
   123  			n.ResourceName))
   124  	}
   125  
   126  	if len(warns) == 0 && len(errs) == 0 {
   127  		return nil, nil
   128  	}
   129  
   130  	return nil, &EvalValidateError{
   131  		Warnings: warns,
   132  		Errors:   errs,
   133  	}
   134  }