Skip to content

Commit 780283b

Browse files
modular-magiciantrodge
authored andcommitted
SecretManager Secret: Prevent recreation for "automatic" to "auto" (#9030) (#6325)
Signed-off-by: Modular Magician <[email protected]>
1 parent fbe1c75 commit 780283b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

.changelog/9030.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
secretmanager: fixed an issue in `google_secretmanager_secret` where replacing `replication.automatic` with `replication.auto` would destroy and recreate the resource
3+
```

google-beta/services/secretmanager/resource_secret_manager_secret.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,46 @@
1818
package secretmanager
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"log"
2324
"reflect"
2425
"strings"
2526
"time"
2627

28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
2729
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2830

2931
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
3032
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3133
)
3234

35+
// Prevent ForceNew when upgrading replication.automatic -> replication.auto
36+
func secretManagerSecretAutoCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
37+
oAutomatic, nAutomatic := diff.GetChange("replication.0.automatic")
38+
_, nAuto := diff.GetChange("replication.0.auto")
39+
autoLen := len(nAuto.([]interface{}))
40+
41+
// Do not ForceNew if we are removing "automatic" while adding "auto"
42+
if oAutomatic == true && nAutomatic == false && autoLen > 0 {
43+
return nil
44+
}
45+
46+
if diff.HasChange("replication.0.automatic") {
47+
if err := diff.ForceNew("replication.0.automatic"); err != nil {
48+
return err
49+
}
50+
}
51+
52+
if diff.HasChange("replication.0.auto") {
53+
if err := diff.ForceNew("replication.0.auto"); err != nil {
54+
return err
55+
}
56+
}
57+
58+
return nil
59+
}
60+
3361
func ResourceSecretManagerSecret() *schema.Resource {
3462
return &schema.Resource{
3563
Create: resourceSecretManagerSecretCreate,
@@ -47,6 +75,10 @@ func ResourceSecretManagerSecret() *schema.Resource {
4775
Delete: schema.DefaultTimeout(20 * time.Minute),
4876
},
4977

78+
CustomizeDiff: customdiff.All(
79+
secretManagerSecretAutoCustomizeDiff,
80+
),
81+
5082
Schema: map[string]*schema.Schema{
5183
"replication": {
5284
Type: schema.TypeList,
@@ -60,7 +92,6 @@ after the Secret has been created.`,
6092
"auto": {
6193
Type: schema.TypeList,
6294
Optional: true,
63-
ForceNew: true,
6495
Description: `The Secret will automatically be replicated without any restrictions.`,
6596
MaxItems: 1,
6697
Elem: &schema.Resource{
@@ -90,7 +121,6 @@ encryption is used.`,
90121
Type: schema.TypeBool,
91122
Optional: true,
92123
Deprecated: "`automatic` is deprecated and will be removed in a future major release. Use `auto` instead.",
93-
ForceNew: true,
94124
Description: `The Secret will automatically be replicated without any restrictions.`,
95125
ExactlyOneOf: []string{"replication.0.automatic", "replication.0.user_managed", "replication.0.auto"},
96126
},

0 commit comments

Comments
 (0)