diff --git a/mmv1/products/secretmanager/Secret.yaml b/mmv1/products/secretmanager/Secret.yaml index c2e8a0832e77..1fe39265855f 100644 --- a/mmv1/products/secretmanager/Secret.yaml +++ b/mmv1/products/secretmanager/Secret.yaml @@ -49,6 +49,8 @@ examples: import_format: ['projects/{{project}}/secrets/{{secret_id}}'] custom_code: !ruby/object:Provider::Terraform::CustomCode pre_update: templates/terraform/pre_update/secret_manager_secret.go.erb + constants: templates/terraform/constants/secret_manager_secret.go +custom_diff: ['secretManagerSecretAutoCustomizeDiff'] parameters: - !ruby/object:Api::Type::String name: secretId @@ -125,7 +127,8 @@ properties: properties: - !ruby/object:Api::Type::Boolean name: automatic - immutable: true + # Immutability is handled by the custom diff function until "automatic" is removed + # immutable: true exactly_one_of: - replication.0.automatic - replication.0.user_managed @@ -137,7 +140,8 @@ properties: - !ruby/object:Api::Type::NestedObject name: auto api_name: automatic - immutable: true + # Immutability is handled by the custom diff function until "automatic" is removed + # immutable: true exactly_one_of: - replication.0.automatic - replication.0.user_managed diff --git a/mmv1/templates/terraform/constants/secret_manager_secret.go b/mmv1/templates/terraform/constants/secret_manager_secret.go new file mode 100644 index 000000000000..13db855f5e7e --- /dev/null +++ b/mmv1/templates/terraform/constants/secret_manager_secret.go @@ -0,0 +1,25 @@ +// Prevent ForceNew when upgrading replication.automatic -> replication.auto +func secretManagerSecretAutoCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error { + oAutomatic, nAutomatic := diff.GetChange("replication.0.automatic") + _, nAuto := diff.GetChange("replication.0.auto") + autoLen := len(nAuto.([]interface{})) + + // Do not ForceNew if we are removing "automatic" while adding "auto" + if oAutomatic == true && nAutomatic == false && autoLen > 0 { + return nil + } + + if diff.HasChange("replication.0.automatic") { + if err := diff.ForceNew("replication.0.automatic"); err != nil { + return err + } + } + + if diff.HasChange("replication.0.auto") { + if err := diff.ForceNew("replication.0.auto"); err != nil { + return err + } + } + + return nil +}