feat(provisioner): associate resources with `coder_devcontainer` by DanielleMaywood · Pull Request #21602 · coder/coder

We could do the following:

Introduce a new function resourceDependsOn

// resourceDependsOn checks if a resource depends on a target entity (agent or devcontainer).
// During Plan phase (when IDs aren't available), it uses graph edge analysis.
// During Provision phase (when IDs are present), it compares IDs directly.
func resourceDependsOn(graph *gographviz.Graph, resource *tfjson.StateResource, resourceTargetID, targetType, targetName, targetID string) bool {
	// Provision: IDs available, compare directly
	if targetID != "" || resourceTargetID != "" {
		return targetID == resourceTargetID
	}

	// Plan: IDs not yet available, check graph edges
	resourceNodeSuffix := fmt.Sprintf(`] %s.%s (expand)"`, resource.Type, resource.Name)
	targetNodeSuffix := fmt.Sprintf(`] %s.%s (expand)"`, targetType, targetName)

	for _, dst := range graph.Edges.SrcToDsts {
		for _, edges := range dst {
			for _, edge := range edges {
				if strings.HasSuffix(edge.Src, resourceNodeSuffix) &&
					strings.HasSuffix(edge.Dst, targetNodeSuffix) {
					return true
				}
			}
		}
	}
	return false
}

As well as some constants

// Terraform resource type constants for the Coder provider.
const (
	tfCoderAgent         = "coder_agent"
	tfCoderDevcontainer  = "coder_devcontainer"
	...
)

And then the callsites could look like

resourceDependsOn(graph, resource, attrs.AgentID, tfCoderAgent, agent.Name, agent.Id)
resourceDependsOn(graph, resource, attrs.AgentID, tfCoderDevcontainer, dc.Name, dc.SubagentId)