-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_me_ipxe_script.go
105 lines (83 loc) · 2.61 KB
/
resource_me_ipxe_script.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
package ovh
import (
"fmt"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceMeIpxeScript() *schema.Resource {
return &schema.Resource{
Create: resourceMeIpxeScriptCreate,
Read: resourceMeIpxeScriptRead,
Delete: resourceMeIpxeScriptDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of your script",
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "For documentation purpose only. This attribute is not passed to the OVH API as it cannot be retrieved back. Instead a fake description ('$name auto description') is passed at creation time.",
},
"script": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Content of your IPXE script",
},
},
}
}
// Common function with the datasource
func resourceMeIpxeScriptRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
r := &MeIpxeScriptResponse{}
endpoint := fmt.Sprintf("/me/ipxeScript/%s", url.PathEscape(d.Id()))
if err := config.OVHClient.Get(endpoint, r); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
d.Set("name", r.Name)
d.Set("script", r.Script)
return nil
}
func resourceMeIpxeScriptCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
name := d.Get("name").(string)
script := d.Get("script").(string)
params := &MeIpxeScriptCreateOpts{
Description: fmt.Sprintf("%s auto description", name),
Name: name,
Script: script,
}
response := &MeIpxeScriptResponse{}
log.Printf("[DEBUG] Will create IpxeScript: %s", params)
err := config.OVHClient.Post("/me/ipxeScript", params, response)
if err != nil {
return fmt.Errorf("Error creating IpxeScript with params %s:\n\t %q", params, err)
}
d.SetId(response.Name)
return resourceMeIpxeScriptRead(d, meta)
}
func resourceMeIpxeScriptDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
err := config.OVHClient.Delete(
fmt.Sprintf("/me/ipxeScript/%s", url.PathEscape(d.Id())),
nil,
)
if err != nil {
return fmt.Errorf("Unable to delete IpxeScript named %s:\n\t %q", d.Id(), err)
}
log.Printf("[DEBUG] Deleted IpxeScript %s", d.Id())
d.SetId("")
return nil
}