forked from DataDog/terraform-provider-datadog
/
utils.go
155 lines (133 loc) · 5.07 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
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
package utils
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
datadogV2 "github.com/DataDog/datadog-api-client-go/api/v2/datadog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
"github.com/Telemetron/terraform-provider-datadog/v3/version"
)
// DatadogProvider holds a reference to the provider
var DatadogProvider *schema.Provider
// TranslateClientError turns an error into a message
func TranslateClientError(err error, httpresp *http.Response, msg string) error {
if msg == "" {
msg = "an error occurred"
}
if httpresp != nil && httpresp.Request != nil {
msg = fmt.Sprintf("%s from %s", msg, httpresp.Request.URL.String())
}
if apiErr, ok := err.(CustomRequestAPIError); ok {
return fmt.Errorf(msg+": %v: %s", err, apiErr.Body())
}
if apiErr, ok := err.(datadogV1.GenericOpenAPIError); ok {
return fmt.Errorf(msg+": %v: %s", err, apiErr.Body())
}
if apiErr, ok := err.(datadogV2.GenericOpenAPIError); ok {
return fmt.Errorf(msg+": %v: %s", err, apiErr.Body())
}
if errURL, ok := err.(*url.Error); ok {
return fmt.Errorf(msg+" (url.Error): %s", errURL)
}
return fmt.Errorf(msg+": %s", err.Error())
}
// CheckForUnparsed takes in a API response object and returns an error if it contains an unparsed element
func CheckForUnparsed(resp interface{}) error {
if unparsed, invalidPart := datadogV1.ContainsUnparsedObject(resp); unparsed {
return fmt.Errorf("object contains unparsed element: %+v", invalidPart)
}
return nil
}
// TranslateClientErrorDiag returns client error as type diag.Diagnostics
func TranslateClientErrorDiag(err error, httpresp *http.Response, msg string) diag.Diagnostics {
return diag.FromErr(TranslateClientError(err, httpresp, msg))
}
// GetUserAgent augments the default user agent with provider details
func GetUserAgent(clientUserAgent string) string {
return fmt.Sprintf("terraform-provider-datadog/%s (terraform %s; terraform-cli %s) %s",
version.ProviderVersion,
meta.SDKVersionString(),
DatadogProvider.TerraformVersion,
clientUserAgent)
}
// GetMetadataFromJSON decodes passed JSON data
func GetMetadataFromJSON(jsonBytes []byte, unmarshalled interface{}) error {
decoder := json.NewDecoder(bytes.NewReader(jsonBytes))
// make sure we return errors on attributes that we don't expect in metadata
decoder.DisallowUnknownFields()
if err := decoder.Decode(unmarshalled); err != nil {
return fmt.Errorf("failed to unmarshal metadata_json: %s", err)
}
return nil
}
// ConvertToSha256 builds a SHA256 hash of the passed string
func ConvertToSha256(content string) string {
data := []byte(content)
hash := sha256.Sum256(data)
return fmt.Sprintf("%x", hash[:])
}
// AccountAndNamespaceFromID returns account and namespace from an ID
func AccountAndNamespaceFromID(id string) (string, string, error) {
result := strings.SplitN(id, ":", 2)
if len(result) != 2 {
return "", "", fmt.Errorf("error extracting account ID and namespace: %s", id)
}
return result[0], result[1], nil
}
// AccountAndRoleFromID returns account and role from an ID
func AccountAndRoleFromID(id string) (string, string, error) {
result := strings.SplitN(id, ":", 2)
if len(result) != 2 {
return "", "", fmt.Errorf("error extracting account ID and Role name from an Amazon Web Services integration id: %s", id)
}
return result[0], result[1], nil
}
// AccountAndLambdaArnFromID returns account and Lambda ARN from an ID
func AccountAndLambdaArnFromID(id string) (string, string, error) {
result := strings.Split(id, " ")
if len(result) != 2 {
return "", "", fmt.Errorf("error extracting account ID and Lambda ARN from an AWS integration id: %s", id)
}
return result[0], result[1], nil
}
// TenantAndClientFromID returns azure account and client from an ID
func TenantAndClientFromID(id string) (string, string, error) {
result := strings.SplitN(id, ":", 2)
if len(result) != 2 {
return "", "", fmt.Errorf("error extracting tenant name and client ID from an Azure integration id: %s", id)
}
return result[0], result[1], nil
}
// AccountNameAndChannelNameFromID returns slack account and channel from an ID
func AccountNameAndChannelNameFromID(id string) (string, string, error) {
result := strings.SplitN(id, ":", 2)
if len(result) != 2 {
return "", "", fmt.Errorf("error extracting account name and channel name: %s", id)
}
return result[0], result[1], nil
}
// ConvertResponseByteToMap converts JSON []byte to map[string]interface{}
func ConvertResponseByteToMap(b []byte) (map[string]interface{}, error) {
convertedMap := make(map[string]interface{})
err := json.Unmarshal(b, &convertedMap)
if err != nil {
return nil, err
}
return convertedMap, nil
}
// DeleteKeyInMap deletes key (in dot notation) in map
func DeleteKeyInMap(mapObject map[string]interface{}, keyList []string) {
if len(keyList) == 1 {
delete(mapObject, keyList[0])
} else if m, ok := mapObject[keyList[0]].(map[string]interface{}); ok {
DeleteKeyInMap(m, keyList[1:])
}
return
}