-
Notifications
You must be signed in to change notification settings - Fork 3
/
turbot_guardrails.go
143 lines (115 loc) · 3.47 KB
/
turbot_guardrails.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
package credential
import (
"context"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/v2/modconfig"
"github.com/turbot/pipe-fittings/v2/utils"
"github.com/zclconf/go-cty/cty"
)
type GuardrailsCredential struct {
CredentialImpl
AccessKey *string `json:"access_key,omitempty" cty:"access_key" hcl:"access_key,optional"`
SecretKey *string `json:"secret_key,omitempty" cty:"secret_key" hcl:"secret_key,optional"`
Workspace *string `json:"workspace,omitempty" cty:"workspace" hcl:"workspace,optional"`
}
func (c *GuardrailsCredential) getEnv() map[string]cty.Value {
env := map[string]cty.Value{}
if c.AccessKey != nil {
env["TURBOT_ACCESS_KEY"] = cty.StringVal(*c.AccessKey)
}
if c.SecretKey != nil {
env["TURBOT_SECRET_KEY"] = cty.StringVal(*c.SecretKey)
}
if c.Workspace != nil {
env["TURBOT_WORKSPACE"] = cty.StringVal(*c.Workspace)
}
return env
}
func (c *GuardrailsCredential) 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 *GuardrailsCredential) 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.(*GuardrailsCredential)
if !ok {
return false
}
if !utils.PtrEqual(c.AccessKey, other.AccessKey) {
return false
}
if !utils.PtrEqual(c.SecretKey, other.SecretKey) {
return false
}
if !utils.PtrEqual(c.Workspace, other.Workspace) {
return false
}
return true
}
func (c *GuardrailsCredential) Resolve(ctx context.Context) (Credential, error) {
guardrailsAccessKeyEnvVar := os.Getenv("TURBOT_ACCESS_KEY")
guardrailsSecretKeyEnvVar := os.Getenv("TURBOT_SECRET_KEY")
guardrailsWorkspaceEnvVar := os.Getenv("TURBOT_WORKSPACE")
// Don't modify existing credential, resolve to a new one
newCreds := &GuardrailsCredential{
CredentialImpl: c.CredentialImpl,
Workspace: c.Workspace,
}
if c.AccessKey == nil {
newCreds.AccessKey = &guardrailsAccessKeyEnvVar
} else {
newCreds.AccessKey = c.AccessKey
}
if c.SecretKey == nil {
newCreds.SecretKey = &guardrailsSecretKeyEnvVar
} else {
newCreds.SecretKey = c.SecretKey
}
if c.Workspace == nil {
newCreds.Workspace = &guardrailsWorkspaceEnvVar
} else {
newCreds.Workspace = c.Workspace
}
return newCreds, nil
}
func (c *GuardrailsCredential) GetTtl() int {
return -1
}
func (c *GuardrailsCredential) Validate() hcl.Diagnostics {
return hcl.Diagnostics{}
}
type GuardrailsConnectionConfig struct {
AccessKey *string `cty:"access_key" hcl:"access_key,optional"`
Profile *string `cty:"profile" hcl:"profile,optional"`
SecretKey *string `cty:"secret_key" hcl:"secret_key,optional"`
Workspace *string `cty:"workspace" hcl:"workspace,optional"`
}
func (c *GuardrailsConnectionConfig) GetCredential(name string, shortName string) Credential {
guardrailsCred := &GuardrailsCredential{
CredentialImpl: CredentialImpl{
HclResourceImpl: modconfig.HclResourceImpl{
FullName: name,
ShortName: shortName,
UnqualifiedName: name,
},
Type: "guardrails",
},
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
Workspace: c.Workspace,
}
return guardrailsCred
}