@@ -427,9 +427,11 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
427
427
for _ , agents := range resourceAgents {
428
428
for _ , agent := range agents {
429
429
// Find agents with the matching ID and associate them!
430
- if agent .Id != attrs .AgentID {
430
+
431
+ if ! dependsOnAgent (graph , agent , attrs .AgentID , resource ) {
431
432
continue
432
433
}
434
+
433
435
agent .Apps = append (agent .Apps , & proto.App {
434
436
Slug : attrs .Slug ,
435
437
DisplayName : attrs .DisplayName ,
@@ -748,6 +750,30 @@ func convertAddressToLabel(address string) string {
748
750
return cut
749
751
}
750
752
753
+ func dependsOnAgent (graph * gographviz.Graph , agent * proto.Agent , appAgentID string , resource * tfjson.StateResource ) bool {
754
+ // Plan: we need to find if there is edge between the agent and the app.
755
+ if agent .Id == "" && appAgentID == "" {
756
+ appNodeSuffix := fmt .Sprintf (`] coder_app.%s (expand)"` , resource .Name )
757
+ agentNodeSuffix := fmt .Sprintf (`] coder_agent.%s (expand)"` , agent .Name )
758
+
759
+ // Traverse the graph to check if the coder_app depends on coder_agent.
760
+ for _ , dst := range graph .Edges .SrcToDsts {
761
+ for _ , edges := range dst {
762
+ for _ , edge := range edges {
763
+ if strings .HasSuffix (edge .Src , appNodeSuffix ) &&
764
+ strings .HasSuffix (edge .Dst , agentNodeSuffix ) {
765
+ return true
766
+ }
767
+ }
768
+ }
769
+ }
770
+ return false
771
+ }
772
+
773
+ // Provision: agent ID and app ID are present
774
+ return agent .Id == appAgentID
775
+ }
776
+
751
777
type graphResource struct {
752
778
Label string
753
779
Depth uint
0 commit comments