Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add google_oracle_database_db_nodes datasource #11988

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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_db_nodes": oracledatabase.DataSourceOracleDatabaseDbNodes(),
"google_oracle_database_db_servers": oracledatabase.DataSourceOracleDatabaseDbServers(),
"google_organization": resourcemanager.DataSourceGoogleOrganization(),
"google_privateca_certificate_authority": privateca.DataSourcePrivatecaCertificateAuthority(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package oracledatabase

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 DataSourceOracleDatabaseDbNodes() *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",
},
"cloud_vm_cluster": {
Type: schema.TypeString,
Required: true,
Description: "vmcluster",
},
"db_nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
melinath marked this conversation as resolved.
Show resolved Hide resolved
Description: "The dbnode name",
},
"properties": {
Type: schema.TypeList,
Optional: true,
melinath marked this conversation as resolved.
Show resolved Hide resolved
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ocid": {
Type: schema.TypeString,
Computed: true,
Description: "Output only",
},
"ocpu_count": {
Type: schema.TypeInt,
Optional: true,
melinath marked this conversation as resolved.
Show resolved Hide resolved
Description: "Output only",
},
"memory_size_gb": {
Type: schema.TypeInt,
Computed: true,
Description: "Output only",
},
"db_node_storage_size_gb": {
Type: schema.TypeInt,
Computed: true,
Description: "Output only",
},
"db_server_ocid": {
Type: schema.TypeString,
Computed: true,
Description: "Output only",
},
"hostname": {
Type: schema.TypeString,
Computed: true,
Description: "Output only",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "Output only",
},
"total_cpu_core_count": {
Type: schema.TypeInt,
Computed: true,
Description: "Output only",
},
},
},
},
},
},
},
}
return &schema.Resource{
Read: DataSourceOracleDatabaseDbNodesRead,
Schema: dsSchema,
}
}

func DataSourceOracleDatabaseDbNodesRead(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}}/cloudVmClusters/{{cloud_vm_cluster}}/dbNodes")
if err != nil {
return err
}
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for DbNode: %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 DbNode: %s", err)
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading DbNode: %s", err)
}
if err := d.Set("db_nodes", flattenOracleDatabaseDbNodes(res["dbNodes"], d, config)); err != nil {
return fmt.Errorf("Error reading DbNode: %s", err)
}
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/cloudVmClusters/{{cloud_vm_cluster}}/dbNodes")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return nil
}

func flattenOracleDatabaseDbNodes(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": flattenOracleDatabaseDbNodeName(original["name"], d, config),
"properties": flattenOracleDatabaseDbNodeProperties(original["properties"], d, config),
})
}

return transformed
}

func flattenOracleDatabaseDbNodeName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodeProperties(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["ocid"] = flattenOracleDatabaseDbNodePropertiesOcid(original["ocid"], d, config)
transformed["ocpu_count"] = flattenOracleDatabaseDbNodePropertiesOcpuCount(original["ocpuCount"], d, config)
transformed["memory_size_gb"] = flattenOracleDatabaseDbNodePropertiesMemorySizeGb(original["memorySizeGb"], d, config)
transformed["db_node_storage_size_gb"] = flattenOracleDatabaseDbNodePropertiesDbNodeStorageSizeGb(original["dbNodeStorageSizeGb"], d, config)
transformed["db_server_ocid"] = flattenOracleDatabaseDbNodePropertiesDbServerOcid(original["dbServerOcid"], d, config)
transformed["hostname"] = flattenOracleDatabaseDbNodePropertiesHostname(original["hostname"], d, config)
transformed["state"] = flattenOracleDatabaseDbNodePropertiesState(original["state"], d, config)
transformed["total_cpu_core_count"] = flattenOracleDatabaseDbNodePropertiesTotalCpuCoreCount(original["totalCpuCoreCount"], d, config)

return []interface{}{transformed}
}

