diff --git a/terraform/context.go b/terraform/context.go index 41f5e2a36..8d49d3859 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -528,8 +528,9 @@ func (c *Context) Plan() (*Plan, error) { var err error if c.destroy && X_newDestroy { graph, err = (&DestroyPlanGraphBuilder{ - Module: c.module, - State: c.state, + Module: c.module, + State: c.state, + Targets: c.targets, }).Build(RootModulePath) } else { graph, err = c.Graph(&ContextGraphOpts{Validate: true}) diff --git a/terraform/graph_builder_destroy_plan.go b/terraform/graph_builder_destroy_plan.go index 64a7d1c2f..2b1444fd9 100644 --- a/terraform/graph_builder_destroy_plan.go +++ b/terraform/graph_builder_destroy_plan.go @@ -16,6 +16,9 @@ type DestroyPlanGraphBuilder struct { // State is the current state State *State + + // Targets are resources to target + Targets []string } // See GraphBuilder @@ -41,6 +44,9 @@ func (b *DestroyPlanGraphBuilder) Steps() []GraphTransformer { State: b.State, }, + // Target + &TargetsTransformer{Targets: b.Targets}, + // Attach the configuration to any resources &AttachResourceConfigTransformer{Module: b.Module}, diff --git a/terraform/graph_config_node.go b/terraform/graph_config_node.go index ea4645d72..57d565ca2 100644 --- a/terraform/graph_config_node.go +++ b/terraform/graph_config_node.go @@ -24,8 +24,6 @@ type graphNodeConfig interface { // configuration graph need to implement in order to be be addressed / targeted // properly. type GraphNodeAddressable interface { - graphNodeConfig - ResourceAddress() *ResourceAddress } @@ -35,7 +33,5 @@ type GraphNodeAddressable interface { // provided will contain every target provided, and each implementing graph // node must filter this list to targets considered relevant. type GraphNodeTargetable interface { - GraphNodeAddressable - SetTargets([]ResourceAddress) } diff --git a/terraform/node_resource_abstract.go b/terraform/node_resource_abstract.go index cddccaef2..9ba303a6e 100644 --- a/terraform/node_resource_abstract.go +++ b/terraform/node_resource_abstract.go @@ -28,6 +28,8 @@ type NodeAbstractResource struct { Config *config.Resource // Config is the resource in the config ResourceState *ResourceState // ResourceState is the ResourceState for this + + Targets []ResourceAddress // Set from GraphNodeTargetable } func (n *NodeAbstractResource) Name() string { @@ -111,6 +113,16 @@ func (n *NodeAbstractResource) ResourceAddr() *ResourceAddress { return n.Addr } +// GraphNodeAddressable, TODO: remove, used by target, should unify +func (n *NodeAbstractResource) ResourceAddress() *ResourceAddress { + return n.ResourceAddr() +} + +// GraphNodeTargetable +func (n *NodeAbstractResource) SetTargets(targets []ResourceAddress) { + n.Targets = targets +} + // GraphNodeAttachResourceState func (n *NodeAbstractResource) AttachResourceState(s *ResourceState) { n.ResourceState = s diff --git a/terraform/node_resource_destroy.go b/terraform/node_resource_destroy.go index 52c34d1c7..8aed56381 100644 --- a/terraform/node_resource_destroy.go +++ b/terraform/node_resource_destroy.go @@ -63,6 +63,11 @@ func (n *NodeDestroyResource) DynamicExpand(ctx EvalContext) (*Graph, error) { View: n.Config.Id(), }) + // Target + steps = append(steps, &TargetsTransformer{ + ParsedTargets: n.Targets, + }) + // Always end with the root being added steps = append(steps, &RootTransformer{}) diff --git a/terraform/transform_targets.go b/terraform/transform_targets.go index 84393796e..0ba98ee15 100644 --- a/terraform/transform_targets.go +++ b/terraform/transform_targets.go @@ -28,8 +28,10 @@ func (t *TargetsTransformer) Transform(g *Graph) error { if err != nil { return err } + t.ParsedTargets = addrs } + if len(t.ParsedTargets) > 0 { targetedNodes, err := t.selectTargetedNodes(g, t.ParsedTargets) if err != nil { @@ -50,6 +52,7 @@ func (t *TargetsTransformer) Transform(g *Graph) error { } } } + return nil } @@ -62,6 +65,7 @@ func (t *TargetsTransformer) parseTargetAddresses() ([]ResourceAddress, error) { } addrs[i] = *ta } + return addrs, nil } @@ -107,6 +111,7 @@ func (t *TargetsTransformer) selectTargetedNodes( } } } + return targetedNodes, nil } @@ -116,12 +121,14 @@ func (t *TargetsTransformer) nodeIsTarget( if !ok { return false } + addr := r.ResourceAddress() for _, targetAddr := range addrs { if targetAddr.Equals(addr) { return true } } + return false }