-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_me_installation_template_partition_scheme_hardware_raid.go
181 lines (149 loc) · 5.26 KB
/
resource_me_installation_template_partition_scheme_hardware_raid.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
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 resourceMeInstallationTemplatePartitionSchemeHardwareRaid() *schema.Resource {
return &schema.Resource{
Create: resourceMeInstallationTemplatePartitionSchemeHardwareRaidCreate,
Read: resourceMeInstallationTemplatePartitionSchemeHardwareRaidRead,
Update: resourceMeInstallationTemplatePartitionSchemeHardwareRaidUpdate,
Delete: resourceMeInstallationTemplatePartitionSchemeHardwareRaidDelete,
Importer: &schema.ResourceImporter{
State: resourceMeInstallationTemplatePartitionSchemeHardwareRaidImportState,
},
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",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Hardware RAID name",
},
"disks": {
Type: schema.TypeList,
Required: true,
Description: "Disk List. Syntax is cX:dY for disks and [cX:dY,cX:dY] for groups. With X and Y resp. the controller id and the disk id",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"mode": {
Type: schema.TypeString,
Required: true,
Description: "RAID mode (raid0, raid1, raid10, raid5, raid50, raid6, raid60)",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateRAIDMode(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"step": {
Type: schema.TypeInt,
Required: true,
Description: "Specifies the creation order of the hardware RAID",
},
},
}
}
func resourceMeInstallationTemplatePartitionSchemeHardwareRaidImportState(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/name formatted")
}
templateName := splitId[0]
schemeName := splitId[1]
name := splitId[2]
d.Set("template_name", templateName)
d.Set("scheme_name", schemeName)
d.Set("name", name)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceMeInstallationTemplatePartitionSchemeHardwareRaidCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
opts := (&HardwareRaidCreateOrUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid",
url.PathEscape(templateName),
url.PathEscape(schemeName),
)
if err := config.OVHClient.Post(endpoint, opts, nil); err != nil {
return fmt.Errorf("Calling %s with opts %v:\n\t %q", endpoint, opts, err)
}
d.SetId(fmt.Sprintf("%s/%s/%s", templateName, schemeName, opts.Name))
return resourceMeInstallationTemplatePartitionSchemeHardwareRaidRead(d, meta)
}
func resourceMeInstallationTemplatePartitionSchemeHardwareRaidUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
name := d.Get("name").(string)
opts := (&HardwareRaidCreateOrUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid/%s",
url.PathEscape(templateName),
url.PathEscape(schemeName),
url.PathEscape(name),
)
if err := config.OVHClient.Put(endpoint, opts, nil); err != nil {
return fmt.Errorf("Calling %s with opts %v:\n\t %q", endpoint, opts, err)
}
return resourceMeInstallationTemplatePartitionSchemeHardwareRaidRead(d, meta)
}
func resourceMeInstallationTemplatePartitionSchemeHardwareRaidDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
name := d.Get("name").(string)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid/%s",
url.PathEscape(templateName),
url.PathEscape(schemeName),
url.PathEscape(name),
)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return fmt.Errorf("Error calling %s: %s \n", endpoint, err.Error())
}
return nil
}
func resourceMeInstallationTemplatePartitionSchemeHardwareRaidRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
schemeName := d.Get("scheme_name").(string)
name := d.Get("name").(string)
r := &HardwareRaid{}
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid/%s",
url.PathEscape(templateName),
url.PathEscape(schemeName),
url.PathEscape(name),
)
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, name))
return nil
}