mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
error if import target is move source (#33192)
It is invalid for any import block to have a "to" argument matching any moved block's "from" argument.
This commit is contained in:
parent
bd6ba6cf99
commit
1172d40d7b
@ -402,6 +402,11 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// "Moved" blocks just append, because they are all independent of one
|
||||||
|
// another at this level. (We handle any references between them at
|
||||||
|
// runtime.)
|
||||||
|
m.Moved = append(m.Moved, file.Moved...)
|
||||||
|
|
||||||
for _, i := range file.Import {
|
for _, i := range file.Import {
|
||||||
for _, mi := range m.Import {
|
for _, mi := range m.Import {
|
||||||
if i.To.Equal(mi.To) {
|
if i.To.Equal(mi.To) {
|
||||||
@ -441,14 +446,25 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
|
|||||||
// will already have been caught.
|
// will already have been caught.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// It is invalid for any import block to have a "to" argument matching
|
||||||
|
// any moved block's "from" argument.
|
||||||
|
for _, mb := range m.Moved {
|
||||||
|
// Comparing string serialisations is good enough here, because we
|
||||||
|
// only care about equality in the case that both addresses are
|
||||||
|
// AbsResourceInstances.
|
||||||
|
if mb.From.String() == i.To.String() {
|
||||||
|
diags = append(diags, &hcl.Diagnostic{
|
||||||
|
Severity: hcl.DiagError,
|
||||||
|
Summary: "Cannot import to a move source",
|
||||||
|
Detail: "An import block for ID %q targets resource address %s, but this address appears in the \"from\" argument of a moved block, which is invalid. Please change the import target to a different address, such as the move target.",
|
||||||
|
Subject: &i.DeclRange,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m.Import = append(m.Import, i)
|
m.Import = append(m.Import, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// "Moved" blocks just append, because they are all independent of one
|
|
||||||
// another at this level. (We handle any references between them at
|
|
||||||
// runtime.)
|
|
||||||
m.Moved = append(m.Moved, file.Moved...)
|
|
||||||
|
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
internal/configs/testdata/invalid-modules/import-to-moved-from/main.tf
vendored
Normal file
12
internal/configs/testdata/invalid-modules/import-to-moved-from/main.tf
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import {
|
||||||
|
id = "foo/bar"
|
||||||
|
to = local_file.foo_bar
|
||||||
|
}
|
||||||
|
|
||||||
|
moved {
|
||||||
|
from = local_file.foo_bar
|
||||||
|
to = local_file.bar_baz
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "local_file" "bar_baz" {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user