From f3e2f8ea33fb16dd347703936c8fc4b9802b4ab2 Mon Sep 17 00:00:00 2001 From: d-lau Date: Wed, 16 Sep 2020 18:31:21 +0900 Subject: [PATCH] fix removing extra double quotes, fix skipping every other resource --- attribute.go | 2 +- parse.go | 4 +- parse_test.go | 164 ++++++++++++++++++++++++++++++++++++++++++ resource.go | 5 ++ test/resources.stdout | 46 ++++++++++++ 5 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 test/resources.stdout diff --git a/attribute.go b/attribute.go index 29c72b6..1661c99 100644 --- a/attribute.go +++ b/attribute.go @@ -191,5 +191,5 @@ func removeChangeTypeCharacters(line string) string { } func dequote(line string) string { - return strings.Trim(line, "\"") + return strings.TrimPrefix(strings.TrimSuffix(line, "\""), "\"") } diff --git a/parse.go b/parse.go index c31cd3e..f3d0b30 100644 --- a/parse.go +++ b/parse.go @@ -73,8 +73,10 @@ func parseResource(s *bufio.Scanner) (*ResourceChange, error) { for s.Scan() { text := formatInput(s.Bytes()) switch { - case IsResourceCommentLine(text), strings.Contains(text, CHANGES_END_STRING): + case IsResourceTerminator(text): return rc, nil + case IsResourceCommentLine(text), strings.Contains(text, CHANGES_END_STRING): + return nil, fmt.Errorf("unexpected line while parsing resource attribute: %s", text) case IsMapAttributeChangeLine(text): ma, err := parseMapAttribute(s) if err != nil { diff --git a/parse_test.go b/parse_test.go index a257f89..cd5568c 100644 --- a/parse_test.go +++ b/parse_test.go @@ -25,6 +25,170 @@ func TestParse(t *testing.T) { // file: "test/nochanges.stdout", // expected: nil, // }, + "resources": { + file: "test/resources.stdout", + expected: []*ResourceChange{ + &ResourceChange{ + Address: "module.my-module.github_team_membership.member", + ModuleAddress: "module.my-module", + Type: "github_team_membership", + Name: "member", + UpdateType: DestroyResource, + AttributeChanges: []*AttributeChange{ + &AttributeChange{ + Name: "etag", + OldValue: `W/\"etag-0\"`, + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "id", + OldValue: "1234567:dev0", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "role", + OldValue: "member", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "team_id", + OldValue: "1234567", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "username", + OldValue: "dev0", + NewValue: nil, + UpdateType: DestroyResource, + }, + }, + }, + &ResourceChange{ + Address: "module.my-module.github_team_membership.member[1]", + ModuleAddress: "module.my-module", + Type: "github_team_membership", + Name: "member", + Index: 1, + UpdateType: DestroyResource, + AttributeChanges: []*AttributeChange{ + &AttributeChange{ + Name: "etag", + OldValue: `W/\"etag-1\"`, + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "id", + OldValue: "1234567:dev1", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "role", + OldValue: "member", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "team_id", + OldValue: "1234567", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "username", + OldValue: "dev1", + NewValue: nil, + UpdateType: DestroyResource, + }, + }, + }, + &ResourceChange{ + Address: "module.my-module.github_team_membership.member[2]", + ModuleAddress: "module.my-module", + Type: "github_team_membership", + Name: "member", + Index: 2, + UpdateType: DestroyResource, + AttributeChanges: []*AttributeChange{ + &AttributeChange{ + Name: "etag", + OldValue: `W/\"etag-2\"`, + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "id", + OldValue: "1234567:dev2", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "role", + OldValue: "member", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "team_id", + OldValue: "1234567", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "username", + OldValue: "dev2", + NewValue: nil, + UpdateType: DestroyResource, + }, + }, + }, + &ResourceChange{ + Address: "module.my-module.github_team_membership.member[3]", + ModuleAddress: "module.my-module", + Type: "github_team_membership", + Name: "member", + Index: 3, + UpdateType: DestroyResource, + AttributeChanges: []*AttributeChange{ + &AttributeChange{ + Name: "etag", + OldValue: `W/\"etag-3\"`, + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "id", + OldValue: "1234567:dev3", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "role", + OldValue: "member", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "team_id", + OldValue: "1234567", + NewValue: nil, + UpdateType: DestroyResource, + }, + &AttributeChange{ + Name: "username", + OldValue: "dev3", + NewValue: nil, + UpdateType: DestroyResource, + }, + }, + }, + }, + }, "array": { file: "test/array.stdout", expected: []*ResourceChange{ diff --git a/resource.go b/resource.go index 1a9f513..d5158dd 100644 --- a/resource.go +++ b/resource.go @@ -63,6 +63,11 @@ func IsResourceCommentLine(line string) bool { return strings.HasPrefix(trimmed, "#") && !strings.HasSuffix(trimmed, RESOURCE_READ_VALUES_NOT_YET_KNOWN) } +// IsResourceTerminator returns true if the line is a "}" +func IsResourceTerminator(line string) bool { + return strings.TrimSpace(line) == "}" +} + // IsResourceChangeLine returns true if the line is a valid resource change line // A valid line starts with the change type, then "resource" or "data", and then the type and name, followed by a { // Example: + resource "type" "name" { diff --git a/test/resources.stdout b/test/resources.stdout new file mode 100644 index 0000000..ec8aead --- /dev/null +++ b/test/resources.stdout @@ -0,0 +1,46 @@ +------------------------------------------------------------------------ + +An execution plan has been generated and is shown below. +Resource actions are indicated with the following symbols: + - destroy + +Terraform will perform the following actions: + + # module.my-module.github_team_membership.member will be destroyed + - resource "github_team_membership" "member" { + - etag = "W/\"etag-0\"" -> null + - id = "1234567:dev0" -> null + - role = "member" -> null + - team_id = "1234567" -> null + - username = "dev0" -> null + } + + # module.my-module.github_team_membership.member[1] will be destroyed + - resource "github_team_membership" "member" { + - etag = "W/\"etag-1\"" -> null + - id = "1234567:dev1" -> null + - role = "member" -> null + - team_id = "1234567" -> null + - username = "dev1" -> null + } + + # module.my-module.github_team_membership.member[2] will be destroyed + - resource "github_team_membership" "member" { + - etag = "W/\"etag-2\"" -> null + - id = "1234567:dev2" -> null + - role = "member" -> null + - team_id = "1234567" -> null + - username = "dev2" -> null + } + + # module.my-module.github_team_membership.member[3] will be destroyed + - resource "github_team_membership" "member" { + - etag = "W/\"etag-3\"" -> null + - id = "1234567:dev3" -> null + - role = "member" -> null + - team_id = "1234567" -> null + - username = "dev3" -> null + } + + +Plan: 0 to add, 0 to change, 4 to destroy.