-
Notifications
You must be signed in to change notification settings - Fork 5
Add error message to resource importer result #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c1fa316
1e49218
4a69797
41d1f66
cde5be8
6ebd0cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,6 +160,7 @@ export class ResourceStateImporter { | |
| completionItem: undefined, | ||
| failedImports: {}, | ||
| successfulImports: {}, | ||
| failureReasons: {}, | ||
| }; | ||
|
|
||
| const generatedLogicalIds = new Set<string>(); | ||
|
|
@@ -176,36 +177,31 @@ export class ResourceStateImporter { | |
| for (const resourceIdentifier of resourceSelection.resourceIdentifiers) { | ||
| try { | ||
| const resourceState = await this.resourceStateManager.getResource(resourceType, resourceIdentifier); | ||
| if (resourceState) { | ||
| this.getOrCreate(importResult.successfulImports, resourceType, []).push(resourceIdentifier); | ||
| const logicalId = this.generateUniqueLogicalId( | ||
| resourceType, | ||
| resourceIdentifier, | ||
| syntaxTree, | ||
| generatedLogicalIds, | ||
| parentResourceType, | ||
| ); | ||
| generatedLogicalIds.add(logicalId); | ||
| fetchedResourceStates.push({ | ||
| [logicalId]: { | ||
| Type: resourceType, | ||
| DeletionPolicy: | ||
| purpose === ResourceStatePurpose.IMPORT ? DeletionPolicyOnImport : undefined, | ||
| Properties: this.applyTransformations( | ||
| resourceState.properties, | ||
| schema, | ||
| purpose, | ||
| logicalId, | ||
| ), | ||
| Metadata: await this.createMetadata(resourceIdentifier, purpose), | ||
| }, | ||
| }); | ||
| } else { | ||
| this.getOrCreate(importResult.failedImports, resourceType, []).push(resourceIdentifier); | ||
| } | ||
| this.getOrCreate(importResult.successfulImports, resourceType, []).push(resourceIdentifier); | ||
| const logicalId = this.generateUniqueLogicalId( | ||
| resourceType, | ||
| resourceIdentifier, | ||
| syntaxTree, | ||
| generatedLogicalIds, | ||
| parentResourceType, | ||
| ); | ||
| generatedLogicalIds.add(logicalId); | ||
| fetchedResourceStates.push({ | ||
| [logicalId]: { | ||
| Type: resourceType, | ||
| DeletionPolicy: | ||
| purpose === ResourceStatePurpose.IMPORT ? DeletionPolicyOnImport : undefined, | ||
| Properties: this.applyTransformations(resourceState.properties, schema, purpose, logicalId), | ||
| Metadata: await this.createMetadata(resourceIdentifier, purpose), | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| log.error(error, `Error importing resource state for ${resourceType} id: ${resourceIdentifier}`); | ||
| this.getOrCreate(importResult.failedImports, resourceType, []).push(resourceIdentifier); | ||
| const errorMessage = error instanceof Error ? error.message : String(error); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
| importResult.failureReasons ??= {}; | ||
| importResult.failureReasons[resourceType] ??= {}; | ||
| importResult.failureReasons[resourceType][resourceIdentifier] = errorMessage; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,13 +59,14 @@ export class ResourceStateManager implements SettingsConfigurable, Closeable { | |
| this.initializeCounters(); | ||
| } | ||
|
|
||
| @Measure({ name: 'getResource', captureErrorType: true }) | ||
| public async getResource(typeName: ResourceType, identifier: ResourceId): Promise<ResourceState | undefined> { | ||
| @Measure({ name: 'getResource' }) | ||
| public async getResource(typeName: ResourceType, identifier: ResourceId): Promise<ResourceState> { | ||
| const cachedResources = this.getResourceState(typeName, identifier); | ||
| if (cachedResources) { | ||
| this.telemetry.count('state.hit', 1); | ||
| return cachedResources; | ||
| } | ||
|
|
||
| this.telemetry.count('state.miss', 1); | ||
|
|
||
| let output: GetResourceCommandOutput | undefined = undefined; | ||
|
|
@@ -75,20 +76,20 @@ export class ResourceStateManager implements SettingsConfigurable, Closeable { | |
| } catch (error) { | ||
| if (error instanceof ResourceNotFoundException) { | ||
| log.info(`No resource found for type ${typeName} and identifier "${identifier}"`); | ||
| return; | ||
| } else { | ||
| log.error(error, `CCAPI GetResource failed for type ${typeName} and identifier "${identifier}"`); | ||
| } | ||
|
|
||
| if (isClientError(error)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this line doing anything anymore? Since there is no more return - it just logs |
||
| log.info(`Client error for type ${typeName} and identifier "${identifier}"`); | ||
| return; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| if (!output?.TypeName || !output?.ResourceDescription?.Identifier || !output?.ResourceDescription?.Properties) { | ||
| log.error( | ||
| throw new Error( | ||
| `GetResource output is missing required fields for type ${typeName} with identifier "${identifier}"`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const value: ResourceState = { | ||
|
|
@@ -136,8 +137,9 @@ export class ResourceStateManager implements SettingsConfigurable, Closeable { | |
| typeName: string, | ||
| identifier: string, | ||
| ): Promise<{ found: boolean; resourceList?: ResourceList }> { | ||
| const resource = await this.getResource(typeName, identifier); | ||
| if (!resource) { | ||
| try { | ||
| await this.getResource(typeName, identifier); | ||
| } catch { | ||
| return { found: false }; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,7 +115,7 @@ describe('ResourceStateCompletionProvider', () => { | |
| }); | ||
|
|
||
| mockComponents.schemaRetriever.getDefault.returns(s3Schemas); | ||
| mockComponents.resourceStateManager.getResource.resolves(undefined); | ||
| mockComponents.resourceStateManager.getResource.resolves(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this being mocked correctly? Isn't the return type of |
||
|
|
||
| const result = await provider.getCompletions(context, mockYamlParams); | ||
|
|
||
|
|
@@ -132,12 +132,7 @@ describe('ResourceStateCompletionProvider', () => { | |
| }); | ||
|
|
||
| mockComponents.schemaRetriever.getDefault.returns(s3Schemas); | ||
| mockComponents.resourceStateManager.getResource.resolves({ | ||
| typeName: 'AWS::S3::Bucket', | ||
| identifier: 'test', | ||
| properties: '', | ||
| createdTimestamp: new Date() as any, | ||
| }); | ||
| mockComponents.resourceStateManager.getResource.rejects(new Error('Invalid resource state')); | ||
|
|
||
| const result = await provider.getCompletions(context, mockYamlParams); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this have to be initialized? the type says its optional