-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_ip_service.go
215 lines (180 loc) · 5.09 KB
/
resource_ip_service.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
package ovh
import (
"fmt"
"log"
"net/url"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceIpService() *schema.Resource {
return &schema.Resource{
Create: resourceIpServiceCreate,
Update: resourceIpServiceUpdate,
Read: resourceIpServiceRead,
Delete: resourceIpServiceDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{d}, nil
},
},
Schema: resourceIpServiceSchema(),
}
}
func resourceIpServiceSchema() map[string]*schema.Schema {
schema := map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Description: "Custom description on your ip",
Optional: true,
Computed: true,
},
//computed
"can_be_terminated": {
Type: schema.TypeBool,
Computed: true,
},
"country": {
Type: schema.TypeString,
Computed: true,
},
"ip": {
Type: schema.TypeString,
Computed: true,
},
"organisation_id": {
Type: schema.TypeString,
Computed: true,
},
"routed_to": {
Type: schema.TypeList,
Description: "Routage information",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "Service where ip is routed to",
Computed: true,
},
},
},
},
"service_name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Description: "Possible values for ip type",
Computed: true,
},
}
for k, v := range genericOrderSchema(true) {
schema[k] = v
}
return schema
}
func resourceIpServiceCreate(d *schema.ResourceData, meta interface{}) error {
if err := orderCreate(d, meta, "ip"); err != nil {
return fmt.Errorf("Could not order ip: %q", err)
}
return resourceIpServiceUpdate(d, meta)
}
func resourceIpServiceUpdate(d *schema.ResourceData, meta interface{}) error {
_, details, err := orderRead(d, meta)
if err != nil {
return fmt.Errorf("Could not read ip order: %q", err)
}
serviceName := details[0].Domain
config := meta.(*Config)
log.Printf("[DEBUG] Will update ip: %s", serviceName)
opts := (&IpServiceUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf("/ip/service/%s",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Put(endpoint, opts, nil); err != nil {
return fmt.Errorf("calling Put %s: %q", endpoint, err)
}
return resourceIpServiceRead(d, meta)
}
func resourceIpServiceRead(d *schema.ResourceData, meta interface{}) error {
_, details, err := orderRead(d, meta)
if err != nil {
return fmt.Errorf("Could not read ip order: %q", err)
}
serviceName := details[0].Domain
config := meta.(*Config)
log.Printf("[DEBUG] Will read ip: %s", serviceName)
r := &IpService{}
endpoint := fmt.Sprintf("/ip/service/%s",
url.PathEscape(serviceName),
)
// This retry logic is there to handle a known API bug
// which happens while an ipblock is attached/detached from
// a Vrack
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
if err := config.OVHClient.Get(endpoint, &r); err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 400 {
log.Printf("[DEBUG] known API bug when attaching/detaching vrack")
return resource.RetryableError(err)
}
err = helpers.CheckDeleted(d, err, endpoint)
if err != nil {
return resource.NonRetryableError(err)
}
return nil
}
// Successful Get
return nil
})
if err != nil {
return err
}
d.Set("service_name", serviceName)
// set resource attributes
for k, v := range r.ToMap() {
d.Set(k, v)
}
return nil
}
func resourceIpServiceDelete(d *schema.ResourceData, meta interface{}) error {
_, details, err := orderRead(d, meta)
if err != nil {
return fmt.Errorf("Could not read ip order: %q", err)
}
config := meta.(*Config)
id := d.Id()
serviceName := details[0].Domain
terminate := func() (string, error) {
log.Printf("[DEBUG] Will terminate ip %s for order %s", serviceName, id)
endpoint := fmt.Sprintf(
"/ip/service/%s/terminate",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Post(endpoint, nil, nil); err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && (errOvh.Code == 404 || errOvh.Code == 460) {
return "", nil
}
return "", fmt.Errorf("calling Post %s:\n\t %q", endpoint, err)
}
return serviceName, nil
}
confirmTerminate := func(token string) error {
log.Printf("[DEBUG] Will confirm termination of ip %s for order %s", serviceName, id)
endpoint := fmt.Sprintf(
"/ip/service/%s/confirmTermination",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Post(endpoint, &IpServiceConfirmTerminationOpts{Token: token}, nil); err != nil {
return fmt.Errorf("calling Post %s:\n\t %q", endpoint, err)
}
return nil
}
if err := orderDelete(d, meta, terminate, confirmTerminate); err != nil {
return err
}
return nil
}