Skip to content

Commit

Permalink
Merge pull request #7 from drlau/finalize-0.0.6
Browse files Browse the repository at this point in the history
handle no-ops properly, initialize jsonencode
  • Loading branch information
drlau authored Sep 15, 2020
2 parents 6c05677 + 45a1e7c commit f117ce4
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 60 deletions.
9 changes: 6 additions & 3 deletions array_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type ArrayAttributeChange struct {
// This requires the line to start with "+", "-" or "~", not be followed with "resource" or "data", and ends with "[".
func IsArrayAttributeChangeLine(line string) bool {
line = strings.TrimSpace(line)
validPrefix := strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") || strings.HasPrefix(line, "~")
// validPrefix := strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") || strings.HasPrefix(line, "~")
validSuffix := strings.HasSuffix(line, "[") || IsOneLineEmptyArrayAttribute(line)
return validPrefix && validSuffix && !IsResourceChangeLine(line)
return validSuffix && !IsResourceChangeLine(line)
}

// IsArrayAttributeTerminator returns true if the line is "]" or "] -> null"
Expand Down Expand Up @@ -64,7 +64,10 @@ func NewArrayAttributeChangeFromLine(line string) (*ArrayAttributeChange, error)
UpdateType: UpdateInPlaceResource,
}, nil
} else {
return nil, fmt.Errorf("unrecognized line pattern")
return &ArrayAttributeChange{
Name: attributeName,
UpdateType: NoOpResource,
}, nil
}
}

