-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_me_installation_template_partition_scheme.go
143 lines (114 loc) · 4.11 KB
/
resource_me_installation_template_partition_scheme.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
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 resourceMeInstallationTemplatePartitionScheme() *schema.Resource {
return &schema.Resource{
Create: resourceMeInstallationTemplatePartitionSchemeCreate,
Read: resourceMeInstallationTemplatePartitionSchemeRead,
Update: resourceMeInstallationTemplatePartitionSchemeUpdate,
Delete: resourceMeInstallationTemplatePartitionSchemeDelete,
Importer: &schema.ResourceImporter{
State: resourceMeInstallationTemplatePartitionSchemeImportState,
},
Schema: map[string]*schema.Schema{
"template_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "This template name",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "name of this partitioning scheme",
},
"priority": {
Type: schema.TypeInt,
Required: true,
Description: "on a reinstall, if a partitioning scheme is not specified, the one with the higher priority will be used by default, among all the compatible partitioning schemes (given the underlying hardware specifications)",
},
},
}
}
func resourceMeInstallationTemplatePartitionSchemeImportState(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 template_name/scheme_name formatted")
}
templateName := splitId[0]
schemeName := splitId[1]
d.Set("template_name", templateName)
d.Set("name", schemeName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceMeInstallationTemplatePartitionSchemeCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
opts := (&PartitionSchemeCreateOrUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf("/me/installationTemplate/%s/partitionScheme", templateName)
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(fmt.Sprintf(
"%s/%s",
url.PathEscape(templateName),
url.PathEscape(opts.Name),
))
return resourceMeInstallationTemplatePartitionSchemeRead(d, meta)
}
func resourceMeInstallationTemplatePartitionSchemeUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
opts := (&PartitionSchemeCreateOrUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s",
url.PathEscape(templateName),
url.PathEscape(opts.Name),
)
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 resourceMeInstallationTemplatePartitionSchemeRead(d, meta)
}
func resourceMeInstallationTemplatePartitionSchemeDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
name := d.Get("name").(string)
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s",
url.PathEscape(templateName),
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 resourceMeInstallationTemplatePartitionSchemeRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
templateName := d.Get("template_name").(string)
name := d.Get("name").(string)
r := &PartitionScheme{}
endpoint := fmt.Sprintf(
"/me/installationTemplate/%s/partitionScheme/%s",
url.PathEscape(templateName),
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", templateName, name))
return nil
}