Skip to content

Commit ca296ec

Browse files
committed
handle duplicate TF IDs
1 parent 4e4ad40 commit ca296ec

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

provisioner/terraform/resources.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
185185
tfResourcesByLabel := map[string]map[string]*tfjson.StateResource{}
186186

187187
// Map resource IDs to labels for efficient lookup when processing metadata
188-
labelByResourceID := map[string]string{}
188+
// Multiple resources can have the same ID, so we store a slice of labels
189+
labelsByResourceID := map[string][]string{}
189190

190191
// Extra array to preserve the order of rich parameters.
191192
tfResourcesRichParameters := make([]*tfjson.StateResource, 0)
@@ -209,10 +210,10 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
209210
}
210211
tfResourcesByLabel[label][resource.Address] = resource
211212

212-
// Build the ID to label map
213+
// Build the ID to labels map - multiple resources can have the same ID
213214
if idAttr, hasID := resource.AttributeValues["id"]; hasID {
214215
if idStr, ok := idAttr.(string); ok && idStr != "" {
215-
labelByResourceID[idStr] = label
216+
labelsByResourceID[idStr] = append(labelsByResourceID[idStr], label)
216217
}
217218
}
218219
}
@@ -662,9 +663,16 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
662663
// First, check if ResourceID is provided and try to find the resource by ID
663664
if attrs.ResourceID != "" {
664665
// Look for a resource with matching ID
665-
foundLabel, foundByID := labelByResourceID[attrs.ResourceID]
666-
if foundByID {
667-
targetLabel = foundLabel
666+
foundLabels := labelsByResourceID[attrs.ResourceID]
667+
if len(foundLabels) == 1 {
668+
// Single match - use it
669+
targetLabel = foundLabels[0]
670+
} else if len(foundLabels) > 1 {
671+
// Multiple resources with same ID - this creates ambiguity
672+
logger.Warn(ctx, "multiple resources found with same resource_id, falling back to graph traversal",
673+
slog.F("resource_id", attrs.ResourceID),
674+
slog.F("metadata_address", resource.Address),
675+
slog.F("matching_labels", foundLabels))
668676
} else {
669677
// If we couldn't find by ID, fall back to graph traversal
670678
logger.Warn(ctx, "coder_metadata resource_id not found, falling back to graph traversal",

0 commit comments

Comments
 (0)