-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_me_installation_template.go
335 lines (294 loc) · 9.91 KB
/
resource_me_installation_template.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package ovh
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/go-ovh/ovh"
)
func resourceMeInstallationTemplate() *schema.Resource {
return &schema.Resource{
Create: resourceMeInstallationTemplateCreate,
Read: resourceMeInstallationTemplateRead,
Update: resourceMeInstallationTemplateUpdate,
Delete: resourceMeInstallationTemplateDelete,
Importer: &schema.ResourceImporter{
State: resourceMeInstallationTemplateImportState,
},
Schema: map[string]*schema.Schema{
"base_template_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "OVH template name yours will be based on, choose one among the list given by compatibleTemplates function",
},
"default_language": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The default language of this template",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateLanguageCode(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"template_name": {
Type: schema.TypeString,
Required: true,
Description: "This template name",
},
"remove_default_partition_schemes": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Remove default partition schemes at creation",
},
"customization": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"change_log": {
Type: schema.TypeString,
Deprecated: "field is not used anymore",
Optional: true,
Description: "Template change log details",
},
"custom_hostname": {
Type: schema.TypeString,
Optional: true,
Description: "Set up the server using the provided hostname instead of the default hostname",
},
"post_installation_script_link": {
Type: schema.TypeString,
Optional: true,
Description: "Indicate the URL where your postinstall customisation script is located",
},
"post_installation_script_return": {
Type: schema.TypeString,
Optional: 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'",
},
"rating": {
Type: schema.TypeInt,
Deprecated: "field is not used anymore",
Optional: true,
},
"ssh_key_name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the ssh key that should be installed. Password login will be disabled",
},
"use_distribution_kernel": {
Type: schema.TypeBool,
Optional: true,
Description: "Use the distribution's native kernel instead of the recommended OVH Kernel",
},
},
},
},
"available_languages": {
Type: schema.TypeList,
Computed: true,
Description: "List of all language available for this template",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"beta": {
Type: schema.TypeBool,
Computed: true,
Description: "This distribution is new and, although tested and functional, may still display odd behaviour",
},
"bit_format": {
Type: schema.TypeInt,
Computed: true,
Description: "This template bit format (32 or 64)",
},
"category": {
Type: schema.TypeString,
Computed: true,
Description: "Category of this template (informative only). (basic, customer, hosting, other, readyToUse, virtualisation)",
},
"deprecated": {
Type: schema.TypeBool,
Computed: true,
Description: "is this distribution deprecated",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "information about this template",
},
"distribution": {
Type: schema.TypeString,
Computed: true,
Description: "the distribution this template is based on",
},
"family": {
Type: schema.TypeString,
Computed: true,
Description: "this template family type (bsd,linux,solaris,windows)",
},
"hard_raid_configuration": {
Type: schema.TypeBool,
Computed: true,
Description: "This distribution supports hardware raid configuration through the OVH API",
},
"filesystems": {
Type: schema.TypeList,
Computed: true,
Description: "Filesystems available (btrfs,ext3,ext4,ntfs,reiserfs,swap,ufs,xfs,zfs)",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"last_modification": {
Type: schema.TypeString,
Computed: true,
Description: "Date of last modification of the base image",
},
"lvm_ready": {
Type: schema.TypeBool,
Computed: true,
Description: "This distribution supports Logical Volumes (Linux LVM)",
},
"supports_distribution_kernel": {
Type: schema.TypeBool,
Computed: true,
Description: "This distribution supports installation using the distribution's native kernel instead of the recommended OVH kernel",
},
"supports_gpt_label": {
Type: schema.TypeBool,
Computed: true,
Description: "This distribution supports the GUID Partition Table (GPT), providing up to 128 partitions that can have more than 2TB",
},
"supports_rtm": {
Type: schema.TypeBool,
Computed: true,
Description: "This distribution supports RTM software",
},
"supports_sql_server": {
Type: schema.TypeBool,
Computed: true,
Description: "This distribution supports the microsoft SQL server",
},
"supports_uefi": {
Type: schema.TypeString,
Computed: true,
Description: "This distribution supports UEFI setup (no,only,yes)",
},
},
}
}
func resourceMeInstallationTemplateImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, "/", 2)
if len(splitId) != 2 {
return nil, fmt.Errorf("Import Id is not base_template_name/template_name formatted")
}
baseTemplateName := splitId[0]
templateName := splitId[1]
d.SetId(templateName)
d.Set("base_template_name", baseTemplateName)
d.Set("template_name", templateName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceMeInstallationTemplateCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
opts := (&InstallationTemplateCreateOpts{}).FromResource(d)
endpoint := "/me/installationTemplate"
// the resource is created via the POST endpoint, then updated
// via the PUT endpoint to apply customizations.
if err := config.OVHClient.Post(endpoint, opts, nil); err != nil {
return fmt.Errorf("Error calling POST %s with opts %v:\n\t %q", endpoint, opts, err)
}
d.SetId(d.Get("template_name").(string))
// We call the update method to put customization opts
updateOpts := (&InstallationTemplateUpdateOpts{}).FromResource(d)
endpoint = fmt.Sprintf(
"/me/installationTemplate/%s",
url.PathEscape(d.Id()),
)
if err := config.OVHClient.Put(endpoint, updateOpts, nil); err != nil {
return fmt.Errorf("Error calling PUT %s with opts %v:\n\t %q", endpoint, opts, err)
}
// handle remove_default_partitions option
removeDefaultPartitions := false
if v, ok := d.GetOk("remove_default_partition_schemes"); ok {
removeDefaultPartitions = v.(bool)
}
d.Set("remove_default_partition_schemes", removeDefaultPartitions)
if removeDefaultPartitions {
templateName := d.Get("template_name").(string)
defaultSchemes, err := getPartitionSchemeIds(templateName, config.OVHClient)
if err != nil {
return err
}
for _, scheme := range defaultSchemes {
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s",
url.PathEscape(templateName),
url.PathEscape(scheme),
)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return fmt.Errorf("Error calling DELETE %s: %s \n", endpoint, err.Error())
}
}
}
return resourceMeInstallationTemplateRead(d, meta)
}
func resourceMeInstallationTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
opts := (&InstallationTemplateUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s",
url.PathEscape(d.Id()),
)
if err := config.OVHClient.Put(endpoint, opts, nil); err != nil {
return fmt.Errorf("Error calling PUT %s with opts %v:\n\t %q", endpoint, opts, err)
}
return resourceMeInstallationTemplateRead(d, meta)
}
func resourceMeInstallationTemplateDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
name := d.Get("template_name").(string)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s",
url.PathEscape(name),
)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return fmt.Errorf("Error calling DELETE %s: %s \n", endpoint, err.Error())
}
return nil
}
func resourceMeInstallationTemplateRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
template, err := getInstallationTemplate(d, config.OVHClient)
if err != nil {
return err
}
// set attributes
for k, v := range template.ToMap() {
d.Set(k, v)
}
return nil
}
func getInstallationTemplate(d *schema.ResourceData, client *ovh.Client) (*InstallationTemplate, error) {
r := &InstallationTemplate{}
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s",
url.PathEscape(d.Get("template_name").(string)),
)
if err := client.Get(endpoint, r); err != nil {
return nil, helpers.CheckDeleted(d, err, endpoint)
}
return r, nil
}