func flattenOracleDatabaseDbNodePropertiesOcid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodePropertiesOcpuCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodePropertiesMemorySizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodePropertiesDbNodeStorageSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodePropertiesDbServerOcid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodePropertiesHostname(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodePropertiesState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenOracleDatabaseDbNodePropertiesTotalCpuCoreCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package oracledatabase_test

import (
"fmt"
"testing"

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

func TestAccOracleDatabaseDbNodes_basic(t *testing.T) {
t.Parallel()
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccOracleDatabaseDbNodesConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.#"),
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.0.name"),
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.0.properties.#"),
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.1.name"),
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.1.properties.#"),
resource.TestCheckResourceAttr("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.0.properties.0.state", "AVAILABLE"),
resource.TestCheckResourceAttr("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.1.properties.0.state", "AVAILABLE"),
),
},
},
})
}

func testAccOracleDatabaseDbNodesConfig() string {
return fmt.Sprintf(`
data "google_oracle_database_db_nodes" "my_db_nodes"{
location = "us-east4"
project = "oci-terraform-testing"
cloud_vm_cluster = "ofake-do-not-delete-tf-vmcluster"
melinath marked this conversation as resolved.
Show resolved Hide resolved
}
`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
subcategory: "Oracle Database"
description: |-
List all database nodes of a Cloud VmCluster.
---

# google_oracle_database_db_nodes

List all DbNodes of a Cloud VmCluster.
tulika-aakriti marked this conversation as resolved.
Show resolved Hide resolved

For more information see the
[API](https://cloud.google.com/oracle/database/docs/reference/rest/v1/projects.locations.cloudVmClusters.dbNodes).

## Example Usage

```hcl
data "google_oracle_database_db_nodes" "my_db_nodes"{
location = "us-east4"
cloud_vm_cluster = "vmcluster-id"
}
```

## Argument Reference

The following arguments are supported:

* `cloud_vm_cluster` - (Required) The ID of the VM Cluster.

* `location` - (Required) The location of the resource.

- - -
melinath marked this conversation as resolved.
Show resolved Hide resolved
* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

* `db_nodes` - (Output) List of dbNodes. Structure is [documented below](#nested_dbnodes).
melinath marked this conversation as resolved.
Show resolved Hide resolved

<a name="nested_dbnodes"></a> The `db_nodes` block supports:

* `name` - User friendly name for the resource.
melinath marked this conversation as resolved.
Show resolved Hide resolved

* `properties` - Various properties of the database node. Structure is [documented below](#nested_properties).

<a name="nested_properties"></a> The `properties` block supports:

* `ocid`- OCID of database node.

* `ocpu_count` - OCPU count per database node.

* `memory_size_gb` - The allocated memory in GBs on the database node.

* `db_node_storage_size_gb` - The allocated local node storage in GBs on the database node.

* `db_server_ocid` - The OCID of the Database server associated with the database node.

* `hostname` - The host name for the database node.

* `state` - State of the database node.
<a name="nested_states"></a>Allowed values for `state` are:<br>
melinath marked this conversation as resolved.
Show resolved Hide resolved
`STATE_UNSPECIFIED` - Default unspecified value.<br>
melinath marked this conversation as resolved.
Show resolved Hide resolved
`PROVISIONING` - Indicates that the resource is being provisioned.<br>
`AVAILABLE` - Indicates that the resource is available.<br>
`UPDATING` - Indicates that the resource is being updated.<br>
`STOPPING` - Indicates that the resource is being stopped.<br>
`STOPPED` - Indicates that the resource is stopped.<br>
`STARTING` - Indicates that the resource is being started.<br>
`TERMINATING` - Indicates that the resource is being terminated.<br>
`TERMINATED` - Indicates that the resource is terminated.<br>
`FAILED` - Indicates that the resource has failed.<br>

* `total_cpu_core_count` - The total number of CPU cores reserved on the database node.