mirror of https://github.com/hashicorp/terraform
parent
d23733263f
commit
947fa4e669
@ -0,0 +1,69 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/config"
|
||||
"github.com/hashicorp/terraform/helper/multierror"
|
||||
)
|
||||
|
||||
// Context represents all the context that Terraform needs in order to
|
||||
// perform operations on infrastructure. This structure is built using
|
||||
// ContextOpts and NewContext. See the documentation for those.
|
||||
//
|
||||
// Additionally, a context can be created from a Plan using Plan.Context.
|
||||
type Context struct {
|
||||
config *config.Config
|
||||
diff *Diff
|
||||
hooks []Hook
|
||||
state *State
|
||||
providers map[string]ResourceProviderFactory
|
||||
variables map[string]string
|
||||
}
|
||||
|
||||
// ContextOpts are the user-creatable configuration structure to create
|
||||
// a context with NewContext.
|
||||
type ContextOpts struct {
|
||||
Config *config.Config
|
||||
Diff *Diff
|
||||
Hooks []Hook
|
||||
State *State
|
||||
Providers map[string]ResourceProviderFactory
|
||||
Variables map[string]string
|
||||
}
|
||||
|
||||
// NewContext creates a new context.
|
||||
//
|
||||
// Once a context is created, the pointer values within ContextOpts should
|
||||
// not be mutated in any way, since the pointers are copied, not the values
|
||||
// themselves.
|
||||
func NewContext(opts *ContextOpts) *Context {
|
||||
return &Context{
|
||||
config: opts.Config,
|
||||
diff: opts.Diff,
|
||||
hooks: opts.Hooks,
|
||||
state: opts.State,
|
||||
providers: opts.Providers,
|
||||
variables: opts.Variables,
|
||||
}
|
||||
}
|
||||
|
||||
// Validate validates the configuration and returns any warnings or errors.
|
||||
func (c *Context) Validate() ([]string, []error) {
|
||||
var rerr *multierror.Error
|
||||
|
||||
// Validate the configuration itself
|
||||
if err := c.config.Validate(); err != nil {
|
||||
rerr = multierror.ErrorAppend(rerr, err)
|
||||
}
|
||||
|
||||
// Validate the user variables
|
||||
if errs := smcUserVariables(c.config, c.variables); len(errs) > 0 {
|
||||
rerr = multierror.ErrorAppend(rerr, errs...)
|
||||
}
|
||||
|
||||
var errs []error
|
||||
if rerr != nil && len(rerr.Errors) > 0 {
|
||||
errs = rerr.Errors
|
||||
}
|
||||
|
||||
return nil, errs
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestContextValidate(t *testing.T) {
|
||||
config := testConfig(t, "validate-good")
|
||||
c := testContext(t, &ContextOpts{
|
||||
Config: config,
|
||||
})
|
||||
|
||||
w, e := c.Validate()
|
||||
if len(w) > 0 {
|
||||
t.Fatalf("bad: %#v", w)
|
||||
}
|
||||
if len(e) > 0 {
|
||||
t.Fatalf("bad: %#v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextValidate_badVar(t *testing.T) {
|
||||
config := testConfig(t, "validate-bad-var")
|
||||
c := testContext(t, &ContextOpts{
|
||||
Config: config,
|
||||
})
|
||||
|
||||
w, e := c.Validate()
|
||||
if len(w) > 0 {
|
||||
t.Fatalf("bad: %#v", w)
|
||||
}
|
||||
if len(e) == 0 {
|
||||
t.Fatalf("bad: %#v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextValidate_requiredVar(t *testing.T) {
|
||||
config := testConfig(t, "validate-required-var")
|
||||
c := testContext(t, &ContextOpts{
|
||||
Config: config,
|
||||
})
|
||||
|
||||
w, e := c.Validate()
|
||||
if len(w) > 0 {
|
||||
t.Fatalf("bad: %#v", w)
|
||||
}
|
||||
if len(e) == 0 {
|
||||
t.Fatalf("bad: %#v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func testContext(t *testing.T, opts *ContextOpts) *Context {
|
||||
return NewContext(opts)
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MultiError is an error type to track multiple errors. This is used to
|
||||
// accumulate errors in cases such as configuration parsing, and returning
|
||||
// them as a single error.
|
||||
type MultiError struct {
|
||||
Errors []error
|
||||
}
|
||||
|
||||
func (e *MultiError) Error() string {
|
||||
points := make([]string, len(e.Errors))
|
||||
for i, err := range e.Errors {
|
||||
points[i] = fmt.Sprintf("* %s", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"%d error(s) occurred:\n\n%s",
|
||||
len(e.Errors), strings.Join(points, "\n"))
|
||||
}
|
||||
|
||||
// MultiErrorAppend is a helper function that will append more errors
|
||||
// onto a MultiError in order to create a larger multi-error. If the
|
||||
// original error is not a MultiError, it will be turned into one.
|
||||
func MultiErrorAppend(err error, errs ...error) *MultiError {
|
||||
if err == nil {
|
||||
err = new(MultiError)
|
||||
}
|
||||
|
||||
switch err := err.(type) {
|
||||
case *MultiError:
|
||||
if err == nil {
|
||||
err = new(MultiError)
|
||||
}
|
||||
|
||||
err.Errors = append(err.Errors, errs...)
|
||||
return err
|
||||
default:
|
||||
newErrs := make([]error, len(errs)+1)
|
||||
newErrs[0] = err
|
||||
copy(newErrs[1:], errs)
|
||||
return &MultiError{
|
||||
Errors: newErrs,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,56 +0,0 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMultiError_Impl(t *testing.T) {
|
||||
var raw interface{}
|
||||
raw = &MultiError{}
|
||||
if _, ok := raw.(error); !ok {
|
||||
t.Fatal("MultiError must implement error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiErrorError(t *testing.T) {
|
||||
expected := `2 error(s) occurred:
|
||||
|
||||
* foo
|
||||
* bar`
|
||||
|
||||
errors := []error{
|
||||
errors.New("foo"),
|
||||
errors.New("bar"),
|
||||
}
|
||||
|
||||
multi := &MultiError{errors}
|
||||
if multi.Error() != expected {
|
||||
t.Fatalf("bad: %s", multi.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiErrorAppend_MultiError(t *testing.T) {
|
||||
original := &MultiError{
|
||||
Errors: []error{errors.New("foo")},
|
||||
}
|
||||
|
||||
result := MultiErrorAppend(original, errors.New("bar"))
|
||||
if len(result.Errors) != 2 {
|
||||
t.Fatalf("wrong len: %d", len(result.Errors))
|
||||
}
|
||||
|
||||
original = &MultiError{}
|
||||
result = MultiErrorAppend(original, errors.New("bar"))
|
||||
if len(result.Errors) != 1 {
|
||||
t.Fatalf("wrong len: %d", len(result.Errors))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiErrorAppend_NonMultiError(t *testing.T) {
|
||||
original := errors.New("foo")
|
||||
result := MultiErrorAppend(original, errors.New("bar"))
|
||||
if len(result.Errors) != 2 {
|
||||
t.Fatalf("wrong len: %d", len(result.Errors))
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
resource "aws_instance" "foo" {
|
||||
num = "2"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "${var.foo}"
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
resource "aws_instance" "foo" {
|
||||
num = "2"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "bar"
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
variable "foo" {}
|
||||
|
||||
resource "aws_instance" "web" {
|
||||
ami = "${var.foo}"
|
||||
}
|
||||
Loading…
Reference in new issue