-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_me_installation_template_partition_scheme_partition.go
210 lines (177 loc) · 5.98 KB
/
resource_me_installation_template_partition_scheme_partition.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
package ovh
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceMeInstallationTemplatePartitionSchemePartition() *schema.Resource {
return &schema.Resource{
Create: resourceMeInstallationTemplatePartitionSchemePartitionCreate,
Read: resourceMeInstallationTemplatePartitionSchemePartitionRead,
Update: resourceMeInstallationTemplatePartitionSchemePartitionUpdate,
Delete: resourceMeInstallationTemplatePartitionSchemePartitionDelete,
Importer: &schema.ResourceImporter{
State: resourceMeInstallationTemplatePartitionSchemePartitionImportState,
},
Schema: map[string]*schema.Schema{
"template_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Template name",
},
"scheme_name": {
Type: schema.TypeString,
Required: true,
Description: "name of this partitioning scheme",
},
"mountpoint": {
Type: schema.TypeString,
Required: true,
Description: "partition mount point",
},
"filesystem": {
Type: schema.TypeString,
Required: true,
Description: "Partition filesystem",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateFilesystem(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"size": {
Type: schema.TypeInt,
Required: true,
Description: "size of partition in MB, 0 => rest of the space",
},
"order": {
Type: schema.TypeInt,
Required: true,
Description: "step or order. specifies the creation order of the partition on the disk",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "partition type",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidatePartitionType(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
// optional
"raid": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "raid partition type",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidatePartitionRAIDMode(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"volume_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The volume name needed for proxmox distribution",
},
},
}
}
func resourceMeInstallationTemplatePartitionSchemePartitionImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, "/", 3)
if len(splitId) != 3 {
return nil, fmt.Errorf("Import Id is not template_name/scheme_name/mountpoint formatted")
}
templateName := splitId[0]
schemeName := splitId[1]
mountpoint := splitId[2]
d.Set("template_name", templateName)
d.Set("scheme_name", schemeName)
d.Set("mountpoint", mountpoint)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceMeInstallationTemplatePartitionSchemePartitionCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
opts := (&PartitionCreateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/partition",
url.PathEscape(templateName),
url.PathEscape(schemeName),
)
if err := config.OVHClient.Post(endpoint, opts, nil); err != nil {
return fmt.Errorf("Calling POST %s with opts %v:\n\t %q", endpoint, opts, err)
}
d.SetId(fmt.Sprintf("%s/%s/%s", templateName, schemeName, opts.Mountpoint))
return resourceMeInstallationTemplatePartitionSchemePartitionRead(d, meta)
}
func resourceMeInstallationTemplatePartitionSchemePartitionUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
opts := (&PartitionUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/partition/%s",
url.PathEscape(templateName),
url.PathEscape(schemeName),
url.PathEscape(opts.Mountpoint),
)
if err := config.OVHClient.Put(endpoint, opts, nil); err != nil {
return fmt.Errorf("Calling PUT %s with opts %v:\n\t %q", endpoint, opts, err)
}
return resourceMeInstallationTemplatePartitionSchemePartitionRead(d, meta)
}
func resourceMeInstallationTemplatePartitionSchemePartitionDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
mountpoint := d.Get("mountpoint").(string)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/partition/%s",
url.PathEscape(templateName),
url.PathEscape(schemeName),
url.PathEscape(mountpoint),
)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return fmt.Errorf("Calling DELETE %s: %s \n", endpoint, err.Error())
}
return nil
}
func resourceMeInstallationTemplatePartitionSchemePartitionRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
mountpoint := d.Get("mountpoint").(string)
r := &Partition{}
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/partition/%s",
url.PathEscape(templateName),
url.PathEscape(schemeName),
url.PathEscape(mountpoint),
)
if err := config.OVHClient.Get(endpoint, r); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
// set resource attributes
for k, v := range r.ToMap() {
d.Set(k, v)
}
d.SetId(fmt.Sprintf("%s/%s/%s", templateName, schemeName, mountpoint))
return nil
}