Skip to content

Commit bb5acea

Browse files
niharika-98akshat-jindal-nit
authored andcommitted
Bp tf resource (GoogleCloudPlatform#12158)
1 parent 3271cf7 commit bb5acea

File tree

5 files changed

+383
-0
lines changed

5 files changed

+383
-0
lines changed
+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# Copyright 2024 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
name: 'BackupPlan'
15+
min_version: beta
16+
base_url: projects/{{project}}/locations/{{location}}/backupPlans
17+
create_url: projects/{{project}}/locations/{{location}}/backupPlans/?backup_plan_id={{backup_plan_id}}
18+
self_link: projects/{{project}}/locations/{{location}}/backupPlans/{{backup_plan_id}}
19+
immutable: true
20+
description: A backup plan defines when and how to back up a resource, including the backup's schedule, retention, and location.
21+
import_format:
22+
- 'projects/{{project}}/locations/{{location}}/backupPlans/{{backup_plan_id}}'
23+
references:
24+
guides:
25+
'Official Documentation': 'https://cloud.google.com/backup-disaster-recovery/docs'
26+
api: 'https://cloud.google.com/backup-disaster-recovery/docs/reference/rest'
27+
autogen_async: true
28+
timeouts:
29+
insert_minutes: 60
30+
delete_minutes: 60
31+
examples:
32+
- name: 'backup_dr_backup_plan_simple'
33+
min_version: 'beta'
34+
primary_resource_id: 'my-backup-plan-1'
35+
vars:
36+
backup_plan_id: 'backup-plan-simple-test'
37+
test_env_vars:
38+
project: :PROJECT_NAME
39+
parameters:
40+
- name: 'location'
41+
type: String
42+
required: true
43+
url_param_only: true
44+
description: |
45+
The location for the backup plan
46+
- name: 'backup_plan_id'
47+
type: String
48+
required: true
49+
url_param_only: true
50+
description: |-
51+
The ID of the backup plan
52+
properties:
53+
- name: 'name'
54+
type: String
55+
description: |
56+
The name of backup plan resource created
57+
output: true
58+
- name: 'description'
59+
type: String
60+
default_value: ""
61+
send_empty_value: true
62+
description: |
63+
The description allows for additional details about `BackupPlan` and its use cases to be provided.
64+
- name: 'backupVault'
65+
type: String
66+
description: |
67+
Backup vault where the backups gets stored using this Backup plan.
68+
required: true
69+
diff_suppress_func: 'tpgresource.ProjectNumberDiffSuppress'
70+
- name: 'backupVaultServiceAccount'
71+
type: String
72+
description: |
73+
The Google Cloud Platform Service Account to be used by the BackupVault for taking backups.
74+
output: true
75+
- name: 'resourceType'
76+
type: String
77+
description: |
78+
The resource type to which the `BackupPlan` will be applied. Examples include, "compute.googleapis.com/Instance" and "storage.googleapis.com/Bucket".
79+
required: true
80+
- name: 'createTime'
81+
type: String
82+
description: |
83+
When the `BackupPlan` was created.
84+
output: true
85+
- name: 'updateTime'
86+
type: String
87+
description: |
88+
When the `BackupPlan` was last updated.
89+
output: true
90+
- name: 'backupRules'
91+
type: Array
92+
description: |
93+
The backup rules for this `BackupPlan`. There must be at least one `BackupRule` message.
94+
required: true
95+
item_type:
96+
type: NestedObject
97+
properties:
98+
- name: 'ruleId'
99+
type: String
100+
description: The unique ID of this `BackupRule`. The `rule_id` is unique per `BackupPlan`.
101+
required: true
102+
- name: 'backupRetentionDays'
103+
type: Integer
104+
description: |
105+
Configures the duration for which backup data will be kept. The value should be greater than or equal to minimum enforced retention of the backup vault.
106+
required: true
107+
- name: 'standardSchedule'
108+
type: NestedObject
109+
description: StandardSchedule defines a schedule that runs within the confines of a defined window of days.
110+
required: true
111+
properties:
112+
- name: 'recurrenceType'
113+
type: Enum
114+
description: RecurrenceType enumerates the applicable periodicity for the schedule.
115+
enum_values:
116+
- HOURLY
117+
- DAILY
118+
- WEEKLY
119+
- MONTHLY
120+
- YEARLY
121+
required: true
122+
- name: 'hourlyFrequency'
123+
type: Integer
124+
description: |
125+
Specifies frequency for hourly backups. An hourly frequency of 2 means jobs will run every 2 hours from start time till end time defined.
126+
This is required for `recurrence_type`, `HOURLY` and is not applicable otherwise.
127+
- name: 'daysOfWeek'
128+
type: Array
129+
description: 'Specifies days of week like MONDAY or TUESDAY, on which jobs will run. This is required for `recurrence_type`, `WEEKLY` and is not applicable otherwise.'
130+
item_type:
131+
type: Enum
132+
name: 'DayOfWeekEnum'
133+
description: 'Specifies day of week'
134+
enum_values:
135+
- DAY_OF_WEEK_UNSPECIFIED
136+
- MONDAY
137+
- TUESDAY
138+
- WEDNESDAY
139+
- THURSDAY
140+
- FRIDAY
141+
- SATURDAY
142+
- name: 'daysOfMonth'
143+
type: 'Array'
144+
description: 'Specifies days of months like 1, 5, or 14 on which jobs will run.'
145+
item_type:
146+
type: Integer
147+
- name: 'weekDayOfMonth'
148+
type: NestedObject
149+
description: 'Specifies a week day of the month like FIRST SUNDAY or LAST MONDAY, on which jobs will run.'
150+
properties:
151+
- name: 'weekOfMonth'
152+
type: Enum
153+
description: 'WeekOfMonth enumerates possible weeks in the month, e.g. the first, third, or last week of the month.'
154+
enum_values:
155+
- WEEK_OF_MONTH_UNSPECIFIED
156+
- FIRST
157+
- SECOND
158+
- THIRD
159+
- FOURTH
160+
- LAST
161+
required: true
162+
- name: 'dayOfWeek'
163+
type: Enum
164+
description: 'Specifies the day of the week.'
165+
enum_values:
166+
- DAY_OF_WEEK_UNSPECIFIED
167+
- MONDAY
168+
- TUESDAY
169+
- WEDNESDAY
170+
- THURSDAY
171+
- FRIDAY
172+
- SATURDAY
173+
- SUNDAY
174+
required: true
175+
- name: 'months'
176+
type: Array
177+
description: 'Specifies values of months'
178+
item_type:
179+
type: Enum
180+
name: 'MonthEnum'
181+
description: 'Specifies the months of the year, like `FEBRUARY` and/or `MAY`, on which jobs will run.'
182+
enum_values:
183+
- MONTH_UNSPECIFIED
184+
- JANUARY
185+
- FEBRUARY
186+
- MARCH
187+
- APRIL
188+
- MAY
189+
- JUNE
190+
- JULY
191+
- AUGUST
192+
- SEPTEMBER
193+
- OCTOBER
194+
- NOVEMBER
195+
- DECEMBER
196+
- name: timeZone
197+
type: String
198+
description: The time zone to be used when interpreting the schedule.
199+
required: true
200+
- name: 'backupWindow'
201+
type: NestedObject
202+
description: |
203+
A BackupWindow defines the window of the day during which backup jobs will run. Jobs are queued at the beginning of the window and will be marked as
204+
`NOT_RUN` if they do not start by the end of the window.
205+
properties:
206+
- name: 'startHourOfDay'
207+
type: Integer
208+
send_empty_value: true
209+
description: |
210+
The hour of the day (0-23) when the window starts, for example, if the value of the start hour of the day is 6, that means the backup window starts at 6:00.
211+
required: true
212+
- name: 'endHourOfDay'
213+
type: Integer
214+
description: |
215+
The hour of the day (1-24) when the window ends, for example, if the value of end hour of the day is 10, that means the backup window end time is 10:00.
216+
The end hour of the day should be greater than the start
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
resource "google_backup_dr_backup_vault" "my_backup_vault" {
2+
provider = google-beta
3+
location = "us-central1"
4+
backup_vault_id = "bv-bp-test"
5+
backup_minimum_enforced_retention_duration = "100000s"
6+
}
7+
8+
resource "google_backup_dr_backup_plan" "{{$.PrimaryResourceId}}" {
9+
provider = google-beta
10+
location = "us-central1"
11+
backup_plan_id = "{{index $.Vars "backup_plan_id"}}"
12+
resource_type = "compute.googleapis.com/Instance"
13+
backup_vault = google_backup_dr_backup_vault.my_backup_vault.id
14+
15+
backup_rules {
16+
rule_id = "rule-1"
17+
backup_retention_days = 5
18+
19+
standard_schedule {
20+
recurrence_type = "HOURLY"
21+
hourly_frequency = 6
22+
time_zone = "UTC"
23+
24+
backup_window {
25+
start_hour_of_day = 0
26+
end_hour_of_day = 24
27+
}
28+
}
29+
}
30+
}
31+

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
3535
"google_apphub_discovered_service": apphub.DataSourceApphubDiscoveredService(),
3636
{{- if ne $.TargetVersionName "ga" }}
3737
"google_backup_dr_management_server": backupdr.DataSourceGoogleCloudBackupDRService(),
38+
"google_backup_dr_backup_plan": backupdr.DataSourceGoogleCloudBackupDRBackupPlan(),
3839
{{- end }}
3940
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
4041
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package backupdr
2+
{{- if ne $.TargetVersionName "ga" }}
3+
4+
import (
5+
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
8+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
9+
)
10+
11+
func DataSourceGoogleCloudBackupDRBackupPlan() *schema.Resource {
12+
13+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBackupDRBackupPlan().Schema)
14+
// Set 'Required' schema elements
15+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "backup_plan_id", "location")
16+
17+
// Set 'Optional' schema elements
18+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
19+
return &schema.Resource{
20+
Read: dataSourceGoogleCloudBackupDRBackupPlanRead,
21+
Schema: dsSchema,
22+
}
23+
}
24+
25+
func dataSourceGoogleCloudBackupDRBackupPlanRead(d *schema.ResourceData, meta interface{}) error {
26+
config := meta.(*transport_tpg.Config)
27+
28+
project, err := tpgresource.GetProject(d, config)
29+
if err != nil {
30+
return err
31+
}
32+
33+
location, err := tpgresource.GetLocation(d, config)
34+
if err != nil {
35+
return err
36+
}
37+
38+
backup_plan_id := d.Get("backup_plan_id").(string)
39+
40+
id := fmt.Sprintf("projects/%s/locations/%s/backupPlans/%s", project, location, backup_plan_id)
41+
d.SetId(id)
42+
err = resourceBackupDRBackupPlanRead(d, meta)
43+
if err != nil {
44+
return err
45+
}
46+
47+
if d.Id() == "" {
48+
return fmt.Errorf("%s not found", id)
49+
}
50+
51+
return nil
52+
}
53+
{{- end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package backupdr_test
2+
{{- if ne $.TargetVersionName "ga" }}
3+
4+
import (
5+
"testing"
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/hashicorp/terraform-provider-google/google/acctest"
8+
)
9+
10+
func TestAccDataSourceGoogleBackupDRBackupPlan_basic(t *testing.T) {
11+
t.Parallel()
12+
context := map[string]interface{}{
13+
"random_suffix": acctest.RandString(t, 10),
14+
}
15+
acctest.VcrTest(t, resource.TestCase{
16+
PreCheck: func() { acctest.AccTestPreCheck(t) },
17+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
18+
CheckDestroy: testAccCheckBackupDRBackupPlanDestroyProducer(t),
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataSourceGoogleBackupDRBackupPlan_basic(context),
22+
Check: resource.ComposeTestCheckFunc(
23+
acctest.CheckDataSourceStateMatchesResourceState("data.google_backup_dr_backup_plan.fetch-bp", "google_backup_dr_backup_plan.test"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccDataSourceGoogleBackupDRBackupPlan_basic(context map[string]interface{}) string {
31+
return acctest.Nprintf(`
32+
resource "google_backup_dr_backup_vault" "my-backup-vault-1" {
33+
location ="us-central1"
34+
backup_vault_id = "bv-%{random_suffix}"
35+
description = "This is a second backup vault built by Terraform."
36+
backup_minimum_enforced_retention_duration = "100000s"
37+
labels = {
38+
foo = "bar1"
39+
bar = "baz1"
40+
}
41+
annotations = {
42+
annotations1 = "bar1"
43+
annotations2 = "baz1"
44+
}
45+
force_update = "true"
46+
force_delete = "true"
47+
allow_missing = "true"
48+
}
49+
50+
51+
resource "google_backup_dr_backup_plan" "test" {
52+
location = "us-central1"
53+
backup_plan_id = "bp-test-%{random_suffix}"
54+
resource_type= "compute.googleapis.com/Instance"
55+
backup_vault = google_backup_dr_backup_vault.my-backup-vault-1.name
56+
depends_on=[ google_backup_dr_backup_vault.my-backup-vault-1 ]
57+
lifecycle {
58+
ignore_changes = [backup_vault]
59+
}
60+
backup_rules {
61+
rule_id = "rule-1"
62+
backup_retention_days = 5
63+
standard_schedule {
64+
recurrence_type = "HOURLY"
65+
hourly_frequency = 6
66+
time_zone = "UTC"
67+
backup_window{
68+
start_hour_of_day = 0
69+
end_hour_of_day = 24
70+
}
71+
}
72+
}
73+
}
74+
75+
data "google_backup_dr_backup_plan" "fetch-bp" {
76+
location = "us-central1"
77+
backup_plan_id="bp-test-%{random_suffix}"
78+
depends_on= [ google_backup_dr_backup_plan.test ]
79+
}
80+
`, context)
81+
}
82+
{{- end }}

0 commit comments

Comments
 (0)