/
datadog.go
132 lines (105 loc) · 2.92 KB
/
datadog.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
package credential
import (
"context"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/utils"
"github.com/zclconf/go-cty/cty"
)
type DatadogCredential struct {
CredentialImpl
APIKey *string `json:"api_key,omitempty" cty:"api_key" hcl:"api_key,optional"`
AppKey *string `json:"app_key,omitempty" cty:"app_key" hcl:"app_key,optional"`
APIUrl *string `json:"api_url,omitempty" cty:"api_url" hcl:"api_url,optional"`
}
func (c *DatadogCredential) getEnv() map[string]cty.Value {
env := map[string]cty.Value{}
if c.APIKey != nil {
env["DD_CLIENT_API_KEY"] = cty.StringVal(*c.APIKey)
}
if c.AppKey != nil {
env["DD_CLIENT_APP_KEY"] = cty.StringVal(*c.AppKey)
}
return env
}
func (c *DatadogCredential) CtyValue() (cty.Value, error) {
ctyValue, err := modconfig.GetCtyValue(c)
if err != nil {
return cty.NilVal, err
}
valueMap := ctyValue.AsValueMap()
valueMap["env"] = cty.ObjectVal(c.getEnv())
return cty.ObjectVal(valueMap), nil
}
func (c *DatadogCredential) Equals(otherCredential Credential) bool {
// If both pointers are nil, they are considered equal
if c == nil && helpers.IsNil(otherCredential) {
return true
}
if (c == nil && !helpers.IsNil(otherCredential)) || (c != nil && helpers.IsNil(otherCredential)) {
return false
}
other, ok := otherCredential.(*DatadogCredential)
if !ok {
return false
}
if !utils.PtrEqual(c.APIKey, other.APIKey) {
return false
}
if !utils.PtrEqual(c.AppKey, other.AppKey) {
return false
}
if !utils.PtrEqual(c.APIUrl, other.APIUrl) {
return false
}
return true
}
func (c *DatadogCredential) Resolve(ctx context.Context) (Credential, error) {
datadogAPIKeyEnvVar := os.Getenv("DD_CLIENT_API_KEY")
datadogAppKeyEnvVar := os.Getenv("DD_CLIENT_APP_KEY")
// Don't modify existing credential, resolve to a new one
newCreds := &DatadogCredential{
CredentialImpl: c.CredentialImpl,
APIUrl: c.APIUrl,
}
if c.APIKey == nil {
newCreds.APIKey = &datadogAPIKeyEnvVar
} else {
newCreds.APIKey = c.APIKey
}
if c.AppKey == nil {
newCreds.AppKey = &datadogAppKeyEnvVar
} else {
newCreds.AppKey = c.AppKey
}
return newCreds, nil
}
func (c *DatadogCredential) GetTtl() int {
return -1
}
func (c *DatadogCredential) Validate() hcl.Diagnostics {
return hcl.Diagnostics{}
}
type DatadogConnectionConfig struct {
APIKey *string `cty:"api_key" hcl:"api_key"`
APIUrl *string `cty:"api_url" hcl:"api_url"`
AppKey *string `cty:"app_key" hcl:"app_key"`
}
func (c *DatadogConnectionConfig) GetCredential(name string, shortName string) Credential {
datadogCred := &DatadogCredential{
CredentialImpl: CredentialImpl{
HclResourceImpl: modconfig.HclResourceImpl{
FullName: name,
ShortName: shortName,
UnqualifiedName: name,
},
Type: "datadog",
},
APIKey: c.APIKey,
APIUrl: c.APIUrl,
AppKey: c.AppKey,
}
return datadogCred
}