Skip to content

Commit bd8fcbe

Browse files
Added GET data source references functionality (#15087) (#10707)
[upstream:52e9c674a759cb0b31852adbde9ec65ec47a7e29] Signed-off-by: Modular Magician <[email protected]>
1 parent ddcc47d commit bd8fcbe

File tree

5 files changed

+342
-0
lines changed

5 files changed

+342
-0
lines changed

.changelog/15087.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_backup_dr_data_source_reference`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
221221
"google_backup_dr_data_source": backupdr.DataSourceGoogleCloudBackupDRDataSource(),
222222
"google_backup_dr_backup_vault": backupdr.DataSourceGoogleCloudBackupDRBackupVault(),
223223
"google_backup_dr_data_source_references": backupdr.DataSourceGoogleCloudBackupDRDataSourceReferences(),
224+
"google_backup_dr_data_source_reference": backupdr.DataSourceGoogleCloudBackupDRDataSourceReference(),
224225
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
225226
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
226227
"google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(),

google-beta/services/backupdr/data_source_backup_dr_data_source_reference.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2424
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
2525
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
26+
"google.golang.org/api/googleapi"
2627
)
2728

2829
func DataSourceGoogleCloudBackupDRDataSourceReferences() *schema.Resource {
@@ -181,3 +182,136 @@ func flattenDataSourceReferences(items []interface{}) ([]map[string]interface{},
181182
}
182183
return references, nil
183184
}
185+
186+
func DataSourceGoogleCloudBackupDRDataSourceReference() *schema.Resource {
187+
return &schema.Resource{
188+
Read: dataSourceGoogleCloudBackupDRDataSourceReferenceRead,
189+
Schema: map[string]*schema.Schema{
190+
"data_source_reference_id": {
191+
Type: schema.TypeString,
192+
Required: true,
193+
Description: "The `id` of the data source reference.",
194+
},
195+
"location": {
196+
Type: schema.TypeString,
197+
Required: true,
198+
Description: "The location of the data source reference.",
199+
},
200+
"project": {
201+
Type: schema.TypeString,
202+
Optional: true,
203+
Computed: true,
204+
Description: "The ID of the project in which the resource belongs.",
205+
},
206+
"name": {
207+
Type: schema.TypeString,
208+
Computed: true,
209+
},
210+
"data_source": {
211+
Type: schema.TypeString,
212+
Computed: true,
213+
Description: "The underlying data source resource.",
214+
},
215+
"backup_config_state": {
216+
Type: schema.TypeString,
217+
Computed: true,
218+
Description: "The state of the backup config for the data source.",
219+
},
220+
"backup_count": {
221+
Type: schema.TypeInt,
222+
Computed: true,
223+
Description: "The number of backups for the data source.",
224+
},
225+
"last_backup_state": {
226+
Type: schema.TypeString,
227+
Computed: true,
228+
Description: "The state of the last backup.",
229+
},
230+
"last_successful_backup_time": {
231+
Type: schema.TypeString,
232+
Computed: true,
233+
Description: "The last time a successful backup was made.",
234+
},
235+
"gcp_resource_name": {
236+
Type: schema.TypeString,
237+
Computed: true,
238+
Description: "The GCP resource name for the data source.",
239+
},
240+
"resource_type": {
241+
Type: schema.TypeString,
242+
Computed: true,
243+
},
244+
},
245+
}
246+
}
247+
248+
func dataSourceGoogleCloudBackupDRDataSourceReferenceRead(d *schema.ResourceData, meta interface{}) error {
249+
config := meta.(*transport_tpg.Config)
250+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
251+
if err != nil {
252+
return err
253+
}
254+
project, err := tpgresource.GetProject(d, config)
255+
if err != nil {
256+
return err
257+
}
258+
location := d.Get("location").(string)
259+
dataSourceReferenceId := d.Get("data_source_reference_id").(string)
260+
url := fmt.Sprintf("%sprojects/%s/locations/%s/dataSourceReferences/%s", config.BackupDRBasePath, project, location, dataSourceReferenceId)
261+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
262+
Config: config,
263+
Method: "GET",
264+
Project: project,
265+
RawURL: url,
266+
UserAgent: userAgent,
267+
})
268+
if err != nil {
269+
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
270+
d.SetId("") // Resource not found
271+
return nil
272+
}
273+
return fmt.Errorf("Error reading DataSourceReference: %s", err)
274+
}
275+
276+
if err := flattenDataSourceReference(d, res); err != nil {
277+
return err
278+
}
279+
280+
d.SetId(res["name"].(string))
281+
return nil
282+
}
283+
284+
func flattenDataSourceReference(d *schema.ResourceData, data map[string]interface{}) error {
285+
ref, err := flattenDataSourceReferenceToMap(data)
286+
if err != nil {
287+
return err
288+
}
289+
for k, v := range ref {
290+
if err := d.Set(k, v); err != nil {
291+
return fmt.Errorf("Error setting %s: %s", k, err)
292+
}
293+
}
294+
return nil
295+
}
296+
297+
func flattenDataSourceReferenceToMap(data map[string]interface{}) (map[string]interface{}, error) {
298+
ref := map[string]interface{}{
299+
"name": data["name"],
300+
"data_source": data["dataSource"],
301+
"backup_config_state": data["dataSourceBackupConfigState"],
302+
}
303+
if v, ok := data["dataSourceBackupCount"].(string); ok {
304+
if i, err := strconv.Atoi(v); err == nil {
305+
ref["backup_count"] = i
306+
}
307+
}
308+
if configInfo, ok := data["dataSourceBackupConfigInfo"].(map[string]interface{}); ok {
309+
ref["last_backup_state"] = configInfo["lastBackupState"]
310+
ref["last_successful_backup_time"] = configInfo["lastSuccessfulBackupConsistencyTime"]
311+
}
312+
if resourceInfo, ok := data["dataSourceGcpResourceInfo"].(map[string]interface{}); ok {
313+
ref["gcp_resource_name"] = resourceInfo["gcpResourcename"]
314+
ref["resource_type"] = resourceInfo["type"]
315+
}
316+
return ref, nil
317+
}

google-beta/services/backupdr/data_source_backup_dr_data_source_reference_test.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,129 @@ data "google_backup_dr_data_source_references" "default" {
162162
}
163163
`, context)
164164
}
165+
166+
func TestAccDataSourceGoogleBackupDRDataSourceReference_basic(t *testing.T) {
167+
t.Parallel()
168+
169+
dsRefDataSourceName := "data.google_backup_dr_data_source_reference.default"
170+
context := map[string]interface{}{
171+
"random_suffix": acctest.RandString(t, 10),
172+
}
173+
174+
acctest.VcrTest(t, resource.TestCase{
175+
PreCheck: func() { acctest.AccTestPreCheck(t) },
176+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
177+
ExternalProviders: map[string]resource.ExternalProvider{
178+
"time": {},
179+
},
180+
Steps: []resource.TestStep{
181+
{
182+
// All logic is now in a single HCL block and a single step.
183+
Config: testAccDataSourceGoogleBackupDRDataSourceReference_basic(context),
184+
Check: resource.ComposeTestCheckFunc(
185+
// Check that the singular data source has been populated
186+
resource.TestCheckResourceAttrSet(dsRefDataSourceName, "name"),
187+
resource.TestCheckResourceAttrSet(dsRefDataSourceName, "data_source"),
188+
resource.TestCheckResourceAttrSet(dsRefDataSourceName, "backup_config_state"),
189+
resource.TestCheckResourceAttrSet(dsRefDataSourceName, "gcp_resource_name"),
190+
),
191+
},
192+
},
193+
})
194+
}
195+
196+
func testAccDataSourceGoogleBackupDRDataSourceReference_basic(context map[string]interface{}) string {
197+
return acctest.Nprintf(`
198+
data "google_project" "project" {}
199+
200+
resource "google_service_account" "default" {
201+
account_id = "tf-test-my-custom-%{random_suffix}"
202+
display_name = "Custom SA for VM Instance"
203+
}
204+
205+
resource "google_sql_database_instance" "instance" {
206+
name = "default-%{random_suffix}"
207+
database_version = "MYSQL_8_0"
208+
region = "us-central1"
209+
deletion_protection = false
210+
settings {
211+
tier = "db-f1-micro"
212+
availability_type = "ZONAL"
213+
activation_policy = "ALWAYS"
214+
}
215+
}
216+
217+
resource "google_backup_dr_backup_vault" "my-backup-vault" {
218+
location ="us-central1"
219+
backup_vault_id = "tf-test-bv-%{random_suffix}"
220+
description = "This is a second backup vault built by Terraform."
221+
backup_minimum_enforced_retention_duration = "100000s"
222+
labels = {
223+
foo = "bar1"
224+
bar = "baz1"
225+
}
226+
annotations = {
227+
annotations1 = "bar1"
228+
annotations2 = "baz1"
229+
}
230+
force_update = "true"
231+
force_delete = "true"
232+
allow_missing = "true"
233+
}
234+
235+
resource "google_backup_dr_backup_plan" "foo" {
236+
location = "us-central1"
237+
backup_plan_id = "tf-test-bp-test-%{random_suffix}"
238+
resource_type = "sqladmin.googleapis.com/Instance"
239+
backup_vault = google_backup_dr_backup_vault.my-backup-vault.name
240+
241+
backup_rules {
242+
rule_id = "rule-1"
243+
backup_retention_days = 2
244+
245+
standard_schedule {
246+
recurrence_type = "HOURLY"
247+
hourly_frequency = 6
248+
time_zone = "UTC"
249+
250+
backup_window {
251+
start_hour_of_day = 12
252+
end_hour_of_day = 18
253+
}
254+
}
255+
}
256+
}
257+
258+
resource "google_backup_dr_backup_plan_association" "bpa" {
259+
location = "us-central1"
260+
backup_plan_association_id = "tf-test-bpa-test-%{random_suffix}"
261+
resource = "projects/${data.google_project.project.project_id}/instances/${google_sql_database_instance.instance.name}"
262+
resource_type= "sqladmin.googleapis.com/Instance"
263+
backup_plan = google_backup_dr_backup_plan.foo.name
264+
depends_on = [ google_sql_database_instance.instance ]
265+
}
266+
267+
data "google_backup_dr_data_source_references" "all_refs" {
268+
project = data.google_project.project.project_id
269+
location = "us-central1"
270+
resource_type = "sqladmin.googleapis.com/Instance"
271+
depends_on = [google_backup_dr_backup_plan_association.bpa]
272+
}
273+
274+
locals {
275+
// Directly get the name from the first item in the list.
276+
ds_ref_name = data.google_backup_dr_data_source_references.all_refs.data_source_references[0].name
277+
278+
// Split the name string and take the last element, which is the ID.
279+
data_source_reference_id = element(split("/", local.ds_ref_name), 5)
280+
}
281+
282+
// Now, use the singular data source to fetch the specific reference by its ID.
283+
data "google_backup_dr_data_source_reference" "default" {
284+
project = data.google_project.project.project_id
285+
location = "us-central1"
286+
data_source_reference_id = local.data_source_reference_id
287+
}
288+
289+
`, context)
290+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** Type: Handwritten ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This code is generated by Magic Modules using the following:
9+
#
10+
# Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/website/docs/d/backup_dr_data_source_reference.html.markdown
11+
#
12+
# DO NOT EDIT this file directly. Any changes made to this file will be
13+
# overwritten during the next generation cycle.
14+
#
15+
# ----------------------------------------------------------------------------
16+
subcategory: "Backup and DR Service"
17+
description: |-
18+
Get information about a specific Backup and DR data source reference.
19+
---
20+
21+
# google_backup_dr_data_source_reference
22+
23+
Get information about a specific Backup and DR data source reference.
24+
25+
~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider.
26+
See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources.
27+
28+
## Example Usage
29+
30+
This example shows how to get the details of a specific data source reference by its ID. The ID is often obtained from the `google_backup_dr_data_source_references` data source.
31+
32+
```hcl
33+
data "google_backup_dr_data_source_references" "all_sql_references" {
34+
location = "us-central1"
35+
resource_type = "sqladmin.googleapis.com/Instance"
36+
}
37+
38+
data "google_backup_dr_data_source_reference" "my_reference" {
39+
location = "us-central1"
40+
data_source_reference_id = element(split("/", data.google_backup_dr_data_source_references.all_sql_references.data_source_references[0].name), 5)
41+
}
42+
43+
output "specific_reference_gcp_resource_name" {
44+
value = data.google_backup_dr_data_source_reference.my_reference.gcp_resource_name
45+
}
46+
```
47+
48+
## Argument Reference
49+
50+
The following arguments are supported:
51+
52+
* `location `- (Required) The location of the data source references.
53+
54+
* `data_source_reference_id` - (Required) The id of the data source reference.
55+
56+
* `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
57+
58+
## Attributes Reference
59+
60+
In addition to the arguments listed above, the following attributes are exported:
61+
62+
1. `name`- The full name of the data source reference.
63+
64+
2. `data_source`- The underlying data source resource.
65+
66+
3. `backup_config_state`- The state of the backup config for the data source.
67+
68+
4. `backup_count`- The number of backups for the data source.
69+
70+
5. `last_backup_state`- The state of the last backup.
71+
72+
6. `last_successful_backup_time`- The last time a successful backup was made.
73+
74+
7. `gcp_resource_name`- The GCP resource name for the data source.
75+
76+
8. `resource_type`- The type of the referenced resource.
77+
78+
See [google_backup_dr_data_source_reference](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/backup_dr_data_source_reference) resource for details of the available attributes.

0 commit comments

Comments
 (0)