github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/terraform/node_resource_apply.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/muratcelep/terraform/not-internal/addrs"
     7  	"github.com/muratcelep/terraform/not-internal/dag"
     8  	"github.com/muratcelep/terraform/not-internal/lang"
     9  	"github.com/muratcelep/terraform/not-internal/tfdiags"
    10  )
    11  
    12  // nodeExpandApplyableResource handles the first layer of resource
    13  // expansion during apply. Even though the resource instances themselves are
    14  // already expanded from the plan, we still need to expand the
    15  // NodeApplyableResource nodes into their respective modules.
    16  type nodeExpandApplyableResource struct {
    17  	*NodeAbstractResource
    18  }
    19  
    20  var (
    21  	_ GraphNodeDynamicExpandable    = (*nodeExpandApplyableResource)(nil)
    22  	_ GraphNodeReferenceable        = (*nodeExpandApplyableResource)(nil)
    23  	_ GraphNodeReferencer           = (*nodeExpandApplyableResource)(nil)
    24  	_ GraphNodeConfigResource       = (*nodeExpandApplyableResource)(nil)
    25  	_ GraphNodeAttachResourceConfig = (*nodeExpandApplyableResource)(nil)
    26  	_ graphNodeExpandsInstances     = (*nodeExpandApplyableResource)(nil)
    27  	_ GraphNodeTargetable           = (*nodeExpandApplyableResource)(nil)
    28  )
    29  
    30  func (n *nodeExpandApplyableResource) expandsInstances() {
    31  }
    32  
    33  func (n *nodeExpandApplyableResource) References() []*addrs.Reference {
    34  	return (&NodeApplyableResource{NodeAbstractResource: n.NodeAbstractResource}).References()
    35  }
    36  
    37  func (n *nodeExpandApplyableResource) Name() string {
    38  	return n.NodeAbstractResource.Name() + " (expand)"
    39  }
    40  
    41  func (n *nodeExpandApplyableResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
    42  	var g Graph
    43  
    44  	expander := ctx.InstanceExpander()
    45  	moduleInstances := expander.ExpandModule(n.Addr.Module)
    46  	for _, module := range moduleInstances {
    47  		g.Add(&NodeApplyableResource{
    48  			NodeAbstractResource: n.NodeAbstractResource,
    49  			Addr:                 n.Addr.Resource.Absolute(module),
    50  		})
    51  	}
    52  
    53  	return &g, nil
    54  }
    55  
    56  // NodeApplyableResource represents a resource that is "applyable":
    57  // it may need to have its record in the state adjusted to match configuration.
    58  //
    59  // Unlike in the plan walk, this resource node does not DynamicExpand. Instead,
    60  // it should be inserted into the same graph as any instances of the nodes
    61  // with dependency edges ensuring that the resource is evaluated before any
    62  // of its instances, which will turn ensure that the whole-resource record
    63  // in the state is suitably prepared to receive any updates to instances.
    64  type NodeApplyableResource struct {
    65  	*NodeAbstractResource
    66  
    67  	Addr addrs.AbsResource
    68  }
    69  
    70  var (
    71  	_ GraphNodeModuleInstance       = (*NodeApplyableResource)(nil)
    72  	_ GraphNodeConfigResource       = (*NodeApplyableResource)(nil)
    73  	_ GraphNodeExecutable           = (*NodeApplyableResource)(nil)
    74  	_ GraphNodeProviderConsumer     = (*NodeApplyableResource)(nil)
    75  	_ GraphNodeAttachResourceConfig = (*NodeApplyableResource)(nil)
    76  	_ GraphNodeReferencer           = (*NodeApplyableResource)(nil)
    77  )
    78  
    79  func (n *NodeApplyableResource) Path() addrs.ModuleInstance {
    80  	return n.Addr.Module
    81  }
    82  
    83  func (n *NodeApplyableResource) References() []*addrs.Reference {
    84  	if n.Config == nil {
    85  		log.Printf("[WARN] NodeApplyableResource %q: no configuration, so can't determine References", dag.VertexName(n))
    86  		return nil
    87  	}
    88  
    89  	var result []*addrs.Reference
    90  
    91  	// Since this node type only updates resource-level metadata, we only
    92  	// need to worry about the parts of the configuration that affect
    93  	// our "each mode": the count and for_each meta-arguments.
    94  	refs, _ := lang.ReferencesInExpr(n.Config.Count)
    95  	result = append(result, refs...)
    96  	refs, _ = lang.ReferencesInExpr(n.Config.ForEach)
    97  	result = append(result, refs...)
    98  
    99  	return result
   100  }
   101  
   102  // GraphNodeExecutable
   103  func (n *NodeApplyableResource) Execute(ctx EvalContext, op walkOperation) tfdiags.Diagnostics {
   104  	if n.Config == nil {
   105  		// Nothing to do, then.
   106  		log.Printf("[TRACE] NodeApplyableResource: no configuration present for %s", n.Name())
   107  		return nil
   108  	}
   109  
   110  	return n.writeResourceState(ctx, n.Addr)
   111  }