Skip to content

Commit c2f045f

Browse files
Deep FuriyaZee2413
authored andcommitted
updated resource state importer to have custom logical id if called from related resources workflow
1 parent 3f40e33 commit c2f045f

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/resourceState/ResourceStateImporter.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class ResourceStateImporter {
5353

5454
@Measure({ name: 'importResourceState' })
5555
public async importResourceState(params: ResourceStateParams): Promise<ResourceStateResult> {
56-
const { resourceSelections, textDocument, purpose } = params;
56+
const { resourceSelections, textDocument, purpose, parentResourceType } = params;
5757

5858
this.telemetry.count(`purpose.${purpose.toLowerCase()}`, 1);
5959

@@ -75,6 +75,7 @@ export class ResourceStateImporter {
7575
resourceSelections,
7676
syntaxTree,
7777
purpose,
78+
parentResourceType,
7879
);
7980

8081
this.recordStateFetchMetrics(resourceSelections, importResult);
@@ -152,6 +153,7 @@ export class ResourceStateImporter {
152153
resourceSelections: ResourceSelection[],
153154
syntaxTree: SyntaxTree,
154155
purpose: ResourceStatePurpose,
156+
parentResourceType?: string,
155157
): Promise<{ fetchedResourceStates: ResourceTemplateFormat[]; importResult: ResourceStateResult }> {
156158
const fetchedResourceStates: ResourceTemplateFormat[] = [];
157159
const importResult: ResourceStateResult = {
@@ -181,6 +183,7 @@ export class ResourceStateImporter {
181183
resourceIdentifier,
182184
syntaxTree,
183185
generatedLogicalIds,
186+
parentResourceType,
184187
);
185188
generatedLogicalIds.add(logicalId);
186189
fetchedResourceStates.push({
@@ -214,6 +217,7 @@ export class ResourceStateImporter {
214217
resourceIdentifier: string,
215218
syntaxTree: SyntaxTree,
216219
idsAlreadyGenerated?: Set<string>,
220+
parentResourceType?: string,
217221
): string {
218222
const entities = getEntityMap(syntaxTree, TopLevelSection.Resources);
219223
const existingLogicalIds = new Set<string>(entities?.keys());
@@ -225,12 +229,28 @@ export class ResourceStateImporter {
225229
}
226230
}
227231

228-
return this.generateLogicalId(resourceType, resourceIdentifier, existingLogicalIds);
232+
return this.generateLogicalId(resourceType, resourceIdentifier, existingLogicalIds, parentResourceType);
229233
}
230234

231-
private generateLogicalId(resourceType: string, identifier: string, existingLogicalIds?: Set<string>): string {
235+
private generateLogicalId(
236+
resourceType: string,
237+
identifier: string,
238+
existingLogicalIds?: Set<string>,
239+
parentResourceType?: string,
240+
): string {
232241
const parts = resourceType.split('::');
233-
const baseName = parts.length >= 3 ? parts[1] + parts[2] : parts[parts.length - 1];
242+
const resourceTypeName = parts.length >= 3 ? parts[1] + parts[2] : parts[parts.length - 1];
243+
244+
let baseName: string;
245+
if (parentResourceType) {
246+
// Generate relationship-aware name like "IAMRoleRelatedToS3Bucket"
247+
const parentParts = parentResourceType.split('::');
248+
const parentTypeName =
249+
parentParts.length >= 3 ? parentParts[1] + parentParts[2] : parentParts[parentParts.length - 1];
250+
baseName = `${resourceTypeName}RelatedTo${parentTypeName}`;
251+
} else {
252+
baseName = resourceTypeName;
253+
}
234254

235255
if (!existingLogicalIds?.has(baseName)) {
236256
return baseName;

src/resourceState/ResourceStateTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface ResourceStateParams {
3131
textDocument: TextDocumentIdentifier;
3232
resourceSelections?: ResourceSelection[];
3333
purpose: ResourceStatePurpose;
34+
parentResourceType?: string;
3435
}
3536

3637
export interface ResourceStateResult {

0 commit comments

Comments
 (0)