Skip to content

Commit 3f2cec2

Browse files
Create Terraform Backup Data Source (#12105) (#8762)
[upstream:67564b231b63b16213aa26338910d9e67b0a5864] Signed-off-by: Modular Magician <[email protected]>
1 parent 8ee11a0 commit 3f2cec2

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed

.changelog/12105.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_backup_dr_backup` (beta)
3+
```

google-beta/provider/provider_mmv1_resources.go

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
172172
"google_apphub_application": apphub.DataSourceGoogleApphubApplication(),
173173
"google_apphub_discovered_service": apphub.DataSourceApphubDiscoveredService(),
174174
"google_backup_dr_management_server": backupdr.DataSourceGoogleCloudBackupDRService(),
175+
"google_backup_dr_backup": backupdr.DataSourceGoogleCloudBackupDRBackup(),
175176
"google_backup_dr_backup_plan_association": backupdr.DataSourceGoogleCloudBackupDRBackupPlanAssociation(),
176177
"google_backup_dr_backup_plan": backupdr.DataSourceGoogleCloudBackupDRBackupPlan(),
177178
"google_backup_dr_data_source": backupdr.DataSourceGoogleCloudBackupDRDataSource(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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 DataSourceGoogleCloudBackupDRBackup() *schema.Resource {
13+
dsSchema := map[string]*schema.Schema{
14+
"name": {
15+
Type: schema.TypeString,
16+
Computed: true,
17+
Description: `Name of resource`,
18+
},
19+
"backups": {
20+
Type: schema.TypeList,
21+
Computed: true,
22+
Description: `List of all backups under data source.`,
23+
Elem: &schema.Resource{
24+
Schema: map[string]*schema.Schema{
25+
"name": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
Description: `Name of the resource.`,
29+
},
30+
"location": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
Description: `Location of the resource.`,
34+
},
35+
"backup_id": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
Description: `Id of the requesting object, Backup.`,
39+
},
40+
"backup_vault_id": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
Description: `Name of the Backup Vault associated with Backup.`,
44+
},
45+
"data_source_id": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
Description: `Name of the Data Source associated with Backup.`,
49+
},
50+
},
51+
},
52+
},
53+
"location": {
54+
Type: schema.TypeString,
55+
Required: true,
56+
},
57+
"project": {
58+
Type: schema.TypeString,
59+
Required: true,
60+
},
61+
"data_source_id": {
62+
Type: schema.TypeString,
63+
Required: true,
64+
},
65+
"backup_vault_id": {
66+
Type: schema.TypeString,
67+
Required: true,
68+
},
69+
}
70+
71+
return &schema.Resource{
72+
Read: dataSourceGoogleCloudBackupDrBackupRead,
73+
Schema: dsSchema,
74+
}
75+
}
76+
77+
func dataSourceGoogleCloudBackupDrBackupRead(d *schema.ResourceData, meta interface{}) error {
78+
config := meta.(*transport_tpg.Config)
79+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
80+
if err != nil {
81+
return err
82+
}
83+
84+
project, err := tpgresource.GetProject(d, config)
85+
if err != nil {
86+
return err
87+
}
88+
89+
location, err := tpgresource.GetLocation(d, config)
90+
if err != nil {
91+
return err
92+
}
93+
if len(location) == 0 {
94+
return fmt.Errorf("Cannot determine location: set location in this data source or at provider-level")
95+
}
96+
97+
billingProject := project
98+
url, err := tpgresource.ReplaceVars(d, config, "{{BackupDRBasePath}}projects/{{project}}/locations/{{location}}/backupVaults/{{backup_vault_id}}/dataSources/{{data_source_id}}/backups")
99+
100+
fmt.Sprintf("url: %s", url)
101+
102+
if err != nil {
103+
return err
104+
}
105+
106+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
107+
billingProject = bp
108+
}
109+
110+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
111+
Config: config,
112+
Method: "GET",
113+
Project: billingProject,
114+
RawURL: url,
115+
UserAgent: userAgent,
116+
})
117+
118+
if err != nil {
119+
return fmt.Errorf("Error reading BackupVault: %s", err)
120+
}
121+
122+
if err := d.Set("backups", flattenDataSourceBackupDRBackups(res["backups"], d, config)); err != nil {
123+
return fmt.Errorf("Error reading Backup: %s", err)
124+
}
125+
126+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/backupVaults/{{backup_vault_id}}/dataSources/{{data_source_id}}/backups")
127+
d.SetId(id)
128+
d.Set("name", id)
129+
130+
return nil
131+
}
132+
133+
func flattenDataSourceBackupDRBackups(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
134+
if v == nil {
135+
return v
136+
}
137+
l := v.([]interface{})
138+
transformed := make([]interface{}, 0, len(l))
139+
140+
for _, raw := range l {
141+
original := raw.(map[string]interface{})
142+
if len(original) < 1 {
143+
continue
144+
}
145+
transformed = append(transformed, map[string]interface{}{
146+
"name": flattenDataSourceBackupDRBackupsName(original["name"], d, config),
147+
"location": flattenDataSourceBackupDRBackupsLocation(original["location"], d, config),
148+
"backup_id": flattenDataSourceBackupDRBackupsBackupId(original["backupId"], d, config),
149+
"backup_vault_id": flattenDataSourceBackupDRBackupsBackupVaultId(original["backupVaultId"], d, config),
150+
"data_source_id": flattenDataSourceBackupDRBackupsDataSourceId(original["dataSourceId"], d, config),
151+
})
152+
}
153+
return transformed
154+
}
155+
156+
func flattenDataSourceBackupDRBackupsName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
157+
return v
158+
}
159+
160+
func flattenDataSourceBackupDRBackupsLocation(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
161+
return v
162+
}
163+
164+
func flattenDataSourceBackupDRBackupsBackupId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
165+
return v
166+
}
167+
168+
func flattenDataSourceBackupDRBackupsBackupVaultId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
169+
return v
170+
}
171+
172+
func flattenDataSourceBackupDRBackupsDataSourceId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
173+
return v
174+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package backupdr_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
11+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
12+
)
13+
14+
func TestAccDataSourceGoogleCloudBackupDRBackup_basic(t *testing.T) {
15+
t.Parallel()
16+
17+
project := envvar.GetTestProjectFromEnv()
18+
location := "us-central1"
19+
backupVaultId := "bv-test"
20+
dataSourceId := "ds-test"
21+
22+
name := fmt.Sprintf("projects/%s/locations/%s/backupVaults/%s/dataSources/%s/backups", project, location, backupVaultId, dataSourceId)
23+
24+
context := map[string]interface{}{
25+
"backup_vault_id": backupVaultId,
26+
"data_source_id": dataSourceId,
27+
}
28+
29+
acctest.VcrTest(t, resource.TestCase{
30+
PreCheck: func() { acctest.AccTestPreCheck(t) },
31+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccDataSourceGoogleCloudBackupDRBackup_basic(context),
35+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("data.google_backup_dr_backup.foo", "name", name)),
36+
},
37+
},
38+
})
39+
}
40+
41+
func testAccDataSourceGoogleCloudBackupDRBackup_basic(context map[string]interface{}) string {
42+
return acctest.Nprintf(`
43+
data "google_project" "project" {
44+
}
45+
46+
data "google_backup_dr_backup" "foo" {
47+
project = data.google_project.project.project_id
48+
location = "us-central1"
49+
backup_vault_id = "%{backup_vault_id}"
50+
data_source_id = "%{data_source_id}"
51+
}
52+
53+
`, context)
54+
}

0 commit comments

Comments
 (0)