Skip to content

Commit

Permalink
Added support for auto and deprecated automatic field in `google_…
Browse files Browse the repository at this point in the history
…secret_manager_secret` resource (#8838) (#15793)

* Added support for automatic_replication field in google_secret_manager_secret resource

* Replaced deprecated field automatic with automatic_replication in terraform configs

* Improved the deprecation_message

* Changed the name of field from automatic_replication to auto

* Adjusted white spaces

* Added test case to cover the automatic field

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 11, 2023
1 parent bdd8862 commit 7762094
Show file tree
Hide file tree
Showing 27 changed files with 485 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changelog/8838.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
secretmanager: added `auto` field to `google_secret_manager_secret` resource
```
```release-note:deprecation
secretmanager: deprecated `automatic` field on `google_secret_manager_secret`. Use `auto` instead.
```
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret" {
secret_id = "secret%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down Expand Up @@ -400,7 +400,7 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret" {
secret_id = "secret%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down
8 changes: 4 additions & 4 deletions google/services/cloudrun/resource_cloud_run_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret1" {
secret_id = "%s"
replication {
automatic = true
auto {}
}
}
resource "google_secret_manager_secret" "secret2" {
secret_id = "%s"
replication {
automatic = true
auto {}
}
}
Expand Down Expand Up @@ -309,14 +309,14 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret1" {
secret_id = "%s"
replication {
automatic = true
auto {}
}
}
resource "google_secret_manager_secret" "secret2" {
secret_id = "%s"
replication {
automatic = true
auto {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret" {
secret_id = "secret%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down Expand Up @@ -331,7 +331,7 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret" {
secret_id = "secret%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret" {
secret_id = "tf-test-secret-1%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down Expand Up @@ -361,7 +361,7 @@ data "google_project" "project" {
resource "google_secret_manager_secret" "secret" {
secret_id = "tf-test-secret-1%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ resource "google_secret_manager_secret" "secret-basic" {
secret_id = "tf-test-secret-name%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ resource "google_secret_manager_secret" "secret-basic" {
secret_id = "tf-test-secret-name%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ resource "google_secret_manager_secret" "secret-basic" {
secret_id = "tf-test-secret-name%{random_suffix}"
replication {
automatic = true
auto {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ resource "google_secret_manager_secret" "secret-basic" {
label = "my-label"
}
replication {
automatic = true
auto {}
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ resource "google_secret_manager_secret" "secret-basic" {
label = "my-label"
}
replication {
automatic = true
auto {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ resource "google_secret_manager_secret" "secret-basic" {
label = "my-label"
}
replication {
automatic = true
auto {}
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ resource "google_secret_manager_secret" "secret-basic" {
label = "my-label"
}
replication {
automatic = true
auto {}
}
}
Expand Down
139 changes: 130 additions & 9 deletions google/services/secretmanager/resource_secret_manager_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,42 @@ after the Secret has been created.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auto": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The Secret will automatically be replicated without any restrictions.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"customer_managed_encryption": {
Type: schema.TypeList,
Optional: true,
Description: `The customer-managed encryption configuration of the Secret.
If no configuration is provided, Google-managed default
encryption is used.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_name": {
Type: schema.TypeString,
Required: true,
Description: `The resource name of the Cloud KMS CryptoKey used to encrypt secret payloads.`,
},
},
},
},
},
},
ExactlyOneOf: []string{"replication.0.automatic", "replication.0.user_managed", "replication.0.auto"},
},
"automatic": {
Type: schema.TypeBool,
Optional: true,
Deprecated: "`automatic` is deprecated and will be removed in a future major release. Use `auto` instead.",
ForceNew: true,
Description: `The Secret will automatically be replicated without any restrictions.`,
ExactlyOneOf: []string{"replication.0.automatic", "replication.0.user_managed"},
ExactlyOneOf: []string{"replication.0.automatic", "replication.0.user_managed", "replication.0.auto"},
},
"user_managed": {
Type: schema.TypeList,
Expand Down Expand Up @@ -106,7 +136,7 @@ after the Secret has been created.`,
},
},
},
ExactlyOneOf: []string{"replication.0.automatic", "replication.0.user_managed"},
ExactlyOneOf: []string{"replication.0.automatic", "replication.0.user_managed", "replication.0.auto"},
},
},
},
Expand Down Expand Up @@ -645,8 +675,14 @@ func flattenSecretManagerSecretReplication(v interface{}, d *schema.ResourceData
return nil
}
transformed := make(map[string]interface{})
transformed["automatic"] =
flattenSecretManagerSecretReplicationAutomatic(original["automatic"], d, config)
_, ok := d.GetOk("replication.0.automatic")
if ok {
transformed["automatic"] =
flattenSecretManagerSecretReplicationAutomatic(original["automatic"], d, config)
} else {
transformed["auto"] =
flattenSecretManagerSecretReplicationAuto(original["automatic"], d, config)
}
transformed["user_managed"] =
flattenSecretManagerSecretReplicationUserManaged(original["userManaged"], d, config)
return []interface{}{transformed}
Expand All @@ -655,6 +691,33 @@ func flattenSecretManagerSecretReplicationAutomatic(v interface{}, d *schema.Res
return v != nil
}

func flattenSecretManagerSecretReplicationAuto(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
transformed := make(map[string]interface{})
transformed["customer_managed_encryption"] =
flattenSecretManagerSecretReplicationAutoCustomerManagedEncryption(original["customerManagedEncryption"], d, config)
return []interface{}{transformed}
}
func flattenSecretManagerSecretReplicationAutoCustomerManagedEncryption(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["kms_key_name"] =
flattenSecretManagerSecretReplicationAutoCustomerManagedEncryptionKmsKeyName(original["kmsKeyName"], d, config)
return []interface{}{transformed}
}
func flattenSecretManagerSecretReplicationAutoCustomerManagedEncryptionKmsKeyName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenSecretManagerSecretReplicationUserManaged(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -799,11 +862,22 @@ func expandSecretManagerSecretReplication(v interface{}, d tpgresource.Terraform
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedAutomatic, err := expandSecretManagerSecretReplicationAutomatic(original["automatic"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAutomatic); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["automatic"] = transformedAutomatic
if _, ok := d.GetOk("replication.0.automatic"); ok {
transformedAutomatic, err := expandSecretManagerSecretReplicationAutomatic(original["automatic"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAutomatic); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["automatic"] = transformedAutomatic
}
}

if _, ok := d.GetOk("replication.0.auto"); ok {
transformedAuto, err := expandSecretManagerSecretReplicationAuto(original["auto"], d, config)
if err != nil {
return nil, err
} else {
transformed["automatic"] = transformedAuto
}
}

transformedUserManaged, err := expandSecretManagerSecretReplicationUserManaged(original["user_managed"], d, config)
Expand All @@ -824,6 +898,53 @@ func expandSecretManagerSecretReplicationAutomatic(v interface{}, d tpgresource.
return struct{}{}, nil
}

func expandSecretManagerSecretReplicationAuto(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 {
return nil, nil
}

if l[0] == nil {
transformed := make(map[string]interface{})
return transformed, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedCustomerManagedEncryption, err := expandSecretManagerSecretReplicationAutoCustomerManagedEncryption(original["customer_managed_encryption"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedCustomerManagedEncryption); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["customerManagedEncryption"] = transformedCustomerManagedEncryption
}

return transformed, nil
}

func expandSecretManagerSecretReplicationAutoCustomerManagedEncryption(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedKmsKeyName, err := expandSecretManagerSecretReplicationAutoCustomerManagedEncryptionKmsKeyName(original["kms_key_name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedKmsKeyName); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["kmsKeyName"] = transformedKmsKeyName
}

return transformed, nil
}

func expandSecretManagerSecretReplicationAutoCustomerManagedEncryptionKmsKeyName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandSecretManagerSecretReplicationUserManaged(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Loading

0 comments on commit 7762094

Please sign in to comment.