1818package secretmanager
1919
2020import (
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+
3361func 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