Skip to content

Commit

Permalink
fix removing extra double quotes, fix skipping every other resource
Browse files Browse the repository at this point in the history
  • Loading branch information
drlau committed Sep 16, 2020
1 parent f117ce4 commit f3e2f8e
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 2 deletions.
2 changes: 1 addition & 1 deletion attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ func removeChangeTypeCharacters(line string) string {
}

func dequote(line string) string {
return strings.Trim(line, "\"")
return strings.TrimPrefix(strings.TrimSuffix(line, "\""), "\"")
}
4 changes: 3 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
164 changes: 164 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 5 additions & 0 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
46 changes: 46 additions & 0 deletions test/resources.stdout
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit f3e2f8e

Please sign in to comment.