-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_vrack_ip.go
144 lines (116 loc) · 3.54 KB
/
resource_vrack_ip.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
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 resourceVrackIp() *schema.Resource {
return &schema.Resource{
Create: resourceVrackIpCreate,
Read: resourceVrackIpRead,
Delete: resourceVrackIpDelete,
Importer: &schema.ResourceImporter{
State: resourceVrackIpImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The internal name of your vrack",
},
"block": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Your IP block.",
},
// computed
"gateway": {
Type: schema.TypeString,
Computed: true,
Description: "Your gateway",
},
"ip": {
Type: schema.TypeString,
Computed: true,
Description: "Your IP block",
},
"zone": {
Type: schema.TypeString,
Computed: true,
Description: "Where you want your block announced on the network",
},
},
}
}
func resourceVrackIpImportState(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 SERVICE_NAME,IPBlock formatted")
}
serviceName := splitId[0]
block := splitId[1]
d.SetId(fmt.Sprintf("vrack_%s-block_%s", serviceName, block))
d.Set("service_name", serviceName)
d.Set("block", block)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceVrackIpCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
opts := (&VrackIpCreateOpts{}).FromResource(d)
task := &VrackTask{}
endpoint := fmt.Sprintf("/vrack/%s/ip", serviceName)
if err := config.OVHClient.Post(endpoint, opts, task); err != nil {
return fmt.Errorf("Error calling POST %s with opts %v:\n\t %q", endpoint, opts, err)
}
if err := waitForVrackTask(task, config.OVHClient); err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to attach ip %v: %s", serviceName, opts, err)
}
//set id
d.SetId(fmt.Sprintf("vrack_%s-dedicatedserver_%s", serviceName, opts.Block))
return resourceVrackIpRead(d, meta)
}
func resourceVrackIpRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
block := d.Get("block").(string)
ip := &VrackIp{}
endpoint := fmt.Sprintf("/vrack/%s/ip/%s",
url.PathEscape(serviceName),
url.PathEscape(block),
)
if err := config.OVHClient.Get(endpoint, ip); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
d.Set("block", ip.Ip)
// set resource attributes
for k, v := range ip.ToMap() {
d.Set(k, v)
}
return nil
}
func resourceVrackIpDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
block := d.Get("block").(string)
task := &VrackTask{}
endpoint := fmt.Sprintf("/vrack/%s/ip/%s",
url.PathEscape(serviceName),
url.PathEscape(block),
)
if err := config.OVHClient.Delete(endpoint, task); err != nil {
return fmt.Errorf("Error calling DELETE %s with %s/%s:\n\t %q", endpoint, serviceName, block, err)
}
if err := waitForVrackTask(task, config.OVHClient); err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to detach ip (%s): %s", serviceName, block, err)
}
d.SetId("")
return nil
}