-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- `-target` support resource address with module spec prefixed. E.g. `module.mod1.module.mod2[0].module.mod3["foo"].null_resource.test` - When no `-target` is specified, all managed resources in the state will generate their configs, including the ones resides in the nested modules.
- Loading branch information
Showing
6 changed files
with
390 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package addr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func ptr[T any](in T) *T { | ||
return &in | ||
} | ||
|
||
func TestParseModuleAddr(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input string | ||
addr ModuleAddr | ||
err bool | ||
}{ | ||
{ | ||
name: "one module", | ||
input: "module.mod1", | ||
addr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "module instance (key)", | ||
input: `module.mod1["foo"]`, | ||
addr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
Key: ptr("foo"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "module instance (idx)", | ||
input: `module.mod1[0]`, | ||
addr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
Index: ptr(0), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "nested module instance", | ||
input: `module.mod1[0].module.mod2["foo"].module.mod3`, | ||
addr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
Index: ptr(0), | ||
}, | ||
{ | ||
Name: "mod2", | ||
Key: ptr("foo"), | ||
}, | ||
{ | ||
Name: "mod3", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalid module", | ||
input: "mod1", | ||
err: true, | ||
}, | ||
{ | ||
name: "invalid module instance", | ||
input: "module.mod1[]", | ||
err: true, | ||
}, | ||
{ | ||
name: "invalid module instance key", | ||
input: "module.mod1[xyz]", | ||
err: true, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
addr, err := ParseModuleAddr(tt.input) | ||
if tt.err { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, tt.addr, addr) | ||
}) | ||
} | ||
} | ||
|
||
func TestParseResourceAddr(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input string | ||
addr ResourceAddr | ||
err bool | ||
}{ | ||
{ | ||
name: "resource only", | ||
input: "null_resource.test", | ||
addr: ResourceAddr{ | ||
Type: "null_resource", | ||
Name: "test", | ||
}, | ||
}, | ||
{ | ||
name: "resource with module", | ||
input: "module.mod1.null_resource.test", | ||
addr: ResourceAddr{ | ||
ModuleAddr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
}, | ||
}, | ||
Type: "null_resource", | ||
Name: "test", | ||
}, | ||
}, | ||
{ | ||
name: "resource with module instance (key)", | ||
input: `module.mod1["foo"].null_resource.test`, | ||
addr: ResourceAddr{ | ||
ModuleAddr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
Key: ptr("foo"), | ||
}, | ||
}, | ||
Type: "null_resource", | ||
Name: "test", | ||
}, | ||
}, | ||
{ | ||
name: "resource with module instance (idx)", | ||
input: `module.mod1[0].null_resource.test`, | ||
addr: ResourceAddr{ | ||
ModuleAddr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
Index: ptr(0), | ||
}, | ||
}, | ||
Type: "null_resource", | ||
Name: "test", | ||
}, | ||
}, | ||
{ | ||
name: "resource with nested module instance", | ||
input: `module.mod1[0].module.mod2["foo"].module.mod3.null_resource.test`, | ||
addr: ResourceAddr{ | ||
ModuleAddr: []ModuleStep{ | ||
{ | ||
Name: "mod1", | ||
Index: ptr(0), | ||
}, | ||
{ | ||
Name: "mod2", | ||
Key: ptr("foo"), | ||
}, | ||
{ | ||
Name: "mod3", | ||
}, | ||
}, | ||
Type: "null_resource", | ||
Name: "test", | ||
}, | ||
}, | ||
{ | ||
name: "invalid resource addr", | ||
input: "null_resource", | ||
err: true, | ||
}, | ||
{ | ||
name: "invalid resource addr with module", | ||
input: "mod1.null_resource.test", | ||
err: true, | ||
}, | ||
{ | ||
name: "invalid resource addr with module instance", | ||
input: "module.mod1[].null_resource.test", | ||
err: true, | ||
}, | ||
{ | ||
name: "invalid resource addr with module instance key", | ||
input: "module.mod1[xyz].null_resource.test", | ||
err: true, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
addr, err := ParseResourceAddr(tt.input) | ||
if tt.err { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, tt.addr, *addr) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.