Skip to content

Commit

Permalink
Merge 9b2b233 into 2ec6bda
Browse files Browse the repository at this point in the history
  • Loading branch information
Druotic committed Jul 30, 2018
2 parents 2ec6bda + 9b2b233 commit ceea054
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 36 deletions.
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Changed {
action: Action;
type: string;
name: string;
path: string;
changedAttributes: ChangedAttributesMap;
newResourceRequired: boolean;
tainted: boolean;
Expand Down Expand Up @@ -91,14 +92,11 @@ ACTION_MAPPING['-/+'] = Action.REPLACE;
ACTION_MAPPING['~'] = Action.UPDATE;
ACTION_MAPPING['<='] = Action.READ;

const ACTION_LINE_REGEX = /((?:module\.\w+\.)*)(data\.)?([^.]+)\.([^ ]+)( \(tainted\))?( \(new resource required\))?$/;
const ACTION_LINE_REGEX = /^(?:((?:.*\.)?module\.[^.]*)\.)?(?:(data)\.)?([^.]+)\.([^ ]+)( \(tainted\))?( \(new resource required\))?$/;
const ATTRIBUTE_LINE_REGEX = /^ {6}[^ ]/;

// Convert something like "module.test1.module.test2" to "test1.test2"
function parseModulePath (rawModuleStr: string) {
// remove the trailing "."
rawModuleStr = rawModuleStr.substring(0, rawModuleStr.length - 1);

// Convert something like "module.test1.module.test2" to "test1.test2"
return rawModuleStr.split(/\.?module./).slice(1).join('.');
}

Expand Down Expand Up @@ -133,12 +131,15 @@ function parseActionLine (offset: number, line: string, action: Action, result:
}

const [, module, dataSourceStr, type, name, taintedStr, newResourceRequiredStr] = match;
const fullyQualifiedPath = [module, dataSourceStr, type, name].filter(str =>
str && str.length > 0).join('.');

let change;
change = {
action: action,
type: type,
name: name,
path: fullyQualifiedPath,
changedAttributes: {}
} as Changed;

Expand Down
4 changes: 4 additions & 0 deletions test/unit/data/00-terraform-plan.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"action": "update",
"type": "aws_ecs_service",
"name": "sample_app",
"path": "aws_ecs_service.sample_app",
"changedAttributes": {
"task_definition": {
"old": {
Expand All @@ -22,6 +23,7 @@
"action": "replace",
"type": "aws_ecs_task_definition",
"name": "sample_app",
"path": "aws_ecs_task_definition.sample_app",
"changedAttributes": {
"id": {
"old": {
Expand Down Expand Up @@ -98,6 +100,7 @@
"action": "replace",
"type": "null_resource",
"name": "promote_images",
"path": "null_resource.promote_images",
"changedAttributes": {
"id": {
"old": {
Expand Down Expand Up @@ -139,6 +142,7 @@
"action": "read",
"type": "external",
"name": "ecr_image_digests",
"path": "data.external.ecr_image_digests",
"changedAttributes": {
"id": {
"new": {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/data/01-terraform-plan.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"action": "update",
"type": "aws_ecs_service",
"name": "sample_app",
"path": "aws_ecs_service.sample_app",
"changedAttributes": {
"task_definition": {
"old": {
Expand All @@ -22,6 +23,7 @@
"action": "replace",
"type": "aws_ecs_task_definition",
"name": "sample_app",
"path": "aws_ecs_task_definition.sample_app",
"changedAttributes": {
"id": {
"old": {
Expand Down Expand Up @@ -98,6 +100,7 @@
"action": "replace",
"type": "null_resource",
"name": "promote_images",
"path": "null_resource.promote_images",
"changedAttributes": {
"id": {
"old": {
Expand Down Expand Up @@ -139,6 +142,7 @@
"action": "read",
"type": "external",
"name": "ecr_image_digests",
"path": "data.external.ecr_image_digests",
"changedAttributes": {
"id": {
"new": {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/data/02-terraform-plan.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"action": "update",
"type": "aws_ecs_service",
"name": "sample_app",
"path": "aws_ecs_service.sample_app",
"changedAttributes": {
"unterminated_string": {
"new": {
Expand Down Expand Up @@ -53,6 +54,7 @@
"action": "replace",
"type": "aws_ecs_task_definition",
"name": "sample_app",
"path": "aws_ecs_task_definition.sample_app",
"changedAttributes": {
"id": {
"new": {
Expand All @@ -70,6 +72,7 @@
"action": "read",
"type": "external",
"name": "ecr_image_digests",
"path": "data.external.ecr_image_digests",
"changedAttributes": {
"attribute-with-no-value": {}
}
Expand Down
5 changes: 5 additions & 0 deletions test/unit/data/03-terraform-plan.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@
"action": "create",
"type": "aws_ecs_service",
"name": "this_is_created",
"path": "aws_ecs_service.this_is_created",
"changedAttributes": {}
},
{
"action": "destroy",
"type": "aws_ecs_service",
"name": "this_is_destroyed",
"path": "aws_ecs_service.this_is_destroyed",
"changedAttributes": {}
},
{
"action": "replace",
"type": "aws_ecs_service",
"name": "this_is_replaced",
"path": "aws_ecs_service.this_is_replaced",
"changedAttributes": {}
},
{
"action": "update",
"type": "aws_ecs_service",
"name": "this_is_updated",
"path": "aws_ecs_service.this_is_updated",
"changedAttributes": {}
}
],
Expand All @@ -36,6 +40,7 @@
"action": "read",
"type": "external",
"name": "this_is_a_read",
"path": "data.external.this_is_a_read",
"changedAttributes": {}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"action": "update",
"type": "aws_ecs_service",
"name": "sample_app",
"path": "aws_ecs_service.sample_app",
"changedAttributes": {
"task_definition": {
"new": {
Expand All @@ -16,4 +17,4 @@
}
],
"changedDataSources": []
}
}
3 changes: 2 additions & 1 deletion test/unit/data/08-no-attribute-name.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"action": "update",
"type": "aws_ecs_service",
"name": "blah",
"path": "aws_ecs_service.blah",
"changedAttributes": {}
}
],
"changedDataSources": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"action": "update",
"type": "aws_ecs_service",
"name": "sample_app",
"path": "aws_ecs_service.sample_app",
"changedAttributes": {
"task_definition": {
"old": {
Expand All @@ -22,6 +23,7 @@
"action": "replace",
"type": "aws_ecs_task_definition",
"name": "sample_app",
"path": "aws_ecs_task_definition.sample_app",
"changedAttributes": {
"id": {
"old": {
Expand Down Expand Up @@ -98,6 +100,7 @@
"action": "replace",
"type": "null_resource",
"name": "promote_images",
"path": "null_resource.promote_images",
"changedAttributes": {
"id": {
"old": {
Expand Down Expand Up @@ -139,6 +142,7 @@
"action": "read",
"type": "external",
"name": "ecr_image_digests",
"path": "data.external.ecr_image_digests",
"changedAttributes": {
"id": {
"new": {
Expand Down
1 change: 1 addition & 0 deletions test/unit/data/10-issue-4.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"action": "create",
"type": "aws_iam_role",
"name": "terraform_demo",
"path": "aws_iam_role.terraform_demo",
"changedAttributes": {
"id": {
"new": {
Expand Down
1 change: 1 addition & 0 deletions test/unit/data/11-tainted-resource.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"action": "replace",
"type": "aws_ecs_task_definition",
"name": "sample_app",
"path": "aws_ecs_task_definition.sample_app",
"tainted": true,
"changedAttributes": {
"id": {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/data/12-modules.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"action": "create",
"type": "null_resource",
"name": "test0",
"path": "null_resource.test0",
"changedAttributes": {
"id": {
"new": {
Expand All @@ -30,6 +31,7 @@
"module": "test1",
"type": "null_resource",
"name": "test1",
"path": "module.test1.null_resource.test1",
"changedAttributes": {
"id": {
"new": {
Expand All @@ -55,6 +57,7 @@
"module": "test1.test2",
"type": "null_resource",
"name": "test2",
"path": "module.test1.module.test2.null_resource.test2",
"changedAttributes": {
"id": {
"new": {
Expand Down

0 comments on commit ceea054

Please sign in to comment.