|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package oracledatabase |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 10 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 11 | +) |
| 12 | + |
| 13 | +func DataSourceOracleDatabaseDbNodes() *schema.Resource { |
| 14 | + dsSchema := map[string]*schema.Schema{ |
| 15 | + "project": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Optional: true, |
| 18 | + Description: "The ID of the project in which the dataset is located. If it is not provided, the provider project is used.", |
| 19 | + }, |
| 20 | + "location": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + Description: "location", |
| 24 | + }, |
| 25 | + "cloud_vm_cluster": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + Description: "vmcluster", |
| 29 | + }, |
| 30 | + "db_nodes": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Computed: true, |
| 33 | + Elem: &schema.Resource{ |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "name": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: "The dbnode name", |
| 39 | + }, |
| 40 | + "properties": { |
| 41 | + Type: schema.TypeList, |
| 42 | + Computed: true, |
| 43 | + Elem: &schema.Resource{ |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "ocid": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "Output only", |
| 49 | + }, |
| 50 | + "ocpu_count": { |
| 51 | + Type: schema.TypeInt, |
| 52 | + Computed: true, |
| 53 | + Description: "Output only", |
| 54 | + }, |
| 55 | + "memory_size_gb": { |
| 56 | + Type: schema.TypeInt, |
| 57 | + Computed: true, |
| 58 | + Description: "Output only", |
| 59 | + }, |
| 60 | + "db_node_storage_size_gb": { |
| 61 | + Type: schema.TypeInt, |
| 62 | + Computed: true, |
| 63 | + Description: "Output only", |
| 64 | + }, |
| 65 | + "db_server_ocid": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Output only", |
| 69 | + }, |
| 70 | + "hostname": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "Output only", |
| 74 | + }, |
| 75 | + "state": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Output only", |
| 79 | + }, |
| 80 | + "total_cpu_core_count": { |
| 81 | + Type: schema.TypeInt, |
| 82 | + Computed: true, |
| 83 | + Description: "Output only", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + return &schema.Resource{ |
| 93 | + Read: DataSourceOracleDatabaseDbNodesRead, |
| 94 | + Schema: dsSchema, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func DataSourceOracleDatabaseDbNodesRead(d *schema.ResourceData, meta interface{}) error { |
| 99 | + config := meta.(*transport_tpg.Config) |
| 100 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + url, err := tpgresource.ReplaceVars(d, config, "{{OracleDatabaseBasePath}}projects/{{project}}/locations/{{location}}/cloudVmClusters/{{cloud_vm_cluster}}/dbNodes") |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + billingProject := "" |
| 109 | + project, err := tpgresource.GetProject(d, config) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("Error fetching project for DbNode: %s", err) |
| 112 | + } |
| 113 | + billingProject = project |
| 114 | + // err == nil indicates that the billing_project value was found |
| 115 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 116 | + billingProject = bp |
| 117 | + } |
| 118 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 119 | + Config: config, |
| 120 | + Method: "GET", |
| 121 | + Project: billingProject, |
| 122 | + RawURL: url, |
| 123 | + UserAgent: userAgent, |
| 124 | + }) |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("Error reading DbNode: %s", err) |
| 128 | + } |
| 129 | + |
| 130 | + if err := d.Set("project", project); err != nil { |
| 131 | + return fmt.Errorf("Error reading DbNode: %s", err) |
| 132 | + } |
| 133 | + if err := d.Set("db_nodes", flattenOracleDatabaseDbNodes(res["dbNodes"], d, config)); err != nil { |
| 134 | + return fmt.Errorf("Error reading DbNode: %s", err) |
| 135 | + } |
| 136 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/cloudVmClusters/{{cloud_vm_cluster}}/dbNodes") |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("Error constructing id: %s", err) |
| 139 | + } |
| 140 | + d.SetId(id) |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +func flattenOracleDatabaseDbNodes(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) []map[string]interface{} { |
| 145 | + if v == nil { |
| 146 | + return nil |
| 147 | + } |
| 148 | + l := v.([]interface{}) |
| 149 | + transformed := make([]map[string]interface{}, 0) |
| 150 | + for _, raw := range l { |
| 151 | + original := raw.(map[string]interface{}) |
| 152 | + transformed = append(transformed, map[string]interface{}{ |
| 153 | + "name": flattenOracleDatabaseDbNodeName(original["name"], d, config), |
| 154 | + "properties": flattenOracleDatabaseDbNodeProperties(original["properties"], d, config), |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + return transformed |
| 159 | +} |
| 160 | + |
| 161 | +func flattenOracleDatabaseDbNodeName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 162 | + return v |
| 163 | +} |
| 164 | + |
| 165 | +func flattenOracleDatabaseDbNodeProperties(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 166 | + if v == nil { |
| 167 | + return nil |
| 168 | + } |
| 169 | + original := v.(map[string]interface{}) |
| 170 | + if len(original) == 0 { |
| 171 | + return nil |
| 172 | + } |
| 173 | + transformed := make(map[string]interface{}) |
| 174 | + transformed["ocid"] = flattenOracleDatabaseDbNodePropertiesOcid(original["ocid"], d, config) |
| 175 | + transformed["ocpu_count"] = flattenOracleDatabaseDbNodePropertiesOcpuCount(original["ocpuCount"], d, config) |
| 176 | + transformed["memory_size_gb"] = flattenOracleDatabaseDbNodePropertiesMemorySizeGb(original["memorySizeGb"], d, config) |
| 177 | + transformed["db_node_storage_size_gb"] = flattenOracleDatabaseDbNodePropertiesDbNodeStorageSizeGb(original["dbNodeStorageSizeGb"], d, config) |
| 178 | + transformed["db_server_ocid"] = flattenOracleDatabaseDbNodePropertiesDbServerOcid(original["dbServerOcid"], d, config) |
| 179 | + transformed["hostname"] = flattenOracleDatabaseDbNodePropertiesHostname(original["hostname"], d, config) |
| 180 | + transformed["state"] = flattenOracleDatabaseDbNodePropertiesState(original["state"], d, config) |
| 181 | + transformed["total_cpu_core_count"] = flattenOracleDatabaseDbNodePropertiesTotalCpuCoreCount(original["totalCpuCoreCount"], d, config) |
| 182 | + |
| 183 | + return []interface{}{transformed} |
| 184 | +} |
| 185 | + |
| 186 | +func flattenOracleDatabaseDbNodePropertiesOcid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 187 | + return v |
| 188 | +} |
| 189 | + |
| 190 | +func flattenOracleDatabaseDbNodePropertiesOcpuCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 191 | + return v |
| 192 | +} |
| 193 | + |
| 194 | +func flattenOracleDatabaseDbNodePropertiesMemorySizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 195 | + return v |
| 196 | +} |
| 197 | + |
| 198 | +func flattenOracleDatabaseDbNodePropertiesDbNodeStorageSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 199 | + return v |
| 200 | +} |
| 201 | + |
| 202 | +func flattenOracleDatabaseDbNodePropertiesDbServerOcid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 203 | + return v |
| 204 | +} |
| 205 | + |
| 206 | +func flattenOracleDatabaseDbNodePropertiesHostname(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 207 | + return v |
| 208 | +} |
| 209 | + |
| 210 | +func flattenOracleDatabaseDbNodePropertiesState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 211 | + return v |
| 212 | +} |
| 213 | + |
| 214 | +func flattenOracleDatabaseDbNodePropertiesTotalCpuCoreCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 215 | + return v |
| 216 | +} |
0 commit comments