forked from ovh/terraform-provider-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cloud_project_database.go
219 lines (207 loc) · 5.93 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
package ovh
import (
"fmt"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCloudProjectDatabase() *schema.Resource {
return &schema.Resource{
Read: 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_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,
},
"kafka_rest_api": {
Type: schema.TypeBool,
Description: "Defines whether the REST API 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,
},
},
}
}
func dataSourceCloudProjectDatabaseRead(d *schema.ResourceData, meta interface{}) error {
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.Get(serviceEndpoint, res); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", serviceEndpoint, err)
}
nodesEndpoint := fmt.Sprintf("%s/node", serviceEndpoint)
nodeList := &[]string{}
if err := config.OVHClient.Get(nodesEndpoint, nodeList); err != nil {
return fmt.Errorf("unable to get database %s nodes: %v", res.Id, err)
}
if len(*nodeList) == 0 {
return fmt.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.Get(nodeEndpoint, node); err != nil {
return fmt.Errorf("unable to get database %s node %s: %v", res.Id, (*nodeList)[0], err)
}
res.Region = node.Region
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
}