Merge pull request #377 from hashicorp/f-path-var
Add ${path.X} variables for path referencing
This commit is contained in:
commit
e922b16dba
|
@ -201,17 +201,22 @@ func (c *Config) Validate() error {
|
|||
|
||||
// Check that all count variables are valid.
|
||||
for source, vs := range vars {
|
||||
for _, v := range vs {
|
||||
cv, ok := v.(*CountVariable)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if cv.Type == CountValueInvalid {
|
||||
for _, rawV := range vs {
|
||||
switch v := rawV.(type) {
|
||||
case *CountVariable:
|
||||
if v.Type == CountValueInvalid {
|
||||
errs = append(errs, fmt.Errorf(
|
||||
"%s: invalid count variable: %s",
|
||||
source,
|
||||
cv.FullKey()))
|
||||
v.FullKey()))
|
||||
}
|
||||
case *PathVariable:
|
||||
if v.Type == PathValueInvalid {
|
||||
errs = append(errs, fmt.Errorf(
|
||||
"%s: invalid path variable: %s",
|
||||
source,
|
||||
v.FullKey()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -144,6 +144,20 @@ func TestConfigValidate_outputBadField(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_pathVar(t *testing.T) {
|
||||
c := testConfig(t, "validate-path-var")
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatal("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_pathVarInvalid(t *testing.T) {
|
||||
c := testConfig(t, "validate-path-var-invalid")
|
||||
if err := c.Validate(); err == nil {
|
||||
t.Fatal("should not be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_unknownThing(t *testing.T) {
|
||||
c := testConfig(t, "validate-unknownthing")
|
||||
if err := c.Validate(); err == nil {
|
||||
|
|
|
@ -75,6 +75,22 @@ type ModuleVariable struct {
|
|||
key string
|
||||
}
|
||||
|
||||
// A PathVariable is a variable that references path information about the
|
||||
// module.
|
||||
type PathVariable struct {
|
||||
Type PathValueType
|
||||
key string
|
||||
}
|
||||
|
||||
type PathValueType byte
|
||||
|
||||
const (
|
||||
PathValueInvalid PathValueType = iota
|
||||
PathValueCwd
|
||||
PathValueModule
|
||||
PathValueRoot
|
||||
)
|
||||
|
||||
// A ResourceVariable is a variable that is referencing the field
|
||||
// of a resource, such as "${aws_instance.foo.ami}"
|
||||
type ResourceVariable struct {
|
||||
|
@ -101,6 +117,8 @@ type UserVariable struct {
|
|||
func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
|
||||
if strings.HasPrefix(v, "count.") {
|
||||
return NewCountVariable(v)
|
||||
} else if strings.HasPrefix(v, "path.") {
|
||||
return NewPathVariable(v)
|
||||
} else if strings.HasPrefix(v, "var.") {
|
||||
return NewUserVariable(v)
|
||||
} else if strings.HasPrefix(v, "module.") {
|
||||
|
@ -206,6 +224,28 @@ func (v *ModuleVariable) FullKey() string {
|
|||
return v.key
|
||||
}
|
||||
|
||||
func NewPathVariable(key string) (*PathVariable, error) {
|
||||
var fieldType PathValueType
|
||||
parts := strings.SplitN(key, ".", 2)
|
||||
switch parts[1] {
|
||||
case "cwd":
|
||||
fieldType = PathValueCwd
|
||||
case "module":
|
||||
fieldType = PathValueModule
|
||||
case "root":
|
||||
fieldType = PathValueRoot
|
||||
}
|
||||
|
||||
return &PathVariable{
|
||||
Type: fieldType,
|
||||
key: key,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *PathVariable) FullKey() string {
|
||||
return v.key
|
||||
}
|
||||
|
||||
func NewResourceVariable(key string) (*ResourceVariable, error) {
|
||||
parts := strings.SplitN(key, ".", 3)
|
||||
if len(parts) < 3 {
|
||||
|
|
|
@ -45,6 +45,14 @@ func TestNewInterpolatedVariable(t *testing.T) {
|
|||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"path.module",
|
||||
&PathVariable{
|
||||
Type: PathValueModule,
|
||||
key: "path.module",
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
# Hello
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# Hello
|
||||
|
||||
module "bar" {
|
||||
source = "./bar"
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
# Hello
|
||||
|
||||
module "foo" {
|
||||
source = "./foo"
|
||||
}
|
|
@ -67,6 +67,20 @@ func (t *Tree) Config() *config.Config {
|
|||
return t.config
|
||||
}
|
||||
|
||||
// Child returns the child with the given path (by name).
|
||||
func (t *Tree) Child(path []string) *Tree {
|
||||
if len(path) == 0 {
|
||||
return t
|
||||
}
|
||||
|
||||
c := t.Children()[path[0]]
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return c.Child(path[1:])
|
||||
}
|
||||
|
||||
// Children returns the children of this tree (the modules that are
|
||||
// imported by this root).
|
||||
//
|
||||
|
|
|
@ -6,6 +6,42 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestTreeChild(t *testing.T) {
|
||||
storage := testStorage(t)
|
||||
tree := NewTree("", testConfig(t, "child"))
|
||||
if err := tree.Load(storage, GetModeGet); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Should be able to get the root child
|
||||
if c := tree.Child([]string{}); c == nil {
|
||||
t.Fatal("should not be nil")
|
||||
} else if c.Name() != "root" {
|
||||
t.Fatalf("bad: %#v", c.Name())
|
||||
}
|
||||
|
||||
// Should be able to get the root child
|
||||
if c := tree.Child(nil); c == nil {
|
||||
t.Fatal("should not be nil")
|
||||
} else if c.Name() != "root" {
|
||||
t.Fatalf("bad: %#v", c.Name())
|
||||
}
|
||||
|
||||
// Should be able to get the foo child
|
||||
if c := tree.Child([]string{"foo"}); c == nil {
|
||||
t.Fatal("should not be nil")
|
||||
} else if c.Name() != "foo" {
|
||||
t.Fatalf("bad: %#v", c.Name())
|
||||
}
|
||||
|
||||
// Should be able to get the nested child
|
||||
if c := tree.Child([]string{"foo", "bar"}); c == nil {
|
||||
t.Fatal("should not be nil")
|
||||
} else if c.Name() != "bar" {
|
||||
t.Fatalf("bad: %#v", c.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeLoad(t *testing.T) {
|
||||
storage := testStorage(t)
|
||||
tree := NewTree("", testConfig(t, "basic"))
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
resource "aws_instance" "foo" {
|
||||
foo = "${path.nope}"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
resource "aws_instance" "foo" {
|
||||
foo = "${path.module}"
|
||||
}
|
|
@ -3,6 +3,7 @@ package terraform
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -1437,6 +1438,24 @@ func (c *walkContext) computeVars(
|
|||
}
|
||||
|
||||
vs[n] = value
|
||||
case *config.PathVariable:
|
||||
switch v.Type {
|
||||
case config.PathValueCwd:
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
"Couldn't get cwd for var %s: %s",
|
||||
v.FullKey(), err)
|
||||
}
|
||||
|
||||
vs[n] = wd
|
||||
case config.PathValueModule:
|
||||
if t := c.Context.module.Child(c.Path[1:]); t != nil {
|
||||
vs[n] = t.Config().Dir
|
||||
}
|
||||
case config.PathValueRoot:
|
||||
vs[n] = c.Context.module.Config().Dir
|
||||
}
|
||||
case *config.ResourceVariable:
|
||||
var attr string
|
||||
var err error
|
||||
|
|
|
@ -2,6 +2,7 @@ package terraform
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
@ -2822,6 +2823,43 @@ func TestContextPlan_moduleDestroy(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContextPlan_pathVar(t *testing.T) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
m := testModule(t, "plan-path-var")
|
||||
p := testProvider("aws")
|
||||
p.DiffFn = testDiffFn
|
||||
ctx := testContext(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
plan, err := ctx.Plan(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(plan.String())
|
||||
expected := strings.TrimSpace(testTerraformPlanPathVarStr)
|
||||
|
||||
// Warning: this ordering REALLY matters for this test. The
|
||||
// order is: cwd, module, root.
|
||||
expected = fmt.Sprintf(
|
||||
expected,
|
||||
cwd,
|
||||
m.Config().Dir,
|
||||
m.Config().Dir)
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextPlan_diffVar(t *testing.T) {
|
||||
m := testModule(t, "plan-diffvar")
|
||||
p := testProvider("aws")
|
||||
|
|
|
@ -875,3 +875,18 @@ STATE:
|
|||
|
||||
<no state>
|
||||
`
|
||||
|
||||
const testTerraformPlanPathVarStr = `
|
||||
DIFF:
|
||||
|
||||
CREATE: aws_instance.foo
|
||||
cwd: "" => "%s/barpath"
|
||||
module: "" => "%s/foopath"
|
||||
root: "" => "%s/barpath"
|
||||
type: "" => "aws_instance"
|
||||
|
||||
STATE:
|
||||
|
||||
<no state>
|
||||
`
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
resource "aws_instance" "foo" {
|
||||
cwd = "${path.cwd}/barpath"
|
||||
module = "${path.module}/foopath"
|
||||
root = "${path.root}/barpath"
|
||||
}
|
|
@ -39,6 +39,12 @@ For example, `${count.index}` will interpolate the current index
|
|||
in a multi-count resource. For more information on count, see the
|
||||
resource configuration page.
|
||||
|
||||
**To reference path information**, the syntax is `path.TYPE`.
|
||||
TYPE can be `cwd`, `module`, or `root`. `cwd` will interpolate the
|
||||
cwd. `module` will interpolate the path to the current module. `root`
|
||||
will interpolate the path of the root module. In general, you probably
|
||||
want the `path.module` variable.
|
||||
|
||||
## Built-in Functions
|
||||
|
||||
Terraform ships with built-in functions. Functions are called with
|
||||
|
|
|
@ -79,6 +79,32 @@ And that is all there is to it. Variables and outputs are used to configure
|
|||
modules and provide results. Resources within a module are isolated,
|
||||
and the whole thing is managed as a single unit.
|
||||
|
||||
## Paths and Embedded Files
|
||||
|
||||
It is sometimes useful to embed files within the module that aren't
|
||||
Terraform configuration files, such as a script to provision a resource
|
||||
or a file to upload.
|
||||
|
||||
In these cases, you can't use a relative path, since paths in Terraform
|
||||
are generally relative to the working directory that Terraform was executed
|
||||
from. Instead, you want to use a module-relative path. To do this, use
|
||||
the [path interpolated variables](/docs/configuration/interpolation.html).
|
||||
|
||||
An example is shown below:
|
||||
|
||||
```
|
||||
resource "aws_instance" "server" {
|
||||
...
|
||||
|
||||
provisioner "remote-exec" {
|
||||
script = "${path.module}/script.sh"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In the above, we use `${path.module}` to get a module-relative path. This
|
||||
is usually what you'll want in any case.
|
||||
|
||||
## Nested Modules
|
||||
|
||||
You can use a module within a module just like you would anywhere else.
|
||||
|
|
Loading…
Reference in New Issue