Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/metadata/TerraformMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ class TerraformMetadata extends DefaultMetadata {
*/
getAttributeDefinition(attribute) {
const subAttributes = attribute.attributes || [];
const itemDefinition = attribute.itemDefinition || [];
return new TerraformComponentAttributeDefinition({
...attribute,
definedAttributes: subAttributes.map(this.getAttributeDefinition),
itemDefinition: itemDefinition.map(this.getAttributeDefinition),
});
}

Expand Down
58 changes: 47 additions & 11 deletions src/parser/TerraformListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TerraformListener extends antlr4.tree.ParseTreeListener {
this.currentComponent = null;
this.currentBlockType = null;
this.currentField = null;
this.currentObjectField = null;
this.currentArrayOfObjectField = null;
this.currentFile = null;
this.fieldsTree = [];
this.currentVariable = null;
Expand All @@ -41,7 +41,13 @@ class TerraformListener extends antlr4.tree.ParseTreeListener {
}

getAttributeDefinition(object, name) {
return object.definition?.definedAttributes.find((def) => def.name === name) || null;
let definition = object.definition?.definedAttributes.find((def) => def.name === name);

if (!definition) {
definition = object.definition?.itemDefinition?.at(0)
?.definedAttributes.find((def) => def.name === name);
}
return definition || null;
}

isVariable() {
Expand Down Expand Up @@ -177,20 +183,50 @@ class TerraformListener extends antlr4.tree.ParseTreeListener {
definition = this.getAttributeDefinition(this.currentComponent, name);
}

this.currentObjectField = new TerraformComponentAttribute({
name,
value: [],
type: 'Object',
definition,
isDynamic: true,
});
if (definition?.type === 'Array') {
const attribute = this.currentComponent.attributes.find((attr) => attr?.name === name);
if (attribute) {
this.currentArrayOfObjectField = attribute;
} else {
this.currentArrayOfObjectField = new TerraformComponentAttribute({
name,
value: [],
type: 'Array',
definition,
isDynamic: true,
});
}
this.currentObjectField = new TerraformComponentAttribute({
value: [],
type: 'Object',
definition: definition.itemDefinition[0],
isDynamic: true,
});
} else {
this.currentObjectField = new TerraformComponentAttribute({
name,
value: [],
type: 'Object',
definition,
isDynamic: true,
});
}
}

// Exit a parse tree produced by terraformParser#block.
exitBlock() {
if (this.fieldsTree.length > 0) {
const field = this.fieldsTree.pop();
if (this.currentArrayOfObjectField) {
this.currentArrayOfObjectField.value.push(this.currentObjectField);
const attribute = this.currentComponent.attributes
.find(({ name }) => name === this.currentArrayOfObjectField.name);

if (!attribute) {
this.currentComponent.attributes.push(this.currentArrayOfObjectField);
}
this.currentObjectField = null;
this.currentArrayOfObjectField = null;
} else if (this.fieldsTree.length > 0) {
const field = this.fieldsTree.pop();
field.value.push(this.currentObjectField);
this.currentObjectField = field;
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/render/TerraformRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class TerraformRenderer extends DefaultRender {
this.template = nunjucks.compile(templates.root, env);
}

getListType(value) {
return value.split(/\(([^)]+)\)/)[1];
}

/**
* Convert all provided components and links in terraform files.
* @param {string} [parentEventId] - Parent event id.
Expand Down Expand Up @@ -97,9 +101,9 @@ class TerraformRenderer extends DefaultRender {
locals: allVariables.filter((v) => v.category === 'local'),
outputs: allVariables.filter((v) => v.category === 'output'),
// This might cause issues with other providers.
isValueReference: (value) => value?.match(/^(data.|var.|local.|module.|aws_|random_)/),
isValueReference: (value) => typeof value === 'string' && value?.match(/^(data.|var.|local.|module.|aws_|random_)/),
isList: (type) => type?.startsWith('list(') || type?.startsWith('set('),
getListType: (value) => value.split(/\(([^)]+)\)/)[1],
getListType: (value) => this.getListType(value),
}).trim()}\n`,
}));

Expand Down
26 changes: 21 additions & 5 deletions src/render/TerraformTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,29 @@ const block = `{% for _block in components %}

{% endfor %}`;

const attribute = `{% if attribute.type == 'Object' %}
{{ attribute.name | indent(level * 4, true) }} {% if not attribute.isDynamic %}= {% endif %}{
{% set level = level+1 %}{% for attr in attribute.value %}{% set attribute = attr %}
const attribute = `{% if attribute.type == 'Array' and attribute.definition.itemType == 'Object' %}
{% set name = attribute.name %}
{% for attr in attribute.value %}
{{ name | indent(level * 4, true)}} {
{% set attribute = attr %}
{% include "attribute" ignore missing %}
{{"}" | indent(level * 4, true) }}
{% endfor %}
{% elif attribute.type == 'Object' %}
{% set name = attribute.name %}
{% if not name is null %}
{{ attribute.name | indent(level * 4, true) }}{% if not attribute.isDynamic %} ={% endif %} {
{% endif %}
{% set level = level + 1 %}
{% for attr in attribute.value %}
{% set attribute = attr %}
{% endfor %}{% set level = level-1 %}
{{ "}" | indent(level * 4, true) }}
{% include "attribute" ignore missing %}
{% set attribute = attr %}
{% endfor %}
{% set level = level - 1 %}
{% if not name is null %}
{{ "}" | indent(level * 4, true) }}
{% endif %}
{% else %}
{{ attribute.name | indent(level * 4, true) }} = {% if attribute.type == 'Array' %}[
{% set level = level+1 %}{% for value in attribute.value %}
Expand Down
122 changes: 122 additions & 0 deletions tests/resources/js/arrayOfObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import TerraformComponent from 'src/models/TerraformComponent';
import TerraformComponentAttribute from 'src/models/TerraformComponentAttribute';
import { getTerraformMetadata } from 'tests/resources/utils';

const metadata = getTerraformMetadata(
'aws',
'src/assets/metadata/aws.json',
);
metadata.parse();
const awsSecGroupDefinition = metadata.pluginData.definitions.components.find(({ type }) => type === 'aws_security_group');
const ingressAttributeDefinition = awsSecGroupDefinition.definedAttributes.find(({ name }) => name === 'ingress');
const ingressFromPortAttributeDefinition = ingressAttributeDefinition.itemDefinition[0].definedAttributes.find(({ name }) => name === 'from_port');
const ingressToPortAttributeDefinition = ingressAttributeDefinition.itemDefinition[0].definedAttributes.find(({ name }) => name === 'to_port');
const ingressProtocolAttributeDefinition = ingressAttributeDefinition.itemDefinition[0].definedAttributes.find(({ name }) => name === 'protocol');
const cirdBlocksAttributeDefinition = ingressAttributeDefinition.itemDefinition[0].definedAttributes.find(({ name }) => name === 'cidr_blocks');
const ipv6CidrBlocksAttributeDefinition = ingressAttributeDefinition.itemDefinition[0].definedAttributes.find(({ name }) => name === 'ipv6_cidr_blocks');
const arrayOfObjectComponents = [
new TerraformComponent({
id: 'aws_security_group_1',
name: null,
path: './array_of_object.tf',
definition: awsSecGroupDefinition,
attributes: [
new TerraformComponentAttribute({
name: 'name',
type: 'String',
value: 'allow_all',
definition: awsSecGroupDefinition.definedAttributes.find(({ name }) => name === 'name'),
}),
new TerraformComponentAttribute({
name: 'description',
type: 'String',
value: 'Allow all inbound traffic',
definition: awsSecGroupDefinition.definedAttributes.find(({ name }) => name === 'description'),
}),
new TerraformComponentAttribute({
name: 'ingress',
type: 'Array',
isDynamic: true,
definition: ingressAttributeDefinition,
value: [
new TerraformComponentAttribute({
type: 'Object',
definition: ingressAttributeDefinition.itemDefinition[0],
isDynamic: true,
value: [
new TerraformComponentAttribute({
name: 'from_port',
type: 'Number',
value: 8080,
definition: ingressFromPortAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'to_port',
type: 'Number',
value: 80,
definition: ingressToPortAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'protocol',
type: 'String',
value: 'TCP',
definition: ingressProtocolAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'cidr_blocks',
type: 'Array',
value: ['192.2.0.0/24', '192.3.0.0/24'],
definition: cirdBlocksAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'ipv6_cidr_blocks',
type: 'Array',
value: ['2001:db8::/32', '2001:db7::/32'],
definition: ipv6CidrBlocksAttributeDefinition,
}),
],
}),
new TerraformComponentAttribute({
type: 'Object',
definition: ingressAttributeDefinition.itemDefinition[0],
isDynamic: true,
value: [
new TerraformComponentAttribute({
name: 'from_port',
type: 'Number',
value: 9090,
definition: ingressFromPortAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'to_port',
type: 'Number',
value: 90,
definition: ingressToPortAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'protocol',
type: 'String',
value: 'UDP',
definition: ingressProtocolAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'cidr_blocks',
type: 'Array',
value: ['192.15.0.0/24', '192.16.0.0/24'],
definition: cirdBlocksAttributeDefinition,
}),
new TerraformComponentAttribute({
name: 'ipv6_cidr_blocks',
type: 'Array',
value: ['2001:dc8::/32', '2001:dc7::/32'],
definition: ipv6CidrBlocksAttributeDefinition,
}),
],
}),
],
}),
],
}),
];

export default arrayOfObjectComponents;
Loading