/
resource_cloud_project_failover_ip_attach.go
218 lines (190 loc) · 5.15 KB
/
resource_cloud_project_failover_ip_attach.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
211
212
213
214
215
216
217
218
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 resourceCloudProjectFailoverIpAttach() *schema.Resource {
return &schema.Resource{
Create: resourceCloudProjectFailoverIpAttachCreate,
Read: resourceCloudProjectFailoverIpAttachRead,
Delete: resourceCloudProjectFailoverIpAttachDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{d}, nil
},
},
Schema: resourceCloudProjectFailoverIpAttachSchema(),
}
}
func resourceCloudProjectFailoverIpAttachSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "The service name",
ForceNew: true,
Required: true,
},
"block": {
Type: schema.TypeString,
Description: "IP block",
Optional: true,
Computed: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateIp(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"continent_code": {
Type: schema.TypeString,
Description: "Ip continent",
Optional: true,
Computed: true,
},
"geo_loc": {
Type: schema.TypeString,
Description: "Ip location",
Optional: true,
Computed: true,
},
"id": {
Type: schema.TypeString,
Description: "Ip id",
Computed: true,
},
"ip": {
Type: schema.TypeString,
Description: "Ip",
Optional: true,
Computed: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateIp(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"progress": {
Type: schema.TypeInt,
Description: "Current operation progress in percent",
Computed: true,
},
"routed_to": {
Type: schema.TypeString,
Description: "Instance where ip is routed to",
Computed: true,
ForceNew: true,
Optional: true,
},
"status": {
Type: schema.TypeString,
Description: "Ip status",
Computed: true,
},
"sub_type": {
Type: schema.TypeString,
Description: "IP sub type",
Computed: true,
},
}
}
func resourceCloudProjectFailoverIpAttachRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
log.Printf("[DEBUG] Will read cloud project ip addresses %s", serviceName)
endpoint := fmt.Sprintf("/cloud/project/%s/ip/failover",
url.PathEscape(serviceName),
)
ips := []FailoverIp{}
if err := config.OVHClient.Get(endpoint, &ips); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
match := false
for _, ip := range ips {
if ip.Ip == d.Get("ip").(string) {
for k, v := range ip.ToMap() {
match = true
if k == "id" {
d.SetId(v.(string))
} else {
err := d.Set(k, v)
if err != nil {
return err
}
}
}
}
}
if !match {
return fmt.Errorf("your query returned no results, " +
"please change your search criteria and try again")
}
return nil
}
func resourceCloudProjectFailoverIpAttachCreate(d *schema.ResourceData, meta interface{}) error {
serviceName := d.Get("service_name").(string)
config := meta.(*Config)
//Fetch Failover IP address to populate ID field
log.Printf("[DEBUG] Will read cloud project ip addresses %s", serviceName)
endpoint := fmt.Sprintf("/cloud/project/%s/ip/failover",
url.PathEscape(serviceName),
)
ips := []FailoverIp{}
if err := config.OVHClient.Get(endpoint, &ips); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
match := false
for _, ip := range ips {
if ip.Ip == d.Get("ip").(string) {
for k, v := range ip.ToMap() {
match = true
if k == "id" {
d.SetId(v.(string))
}
}
}
}
if !match {
return fmt.Errorf("your query returned no results, " +
"please change your search criteria and try again")
}
id := d.Get("id").(string)
log.Printf("[DEBUG] Will attach failover ip to an instance: %s", serviceName)
opts := (&ProjectIpFailoverAttachCreation{}).FromResource(d)
endpoint = fmt.Sprintf("/cloud/project/%s/ip/failover/%s/attach",
url.PathEscape(serviceName),
url.PathEscape(id),
)
ip := &FailoverIp{}
if err := config.OVHClient.Post(endpoint, opts, ip); err != nil {
return fmt.Errorf("calling Put %s: %q", endpoint, err)
}
for k, v := range ip.ToMap() {
match = true
if k != "id" {
err := d.Set(k, v)
if err != nil {
return err
}
}
}
for d.Get("status").(string) == "operationPending" {
if err := resourceCloudProjectFailoverIpAttachRead(d, meta); err != nil {
return err
}
}
return nil
}
func resourceCloudProjectFailoverIpAttachDelete(d *schema.ResourceData, meta interface{}) error {
// Failover IPs cannot be deleted, the best that can be done in this instance is to check it exists
if err := resourceCloudProjectFailoverIpAttachRead(d, meta); err != nil {
return err
}
return nil
}