Skip to content

Commit b49052b

Browse files
Adding google_backup_dr_backup_plan_association resource and datasource (#12159) (#8632)
[upstream:92224148914b1c96000cb1b84cd8dffb196a5a9d] Signed-off-by: Modular Magician <[email protected]>
1 parent a6b3038 commit b49052b

10 files changed

+1273
-2
lines changed

.changelog/12159.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:new-resource
2+
`google_backup_dr_backup_plan_association` (beta)
3+
```
4+
```release-note:new-datasource
5+
`google_backup_dr_backup_plan_association` (beta)
6+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
170170
"google_apphub_application": apphub.DataSourceGoogleApphubApplication(),
171171
"google_apphub_discovered_service": apphub.DataSourceApphubDiscoveredService(),
172172
"google_backup_dr_management_server": backupdr.DataSourceGoogleCloudBackupDRService(),
173+
"google_backup_dr_backup_plan_association": backupdr.DataSourceGoogleCloudBackupDRBackupPlanAssociation(),
173174
"google_backup_dr_backup_plan": backupdr.DataSourceGoogleCloudBackupDRBackupPlan(),
174175
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
175176
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
@@ -501,9 +502,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{
501502
}
502503

503504
// Resources
504-
// Generated resources: 551
505+
// Generated resources: 552
505506
// Generated IAM resources: 291
506-
// Total generated resources: 842
507+
// Total generated resources: 843
507508
var generatedResources = map[string]*schema.Resource{
508509
"google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(),
509510
"google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(),
@@ -584,6 +585,7 @@ var generatedResources = map[string]*schema.Resource{
584585
"google_artifact_registry_repository_iam_policy": tpgiamresource.ResourceIamPolicy(artifactregistry.ArtifactRegistryRepositoryIamSchema, artifactregistry.ArtifactRegistryRepositoryIamUpdaterProducer, artifactregistry.ArtifactRegistryRepositoryIdParseFunc),
585586
"google_artifact_registry_vpcsc_config": artifactregistry.ResourceArtifactRegistryVPCSCConfig(),
586587
"google_backup_dr_backup_plan": backupdr.ResourceBackupDRBackupPlan(),
588+
"google_backup_dr_backup_plan_association": backupdr.ResourceBackupDRBackupPlanAssociation(),
587589
"google_backup_dr_backup_vault": backupdr.ResourceBackupDRBackupVault(),
588590
"google_backup_dr_management_server": backupdr.ResourceBackupDRManagementServer(),
589591
"google_beyondcorp_app_connection": beyondcorp.ResourceBeyondcorpAppConnection(),
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package backupdr
4+
5+
import (
6+
"fmt"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
9+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
10+
)
11+
12+
func DataSourceGoogleCloudBackupDRBackupPlanAssociation() *schema.Resource {
13+
14+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBackupDRBackupPlanAssociation().Schema)
15+
// Set 'Required' schema elements
16+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "backup_plan_association_id", "location")
17+
18+
// Set 'Optional' schema elements
19+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
20+
return &schema.Resource{
21+
Read: dataSourceGoogleCloudBackupDRBackupPlanAssociationRead,
22+
Schema: dsSchema,
23+
}
24+
}
25+
26+
func dataSourceGoogleCloudBackupDRBackupPlanAssociationRead(d *schema.ResourceData, meta interface{}) error {
27+
config := meta.(*transport_tpg.Config)
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+
backup_plan_association_id := d.Get("backup_plan_association_id").(string)
38+
id := fmt.Sprintf("projects/%s/locations/%s/backupPlanAssociations/%s", project, location, backup_plan_association_id)
39+
d.SetId(id)
40+
err = resourceBackupDRBackupPlanAssociationRead(d, meta)
41+
if err != nil {
42+
return err
43+
}
44+
if d.Id() == "" {
45+
return fmt.Errorf("%s not found", id)
46+
}
47+
return nil
48+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package backupdr_test
4+
5+
import (
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
8+
"testing"
9+
)
10+
11+
func TestAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(t *testing.T) {
12+
t.Parallel()
13+
context := map[string]interface{}{
14+
"random_suffix": acctest.RandString(t, 10),
15+
}
16+
acctest.VcrTest(t, resource.TestCase{
17+
PreCheck: func() { acctest.AccTestPreCheck(t) },
18+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(context),
22+
Check: resource.ComposeTestCheckFunc(
23+
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_backup_dr_backup_plan_association.bpa-test", "google_backup_dr_backup_plan_association.bpa", map[string]struct{}{
24+
"resource": {},
25+
},
26+
),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
func testAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(context map[string]interface{}) string {
34+
return acctest.Nprintf(`
35+
36+
resource "google_service_account" "default" {
37+
provider = google-beta
38+
account_id = "tf-test-my-custom-%{random_suffix}"
39+
display_name = "Custom SA for VM Instance"
40+
}
41+
42+
resource "google_compute_instance" "default" {
43+
provider = google-beta
44+
name = "tf-test-compute-instance-%{random_suffix}"
45+
machine_type = "n2-standard-2"
46+
zone = "us-central1-a"
47+
tags = ["foo", "bar"]
48+
boot_disk {
49+
initialize_params {
50+
image = "debian-cloud/debian-11"
51+
labels = {
52+
my_label = "value"
53+
}
54+
}
55+
}
56+
// Local SSD disk
57+
scratch_disk {
58+
interface = "NVME"
59+
}
60+
network_interface {
61+
network = "default"
62+
access_config {
63+
// Ephemeral public IP
64+
}
65+
}
66+
service_account {
67+
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
68+
email = google_service_account.default.email
69+
scopes = ["cloud-platform"]
70+
}
71+
}
72+
resource "google_backup_dr_backup_vault" "my-backup-vault" {
73+
provider = google-beta
74+
location ="us-central1"
75+
backup_vault_id = "tf-test-bv-%{random_suffix}"
76+
description = "This is a second backup vault built by Terraform."
77+
backup_minimum_enforced_retention_duration = "100000s"
78+
labels = {
79+
foo = "bar1"
80+
bar = "baz1"
81+
}
82+
annotations = {
83+
annotations1 = "bar1"
84+
annotations2 = "baz1"
85+
}
86+
force_update = "true"
87+
force_delete = "true"
88+
allow_missing = "true"
89+
}
90+
91+
resource "google_backup_dr_backup_plan" "foo" {
92+
provider = google-beta
93+
location = "us-central1"
94+
backup_plan_id = "tf-test-bp-test-%{random_suffix}"
95+
resource_type = "compute.googleapis.com/Instance"
96+
backup_vault = google_backup_dr_backup_vault.my-backup-vault.name
97+
98+
backup_rules {
99+
rule_id = "rule-1"
100+
backup_retention_days = 2
101+
102+
standard_schedule {
103+
recurrence_type = "HOURLY"
104+
hourly_frequency = 6
105+
time_zone = "UTC"
106+
107+
backup_window {
108+
start_hour_of_day = 12
109+
end_hour_of_day = 18
110+
}
111+
}
112+
}
113+
}
114+
115+
resource "google_backup_dr_backup_plan_association" "bpa" {
116+
provider = google-beta
117+
location = "us-central1"
118+
backup_plan_association_id = "tf-test-bpa-test-%{random_suffix}"
119+
resource = google_compute_instance.default.id
120+
resource_type= "compute.googleapis.com/Instance"
121+
backup_plan = google_backup_dr_backup_plan.foo.name
122+
}
123+
124+
data "google_backup_dr_backup_plan_association" "bpa-test" {
125+
provider = google-beta
126+
location = "us-central1"
127+
backup_plan_association_id="tf-test-bpa-test-%{random_suffix}"
128+
depends_on= [ google_backup_dr_backup_plan_association.bpa ]
129+
}
130+
`, context)
131+
}

0 commit comments

Comments
 (0)