mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
core: ResourceStateKey understands resource modes
Once a data resource gets into the state, the state system needs to be able to parse its id to match it with resources in the configuration. Since data resources live in a separate namespace than managed resources, the extra "mode" discriminator is required to specify which namespace we're talking about, just like we do in the resource configuration.
This commit is contained in:
parent
64f2651204
commit
844e1abdd3
@ -855,6 +855,7 @@ func (m *ModuleState) String() string {
|
|||||||
type ResourceStateKey struct {
|
type ResourceStateKey struct {
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
|
Mode config.ResourceMode
|
||||||
Index int
|
Index int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -863,6 +864,9 @@ func (rsk *ResourceStateKey) Equal(other *ResourceStateKey) bool {
|
|||||||
if rsk == nil || other == nil {
|
if rsk == nil || other == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if rsk.Mode != other.Mode {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if rsk.Type != other.Type {
|
if rsk.Type != other.Type {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -879,10 +883,19 @@ func (rsk *ResourceStateKey) String() string {
|
|||||||
if rsk == nil {
|
if rsk == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if rsk.Index == -1 {
|
var prefix string
|
||||||
return fmt.Sprintf("%s.%s", rsk.Type, rsk.Name)
|
switch rsk.Mode {
|
||||||
|
case config.ManagedResourceMode:
|
||||||
|
prefix = ""
|
||||||
|
case config.DataResourceMode:
|
||||||
|
prefix = "data."
|
||||||
|
default:
|
||||||
|
panic(fmt.Errorf("unknown resource mode %s", rsk.Mode))
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s.%s.%d", rsk.Type, rsk.Name, rsk.Index)
|
if rsk.Index == -1 {
|
||||||
|
return fmt.Sprintf("%s%s.%s", prefix, rsk.Type, rsk.Name)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s%s.%s.%d", prefix, rsk.Type, rsk.Name, rsk.Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseResourceStateKey accepts a key in the format used by
|
// ParseResourceStateKey accepts a key in the format used by
|
||||||
@ -891,10 +904,18 @@ func (rsk *ResourceStateKey) String() string {
|
|||||||
// latter case, the index is returned as -1.
|
// latter case, the index is returned as -1.
|
||||||
func ParseResourceStateKey(k string) (*ResourceStateKey, error) {
|
func ParseResourceStateKey(k string) (*ResourceStateKey, error) {
|
||||||
parts := strings.Split(k, ".")
|
parts := strings.Split(k, ".")
|
||||||
|
mode := config.ManagedResourceMode
|
||||||
|
if len(parts) > 0 && parts[0] == "data" {
|
||||||
|
mode = config.DataResourceMode
|
||||||
|
// Don't need the constant "data" prefix for parsing
|
||||||
|
// now that we've figured out the mode.
|
||||||
|
parts = parts[1:]
|
||||||
|
}
|
||||||
if len(parts) < 2 || len(parts) > 3 {
|
if len(parts) < 2 || len(parts) > 3 {
|
||||||
return nil, fmt.Errorf("Malformed resource state key: %s", k)
|
return nil, fmt.Errorf("Malformed resource state key: %s", k)
|
||||||
}
|
}
|
||||||
rsk := &ResourceStateKey{
|
rsk := &ResourceStateKey{
|
||||||
|
Mode: mode,
|
||||||
Type: parts[0],
|
Type: parts[0],
|
||||||
Name: parts[1],
|
Name: parts[1],
|
||||||
Index: -1,
|
Index: -1,
|
||||||
|
@ -1501,6 +1501,7 @@ func TestParseResourceStateKey(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Input: "aws_instance.foo.3",
|
Input: "aws_instance.foo.3",
|
||||||
Expected: &ResourceStateKey{
|
Expected: &ResourceStateKey{
|
||||||
|
Mode: config.ManagedResourceMode,
|
||||||
Type: "aws_instance",
|
Type: "aws_instance",
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Index: 3,
|
Index: 3,
|
||||||
@ -1509,6 +1510,7 @@ func TestParseResourceStateKey(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Input: "aws_instance.foo.0",
|
Input: "aws_instance.foo.0",
|
||||||
Expected: &ResourceStateKey{
|
Expected: &ResourceStateKey{
|
||||||
|
Mode: config.ManagedResourceMode,
|
||||||
Type: "aws_instance",
|
Type: "aws_instance",
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Index: 0,
|
Index: 0,
|
||||||
@ -1517,11 +1519,21 @@ func TestParseResourceStateKey(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Input: "aws_instance.foo",
|
Input: "aws_instance.foo",
|
||||||
Expected: &ResourceStateKey{
|
Expected: &ResourceStateKey{
|
||||||
|
Mode: config.ManagedResourceMode,
|
||||||
Type: "aws_instance",
|
Type: "aws_instance",
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Input: "data.aws_ami.foo",
|
||||||
|
Expected: &ResourceStateKey{
|
||||||
|
Mode: config.DataResourceMode,
|
||||||
|
Type: "aws_ami",
|
||||||
|
Name: "foo",
|
||||||
|
Index: -1,
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Input: "aws_instance.foo.malformed",
|
Input: "aws_instance.foo.malformed",
|
||||||
ExpectedErr: true,
|
ExpectedErr: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user