@@ -390,7 +390,7 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
390
390
}
391
391
}
392
392
393
- agents [resource .Address ] = agent
393
+ agents [convertAddressToLabel ( resource .Address ) ] = agent
394
394
}
395
395
396
396
for _ , resource := range tfResources {
@@ -400,11 +400,10 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
400
400
if resource .Type == "coder_agent" || resource .Type == "coder_agent_instance" {
401
401
continue
402
402
}
403
- resourceKey := strings .Join ([]string {resource .Type , resource .Name }, "." )
404
403
resources = append (resources , & proto.Resource {
405
404
Name : resource .Name ,
406
405
Type : resource .Type ,
407
- Agents : findAgents (resourceDependencies , agents , resourceKey ),
406
+ Agents : findAgents (resourceDependencies , agents , convertAddressToLabel ( resource . Address ) ),
408
407
})
409
408
}
410
409
@@ -485,8 +484,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
485
484
default :
486
485
agent .Auth = & proto.Agent_InstanceId {}
487
486
}
488
- resourceKey := strings .Join ([]string {resource .Type , resource .Name }, "." )
489
- agents [resourceKey ] = agent
487
+ agents [convertAddressToLabel (resource .Address )] = agent
490
488
}
491
489
492
490
// Manually associate agents with instance IDs.
@@ -529,8 +527,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
529
527
if resource .Type == "coder_agent" || resource .Type == "coder_agent_instance" {
530
528
continue
531
529
}
532
- resourceKey := strings .Join ([]string {resource .Type , resource .Name }, "." )
533
- resourceAgents := findAgents (resourceDependencies , agents , resourceKey )
530
+ resourceAgents := findAgents (resourceDependencies , agents , convertAddressToLabel (resource .Address ))
534
531
for _ , agent := range resourceAgents {
535
532
// Didn't use instance identity.
536
533
if agent .GetToken () != "" {
@@ -696,8 +693,8 @@ func findDependenciesWithLabels(graph *gographviz.Graph, nodeName string) []stri
696
693
// findAgents recursively searches through resource dependencies
697
694
// to find associated agents. Nested is required for indirect
698
695
// dependency matching.
699
- func findAgents (resourceDependencies map [string ][]string , agents map [string ]* proto.Agent , resourceKey string ) []* proto.Agent {
700
- resourceNode , exists := resourceDependencies [resourceKey ]
696
+ func findAgents (resourceDependencies map [string ][]string , agents map [string ]* proto.Agent , resourceLabel string ) []* proto.Agent {
697
+ resourceNode , exists := resourceDependencies [resourceLabel ]
701
698
if ! exists {
702
699
return []* proto.Agent {}
703
700
}
@@ -714,3 +711,9 @@ func findAgents(resourceDependencies map[string][]string, agents map[string]*pro
714
711
}
715
712
return resourceAgents
716
713
}
714
+
715
+ // convertAddressToLabel returns the Terraform address without the count
716
+ // specifier. eg. "module.ec2_dev.ec2_instance.dev[0]" becomes "module.ec2_dev.ec2_instance.dev"
717
+ func convertAddressToLabel (address string ) string {
718
+ return strings .Split (address , "[" )[0 ]
719
+ }
0 commit comments