-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
55 lines (46 loc) · 1.09 KB
/
util.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
package chronicle
import (
"fmt"
"log"
"os"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/googleapi"
)
func multiEnvSearch(ks []string) string {
for _, k := range ks {
if v := os.Getenv(k); v != "" {
return v
}
}
return ""
}
func envSearch(s string) string {
if v := os.Getenv(s); v != "" {
return v
}
return ""
}
// checks if a string is present in a slice.
func contains(s []string, s1 string) bool {
for _, a := range s {
if a == s1 {
return true
}
}
return false
}
func handleNotFoundError(err error, d *schema.ResourceData, resource string) error {
if isGoogleAPIErrorWithCode(err, 404) {
log.Printf("[WARN] Removing %s because it's gone", resource)
// The resource doesn't exist anymore.
d.SetId("")
return nil
}
return fmt.Errorf(
fmt.Sprintf("Error when reading or editing %s: {{err}}", resource), err)
}
func isGoogleAPIErrorWithCode(err error, errCode int) bool {
gerr, ok := errwrap.GetType(err, &googleapi.Error{}).(*googleapi.Error)
return ok && gerr != nil && gerr.Code == errCode
}