forked from ovh/terraform-provider-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_cloud_project_database.go
273 lines (260 loc) · 7.7 KB
/
data_cloud_project_database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package ovh
import (
"context"
"fmt"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCloudProjectDatabase() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectDatabaseRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"engine": {
Type: schema.TypeString,
Description: "Name of the engine of the service",
Required: true,
},
"id": {
Type: schema.TypeString,
Description: "Cluster ID",
Required: true,
},
//Computed
"backup_regions": {
Type: schema.TypeList,
Description: "List of region where backups are pushed",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"backup_time": {
Type: schema.TypeString,
Description: "Time on which backups start every day",
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Description: "Date of the creation of the cluster",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of the cluster",
Computed: true,
},
"endpoints": {
Type: schema.TypeList,
Description: "List of all endpoints of the service",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"component": {
Type: schema.TypeString,
Description: "Type of component the URI relates to",
Computed: true,
},
"domain": {
Type: schema.TypeString,
Description: "Domain of the cluster",
Computed: true,
},
"path": {
Type: schema.TypeString,
Description: "Path of the endpoint",
Computed: true,
},
"port": {
Type: schema.TypeInt,
Description: "Connection port for the endpoint",
Computed: true,
},
"scheme": {
Type: schema.TypeString,
Description: "Scheme used to generate the URI",
Computed: true,
},
"ssl": {
Type: schema.TypeBool,
Description: "Defines whether the endpoint uses SSL",
Computed: true,
},
"ssl_mode": {
Type: schema.TypeString,
Description: "SSL mode used to connect to the service if the SSL is enabled",
Computed: true,
},
"uri": {
Type: schema.TypeString,
Description: "URI of the endpoint",
Computed: true,
},
},
},
},
"flavor": {
Type: schema.TypeString,
Description: "The node flavor used for this cluster",
Computed: true,
},
"ip_restrictions": {
Type: schema.TypeSet,
Description: "IP Blocks authorized to access to the cluster",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Description: "Description of the IP restriction",
Computed: true,
},
"ip": {
Type: schema.TypeString,
Description: "Authorized IP",
Computed: true,
},
"status": {
Type: schema.TypeString,
Description: "Current status of the IP restriction",
Computed: true,
},
},
},
},
"kafka_rest_api": {
Type: schema.TypeBool,
Description: "Defines whether the REST API is enabled on a Kafka cluster",
Computed: true,
},
"kafka_schema_registry": {
Type: schema.TypeBool,
Description: "Defines whether the schema registry is enabled on a Kafka cluster",
Computed: true,
},
"maintenance_time": {
Type: schema.TypeString,
Description: "Time on which maintenances can start every day",
Computed: true,
},
"network_type": {
Type: schema.TypeString,
Description: "Type of network of the cluster",
Computed: true,
},
"nodes": {
Type: schema.TypeList,
Description: "List of nodes composing the service",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"network_id": {
Type: schema.TypeString,
Description: "Private network ID in which the node is",
Computed: true,
},
"region": {
Type: schema.TypeString,
Description: "Region of the node",
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Description: "Private subnet ID in which the node is",
Computed: true,
},
},
},
},
"opensearch_acls_enabled": {
Type: schema.TypeBool,
Description: "Defines whether the ACLs are enabled on an Opensearch cluster",
Computed: true,
},
"plan": {
Type: schema.TypeString,
Description: "Plan of the cluster",
Computed: true,
},
"status": {
Type: schema.TypeString,
Description: "Current status of the cluster",
Computed: true,
},
"version": {
Type: schema.TypeString,
Description: "Version of the engine deployed on the cluster",
Computed: true,
},
"disk_size": {
Type: schema.TypeInt,
Description: "Disk size attributes of the cluster",
Computed: true,
},
"disk_type": {
Type: schema.TypeString,
Description: "Disk type attributes of the cluster",
Computed: true,
},
"advanced_configuration": {
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Advanced configuration key / value",
Computed: true,
},
},
}
}
func dataSourceCloudProjectDatabaseRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
id := d.Get("id").(string)
serviceEndpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(id),
)
res := &CloudProjectDatabaseResponse{}
log.Printf("[DEBUG] Will read database %s from project: %s", id, serviceName)
if err := config.OVHClient.GetWithContext(ctx, serviceEndpoint, res); err != nil {
return diag.Errorf("Error calling GET %s:\n\t %q", serviceEndpoint, err)
}
nodesEndpoint := fmt.Sprintf("%s/node", serviceEndpoint)
nodeList := &[]string{}
if err := config.OVHClient.GetWithContext(ctx, nodesEndpoint, nodeList); err != nil {
return diag.Errorf("unable to get database %s nodes: %v", res.ID, err)
}
if len(*nodeList) == 0 {
return diag.Errorf("no node found for database %s", res.ID)
}
nodeEndpoint := fmt.Sprintf("%s/%s", nodesEndpoint, url.PathEscape((*nodeList)[0]))
node := &CloudProjectDatabaseNodes{}
if err := config.OVHClient.GetWithContext(ctx, nodeEndpoint, node); err != nil {
return diag.Errorf("unable to get database %s node %s: %v", res.ID, (*nodeList)[0], err)
}
res.Region = node.Region
if engine != "mongodb" {
advancedConfigEndpoint := fmt.Sprintf("%s/advancedConfiguration", serviceEndpoint)
advancedConfigMap := &map[string]string{}
if err := config.OVHClient.GetWithContext(ctx, advancedConfigEndpoint, advancedConfigMap); err != nil {
return diag.Errorf("unable to get database %s advanced configuration: %v", res.ID, err)
}
res.AdvancedConfiguration = *advancedConfigMap
}
for k, v := range res.toMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}
log.Printf("[DEBUG] Read Database %+v", res)
return nil
}