Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: -target option to also select resources in descendant modules #15314

Merged
merged 2 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8719,3 +8719,45 @@ func TestContext2Apply_multiRef(t *testing.T) {
t.Fatalf("expected 1 depends_on entry for aws_instance.create, got %q", deps)
}
}

func TestContext2Apply_targetedModuleRecursive(t *testing.T) {
m := testModule(t, "apply-targeted-module-recursive")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Targets: []string{"module.child"},
})

if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}

state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}

mod := state.ModuleByPath([]string{"root", "child", "subchild"})
if mod == nil {
t.Fatalf("no subchild module found in the state!\n\n%#v", state)
}
if len(mod.Resources) != 1 {
t.Fatalf("expected 1 resources, got: %#v", mod.Resources)
}

checkStateString(t, state, `
<no state>
module.child.subchild:
aws_instance.foo:
ID = foo
num = 2
type = aws_instance
`)
}
10 changes: 9 additions & 1 deletion terraform/graph_builder_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ func (b *PlanGraphBuilder) Steps() []GraphTransformer {
&CountBoundaryTransformer{},

// Target
&TargetsTransformer{Targets: b.Targets},
&TargetsTransformer{
Targets: b.Targets,

// Resource nodes from config have not yet been expanded for
// "count", so we must apply targeting without indices. Exact
// targeting will be dealt with later when these resources
// DynamicExpand.
IgnoreIndices: true,
},

// Close opened plugin connections
&CloseProviderTransformer{},
Expand Down
10 changes: 9 additions & 1 deletion terraform/graph_builder_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,15 @@ func (b *RefreshGraphBuilder) Steps() []GraphTransformer {
&ReferenceTransformer{},

// Target
&TargetsTransformer{Targets: b.Targets},
&TargetsTransformer{
Targets: b.Targets,

// Resource nodes from config have not yet been expanded for
// "count", so we must apply targeting without indices. Exact
// targeting will be dealt with later when these resources
// DynamicExpand.
IgnoreIndices: true,
},

// Close opened plugin connections
&CloseProviderTransformer{},
Expand Down
49 changes: 48 additions & 1 deletion terraform/resource_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,53 @@ func ParseResourceAddress(s string) (*ResourceAddress, error) {
}, nil
}

// Contains returns true if and only if the given node is contained within
// the receiver.
//
// Containment is defined in terms of the module and resource heirarchy:
// a resource is contained within its module and any ancestor modules,
// an indexed resource instance is contained with the unindexed resource, etc.
func (addr *ResourceAddress) Contains(other *ResourceAddress) bool {
ourPath := addr.Path
givenPath := other.Path
if len(givenPath) < len(ourPath) {
return false
}
for i := range ourPath {
if ourPath[i] != givenPath[i] {
return false
}
}

// If the receiver is a whole-module address then the path prefix
// matching is all we need.
if !addr.HasResourceSpec() {
return true
}

if addr.Type != other.Type || addr.Name != other.Name || addr.Mode != other.Mode {
return false
}

if addr.Index != -1 && addr.Index != other.Index {
return false
}

if addr.InstanceTypeSet && (addr.InstanceTypeSet != other.InstanceTypeSet || addr.InstanceType != other.InstanceType) {
return false
}

return true
}

// Equals returns true if the receiver matches the given address.
//
// The name of this method is a misnomer, since it doesn't test for exact
// equality. Instead, it tests that the _specified_ parts of each
// address match, treating any unspecified parts as wildcards.
//
// See also Contains, which takes a more heirarchical approach to comparing
// addresses.
func (addr *ResourceAddress) Equals(raw interface{}) bool {
other, ok := raw.(*ResourceAddress)
if !ok {
Expand Down Expand Up @@ -324,7 +371,7 @@ func tokenizeResourceAddress(s string) (map[string]string, error) {
// string "aws_instance.web.tainted[1]"
re := regexp.MustCompile(`\A` +
// "module.foo.module.bar" (optional)
`(?P<path>(?:module\.[^.]+\.?)*)` +
`(?P<path>(?:module\.(?P<module_name>[^.]+)\.?)*)` +
// possibly "data.", if targeting is a data resource
`(?P<data_prefix>(?:data\.)?)` +
// "aws_instance.web" (optional when module path specified)
Expand Down
Loading