Expand Down
22 changes: 18 additions & 4 deletions array_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,27 @@ func TestNewArrayAttributeChangeFromLine(t *testing.T) {
},
"attribute is unchanged": {
line: `attribute [`,
shouldError: true,
expected: nil,
shouldError: false,
expected: &ArrayAttributeChange{
Name: "attribute",
UpdateType: NoOpResource,
},
},
"attribute with delimiter is unchanged": {
line: `attribute = [`,
shouldError: true,
expected: nil,
shouldError: false,
expected: &ArrayAttributeChange{
Name: "attribute",
UpdateType: NoOpResource,
},
},
"unchanged empty array": {
line: `attribute = []`,
shouldError: false,
expected: &ArrayAttributeChange{
Name: "attribute",
UpdateType: NoOpResource,
},
},
"resource line": {
line: `+ resource "type" "name" {`,
Expand Down
44 changes: 22 additions & 22 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ type AttributeChange struct {
// This requires the line to start with "+", "-" or "~", and not be followed with "resource"
func IsAttributeChangeLine(line string) bool {
line = strings.TrimSpace(line)
validPrefix := strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") || strings.HasPrefix(line, "~")
attribute := strings.SplitN(removeChangeTypeCharacters(line), ATTRIBUTE_DEFINITON_DELIMITER, 2)
// validPrefix := strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") || strings.HasPrefix(line, "~")
multilineAttribute := strings.HasSuffix(line, "(") || strings.HasSuffix(line, "{")

return validPrefix && !multilineAttribute && !IsResourceChangeLine(line)
return len(attribute) == 2 && !multilineAttribute && !IsResourceChangeLine(line)
}

// IsAttributeChangeArrayItem returns true if the line is a valid attribute change in an array
func IsAttributeChangeArrayItem(line string) bool {
line = strings.TrimSpace(line)
validSuffix := strings.HasSuffix(line, ",")
multilineAttribute := strings.HasSuffix(line, "(") || strings.HasSuffix(line, "{")

return validSuffix && !multilineAttribute && !IsResourceChangeLine(line)
}

// NewAttributeChangeFromLine initializes an AttributeChange from a line containing an attribute change
Expand All @@ -38,10 +48,10 @@ func NewAttributeChangeFromLine(line string) (*AttributeChange, error) {
return nil, fmt.Errorf("%s is not a valid line to initialize an attributeChange", line)
}

attribute := strings.SplitN(removeChangeTypeCharacters(line), ATTRIBUTE_DEFINITON_DELIMITER, 2)

if strings.HasPrefix(line, "+") {
// add
attribute := strings.SplitN(removeChangeTypeCharacters(line), ATTRIBUTE_DEFINITON_DELIMITER, 2)

return &AttributeChange{
Name: dequote(strings.TrimSpace(attribute[0])),
OldValue: nil,
Expand All @@ -50,19 +60,6 @@ func NewAttributeChangeFromLine(line string) (*AttributeChange, error) {
}, nil
} else if strings.HasPrefix(line, "-") {
// destroy
attribute := strings.SplitN(removeChangeTypeCharacters(line), ATTRIBUTE_DEFINITON_DELIMITER, 2)
if len(attribute) == 1 {
// line does not have an "="
// assume delimited with a space
attribute = strings.Split(attribute[0], " ")
return &AttributeChange{
Name: dequote(attribute[0]),
OldValue: doTypeConversion(attribute[len(attribute)-1]),
NewValue: nil,
UpdateType: DestroyResource,
}, nil
}

values := strings.Split(attribute[1], ATTRIBUTE_CHANGE_DELIMITER)
if len(values) != 2 {
return &AttributeChange{
Expand All @@ -83,13 +80,11 @@ func NewAttributeChangeFromLine(line string) (*AttributeChange, error) {
// replace
updateType := UpdateInPlaceResource

if strings.HasSuffix(line, " # forces replacement") {
if strings.HasSuffix(attribute[1], " # forces replacement") {
updateType = ForceReplaceResource
line = strings.TrimSuffix(line, " # forces replacement")
attribute[1] = strings.TrimSuffix(attribute[1], " # forces replacement")
}

attribute := strings.SplitN(removeChangeTypeCharacters(line), ATTRIBUTE_DEFINITON_DELIMITER, 2)

values := strings.Split(attribute[1], ATTRIBUTE_CHANGE_DELIMITER)
if len(values) != 2 {
if values[0] != SENSITIVE_VALUE {
Expand All @@ -105,7 +100,12 @@ func NewAttributeChangeFromLine(line string) (*AttributeChange, error) {
UpdateType: updateType,
}, nil
} else {
return nil, fmt.Errorf("unrecognized line pattern %s", line)
return &AttributeChange{
Name: dequote(strings.TrimSpace(attribute[0])),
OldValue: doTypeConversion(attribute[1]),
NewValue: doTypeConversion(attribute[1]),
UpdateType: NoOpResource,
}, nil
}
}

Expand Down
21 changes: 8 additions & 13 deletions attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestIsAttributeChangeLine(t *testing.T) {
},
"attribute is unchanged": {
line: `attribute = "old"`,
expected: false,
expected: true,
},
"resource line": {
line: `+ resource "type" "name" {`,
Expand Down Expand Up @@ -121,16 +121,6 @@ func TestNewAttributeChangeFromLine(t *testing.T) {
UpdateType: DestroyResource,
},
},
"empty map is deleted": {
line: `- attribute {}`,
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: nil,
NewValue: nil,
UpdateType: DestroyResource,
},
},
"attribute changed": {
line: `~ attribute = "old" -> "new"`,
shouldError: false,
Expand Down Expand Up @@ -173,8 +163,13 @@ func TestNewAttributeChangeFromLine(t *testing.T) {
},
"attribute is unchanged": {
line: `attribute = "old"`,
shouldError: true,
expected: nil,
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: "old",
NewValue: "old",
UpdateType: NoOpResource,
},
},
"resource line": {
line: `+ resource "type" "name" {`,
Expand Down
3 changes: 3 additions & 0 deletions jsonencode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package tfplanparse

// TODO implement
13 changes: 8 additions & 5 deletions map_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ type MapAttributeChange struct {
// This requires the line to start with "+", "-" or "~", not be followed with "resource" or "data", and ends with "{".
func IsMapAttributeChangeLine(line string) bool {
line = strings.TrimSpace(line)
validPrefix := strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") || strings.HasPrefix(line, "~")
// validPrefix := strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") || strings.HasPrefix(line, "~")
validSuffix := strings.HasSuffix(line, "{") || IsOneLineEmptyMapAttribute(line)
return validPrefix && validSuffix && !IsResourceChangeLine(line)
return validSuffix && !IsResourceChangeLine(line)
}

// IsMapAttributeTerminator returns true if the line is a "}"
// IsMapAttributeTerminator returns true if the line is a "}" or "},"
func IsMapAttributeTerminator(line string) bool {
return strings.TrimSpace(line) == "}"
return strings.TrimSuffix(strings.TrimSpace(line), ",") == "}"
}

// IsOneLineEmptyMapAttribute returns true if the line ends with a "{}"
Expand Down Expand Up @@ -61,7 +61,10 @@ func NewMapAttributeChangeFromLine(line string) (*MapAttributeChange, error) {
UpdateType: UpdateInPlaceResource,
}, nil
} else {
return nil, fmt.Errorf("unrecognized line pattern")
return &MapAttributeChange{
Name: attributeName,
UpdateType: NoOpResource,
}, nil
}
}

Expand Down
22 changes: 18 additions & 4 deletions map_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,27 @@ func TestNewMapAttributeChangeFromLine(t *testing.T) {
},
"attribute is unchanged": {
line: `attribute {`,
shouldError: true,
expected: nil,
shouldError: false,
expected: &MapAttributeChange{
Name: "attribute",
UpdateType: NoOpResource,
},
},
"attribute with delimiter is unchanged": {
line: `attribute = {`,
shouldError: true,
expected: nil,
shouldError: false,
expected: &MapAttributeChange{
Name: "attribute",
UpdateType: NoOpResource,
},
},
"unchanged empty map": {
line: `attribute = {}`,
shouldError: false,
expected: &MapAttributeChange{
Name: "attribute",
UpdateType: NoOpResource,
},
},
"resource line": {
line: `+ resource "type" "name" {`,
Expand Down
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func parseArrayAttribute(s *bufio.Scanner) (*ArrayAttributeChange, error) {
return nil, err
}
result.HeredocAttributeChanges = append(result.HeredocAttributeChanges, ha)
case IsAttributeChangeLine(text):
case IsAttributeChangeArrayItem(text):
if len(result.AttributeChanges) > 0 && (len(result.MapAttributeChanges) > 0 || len(result.ArrayAttributeChanges) > 0) {
return nil, fmt.Errorf("detected a single attribute in an array with map or array attribute changes")
}
Expand Down
74 changes: 66 additions & 8 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,74 @@ func TestParse(t *testing.T) {
file: "test/nestedmap.stdout",
expected: []*ResourceChange{
&ResourceChange{
Address: "module.mymodule.kubernetes_namespace.mynamespace",
ModuleAddress: "module.mymodule",
Type: "kubernetes_namespace",
Name: "mynamespace",
UpdateType: UpdateInPlaceResource,
AttributeChanges: nil,
Address: "module.mymodule.kubernetes_namespace.mynamespace",
ModuleAddress: "module.mymodule",
Type: "kubernetes_namespace",
Name: "mynamespace",
UpdateType: UpdateInPlaceResource,
AttributeChanges: []*AttributeChange{
&AttributeChange{
Name: "id",
OldValue: "namespace-id",
NewValue: "namespace-id",
UpdateType: NoOpResource,
},
},
MapAttributeChanges: []*MapAttributeChange{
&MapAttributeChange{
Name: "metadata",
AttributeChanges: nil,
Name: "metadata",
AttributeChanges: []*AttributeChange{
&AttributeChange{
Name: "generation",
OldValue: 0,
NewValue: 0,
UpdateType: NoOpResource,
},
&AttributeChange{
Name: "name",
OldValue: "my-namespace",
NewValue: "my-namespace",
UpdateType: NoOpResource,
},
&AttributeChange{
Name: "resource_version",
OldValue: "123",
NewValue: "123",
UpdateType: NoOpResource,
},
&AttributeChange{
Name: "self_link",
OldValue: "/api/v1/namespaces/my-namespace",
NewValue: "/api/v1/namespaces/my-namespace",
UpdateType: NoOpResource,
},
&AttributeChange{
Name: "uid",
OldValue: "some-uid-123",
NewValue: "some-uid-123",
UpdateType: NoOpResource,
},
},
MapAttributeChanges: []*MapAttributeChange{
&MapAttributeChange{
Name: "annotations",
UpdateType: NoOpResource,
},
&MapAttributeChange{
Name: "labels",
AttributeChanges: []*AttributeChange{
&AttributeChange{
Name: "label",
OldValue: "value",
NewValue: "value",
UpdateType: NoOpResource,
},
&AttributeChange{
Name: "other",
OldValue: "label",
NewValue: "label",
UpdateType: NoOpResource,
},
&AttributeChange{
Name: "newLabel",
OldValue: nil,
Expand All @@ -113,6 +167,10 @@ func TestParse(t *testing.T) {
},
UpdateType: UpdateInPlaceResource,
},
&MapAttributeChange{
Name: "timeouts",
UpdateType: NoOpResource,
},
},
},
},
Expand Down

0 comments on commit f117ce4

Please sign in to comment.