Skip to content

Commit

Permalink
Add google_backup_dr_backup_vault datasource (#12377) (#20468)
Browse files Browse the repository at this point in the history
[upstream:1a5e7d4d7a56f9af4d3cd8ffd25dbd82cc7201eb]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Nov 25, 2024
1 parent a0dec11 commit 3618f5b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/12377.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_backup_dr_backup_vault`
```
1 change: 1 addition & 0 deletions google/provider/provider_mmv1_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
"google_apphub_application": apphub.DataSourceGoogleApphubApplication(),
"google_apphub_discovered_service": apphub.DataSourceApphubDiscoveredService(),
"google_backup_dr_backup_vault": backupdr.DataSourceGoogleCloudBackupDRBackupVault(),
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
"google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(),
Expand Down
50 changes: 50 additions & 0 deletions google/services/backupdr/data_source_backup_dr_backup_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package backupdr

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceGoogleCloudBackupDRBackupVault() *schema.Resource {

dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBackupDRBackupVault().Schema)

tpgresource.AddRequiredFieldsToSchema(dsSchema, "backup_vault_id", "location")

tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")

return &schema.Resource{
Read: dataSourceGoogleCloudBackupDRBackupVaultRead,
Schema: dsSchema,
}
}

func dataSourceGoogleCloudBackupDRBackupVaultRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}

location, err := tpgresource.GetLocation(d, config)
if err != nil {
return err
}
backup_vault_id := d.Get("backup_vault_id").(string)
id := fmt.Sprintf("projects/%s/locations/%s/backupVaults/%s", project, location, backup_vault_id)
d.SetId(id)
err = resourceBackupDRBackupVaultRead(d, meta)
if err != nil {
return err
}
if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package backupdr_test

import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"testing"
)

func TestAccDataSourceGoogleBackupDRBackupVault_basic(t *testing.T) {
t.Parallel()

random_suffix := acctest.RandString(t, 10)
context := map[string]interface{}{
"random_suffix": random_suffix,
}
id := "tf-test-bv-" + random_suffix
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBackupDRBackupVaultDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleBackupDRBackupVault_basic(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_backup_dr_backup_vault.fetch-bv", "backup_vault_id", id),
),
},
},
})
}

func testAccDataSourceGoogleBackupDRBackupVault_basic(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_backup_dr_backup_vault" "test-bv" {
location = "us-central1"
backup_vault_id = "tf-test-bv-%{random_suffix}"
description = "This is a a backup vault built by Terraform."
backup_minimum_enforced_retention_duration = "100000s"
force_update = "true"
force_delete = "true"
allow_missing = "true"
ignore_backup_plan_references = "false"
ignore_inactive_datasources = "false"
}
data "google_backup_dr_backup_vault" "fetch-bv" {
location = "us-central1"
backup_vault_id = google_backup_dr_backup_vault.test-bv.backup_vault_id
}
`, context)
}
34 changes: 34 additions & 0 deletions website/docs/d/backup_dr_backup_vault.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
subcategory: "Backup and DR BackupVault"
description: |-
Get information about a Backupdr BackupVault.
---

# google_backup_dr_backup_vault

A Backup and DRBackupVault.

## Example Usage

```hcl
data "google_backup_dr_backup_vault" "my-backup-vault" {
location = "us-central1"
backup_vault_id="bv-1"
}
```

## Argument Reference

The following arguments are supported:

* `location` - (Required) The location in which the Backup Vault resource belongs.
* `backup_vault_id` - (Required) The id of Backup Vault resource.

- - -

* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

## Attributes Reference

See [google_backup_dr_backup_vault](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/backup_dr_backup_vault) resource for details of the available attributes.

0 comments on commit 3618f5b

Please sign in to comment.