mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
config: Support create_before_destroy config
This commit is contained in:
parent
a8c730d7a4
commit
a14ea76c84
@ -54,12 +54,13 @@ type ProviderConfig struct {
|
|||||||
// A Terraform resource is something that represents some component that
|
// A Terraform resource is something that represents some component that
|
||||||
// can be created and managed, and has some properties associated with it.
|
// can be created and managed, and has some properties associated with it.
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
Count int
|
Count int
|
||||||
RawConfig *RawConfig
|
RawConfig *RawConfig
|
||||||
Provisioners []*Provisioner
|
Provisioners []*Provisioner
|
||||||
DependsOn []string
|
DependsOn []string
|
||||||
|
CreateBeforeDestroy bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provisioner is a configured provisioner step on a resource.
|
// Provisioner is a configured provisioner step on a resource.
|
||||||
|
@ -394,6 +394,7 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) {
|
|||||||
delete(config, "count")
|
delete(config, "count")
|
||||||
delete(config, "depends_on")
|
delete(config, "depends_on")
|
||||||
delete(config, "provisioner")
|
delete(config, "provisioner")
|
||||||
|
delete(config, "create_before_destroy")
|
||||||
|
|
||||||
rawConfig, err := NewRawConfig(config)
|
rawConfig, err := NewRawConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -457,13 +458,28 @@ func loadResourcesHcl(os *hclobj.Object) ([]*Resource, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the resource should be re-created before
|
||||||
|
// destroying the existing instance
|
||||||
|
var createBeforeDestroy bool
|
||||||
|
if o := obj.Get("create_before_destroy", false); o != nil {
|
||||||
|
err = hcl.DecodeObject(&createBeforeDestroy, o)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"Error parsing create_before_destroy for %s[%s]: %s",
|
||||||
|
t.Key,
|
||||||
|
k,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result = append(result, &Resource{
|
result = append(result, &Resource{
|
||||||
Name: k,
|
Name: k,
|
||||||
Type: t.Key,
|
Type: t.Key,
|
||||||
Count: count,
|
Count: count,
|
||||||
RawConfig: rawConfig,
|
RawConfig: rawConfig,
|
||||||
Provisioners: provisioners,
|
Provisioners: provisioners,
|
||||||
DependsOn: dependsOn,
|
DependsOn: dependsOn,
|
||||||
|
CreateBeforeDestroy: createBeforeDestroy,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -346,6 +346,43 @@ func TestLoad_connections(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoad_createBeforeDestroy(t *testing.T) {
|
||||||
|
c, err := Load(filepath.Join(fixtureDir, "create-before-destroy.tf"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c == nil {
|
||||||
|
t.Fatal("config should not be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := resourcesStr(c.Resources)
|
||||||
|
if actual != strings.TrimSpace(createBeforeDestroyResourcesStr) {
|
||||||
|
t.Fatalf("bad:\n%s", actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for the flag value
|
||||||
|
r := c.Resources[0]
|
||||||
|
if r.Name != "web" && r.Type != "aws_instance" {
|
||||||
|
t.Fatalf("Bad: %#v", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should enable create before destroy
|
||||||
|
if !r.CreateBeforeDestroy {
|
||||||
|
t.Fatalf("Bad: %#v", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
r = c.Resources[1]
|
||||||
|
if r.Name != "bar" && r.Type != "aws_instance" {
|
||||||
|
t.Fatalf("Bad: %#v", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not enable create before destroy
|
||||||
|
if r.CreateBeforeDestroy {
|
||||||
|
t.Fatalf("Bad: %#v", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const basicOutputsStr = `
|
const basicOutputsStr = `
|
||||||
web_ip
|
web_ip
|
||||||
vars
|
vars
|
||||||
@ -523,3 +560,10 @@ foo (required)
|
|||||||
<>
|
<>
|
||||||
<>
|
<>
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const createBeforeDestroyResourcesStr = `
|
||||||
|
aws_instance[bar] (x1)
|
||||||
|
ami
|
||||||
|
aws_instance[web] (x1)
|
||||||
|
ami
|
||||||
|
`
|
||||||
|
10
config/test-fixtures/create-before-destroy.tf
Normal file
10
config/test-fixtures/create-before-destroy.tf
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
resource "aws_instance" "web" {
|
||||||
|
ami = "foo"
|
||||||
|
create_before_destroy = true
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "bar" {
|
||||||
|
ami = "foo"
|
||||||
|
create_before_destroy = false
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user