Skip to content

Commit 52e9c67

Browse files
Added GET data source references functionality (#15087)
1 parent e045265 commit 52e9c67

File tree

4 files changed

+325
-0
lines changed

4 files changed

+325
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
4949
"google_backup_dr_data_source": backupdr.DataSourceGoogleCloudBackupDRDataSource(),
5050
"google_backup_dr_backup_vault": backupdr.DataSourceGoogleCloudBackupDRBackupVault(),
5151
"google_backup_dr_data_source_references": backupdr.DataSourceGoogleCloudBackupDRDataSourceReferences(),
52+
"google_backup_dr_data_source_reference": backupdr.DataSourceGoogleCloudBackupDRDataSourceReference(),
5253
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
5354
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
5455
"google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(),

mmv1/third_party/terraform/services/backupdr/data_source_backup_dr_data_source_reference.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
99
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
10+
"google.golang.org/api/googleapi"
1011
)
1112

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

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