Skip to content

Commit 969111c

Browse files
Adding log_retention_days field to existing bp resource for backupdr (#14622) (#23846)
[upstream:d1d207118dbf770c8a8c7dc820f1954d10219c49] Signed-off-by: Modular Magician <[email protected]>
1 parent 2891332 commit 969111c

6 files changed

+215
-3
lines changed

.changelog/14622.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
backupdr: added `log_retention_days` field to `google_backup_dr_backup_plan` resource
3+
```

google/services/backupdr/data_source_backup_dr_backup_plan_test.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package backupdr_test
1818

1919
import (
20+
"testing"
21+
2022
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
2123
"github.com/hashicorp/terraform-provider-google/google/acctest"
22-
"testing"
2324
)
2425

2526
func TestAccDataSourceGoogleBackupDRBackupPlan_basic(t *testing.T) {
@@ -42,6 +43,26 @@ func TestAccDataSourceGoogleBackupDRBackupPlan_basic(t *testing.T) {
4243
})
4344
}
4445

46+
func TestAccDataSourceGoogleBackupDRBackupPlan_csql(t *testing.T) {
47+
t.Parallel()
48+
context := map[string]interface{}{
49+
"random_suffix": acctest.RandString(t, 10),
50+
}
51+
acctest.VcrTest(t, resource.TestCase{
52+
PreCheck: func() { acctest.AccTestPreCheck(t) },
53+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
54+
CheckDestroy: testAccCheckBackupDRBackupPlanDestroyProducer(t),
55+
Steps: []resource.TestStep{
56+
{
57+
Config: testAccDataSourceGoogleBackupDRBackupPlan_csql(context),
58+
Check: resource.ComposeTestCheckFunc(
59+
acctest.CheckDataSourceStateMatchesResourceState("data.google_backup_dr_backup_plan.fetch-bp", "google_backup_dr_backup_plan.csql-test"),
60+
),
61+
},
62+
},
63+
})
64+
}
65+
4566
func testAccDataSourceGoogleBackupDRBackupPlan_basic(context map[string]interface{}) string {
4667
return acctest.Nprintf(`
4768
resource "google_backup_dr_backup_vault" "my-backup-vault-1" {
@@ -94,3 +115,57 @@ data "google_backup_dr_backup_plan" "fetch-bp" {
94115
}
95116
`, context)
96117
}
118+
119+
func testAccDataSourceGoogleBackupDRBackupPlan_csql(context map[string]interface{}) string {
120+
return acctest.Nprintf(`
121+
resource "google_backup_dr_backup_vault" "my-backup-vault-csql" {
122+
location ="us-central1"
123+
backup_vault_id = "tf-test-bv-%{random_suffix}"
124+
description = "This is a backup vault built by Terraform for cloudsql."
125+
backup_minimum_enforced_retention_duration = "100000s"
126+
labels = {
127+
foo = "bar1"
128+
bar = "baz1"
129+
}
130+
annotations = {
131+
annotations1 = "bar1"
132+
annotations2 = "baz1"
133+
}
134+
force_update = "true"
135+
force_delete = "true"
136+
allow_missing = "true"
137+
}
138+
139+
140+
resource "google_backup_dr_backup_plan" "csql-test" {
141+
location = "us-central1"
142+
backup_plan_id = "tf-test-bp-%{random_suffix}"
143+
resource_type= "sqladmin.googleapis.com/Instance"
144+
backup_vault = google_backup_dr_backup_vault.my-backup-vault-csql.name
145+
depends_on=[ google_backup_dr_backup_vault.my-backup-vault-csql ]
146+
lifecycle {
147+
ignore_changes = [backup_vault]
148+
}
149+
log_retention_days = 4
150+
backup_rules {
151+
rule_id = "rule-1"
152+
backup_retention_days = 5
153+
standard_schedule {
154+
recurrence_type = "HOURLY"
155+
hourly_frequency = 6
156+
time_zone = "UTC"
157+
backup_window{
158+
start_hour_of_day = 0
159+
end_hour_of_day = 24
160+
}
161+
}
162+
}
163+
}
164+
165+
data "google_backup_dr_backup_plan" "fetch-bp" {
166+
location = "us-central1"
167+
backup_plan_id=google_backup_dr_backup_plan.csql-test.backup_plan_id
168+
depends_on= [ google_backup_dr_backup_plan.csql-test ]
169+
}
170+
`, context)
171+
}

google/services/backupdr/resource_backup_dr_backup_plan.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,20 @@ This is required for 'recurrence_type', 'HOURLY' and is not applicable otherwise
196196
Type: schema.TypeString,
197197
Required: true,
198198
Description: `The resource type to which the 'BackupPlan' will be applied.
199-
Examples include, "compute.googleapis.com/Instance", "compute.googleapis.com/Disk", and "storage.googleapis.com/Bucket".`,
199+
Examples include, "compute.googleapis.com/Instance", "compute.googleapis.com/Disk", "sqladmin.googleapis.com/Instance" and "storage.googleapis.com/Bucket".`,
200200
},
201201
"description": {
202202
Type: schema.TypeString,
203203
Optional: true,
204204
Description: `The description allows for additional details about 'BackupPlan' and its use cases to be provided.`,
205205
Default: "",
206206
},
207+
"log_retention_days": {
208+
Type: schema.TypeInt,
209+
Optional: true,
210+
ForceNew: true,
211+
Description: `This is only applicable for CloudSql resource. Days for which logs will be stored. This value should be greater than or equal to minimum enforced log retention duration of the backup vault.`,
212+
},
207213
"backup_vault_service_account": {
208214
Type: schema.TypeString,
209215
Computed: true,
@@ -267,6 +273,12 @@ func resourceBackupDRBackupPlanCreate(d *schema.ResourceData, meta interface{})
267273
} else if v, ok := d.GetOkExists("backup_rules"); !tpgresource.IsEmptyValue(reflect.ValueOf(backupRulesProp)) && (ok || !reflect.DeepEqual(v, backupRulesProp)) {
268274
obj["backupRules"] = backupRulesProp
269275
}
276+
logRetentionDaysProp, err := expandBackupDRBackupPlanLogRetentionDays(d.Get("log_retention_days"), d, config)
277+
if err != nil {
278+
return err
279+
} else if v, ok := d.GetOkExists("log_retention_days"); !tpgresource.IsEmptyValue(reflect.ValueOf(logRetentionDaysProp)) && (ok || !reflect.DeepEqual(v, logRetentionDaysProp)) {
280+
obj["logRetentionDays"] = logRetentionDaysProp
281+
}
270282

271283
url, err := tpgresource.ReplaceVars(d, config, "{{BackupDRBasePath}}projects/{{project}}/locations/{{location}}/backupPlans/?backup_plan_id={{backup_plan_id}}")
272284
if err != nil {
@@ -390,6 +402,9 @@ func resourceBackupDRBackupPlanRead(d *schema.ResourceData, meta interface{}) er
390402
if err := d.Set("backup_rules", flattenBackupDRBackupPlanBackupRules(res["backupRules"], d, config)); err != nil {
391403
return fmt.Errorf("Error reading BackupPlan: %s", err)
392404
}
405+
if err := d.Set("log_retention_days", flattenBackupDRBackupPlanLogRetentionDays(res["logRetentionDays"], d, config)); err != nil {
406+
return fmt.Errorf("Error reading BackupPlan: %s", err)
407+
}
393408

394409
return nil
395410
}
@@ -773,6 +788,23 @@ func flattenBackupDRBackupPlanBackupRulesStandardScheduleBackupWindowEndHourOfDa
773788
return v // let terraform core handle it otherwise
774789
}
775790

