Skip to content

Commit 0e8968d

Browse files
authored
Merge pull request #186 from ovh/fixerrorcasting
fix golang type casting for ovh.APIError
2 parents 2fcf0ec + c5fb12a commit 0e8968d

14 files changed

+24
-38
lines changed

ovh/dedicated_server_task.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ func waitForDedicatedServerTask(serviceName string, task *DedicatedServerTask, c
2626
var err error
2727
task, err = getDedicatedServerTask(serviceName, taskId, c)
2828
if err != nil {
29-
if err.(*ovh.APIError).Code == 500 || err.(*ovh.APIError).Code == 404 {
30-
// retry
29+
if errOvh, ok := err.(*ovh.APIError); ok && (errOvh.Code == 404 || errOvh.Code == 500) {
3130
return resource.RetryableError(err)
3231
}
3332
// other error dont retry and fail

ovh/helpers/helpers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func ConditionalAttributeBool(buff *bytes.Buffer, name string, val *bool) {
286286
// CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so,
287287
// sets the resource ID to the empty string instead of throwing an error.
288288
func CheckDeleted(d *schema.ResourceData, err error, endpoint string) error {
289-
if err.(*ovh.APIError).Code == 404 {
289+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
290290
d.SetId("")
291291
return nil
292292
}
@@ -356,7 +356,7 @@ func GetVrackServiceName(d *schema.ResourceData) (string, error) {
356356
func WaitAvailable(client *ovh.Client, endpoint string, timeout time.Duration) error {
357357
return resource.Retry(timeout, func() *resource.RetryError {
358358
if err := client.Get(endpoint, nil); err != nil {
359-
if err.(*ovh.APIError).Code == 404 {
359+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
360360
return resource.RetryableError(err)
361361
}
362362
return resource.NonRetryableError(err)

ovh/resource_ovh_cloud_project_kube.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func waitForCloudProjectKubeDeleted(client *ovh.Client, serviceName, kubeId stri
228228
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s", serviceName, kubeId)
229229
err := client.Get(endpoint, res)
230230
if err != nil {
231-
if err.(*ovh.APIError).Code == 404 {
231+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
232232
return res, "DELETED", nil
233233
} else {
234234
return res, "", err

ovh/resource_ovh_cloud_project_kube_nodepool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func waitForCloudProjectKubeNodePoolDeleted(client *ovh.Client, serviceName, kub
243243
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/nodepool/%s", serviceName, kubeId, id)
244244
err := client.Get(endpoint, res)
245245
if err != nil {
246-
if err.(*ovh.APIError).Code == 404 {
246+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
247247
return res, "DELETED", nil
248248
} else {
249249
return res, "", err

ovh/resource_ovh_cloud_project_network_private.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func waitForCloudProjectNetworkPrivateDelete(c *ovh.Client, serviceName, CloudPr
288288
endpoint := fmt.Sprintf("/cloud/project/%s/network/private/%s", serviceName, CloudProjectNetworkPrivateId)
289289
err := c.Get(endpoint, r)
290290
if err != nil {
291-
if err.(*ovh.APIError).Code == 404 {
291+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
292292
log.Printf("[DEBUG] private network id %s on project %s deleted", CloudProjectNetworkPrivateId, serviceName)
293293
return r, "DELETED", nil
294294
} else {

ovh/resource_ovh_cloud_project_user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func waitForCloudProjectUser(c *ovh.Client, serviceName, id string) resource.Sta
329329
)
330330
err := c.Get(endpoint, r)
331331
if err != nil {
332-
if err.(*ovh.APIError).Code == 404 {
332+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
333333
log.Printf("[DEBUG] user id %s on project %s deleted", id, serviceName)
334334
return r, "deleted", nil
335335
} else {

ovh/resource_ovh_dedicated_server_install_task.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ package ovh
22

33
import (
44
"fmt"
5-
"log"
65
"net/url"
76
"strconv"
87
"time"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1110
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
12-
13-
"github.com/ovh/go-ovh/ovh"
1411
)
1512

1613
func resourceDedicatedServerInstallTask() *schema.Resource {
@@ -218,15 +215,11 @@ func resourceDedicatedServerInstallTaskRead(d *schema.ResourceData, meta interfa
218215

219216
task, err := getDedicatedServerTask(serviceName, id, config.OVHClient)
220217
if err != nil {
221-
// After some delay, if the task is marked as `done`, the Provider
222-
// may purge it. To avoid raising errors when terraform refreshes its plan,
223-
// 404 errors are ignored on Resource Read, thus some information may be lost
224-
// after a while.
225-
if err.(*ovh.APIError).Code == 404 {
226-
log.Printf("[WARNING] Task id %d on Dedicated Server %s not found. It may have been purged by the Provider", id, serviceName)
227-
return nil
228-
}
229-
return err
218+
return helpers.CheckDeleted(d, err, fmt.Sprintf(
219+
"dedicated server task %s/%s",
220+
serviceName,
221+
d.Id(),
222+
))
230223
}
231224

232225
d.Set("function", task.Function)

ovh/resource_ovh_dedicated_server_reboot_task.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package ovh
22

33
import (
44
"fmt"
5-
"log"
65
"net/url"
76
"strconv"
87
"time"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11-
12-
"github.com/ovh/go-ovh/ovh"
10+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
1311
)
1412

1513
func resourceDedicatedServerRebootTask() *schema.Resource {
@@ -110,15 +108,11 @@ func resourceDedicatedServerRebootTaskRead(d *schema.ResourceData, meta interfac
110108

111109
task, err := getDedicatedServerTask(serviceName, id, config.OVHClient)
112110
if err != nil {
113-
// After some delay, if the task is marked as `done`, the Provider
114-
// may purge it. To avoid raising errors when terraform refreshes its plan,
115-
// 404 errors are ignored on Resource Read, thus some information may be lost
116-
// after a while.
117-
if err.(*ovh.APIError).Code == 404 {
118-
log.Printf("[WARNING] Task id %d on Dedicated Server %s not found. It may have been purged by the Provider", id, serviceName)
119-
return nil
120-
}
121-
return err
111+
return helpers.CheckDeleted(d, err, fmt.Sprintf(
112+
"dedicated server task %s/%s",
113+
serviceName,
114+
d.Id(),
115+
))
122116
}
123117

124118
d.Set("function", task.Function)

ovh/resource_ovh_domain_zone_record.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func ovhDomainZoneRecord(client *ovh.Client, d *schema.ResourceData, id string,
260260
rec,
261261
)
262262
if err != nil {
263-
if err.(*ovh.APIError).Code == 404 && retry {
263+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
264264
return resource.RetryableError(err)
265265
}
266266
return resource.NonRetryableError(err)

ovh/resource_ovh_ip_reverse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func testSweepIpReverse(region string) error {
4040
testIpReverse := os.Getenv("OVH_IP")
4141
endpoint := fmt.Sprintf("/ip/%s/reverse/%s", strings.Replace(testIp, "/", "%2F", 1), testIpReverse)
4242
if err := client.Get(endpoint, &reverse); err != nil {
43-
if err.(*ovh.APIError).Code == 404 {
43+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
4444
// no ip reverse set, nothing to sweep
4545
return nil
4646
}

ovh/resource_ovh_iploadbalancing_vrack_network_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func testSweepIpLoadbalancingVrackNetwork(region string) error {
8383
result := make([]int64, 0)
8484

8585
if err := client.Get(endpoint, &result); err != nil {
86-
if err.(*ovh.APIError).Code == 404 {
86+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
8787
return nil, nil
8888
}
8989
return nil, err

ovh/resource_ovh_vrack_cloudproject_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func testSweepVrackCloudProject(region string) error {
6060
vcp := &VrackCloudProject{}
6161

6262
if err := client.Get(endpoint, vcp); err != nil {
63-
if err.(*ovh.APIError).Code == 404 {
63+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
6464
return nil
6565
}
6666
return err

ovh/resource_ovh_vrack_ip_loadbalancing_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func testSweepVrackIpLoadbalancing(region string) error {
4747
viplb := &VrackIpLoadbalancing{}
4848

4949
if err := client.Get(endpoint, viplb); err != nil {
50-
if err.(*ovh.APIError).Code == 404 {
50+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
5151
return nil
5252
}
5353
return err

ovh/vrack_task.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func waitForVrackTask(task *VrackTask, c *ovh.Client) error {
2424

2525
err := c.Get(endpoint, task)
2626
if err != nil {
27-
if err.(*ovh.APIError).Code == 404 {
27+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
2828
log.Printf("[DEBUG] Task id %d on Vrack %s completed", taskId, vrackId)
2929
return taskId, "completed", nil
3030
} else {

0 commit comments

Comments
 (0)