Skip to content

Commit

Permalink
fix: display module address in details
Browse files Browse the repository at this point in the history
  • Loading branch information
takaishi committed Jul 5, 2023
1 parent 67e68a6 commit 7613871
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const planTemplateBody = `### {{len .CreatedAddresses}} to add, {{len .UpdatedAd
<details><summary>Change details</summary>
{{ range .ResourceChanges }}
{{codeFence}}diff
# {{.ResourceChange.Type}}.{{.ResourceChange.Name}} {{.HeaderSuffix}}
# {{.Header}}
{{.GetUnifiedDiffString}}{{codeFence}}
{{end}}
</details>
Expand Down Expand Up @@ -75,6 +75,16 @@ func (r ResourceChangeData) GetUnifiedDiffString() (string, error) {
return diffText, nil
}

func (r ResourceChangeData) Header() string {
var h []string
for _, s := range []string{r.ResourceChange.ModuleAddress, r.ResourceChange.Type, r.ResourceChange.Name} {
if s != "" {
h = append(h, s)
}
}
return fmt.Sprintf("%s %s", strings.Join(h, "."), r.HeaderSuffix())
}

func (r ResourceChangeData) HeaderSuffix() string {
switch {
case r.ResourceChange.Change.Actions.Create():
Expand Down
1 change: 1 addition & 0 deletions test/plan_test/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Test_render(t *testing.T) {
{name: "aws_sample", wantErr: false},
{name: "iam_policy", wantErr: false},
{name: "include_code_fence", wantErr: false},
{name: "include_module", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions test/testdata/include_module/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### 1 to add, 0 to change, 0 to destroy, 0 to replace.
- add
- module.test1.env_variable.test1
<details><summary>Change details</summary>

````````diff
# module.test1.env_variable.test1 will be created
@@ -1 +1,3 @@
-null
+{
+ "name": "test1"
+}
````````

</details>
3 changes: 3 additions & 0 deletions test/testdata/include_module/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "test1" {
source = "./modules/test1"
}
16 changes: 16 additions & 0 deletions test/testdata/include_module/modules/test1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
required_providers {
env = {
source = "tchupp/env"
version = "0.0.2"
}
}
}

provider "env" {
# Configuration options
}

resource "env_variable" "test1" {
name = "test1"
}
1 change: 1 addition & 0 deletions test/testdata/include_module/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"1.1","terraform_version":"1.2.9","planned_values":{"root_module":{"child_modules":[{"resources":[{"address":"module.test1.env_variable.test1","mode":"managed","type":"env_variable","name":"test1","provider_name":"registry.terraform.io/tchupp/env","schema_version":0,"values":{"name":"test1"},"sensitive_values":{}}],"address":"module.test1"}]}},"resource_changes":[{"address":"module.test1.env_variable.test1","module_address":"module.test1","mode":"managed","type":"env_variable","name":"test1","provider_name":"registry.terraform.io/tchupp/env","change":{"actions":["create"],"before":null,"after":{"name":"test1"},"after_unknown":{"id":true,"value":true},"before_sensitive":false,"after_sensitive":{"value":true}}}],"configuration":{"provider_config":{"module.test1:env":{"name":"env","full_name":"registry.terraform.io/tchupp/env","version_constraint":"0.0.2","module_address":"module.test1"}},"root_module":{"module_calls":{"test1":{"source":"./modules/test1","module":{"resources":[{"address":"env_variable.test1","mode":"managed","type":"env_variable","name":"test1","provider_config_key":"module.test1:env","expressions":{"name":{"constant_value":"test1"}},"schema_version":0}]}}}}}}

0 comments on commit 7613871

Please sign in to comment.