-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_dedicated_server_install_task.go
287 lines (261 loc) · 8.08 KB
/
resource_dedicated_server_install_task.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package ovh
import (
"fmt"
"net/url"
"strconv"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceDedicatedServerInstallTask() *schema.Resource {
return &schema.Resource{
Create: resourceDedicatedServerInstallTaskCreate,
Update: resourceDedicatedServerInstallTaskUpdate,
Read: resourceDedicatedServerInstallTaskRead,
Delete: resourceDedicatedServerInstallTaskDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(45 * time.Minute),
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The internal name of your dedicated server.",
},
"partition_scheme_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Partition scheme name.",
},
"template_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Template name",
},
"bootid_on_destroy": {
Type: schema.TypeInt,
Optional: true,
Description: "If set, reboot the server on the specified boot id during destroy phase",
},
"details": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"change_log": {
Type: schema.TypeString,
Optional: true,
Deprecated: "field is not used anymore",
Description: "Template change log details",
},
"custom_hostname": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Set up the server using the provided hostname instead of the default hostname",
},
"disk_group_id": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: "",
},
"install_rtm": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "",
},
"install_sql_server": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "",
},
"language": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "language",
},
"no_raid": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "",
},
"post_installation_script_link": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Indicate the URL where your postinstall customisation script is located",
},
"post_installation_script_return": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "indicate the string returned by your postinstall customisation script on successful execution. Advice: your script should return a unique validation string in case of succes. A good example is 'loh1Xee7eo OK OK OK UGh8Ang1Gu'",
},
"reset_hw_raid": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "",
},
"soft_raid_devices": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: "",
},
"ssh_key_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Name of the ssh key that should be installed. Password login will be disabled",
},
"use_spla": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "",
},
"use_distrib_kernel": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "Use the distribution's native kernel instead of the recommended OVH Kernel",
},
},
},
},
//Computed
"comment": {
Type: schema.TypeString,
Computed: true,
Description: "Details of this task",
},
"done_date": {
Type: schema.TypeString,
Computed: true,
Description: "Completion date",
},
"function": {
Type: schema.TypeString,
Computed: true,
Description: "Function name",
},
"last_update": {
Type: schema.TypeString,
Computed: true,
Description: "Last update",
},
"start_date": {
Type: schema.TypeString,
Computed: true,
Description: "Task Creation date",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Task status",
},
},
}
}
func resourceDedicatedServerInstallTaskCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
endpoint := fmt.Sprintf(
"/dedicated/server/%s/install/start",
url.PathEscape(serviceName),
)
opts := (&DedicatedServerInstallTaskCreateOpts{}).FromResource(d)
task := &DedicatedServerTask{}
if err := config.OVHClient.Post(endpoint, opts, task); err != nil {
return fmt.Errorf("Error calling POST %s:\n\t %q", endpoint, err)
}
if err := waitForDedicatedServerTask(serviceName, task, config.OVHClient); err != nil {
return err
}
d.SetId(fmt.Sprintf("%d", task.Id))
return dedicatedServerInstallTaskRead(d, meta)
}
func dedicatedServerInstallTaskRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return fmt.Errorf(
"Could not parse install task id %s,%s:\n\t %q",
serviceName,
d.Id(),
err,
)
}
task, err := getDedicatedServerTask(serviceName, id, config.OVHClient)
if err != nil {
return helpers.CheckDeleted(d, err, fmt.Sprintf(
"dedicated server task %s/%s",
serviceName,
d.Id(),
))
}
d.Set("function", task.Function)
d.Set("comment", task.Comment)
d.Set("status", task.Status)
d.Set("last_update", task.LastUpdate.Format(time.RFC3339))
d.Set("done_date", task.DoneDate.Format(time.RFC3339))
d.Set("start_date", task.StartDate.Format(time.RFC3339))
return nil
}
func resourceDedicatedServerInstallTaskUpdate(d *schema.ResourceData, meta interface{}) error {
// nothing to do on update
return resourceDedicatedServerInstallTaskRead(d, meta)
}
func resourceDedicatedServerInstallTaskRead(d *schema.ResourceData, meta interface{}) error {
// Nothing to do on READ
//
// IMPORTANT: This resource doesn't represent a real resource
// but instead a task on a dedicated server. OVH may clean its tasks database after a while
// so that the API may return a 404 on a task id. If we hit a 404 on a READ, then
// terraform will understand that it has to recreate the resource, and consequently
// will trigger new install task on the dedicated server.
// This is something we must avoid!
//
return nil
}
func resourceDedicatedServerInstallTaskDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
bootId := helpers.GetNilIntPointerFromData(d, "bootid_on_destroy")
if bootId != nil {
serviceName := d.Get("service_name").(string)
// before reboot, update bootId accordingly
bootIdEndpoint := fmt.Sprintf("/dedicated/server/%s", url.PathEscape(serviceName))
bootIdReqBody := make(map[string]int)
bootIdReqBody["bootId"] = *bootId
if err := config.OVHClient.Put(bootIdEndpoint, bootIdReqBody, nil); err != nil {
return fmt.Errorf("Error calling PUT %s:\n\t %q", bootIdEndpoint, err)
}
// reboot
endpoint := fmt.Sprintf(
"/dedicated/server/%s/reboot",
url.PathEscape(serviceName),
)
task := &DedicatedServerTask{}
if err := config.OVHClient.Post(endpoint, nil, task); err != nil {
return fmt.Errorf("Error calling POST %s:\n\t %q", endpoint, err)
}
if err := waitForDedicatedServerTask(serviceName, task, config.OVHClient); err != nil {
return err
}
}
// we cant delete the task through the API, just forget about its Id
d.SetId("")
return nil
}