This repository has been archived by the owner on Jun 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
utils.go
129 lines (111 loc) · 3.79 KB
/
utils.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
// Contains functions that don't really belong anywhere else.
package gsuite
import (
"fmt"
"log"
"math/rand"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"google.golang.org/api/googleapi"
)
func handleNotFoundError(err error, d *schema.ResourceData, resource string) error {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Removing %s because it's gone", resource)
// The resource doesn't exist anymore
d.SetId("")
return nil
}
return fmt.Errorf("Error reading %s: %s", resource, err)
}
func retry(retryFunc func() error, minutes int) error {
return retryTime(retryFunc, minutes, false, false)
}
func retryNotFound(retryFunc func() error, minutes int) error {
return retryTime(retryFunc, minutes, true, false)
}
func retryPassDuplicate(retryFunc func() error, minutes int) error {
return retryTime(retryFunc, minutes, true, true)
}
func retryTime(retryFunc func() error, minutes int, retryNotFound bool, retryPassDuplicate bool) error {
wait := 1
return resource.Retry(time.Duration(minutes)*time.Minute, func() *resource.RetryError {
err := retryFunc()
if err == nil {
return nil
}
rand.Seed(time.Now().UnixNano())
randomNumberMiliseconds := rand.Intn(1001)
if retryPassDuplicate {
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Errors[0].Reason == "quotaExceeded" || gerr.Code == 401 || gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
log.Printf("[DEBUG] Retrying quota/server error code...")
time.Sleep(time.Duration(wait)*time.Second + time.Duration(randomNumberMiliseconds))
wait = wait * 2
return resource.RetryableError(gerr)
}
} else {
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Errors[0].Reason == "quotaExceeded" || gerr.Code == 401 || gerr.Code == 409 || gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
log.Printf("[DEBUG] Retrying quota/server error code...")
time.Sleep(time.Duration(wait)*time.Second + time.Duration(randomNumberMiliseconds))
wait = wait * 2
return resource.RetryableError(gerr)
}
}
if retryNotFound {
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 404) {
log.Printf("[DEBUG] Retrying for eventual consistency...")
time.Sleep(time.Duration(wait)*time.Second + time.Duration(randomNumberMiliseconds))
wait = wait * 2
return resource.RetryableError(gerr)
}
}
// Deal with the broken API
if strings.Contains(fmt.Sprintf("%s", err), "Invalid Input: Bad request for \"") && strings.Contains(fmt.Sprintf("%s", err), "\"code\":400") {
log.Printf("[DEBUG] Retrying invalid response from API")
return resource.RetryableError(err)
}
if strings.Contains(fmt.Sprintf("%s", err), "Service unavailable. Please try again") {
log.Printf("[DEBUG] Retrying service unavailable from API")
return resource.RetryableError(err)
}
if strings.Contains(fmt.Sprintf("%s", err), "Eventual consistency. Please try again") {
log.Printf("[DEBUG] Retrying due to eventual consistency")
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
})
}
func mergeSchemas(a, b map[string]*schema.Schema) map[string]*schema.Schema {
merged := make(map[string]*schema.Schema)
for k, v := range a {
merged[k] = v
}
for k, v := range b {
merged[k] = v
}
return merged
}
func convertStringSet(set *schema.Set) []string {
s := make([]string, 0, set.Len())
for _, v := range set.List() {
s = append(s, v.(string))
}
return s
}
func stringSliceDifference(left []string, right []string) []string {
var d []string
for _, l := range left {
f := false
for _, r := range right {
if r == l {
f = true
break
}
}
if !f {
d = append(d, l)
}
}
return d
}