Skip to content
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

Bugfix: unknown component definition #115

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [Unreleased]

### Fixed

- Manage unknown component.

## [0.8.1] - 2024/06/19

### Changed
Expand Down
1 change: 1 addition & 0 deletions public/icons/unknown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 14 additions & 6 deletions src/parser/TerraformListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import antlr4 from 'antlr4';
import TerraformVariable from 'src/models/TerraformVariable';
import TerraformComponentAttribute from 'src/models/TerraformComponentAttribute';
import TerraformComponentDefinition from 'src/models/TerraformComponentDefinition';
import TerraformComponent from 'src/models/TerraformComponent';
import TerraformComponentDefinition from 'src/models/TerraformComponentDefinition';

const getText = (ctx) => ctx.getText().replaceAll('"', '').trim();

Expand All @@ -28,6 +28,16 @@ class TerraformListener extends antlr4.tree.ParseTreeListener {
this.idCounter = 1;
}

getUnknownDefinition(type) {
return new TerraformComponentDefinition({
isContainer: false,
model: 'DefaultModel',
icon: 'unknown',
type: type || 'unknown',
blockType: this.currentBlockType,
});
}

addComponent() {
const typeExternalId = `${this.currentComponent.definition.type}.${this.currentComponent.externalId}`;

Expand Down Expand Up @@ -149,7 +159,8 @@ class TerraformListener extends antlr4.tree.ParseTreeListener {
this.currentComponent = new TerraformComponent();
const type = getText(ctx.name());
this.currentComponent.definition = this.definitions
.find((definition) => definition.blockType === 'module' && definition.type === type) || null;
.find((definition) => definition.blockType === 'module' && definition.type === type)
|| this.getUnknownDefinition(type);
}

// Exit a parse tree produced by terraformParser#module.
Expand Down Expand Up @@ -217,10 +228,7 @@ class TerraformListener extends antlr4.tree.ParseTreeListener {

this.currentComponent.definition = this.definitions
.find((definition) => definition.blockType === this.currentBlockType
&& definition.type === type) || new TerraformComponentDefinition({
blockType: this.currentBlockType,
type,
});
&& definition.type === type) || this.getUnknownDefinition(type);

if (this.currentBlockType === 'provider') {
// special case for provider, we do not have any externalId
Expand Down
38 changes: 38 additions & 0 deletions tests/resources/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const awsInternetGatewayVpcIdDefinition = awsInternetGatewayDefinition.definedAt
const awsRouteTableAssociationDefinition = new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_route_table_association',
icon: 'unknown',
model: 'DefaultModel',
});

const awsSecurityGroupDefinition = metadata.pluginData.definitions.components.find(({ type }) => type === 'aws_security_group');
Expand Down Expand Up @@ -147,6 +149,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'data',
type: 'aws_rds_engine_version',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand All @@ -169,6 +173,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'data',
type: 'aws_availability_zones',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -296,6 +302,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_route_table',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -894,6 +902,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_s3_bucket',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand All @@ -911,6 +921,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_s3_bucket_acl',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand All @@ -934,6 +946,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_lb',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -998,6 +1012,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_lb_target_group',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1035,6 +1051,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_launch_configuration',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1067,6 +1085,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_autoscaling_group',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1131,6 +1151,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_autoscaling_policy',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1168,6 +1190,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_cloudwatch_metric_alarm',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1242,6 +1266,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_autoscaling_policy',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1279,6 +1305,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_cloudwatch_metric_alarm',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1353,6 +1381,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_efs_file_system',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand All @@ -1377,6 +1407,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_lb_listener',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1421,6 +1453,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'aws_efs_mount_target',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down Expand Up @@ -1463,6 +1497,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'random_string',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand All @@ -1485,6 +1521,8 @@ export const mainComponents = [
definition: new TerraformComponentDefinition({
blockType: 'resource',
type: 'random_password',
icon: 'unknown',
model: 'DefaultModel',
}),
attributes: [
new TerraformComponentAttribute({
Expand Down
53 changes: 53 additions & 0 deletions tests/resources/js/unknown_components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import TerraformComponent from 'src/models/TerraformComponent';
import TerraformComponentAttribute from 'src/models/TerraformComponentAttribute';
import { getTerraformMetadata } from 'tests/resources/utils';
import TerraformComponentDefinition from 'src/models/TerraformComponentDefinition';

const metadata = getTerraformMetadata(
'aws',
'src/assets/metadata/aws.json',
);
metadata.parse();

export default [
new TerraformComponent({
id: 'id_1',
externalId: 'id_1',
path: 'new_file.tf',
definition: new TerraformComponentDefinition({
isContainer: false,
model: 'DefaultModel',
icon: 'unknown',
type: 'unknown_ressource',
blockType: 'resource',
}),
attributes: [
new TerraformComponentAttribute({
name: 'value',
type: 'String',
definition: null,
value: 'test',
}),
],
}),
new TerraformComponent({
id: 'id_2',
externalId: 'unknown_module',
path: 'new_file.tf',
definition: new TerraformComponentDefinition({
isContainer: false,
model: 'DefaultModel',
icon: 'unknown',
type: 'unknown_module',
blockType: 'module',
}),
attributes: [
new TerraformComponentAttribute({
name: 'source',
type: 'String',
definition: null,
value: 'test',
}),
],
}),
];
7 changes: 7 additions & 0 deletions tests/resources/tf/unknown_components.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "unknown_ressource" "id_1" {
value = "test"
}

module "unknown_module" {
source = "test"
}
22 changes: 22 additions & 0 deletions tests/unit/parser/TerraformParser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import subObject from 'tests/resources/js/subObject';
import objectAttributeDefinition from 'tests/resources/js/objectAttributeDefinition';
import missingDefinitionOnAttribute from 'tests/resources/js/bug67_missingDefinitionOnAttribute';
import emptyListAttribute from 'tests/resources/js/bug78_emptyListAttribute';
import unknownDefinition from 'tests/resources/js/unknown_components';

describe('Test TerraformParser', () => {
describe('Test methods', () => {
Expand Down Expand Up @@ -576,6 +577,27 @@ describe('Test TerraformParser', () => {
parser.parse(new FileInformation({ path: '' }), inputs);
expect(metadata.pluginData.components).toEqual(emptyResource);
});

it('Should set unknown definition', () => {
const metadata = getTerraformMetadata(
'aws',
'src/assets/metadata/aws.json',
);

metadata.parse();
metadata.pluginData.initLinkDefinitions();

const parser = new TerraformParser(metadata.pluginData);
const inputs = [
new FileInput({
path: 'new_file.tf',
content: fs.readFileSync('tests/resources/tf/unknown_components.tf', 'utf8'),
}),
];

parser.parse(new FileInformation({ path: '' }), inputs);
expect(metadata.pluginData.components).toEqual(unknownDefinition);
});
});
});
});
19 changes: 19 additions & 0 deletions tests/unit/render/TerraformRenderer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,25 @@ describe('Test TerraformRenderer', () => {

expect(new TerraformRender(metadata.pluginData).renderFiles()).toEqual([input]);
});

it('Should generate unknown definition', () => {
const input = new FileInput({
path: 'new_file.tf',
content: fs.readFileSync('tests/resources/tf/unknown_components.tf', 'utf8'),
});
const metadata = getTerraformMetadata(
'aws',
'src/assets/metadata/aws.json',
);

metadata.parse();

const parser = new TerraformParser(metadata.pluginData);

parser.parse(new FileInformation({ path: '' }), [input]);

expect(new TerraformRender(metadata.pluginData).renderFiles()).toEqual([input]);
});
});
});
});
Loading