791+
func flattenBackupDRBackupPlanLogRetentionDays(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
792+
// Handles the string fixed64 format
793+
if strVal, ok := v.(string); ok {
794+
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
795+
return intVal
796+
}
797+
}
798+
799+
// number values are represented as float64
800+
if floatVal, ok := v.(float64); ok {
801+
intVal := int(floatVal)
802+
return intVal
803+
}
804+
805+
return v // let terraform core handle it otherwise
806+
}
807+
776808
func expandBackupDRBackupPlanDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
777809
return v, nil
778810
}
@@ -988,3 +1020,7 @@ func expandBackupDRBackupPlanBackupRulesStandardScheduleBackupWindowStartHourOfD
9881020
func expandBackupDRBackupPlanBackupRulesStandardScheduleBackupWindowEndHourOfDay(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
9891021
return v, nil
9901022
}
1023+
1024+
func expandBackupDRBackupPlanLogRetentionDays(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1025+
return v, nil
1026+
}

google/services/backupdr/resource_backup_dr_backup_plan_generated_meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fields:
2525
- field: 'description'
2626
- field: 'location'
2727
provider_only: true
28+
- field: 'log_retention_days'
2829
- field: 'name'
2930
- field: 'resource_type'
3031
- field: 'update_time'

google/services/backupdr/resource_backup_dr_backup_plan_generated_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,65 @@ resource "google_backup_dr_backup_plan" "my-backup-plan-1" {
8888
`, context)
8989
}
9090

91+
func TestAccBackupDRBackupPlan_backupDrBackupPlanForCsqlResourceExample(t *testing.T) {
92+
t.Parallel()
93+
94+
context := map[string]interface{}{
95+
"random_suffix": acctest.RandString(t, 10),
96+
}
97+
98+
acctest.VcrTest(t, resource.TestCase{
99+
PreCheck: func() { acctest.AccTestPreCheck(t) },
100+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
101+
CheckDestroy: testAccCheckBackupDRBackupPlanDestroyProducer(t),
102+
Steps: []resource.TestStep{
103+
{
104+
Config: testAccBackupDRBackupPlan_backupDrBackupPlanForCsqlResourceExample(context),
105+
},
106+
{
107+
ResourceName: "google_backup_dr_backup_plan.my-csql-backup-plan-1",
108+
ImportState: true,
109+
ImportStateVerify: true,
110+
ImportStateVerifyIgnore: []string{"backup_plan_id", "location"},
111+
},
112+
},
113+
})
114+
}
115+
116+
func testAccBackupDRBackupPlan_backupDrBackupPlanForCsqlResourceExample(context map[string]interface{}) string {
117+
return acctest.Nprintf(`
118+
resource "google_backup_dr_backup_vault" "my_backup_vault" {
119+
location = "us-central1"
120+
backup_vault_id = "tf-test-backup-vault-csql-test%{random_suffix}"
121+
backup_minimum_enforced_retention_duration = "100000s"
122+
}
123+
124+
resource "google_backup_dr_backup_plan" "my-csql-backup-plan-1" {
125+
location = "us-central1"
126+
backup_plan_id = "tf-test-backup-plan-csql-test%{random_suffix}"
127+
resource_type = "sqladmin.googleapis.com/Instance"
128+
backup_vault = google_backup_dr_backup_vault.my_backup_vault.id
129+
130+
backup_rules {
131+
rule_id = "rule-1"
132+
backup_retention_days = 5
133+
134+
standard_schedule {
135+
recurrence_type = "HOURLY"
136+
hourly_frequency = 6
137+
time_zone = "UTC"
138+
139+
backup_window {
140+
start_hour_of_day = 0
141+
end_hour_of_day = 6
142+
}
143+
}
144+
}
145+
log_retention_days = 4
146+
}
147+
`, context)
148+
}
149+
91150
func testAccCheckBackupDRBackupPlanDestroyProducer(t *testing.T) func(s *terraform.State) error {
92151
return func(s *terraform.State) error {
93152
for name, rs := range s.RootModule().Resources {

website/docs/r/backup_dr_backup_plan.html.markdown

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ resource "google_backup_dr_backup_plan" "my-disk-backup-plan-1" {
9898
}
9999
}
100100
```
101+
## Example Usage - Backup Dr Backup Plan For Csql Resource
102+
103+
104+
```hcl
105+
resource "google_backup_dr_backup_vault" "my_backup_vault" {
106+
location = "us-central1"
107+
backup_vault_id = "backup-vault-csql-test"
108+
backup_minimum_enforced_retention_duration = "100000s"
109+
}
110+
111+
resource "google_backup_dr_backup_plan" "my-csql-backup-plan-1" {
112+
location = "us-central1"
113+
backup_plan_id = "backup-plan-csql-test"
114+
resource_type = "sqladmin.googleapis.com/Instance"
115+
backup_vault = google_backup_dr_backup_vault.my_backup_vault.id
116+
117+
backup_rules {
118+
rule_id = "rule-1"
119+
backup_retention_days = 5
120+
121+
standard_schedule {
122+
recurrence_type = "HOURLY"
123+
hourly_frequency = 6
124+
time_zone = "UTC"
125+
126+
backup_window {
127+
start_hour_of_day = 0
128+
end_hour_of_day = 6
129+
}
130+
}
131+
}
132+
log_retention_days = 4
133+
}
134+
```
101135

102136
## Argument Reference
103137

@@ -111,7 +145,7 @@ The following arguments are supported:
111145
* `resource_type` -
112146
(Required)
113147
The resource type to which the `BackupPlan` will be applied.
114-
Examples include, "compute.googleapis.com/Instance", "compute.googleapis.com/Disk", and "storage.googleapis.com/Bucket".
148+
Examples include, "compute.googleapis.com/Instance", "compute.googleapis.com/Disk", "sqladmin.googleapis.com/Instance" and "storage.googleapis.com/Bucket".
115149

116150
* `backup_rules` -
117151
(Required)
@@ -131,6 +165,10 @@ The following arguments are supported:
131165
(Optional)
132166
The description allows for additional details about `BackupPlan` and its use cases to be provided.
133167

168+
* `log_retention_days` -
169+
(Optional)
170+
This is only applicable for CloudSql resource. Days for which logs will be stored. This value should be greater than or equal to minimum enforced log retention duration of the backup vault.
171+
134172
* `project` - (Optional) The ID of the project in which the resource belongs.
135173
If it is not provided, the provider project is used.
136174

0 commit comments

Comments
 (0)