Skip to content

Commit

Permalink
WIP2
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Nov 15, 2024
1 parent f42f6fc commit dd64dd9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 deletions.
29 changes: 28 additions & 1 deletion internal/terraform/context_plan2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6098,6 +6098,11 @@ resource "test_resource" "test" {
resource "test_resource" "test2" {
attr = module.mod.new
}
resource "test_resource" "test3" {
attr = module.mod.old
}
`,
})

Expand All @@ -6122,5 +6127,27 @@ resource "test_resource" "test2" {
})

_, diags := ctx.Plan(m, states.NewState(), SimplePlanOpts(plans.NormalMode, testInputValuesUnset(m.Module.Variables)))
assertDiagnosticsMatch(t, diags, []tfdiags.Diagnostic{})
var expectedDiags tfdiags.Diagnostics
expectedDiags = expectedDiags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 7, Column: 12, Byte: 85},
End: hcl.Pos{Line: 7, Column: 26, Byte: 99},
},
},
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 15, Column: 12, Byte: 213},
End: hcl.Pos{Line: 15, Column: 26, Byte: 227},
},
})

assertDiagnosticsMatch(t, diags, expectedDiags)
}
18 changes: 18 additions & 0 deletions internal/terraform/node_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type nodeExpandOutput struct {
Overrides *mocking.Overrides

Dependencies []addrs.ConfigResource

Dependants []*addrs.Reference
}

var (
Expand Down Expand Up @@ -136,6 +138,7 @@ func (n *nodeExpandOutput) DynamicExpand(ctx EvalContext) (*Graph, tfdiags.Diagn
Planning: n.Planning,
Override: n.getOverrideValue(absAddr.Module),
Dependencies: n.Dependencies,
Dependants: n.Dependants,
}
}

Expand Down Expand Up @@ -283,6 +286,8 @@ type NodeApplyableOutput struct {
// Dependencies is the full set of resources that are referenced by this
// output.
Dependencies []addrs.ConfigResource

Dependants []*addrs.Reference
}

var (
Expand Down Expand Up @@ -379,6 +384,19 @@ func (n *NodeApplyableOutput) References() []*addrs.Reference {

// GraphNodeExecutable
func (n *NodeApplyableOutput) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {

if n.Config.Deprecated && len(n.Dependants) > 0 {
for _, d := range n.Dependants {

diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: n.Config.DeprecatedMessage,
Subject: d.SourceRange.ToHCL().Ptr(),
})
}
}

state := ctx.State()
if state == nil {
return
Expand Down
24 changes: 0 additions & 24 deletions internal/terraform/node_resource_abstract.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,30 +566,6 @@ func (n *NodeAbstractResource) readResourceInstanceStateDeposed(ctx EvalContext,
return obj, diags
}

func (n *NodeAbstractResource) validateUsageOfDeprecatedOutputs(ctx EvalContext) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics

fmt.Printf("\n\t n.References() --> %#v \n", n.References())
for _, ref := range n.References() {
fmt.Printf("\n\t ref --> %#v \n", ref)
if out, ok := ref.Subject.(addrs.ModuleCallInstanceOutput); ok {
fmt.Printf("\n\t out --> %#v \n", out)

// TODO: Find the configuration for the output
// Check if the output is deprecated and emit this diagnostic

// diags = diags.Append(&hcl.Diagnostic{
// Severity: hcl.DiagWarning,
// Summary: "Usage of deprecated output",
// Detail: n.Config.DeprecatedMessage,
// Subject: ref.SourceRange.ToHCL().Ptr(),
// })
}
}

return diags
}

// graphNodesAreResourceInstancesInDifferentInstancesOfSameModule is an
// annoyingly-task-specific helper function that returns true if and only if
// the following conditions hold:
Expand Down
10 changes: 3 additions & 7 deletions internal/terraform/node_resource_plan_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,14 @@ var (
func (n *NodePlannableResourceInstance) Execute(ctx EvalContext, op walkOperation) tfdiags.Diagnostics {
addr := n.ResourceInstanceAddr()

var diags tfdiags.Diagnostics
// TODO: Find better place
diags = diags.Append(diags, n.validateUsageOfDeprecatedOutputs(ctx))

// Eval info is different depending on what kind of resource this is
switch addr.Resource.Resource.Mode {
case addrs.ManagedResourceMode:
return diags.Append(diags, n.managedResourceExecute(ctx))
return n.managedResourceExecute(ctx)
case addrs.DataResourceMode:
return diags.Append(diags, n.dataResourceExecute(ctx))
return n.dataResourceExecute(ctx)
case addrs.EphemeralResourceMode:
return diags.Append(diags, n.ephemeralResourceExecute(ctx))
return n.ephemeralResourceExecute(ctx)
default:
panic(fmt.Errorf("unsupported resource mode %s", n.Config.Mode))
}
Expand Down
2 changes: 0 additions & 2 deletions internal/terraform/node_resource_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,6 @@ func (n *NodeValidatableResource) validateResource(ctx EvalContext) tfdiags.Diag
diags = diags.Append(resp.Diagnostics.InConfigBody(n.Config.Config, n.Addr.String()))
}

diags = diags.Append(diags, n.validateUsageOfDeprecatedOutputs(ctx))

return diags
}

Expand Down
1 change: 1 addition & 0 deletions internal/terraform/transform_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (t *OutputTransformer) transform(g *Graph, c *configs.Config) error {
RefreshOnly: t.RefreshOnly,
Planning: t.Planning,
Overrides: t.Overrides,
Dependants: []*addrs.Reference{},
}

log.Printf("[TRACE] OutputTransformer: adding %s as %T", o.Name, node)
Expand Down
8 changes: 8 additions & 0 deletions internal/terraform/transform_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ func (t *ReferenceTransformer) Transform(g *Graph) error {

if !graphNodesAreResourceInstancesInDifferentInstancesOfSameModule(v, parent) {
g.Connect(dag.BasicEdge(v, parent))

// TODO: Refactor to a custom transformer
// If v is an output, save the reference to the output
if output, ok := parent.(*nodeExpandOutput); ok {
if dependant, ok := v.(GraphNodeReferencer); ok {
output.Dependants = append(output.Dependants, dependant.References()...)
}
}
} else {
log.Printf("[TRACE] ReferenceTransformer: skipping %s => %s inter-module-instance dependency", dag.VertexName(v), dag.VertexName(parent))
}
Expand Down

0 comments on commit dd64dd9

Please sign in to comment.