Skip to content

Commit

Permalink
Data source addition for Backupdr Beta (GoogleCloudPlatform#8854)
Browse files Browse the repository at this point in the history
  • Loading branch information
niharika-98 authored and jialei-chen committed Nov 29, 2023
1 parent 18b9038 commit 78aead8
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mmv1/third_party/terraform/provider/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_alloydb_supported_database_flags": alloydb.DataSourceAlloydbSupportedDatabaseFlags(),
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
<% unless version == 'ga' -%>
"google_backup_dr_management_server": backupdr.DataSourceGoogleCloudBackupDRService(),
<% end -%>
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
"google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<% autogen_exception -%>
package backupdr
<% unless version == 'ga' -%>

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"
"strings"
)

func DataSourceGoogleCloudBackupDRService() *schema.Resource {

dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBackupDRManagementServer().Schema)
tpgresource.AddRequiredFieldsToSchema(dsSchema, "location")

return &schema.Resource{
Read: dataSourceGoogleCloudBackupDRServiceRead,
Schema: dsSchema,
}
}
func flattenBackupDRManagementServerName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenBackupDRManagementServerResourceResp(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) map[string]interface{} {
if v == nil {
fmt.Printf("Interface is nil: %s", v)
}
fmt.Printf("Interface is : %s", v)
l := v.([]interface{})
for _, raw := range l {
// Management server is a singleton resource. It is only present in one location per project. Hence returning only resource present.
return flattenBackupDRManagementServerResource(raw, d, config)
}
return nil
}
func flattenBackupDRManagementServerResource(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) map[string]interface{} {
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["type"] = flattenBackupDRManagementServerType(original["type"], d, config)
transformed["networks"] = flattenBackupDRManagementServerNetworks(original["networks"], d, config)
transformed["oauth2ClientId"] = flattenBackupDRManagementServerOauth2ClientId(original["oauth2ClientId"], d, config)
transformed["managementUri"] = flattenBackupDRManagementServerManagementUri(original["managementUri"], d, config)
transformed["name"] = flattenBackupDRManagementServerName(original["name"], d, config)
return transformed
}

func dataSourceGoogleCloudBackupDRServiceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
billingProject := project
url, err := tpgresource.ReplaceVars(d, config, "{{BackupDRBasePath}}projects/{{project}}/locations/{{location}}/managementServers")
if err != nil {
return err
}
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
})
if err != nil {
return fmt.Errorf("Error reading ManagementServer: %s", err)
}
resourceResponse := flattenBackupDRManagementServerResourceResp(res["managementServers"], d, config)
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading ManagementServer: %s", err)
}

if err := d.Set("type", resourceResponse["type"]); err != nil {
return fmt.Errorf("Error reading ManagementServer: %s", err)
}
if err := d.Set("networks", resourceResponse["networks"]); err != nil {
return fmt.Errorf("Error reading ManagementServer: %s", err)
}
if err := d.Set("oauth2_client_id", resourceResponse["oauth2ClientId"]); err != nil {
return fmt.Errorf("Error reading ManagementServer: %s", err)
}
if err := d.Set("management_uri", resourceResponse["managementUri"]); err != nil {
return fmt.Errorf("Error reading ManagementServer: %s", err)
}

id := fmt.Sprintf("%s", resourceResponse["name"])
d.SetId(id)
name := id[strings.LastIndex(id, "/")+1:]
d.Set("name", name)
return nil
}
<% end -%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<% autogen_exception -%>
package backupdr_test
<% unless version == 'ga' -%>

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

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

context := map[string]interface{}{
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "backupdr-managementserver-basic"),
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleBackupDRManagementServer_basic(context),
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceState("data.google_backup_dr_management_server.foo", "google_backup_dr_management_server.foo"),
),
},
},
})
}

func testAccDataSourceGoogleBackupDRManagementServer_basic(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_compute_network" "default" {
name = "%{network_name}"
}

resource "google_backup_dr_management_server" "foo" {
location = "us-central1"
name = "tf-test-management-server%{random_suffix}"
type = "BACKUP_RESTORE"
networks {
network = data.google_compute_network.default.id
peering_mode = "PRIVATE_SERVICE_ACCESS"
}
}

data "google_backup_dr_management_server" "foo" {
location = "us-central1"
depends_on = [ google_backup_dr_management_server.foo ]
}
`, context)
}
<% end -%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
subcategory: "BackupDR Management Server"
description: |-
Get information about a Backupdr Management server.
---

# google\_backup\_dr\_management\_server

Get information about a Google Backup DR Management server.

~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider.
See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources.

## Example Usage

```hcl
data google_backup_dr_management_server my-backup-dr-management-server {
location = "us-central1"
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Required) The region in which the management server resource belongs.

- - -

## Attributes Reference

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

0 comments on commit 78aead8

Please sign in to comment.