Skip to content

Commit 4cabc2c

Browse files
Feat: Add google_oracle_database_db_nodes datasource (#11988)
Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
1 parent 3108f69 commit 4cabc2c

File tree

4 files changed

+328
-0
lines changed

4 files changed

+328
-0
lines changed

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
168168
"google_monitoring_app_engine_service": monitoring.DataSourceMonitoringServiceAppEngine(),
169169
"google_monitoring_uptime_check_ips": monitoring.DataSourceGoogleMonitoringUptimeCheckIps(),
170170
"google_netblock_ip_ranges": resourcemanager.DataSourceGoogleNetblockIpRanges(),
171+
"google_oracle_database_db_nodes": oracledatabase.DataSourceOracleDatabaseDbNodes(),
171172
"google_oracle_database_db_servers": oracledatabase.DataSourceOracleDatabaseDbServers(),
172173
"google_oracle_database_cloud_vm_cluster": oracledatabase.DataSourceOracleDatabaseCloudVmCluster(),
173174
"google_oracle_database_cloud_exadata_infrastructure":oracledatabase.DataSourceOracleDatabaseCloudExadataInfrastructure(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
package oracledatabase
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
8+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
9+
)
10+
11+
func DataSourceOracleDatabaseDbNodes() *schema.Resource {
12+
dsSchema := map[string]*schema.Schema{
13+
"project": {
14+
Type: schema.TypeString,
15+
Optional: true,
16+
Description: "The ID of the project in which the dataset is located. If it is not provided, the provider project is used.",
17+
},
18+
"location": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
Description: "location",
22+
},
23+
"cloud_vm_cluster": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
Description: "vmcluster",
27+
},
28+
"db_nodes": {
29+
Type: schema.TypeList,
30+
Computed: true,
31+
Elem: &schema.Resource{
32+
Schema: map[string]*schema.Schema{
33+
"name": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: "The dbnode name",
37+
},
38+
"properties": {
39+
Type: schema.TypeList,
40+
Computed: true,
41+
Elem: &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"ocid": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "Output only",
47+
},
48+
"ocpu_count": {
49+
Type: schema.TypeInt,
50+
Computed: true,
51+
Description: "Output only",
52+
},
53+
"memory_size_gb": {
54+
Type: schema.TypeInt,
55+
Computed: true,
56+
Description: "Output only",
57+
},
58+
"db_node_storage_size_gb": {
59+
Type: schema.TypeInt,
60+
Computed: true,
61+
Description: "Output only",
62+
},
63+
"db_server_ocid": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
Description: "Output only",
67+
},
68+
"hostname": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
Description: "Output only",
72+
},
73+
"state": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
Description: "Output only",
77+
},
78+
"total_cpu_core_count": {
79+
Type: schema.TypeInt,
80+
Computed: true,
81+
Description: "Output only",
82+
},
83+
},
84+
},
85+
},
86+
},
87+
},
88+
},
89+
}
90+
return &schema.Resource{
91+
Read: DataSourceOracleDatabaseDbNodesRead,
92+
Schema: dsSchema,
93+
}
94+
}
95+
96+
func DataSourceOracleDatabaseDbNodesRead(d *schema.ResourceData, meta interface{}) error {
97+
config := meta.(*transport_tpg.Config)
98+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
99+
if err != nil {
100+
return err
101+
}
102+
url, err := tpgresource.ReplaceVars(d, config, "{{OracleDatabaseBasePath}}projects/{{project}}/locations/{{location}}/cloudVmClusters/{{cloud_vm_cluster}}/dbNodes")
103+
if err != nil {
104+
return err
105+
}
106+
billingProject := ""
107+
project, err := tpgresource.GetProject(d, config)
108+
if err != nil {
109+
return fmt.Errorf("Error fetching project for DbNode: %s", err)
110+
}
111+
billingProject = project
112+
// err == nil indicates that the billing_project value was found
113+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
114+
billingProject = bp
115+
}
116+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
117+
Config: config,
118+
Method: "GET",
119+
Project: billingProject,
120+
RawURL: url,
121+
UserAgent: userAgent,
122+
})
123+
124+
if err != nil {
125+
return fmt.Errorf("Error reading DbNode: %s", err)
126+
}
127+
128+
if err := d.Set("project", project); err != nil {
129+
return fmt.Errorf("Error reading DbNode: %s", err)
130+
}
131+
if err := d.Set("db_nodes", flattenOracleDatabaseDbNodes(res["dbNodes"], d, config)); err != nil {
132+
return fmt.Errorf("Error reading DbNode: %s", err)
133+
}
134+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/cloudVmClusters/{{cloud_vm_cluster}}/dbNodes")
135+
if err != nil {
136+
return fmt.Errorf("Error constructing id: %s", err)
137+
}
138+
d.SetId(id)
139+
return nil
140+
}
141+
142+
func flattenOracleDatabaseDbNodes(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) []map[string]interface{} {
143+
if v == nil {
144+
return nil
145+
}
146+
l := v.([]interface{})
147+
transformed := make([]map[string]interface{}, 0)
148+
for _, raw := range l {
149+
original := raw.(map[string]interface{})
150+
transformed = append(transformed, map[string]interface{}{
151+
"name": flattenOracleDatabaseDbNodeName(original["name"], d, config),
152+
"properties": flattenOracleDatabaseDbNodeProperties(original["properties"], d, config),
153+
})
154+
}
155+
156+
return transformed
157+
}
158+
159+
func flattenOracleDatabaseDbNodeName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
160+
return v
161+
}
162+
163+
func flattenOracleDatabaseDbNodeProperties(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
164+
if v == nil {
165+
return nil
166+
}
167+
original := v.(map[string]interface{})
168+
if len(original) == 0 {
169+
return nil
170+
}
171+
transformed := make(map[string]interface{})
172+
transformed["ocid"] = flattenOracleDatabaseDbNodePropertiesOcid(original["ocid"], d, config)
173+
transformed["ocpu_count"] = flattenOracleDatabaseDbNodePropertiesOcpuCount(original["ocpuCount"], d, config)
174+
transformed["memory_size_gb"] = flattenOracleDatabaseDbNodePropertiesMemorySizeGb(original["memorySizeGb"], d, config)
175+
transformed["db_node_storage_size_gb"] = flattenOracleDatabaseDbNodePropertiesDbNodeStorageSizeGb(original["dbNodeStorageSizeGb"], d, config)
176+
transformed["db_server_ocid"] = flattenOracleDatabaseDbNodePropertiesDbServerOcid(original["dbServerOcid"], d, config)
177+
transformed["hostname"] = flattenOracleDatabaseDbNodePropertiesHostname(original["hostname"], d, config)
178+
transformed["state"] = flattenOracleDatabaseDbNodePropertiesState(original["state"], d, config)
179+
transformed["total_cpu_core_count"] = flattenOracleDatabaseDbNodePropertiesTotalCpuCoreCount(original["totalCpuCoreCount"], d, config)
180+
181+
return []interface{}{transformed}
182+
}
183+
184+
func flattenOracleDatabaseDbNodePropertiesOcid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
185+
return v
186+
}
187+
188+
func flattenOracleDatabaseDbNodePropertiesOcpuCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
189+
return v
190+
}
191+
192+
func flattenOracleDatabaseDbNodePropertiesMemorySizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
193+
return v
194+
}
195+
196+
func flattenOracleDatabaseDbNodePropertiesDbNodeStorageSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
197+
return v
198+
}
199+
200+
func flattenOracleDatabaseDbNodePropertiesDbServerOcid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
201+
return v
202+
}
203+
204+
func flattenOracleDatabaseDbNodePropertiesHostname(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
205+
return v
206+
}
207+
208+
func flattenOracleDatabaseDbNodePropertiesState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
209+
return v
210+
}
211+
212+
func flattenOracleDatabaseDbNodePropertiesTotalCpuCoreCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
213+
return v
214+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package oracledatabase_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
8+
"github.com/hashicorp/terraform-provider-google/google/acctest"
9+
)
10+
11+
func TestAccOracleDatabaseDbNodes_basic(t *testing.T) {
12+
t.Parallel()
13+
acctest.VcrTest(t, resource.TestCase{
14+
PreCheck: func() { acctest.AccTestPreCheck(t) },
15+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccOracleDatabaseDbNodesConfig(),
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.#"),
21+
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.0.name"),
22+
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.0.properties.#"),
23+
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.1.name"),
24+
resource.TestCheckResourceAttrSet("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.1.properties.#"),
25+
resource.TestCheckResourceAttr("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.0.properties.0.state", "AVAILABLE"),
26+
resource.TestCheckResourceAttr("data.google_oracle_database_db_nodes.my_db_nodes", "db_nodes.1.properties.0.state", "AVAILABLE"),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
func testAccOracleDatabaseDbNodesConfig() string {
34+
return fmt.Sprintf(`
35+
data "google_oracle_database_db_nodes" "my_db_nodes"{
36+
location = "us-east4"
37+
project = "oci-terraform-testing"
38+
cloud_vm_cluster = "ofake-do-not-delete-tf-vmcluster"
39+
}
40+
`)
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
subcategory: "Oracle Database"
3+
description: |-
4+
List all database nodes of a Cloud VmCluster.
5+
---
6+
7+
# google_oracle_database_db_nodes
8+
9+
List all DbNodes of a Cloud VmCluster.
10+
11+
For more information see the
12+
[API](https://cloud.google.com/oracle/database/docs/reference/rest/v1/projects.locations.cloudVmClusters.dbNodes).
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "google_oracle_database_db_nodes" "my_db_nodes"{
18+
location = "us-east4"
19+
cloud_vm_cluster = "vmcluster-id"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `cloud_vm_cluster` - (Required) The ID of the VM Cluster.
28+
29+
* `location` - (Required) The location of the resource.
30+
31+
* `project` - (Optional) The project in which the resource belongs. If it
32+
is not provided, the provider project is used.
33+
34+
## Attributes reference
35+
36+
The following attributes are exported:
37+
38+
* `db_nodes` - List of dbNodes. Structure is [documented below](#nested_dbnodes).
39+
40+
<a name="nested_dbnodes"></a> The `db_nodes` block supports:
41+
42+
* `name` - The name of the database node resource in the following format: projects/{project}/locations/{location}/cloudVmClusters/{cloudVmCluster}/dbNodes/{db_node}
43+
44+
* `properties` - Various properties of the database node. Structure is [documented below](#nested_properties).
45+
46+
<a name="nested_properties"></a> The `properties` block supports:
47+
48+
* `ocid`- OCID of database node.
49+
50+
* `ocpu_count` - OCPU count per database node.
51+
52+
* `memory_size_gb` - The allocated memory in GBs on the database node.
53+
54+
* `db_node_storage_size_gb` - The allocated local node storage in GBs on the database node.
55+
56+
* `db_server_ocid` - The OCID of the Database server associated with the database node.
57+
58+
* `hostname` - The host name for the database node.
59+
60+
* `state` - State of the database node.
61+
<a name="nested_states"></a>Possible values for `state` are:<br>
62+
`PROVISIONING` - Indicates that the resource is being provisioned.<br>
63+
`AVAILABLE` - Indicates that the resource is available.<br>
64+
`UPDATING` - Indicates that the resource is being updated.<br>
65+
`STOPPING` - Indicates that the resource is being stopped.<br>
66+
`STOPPED` - Indicates that the resource is stopped.<br>
67+
`STARTING` - Indicates that the resource is being started.<br>
68+
`TERMINATING` - Indicates that the resource is being terminated.<br>
69+
`TERMINATED` - Indicates that the resource is terminated.<br>
70+
`FAILED` - Indicates that the resource has failed.<br>
71+
72+
* `total_cpu_core_count` - The total number of CPU cores reserved on the database node.

0 commit comments

Comments
 (0)