From 3a7cb23ab04d5bc7731412f549a37a88e04918f7 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 17 Oct 2024 19:20:08 +0000 Subject: [PATCH] Feat add oracledatabase_autonomous_databases datasource (#12018) [upstream:6009556728d64d52ba0f75b95aeed90046434644] Signed-off-by: Modular Magician --- .changelog/12018.txt | 3 + .../provider/provider_mmv1_resources.go | 1 + ...ce_oracle_database_autonomous_databases.go | 114 ++++++++++++++++++ ...acle_database_autonomous_databases_test.go | 43 +++++++ ...atabase_autonomous_databases.html.markdown | 38 ++++++ 5 files changed, 199 insertions(+) create mode 100644 .changelog/12018.txt create mode 100644 google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases.go create mode 100644 google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases_test.go create mode 100644 website/docs/d/oracle_database_autonomous_databases.html.markdown diff --git a/.changelog/12018.txt b/.changelog/12018.txt new file mode 100644 index 0000000000..18b0928ca7 --- /dev/null +++ b/.changelog/12018.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +`google_oracle_database_autonomous_databases` +``` \ No newline at end of file diff --git a/google-beta/provider/provider_mmv1_resources.go b/google-beta/provider/provider_mmv1_resources.go index 84ecfbc486..14b4044d4e 100644 --- a/google-beta/provider/provider_mmv1_resources.go +++ b/google-beta/provider/provider_mmv1_resources.go @@ -293,6 +293,7 @@ var handwrittenDatasources = map[string]*schema.Resource{ "google_monitoring_app_engine_service": monitoring.DataSourceMonitoringServiceAppEngine(), "google_monitoring_uptime_check_ips": monitoring.DataSourceGoogleMonitoringUptimeCheckIps(), "google_netblock_ip_ranges": resourcemanager.DataSourceGoogleNetblockIpRanges(), + "google_oracle_database_autonomous_databases": oracledatabase.DataSourceOracleDatabaseAutonomousDatabases(), "google_oracle_database_db_nodes": oracledatabase.DataSourceOracleDatabaseDbNodes(), "google_oracle_database_db_servers": oracledatabase.DataSourceOracleDatabaseDbServers(), "google_oracle_database_cloud_exadata_infrastructures": oracledatabase.DataSourceOracleDatabaseCloudExadataInfrastructures(), diff --git a/google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases.go b/google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases.go new file mode 100644 index 0000000000..25d71a0610 --- /dev/null +++ b/google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases.go @@ -0,0 +1,114 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package oracledatabase + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +func DataSourceOracleDatabaseAutonomousDatabases() *schema.Resource { + dsSchema := map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Optional: true, + Description: "The ID of the project in which the dataset is located. If it is not provided, the provider project is used.", + }, + "location": { + Type: schema.TypeString, + Required: true, + Description: "location", + }, + "autonomous_databases": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: tpgresource.DatasourceSchemaFromResourceSchema(ResourceOracleDatabaseAutonomousDatabase().Schema), + }, + }, + } + return &schema.Resource{ + Read: dataSourceOracleDatabaseAutonomousDatabasesRead, + Schema: dsSchema, + } + +} + +func dataSourceOracleDatabaseAutonomousDatabasesRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + url, err := tpgresource.ReplaceVars(d, config, "{{OracleDatabaseBasePath}}projects/{{project}}/locations/{{location}}/autonomousDatabases") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + + billingProject := "" + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for autonomousDatabases: %s", err) + } + billingProject = project + // err == nil indicates that the billing_project value was found + 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 autonomousDatabases: %s", err) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error setting autonomousDatabases project: %s", err) + } + + if err := d.Set("autonomous_databases", flattenOracleDatabaseautonomousDatabases(res["autonomousDatabases"], d, config)); err != nil { + return fmt.Errorf("Error setting autonomousDatabases: %s", err) + } + + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/autonomousDatabases") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return nil +} + +func flattenOracleDatabaseautonomousDatabases(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) []map[string]interface{} { + if v == nil { + return nil + } + l := v.([]interface{}) + transformed := make([]map[string]interface{}, 0) + for _, raw := range l { + original := raw.(map[string]interface{}) + transformed = append(transformed, map[string]interface{}{ + "name": flattenOracleDatabaseAutonomousDatabaseName(original["name"], d, config), + "database": flattenOracleDatabaseAutonomousDatabaseDatabase(original["database"], d, config), + "display_name": flattenOracleDatabaseAutonomousDatabaseDisplayName(original["displayName"], d, config), + "entitlement_id": flattenOracleDatabaseAutonomousDatabaseEntitlementId(original["entitlementId"], d, config), + "properties": flattenOracleDatabaseAutonomousDatabaseProperties(original["properties"], d, config), + "labels": flattenOracleDatabaseAutonomousDatabaseLabels(original["labels"], d, config), + "network": flattenOracleDatabaseAutonomousDatabaseNetwork(original["network"], d, config), + "cidr": flattenOracleDatabaseAutonomousDatabaseCidr(original["cidr"], d, config), + "create_time": flattenOracleDatabaseAutonomousDatabaseCreateTime(original["createTime"], d, config), + "terraform_labels": flattenOracleDatabaseAutonomousDatabaseTerraformLabels(original["labels"], d, config), + "effective_labels": flattenOracleDatabaseAutonomousDatabaseEffectiveLabels(original["labels"], d, config), + }) + } + return transformed +} diff --git a/google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases_test.go b/google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases_test.go new file mode 100644 index 0000000000..7a2d331425 --- /dev/null +++ b/google-beta/services/oracledatabase/data_source_oracle_database_autonomous_databases_test.go @@ -0,0 +1,43 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package oracledatabase_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" +) + +func TestAccOracleDatabaseAutonomousDatabases_basic(t *testing.T) { + t.Parallel() + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + Steps: []resource.TestStep{ + { + Config: testAccOracleDatabaseAutonomousDatabases_basic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.#"), + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.0.display_name"), + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.0.cidr"), + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.0.network"), + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.0.entitlement_id"), + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.0.database"), + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.0.properties.#"), + resource.TestCheckResourceAttrSet("data.google_oracle_database_autonomous_databases.my-adbs", "autonomous_databases.0.properties.0.state"), + ), + }, + }, + }) +} + +func testAccOracleDatabaseAutonomousDatabases_basic() string { + return fmt.Sprintf(` +data "google_oracle_database_autonomous_databases" "my-adbs"{ + location = "us-east4" + project = "oci-terraform-testing" +} +`) +} diff --git a/website/docs/d/oracle_database_autonomous_databases.html.markdown b/website/docs/d/oracle_database_autonomous_databases.html.markdown new file mode 100644 index 0000000000..7b160f4a53 --- /dev/null +++ b/website/docs/d/oracle_database_autonomous_databases.html.markdown @@ -0,0 +1,38 @@ +--- +subcategory: "Oracle Database" +description: |- + List all AutonomousDatabases. +--- + +# google_oracle_database_autonomous_databases + +List all AutonomousDatabases. + +For more information see the +[API](https://cloud.google.com/oracle/database/docs/reference/rest/v1/projects.locations.autonomousDatabases). + +## Example Usage + +```hcl +data "google_oracle_database_autonomous_databases" "my-adbs"{ + location = "us-east4" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `location` - (Required) The location of the resource. + +- - - +* `project` - (Optional) The project to which the resource belongs. If it + is not provided, the provider project is used. + +## Attributes Reference + +The following attributes are exported: + +* `AutonomousDatabases` - A list of AutonomousDatabases. + +See [google_oracle_database_autonomous_database](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_oracle_database_autonomous_database#argument-reference) resource for details of the available attributes.