Skip to content

Commit

Permalink
Merge pull request #5 from akihiro17/try-to-parse-json-string-in-value
Browse files Browse the repository at this point in the history
Try to parse JSON string in values
  • Loading branch information
takaishi authored Aug 25, 2022
2 parents ef69c40 + 9510b9f commit 09b74e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"text/template"

tfjson "github.com/hashicorp/terraform-json"
Expand Down Expand Up @@ -57,15 +58,18 @@ func (r ResourceChangeData) GetUnifiedDiffString() (string, error) {
if err != nil {
return "", fmt.Errorf("invalid resource changes (after) : %w", err)
}
// Try to parse JSON string in values
replacer := strings.NewReplacer(`\n`, "\n ", `\"`, "\"")
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(before)),
B: difflib.SplitLines(string(after)),
A: difflib.SplitLines(replacer.Replace(string(before))),
B: difflib.SplitLines(replacer.Replace(string(after))),
Context: 3,
}
diffText, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
return "", fmt.Errorf("failed to create diff: %w", err)
}

return diffText, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Test_newPlanData(t *testing.T) {
{name: "single_replace", wantErr: false},
{name: "all_types_mixed", wantErr: false},
{name: "aws_sample", wantErr: false},
{name: "iam_policy", wantErr: false},
{name: "invalid_json", wantErr: true},
{name: "not_json", wantErr: true},
}
Expand Down Expand Up @@ -56,6 +57,7 @@ func Test_render(t *testing.T) {
{name: "single_replace", wantErr: false},
{name: "all_types_mixed", wantErr: false},
{name: "aws_sample", wantErr: false},
{name: "iam_policy", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions test/testdata/iam_policy/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### 0 to add, 1 to change, 0 to destroy, 0 to replace.
- change
- aws_iam_policy.test_policy
<details><summary>Change details</summary>

```diff
# aws_iam_policy.test_policy will be updated in-place
@@ -12,7 +12,8 @@
"Action": [
"autoscaling:Describe*",
"ec2:Describe*",
- "elasticloadbalancing:Describe*"
+ "elasticloadbalancing:Describe*",
+ "health:Describe*"
],
"Resource": "*"
}
```

</details>
1 change: 1 addition & 0 deletions test/testdata/iam_policy/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"1.0","terraform_version":"1.1.8","planned_values":{"root_module":{"resources":[{"address":"aws_iam_policy.test_policy","mode":"managed","type":"aws_iam_policy","name":"test_policy","provider_name":"registry.terraform.io/hashicorp/aws","schema_version":0,"values":{"arn":"arn:aws:iam::363188159740:policy/test_policy","description":"","id":"arn:aws:iam::363188159740:policy/test_policy","name":"test_policy","name_prefix":null,"path":"/","policy":"{\n \"Version\": \"2012-10-17\",\n \"Statement\": {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"autoscaling:Describe*\",\n \"ec2:Describe*\",\n \"elasticloadbalancing:Describe*\",\n \"health:Describe*\"\n ],\n \"Resource\": \"*\"\n }\n}\n"},"sensitive_values":{}}]}},"resource_changes":[{"address":"aws_iam_policy.test_policy","mode":"managed","type":"aws_iam_policy","name":"test_policy","provider_name":"registry.terraform.io/hashicorp/aws","change":{"actions":["update"],"before":{"arn":"arn:aws:iam::363188159740:policy/test_policy","description":"","id":"arn:aws:iam::363188159740:policy/test_policy","name":"test_policy","name_prefix":null,"path":"/","policy":"{\n \"Version\": \"2012-10-17\",\n \"Statement\": {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"autoscaling:Describe*\",\n \"ec2:Describe*\",\n \"elasticloadbalancing:Describe*\"\n ],\n \"Resource\": \"*\"\n }\n}\n"},"after":{"arn":"arn:aws:iam::363188159740:policy/test_policy","description":"","id":"arn:aws:iam::363188159740:policy/test_policy","name":"test_policy","name_prefix":null,"path":"/","policy":"{\n \"Version\": \"2012-10-17\",\n \"Statement\": {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"autoscaling:Describe*\",\n \"ec2:Describe*\",\n \"elasticloadbalancing:Describe*\",\n \"health:Describe*\"\n ],\n \"Resource\": \"*\"\n }\n}\n"},"after_unknown":{},"before_sensitive":{},"after_sensitive":{}}}],"prior_state":{"format_version":"1.0","terraform_version":"1.1.8","values":{"root_module":{"resources":[{"address":"aws_iam_policy.test_policy","mode":"managed","type":"aws_iam_policy","name":"test_policy","provider_name":"registry.terraform.io/hashicorp/aws","schema_version":0,"values":{"arn":"arn:aws:iam::363188159740:policy/test_policy","description":"","id":"arn:aws:iam::363188159740:policy/test_policy","name":"test_policy","name_prefix":null,"path":"/","policy":"{\n \"Version\": \"2012-10-17\",\n \"Statement\": {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"autoscaling:Describe*\",\n \"ec2:Describe*\",\n \"elasticloadbalancing:Describe*\"\n ],\n \"Resource\": \"*\"\n }\n}\n"},"sensitive_values":{}}]}}},"configuration":{"provider_config":{"aws":{"name":"aws","expressions":{"region":{"constant_value":"ap-northeast-1"}}}},"root_module":{"resources":[{"address":"aws_iam_policy.test_policy","mode":"managed","type":"aws_iam_policy","name":"test_policy","provider_config_key":"aws","expressions":{"name":{"constant_value":"test_policy"},"policy":{"constant_value":"{\n \"Version\": \"2012-10-17\",\n \"Statement\": {\n \"Effect\": \"Allow\",\n \"Action\": [\n \"autoscaling:Describe*\",\n \"ec2:Describe*\",\n \"elasticloadbalancing:Describe*\",\n \"health:Describe*\"\n ],\n \"Resource\": \"*\"\n }\n}\n"}},"schema_version":0}]}}}

0 comments on commit 09b74e0

Please sign in to comment.