-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data source addition for Backupdr Beta (#8854)
- Loading branch information
1 parent
073d61c
commit 191d979
Showing
4 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
mmv1/third_party/terraform/services/backupdr/data_source_backup_dr_management_server.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%> |
56 changes: 56 additions & 0 deletions
56
...ird_party/terraform/services/backupdr/data_source_backup_dr_management_server_test.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%> |
32 changes: 32 additions & 0 deletions
32
.../third_party/terraform/website/docs/d/backup_dr_management_server.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |