-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
216 lines (188 loc) · 5.13 KB
/
provider.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/go-redis/redis"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
)
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REDIS_ADDRESS", "localhost:6379"),
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDIS_PASSWORD", ""),
},
"db": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("0", ""),
},
"tls_enabled": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDIS_TLS_ENABLED", false),
},
"tls_ca_cert": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDIS_TLS_CA_CERT", ""),
},
"tls_client_cert": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDIS_TLS_CLIENT_CERT", ""),
},
"tls_client_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDIS_TLS_CLIENT_KEY", ""),
},
"tls_server_name": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDIS_TLS_SERVER_NAME", ""),
},
},
ResourcesMap: map[string]*schema.Resource{
"redis_key": resourceRedisKey(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
address := d.Get("address").(string)
password := d.Get("password").(string)
db := d.Get("db").(int)
tlsEnabled := d.Get("tls_enabled").(bool)
tlsCACert := d.Get("tls_ca_cert").(string)
tlsClientCert := d.Get("tls_client_cert").(string)
tlsClientKey := d.Get("tls_client_key").(string)
tlsServerName := d.Get("tls_server_name").(string)
var tlsConfig *tls.Config
if tlsEnabled {
tlsConfig = &tls.Config{
ServerName: tlsServerName,
}
if tlsCACert != "" {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(tlsCACert))
tlsConfig.RootCAs = caCertPool
}
if tlsClientCert != "" && tlsClientKey != "" {
cert, err := tls.LoadX509KeyPair(tlsClientCert, tlsClientKey)
if err != nil {
return nil, fmt.Errorf("error loading TLS client certificate and key: %v", err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
}
redisClient := redis.NewClient(&redis.Options{
Addr: address,
Password: password,
DB: db,
TLSConfig: tlsConfig,
})
_, err := redisClient.Ping().Result()
if err != nil {
return nil, fmt.Errorf("error connecting to Redis: %v", err)
}
return redisClient, nil
}
func resourceRedisKey() *schema.Resource {
return &schema.Resource{
Create: resourceRedisKeyCreate,
Read: resourceRedisKeyRead,
Update: resourceRedisKeyUpdate,
Delete: resourceRedisKeyDelete,
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceRedisKeyCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*redis.Client)
key := d.Get("key").(string)
value := d.Get("value").(string)
err := client.Set(key, value, 0).Err()
if err != nil {
return err
}
d.SetId(key)
return nil
}
func resourceRedisKeyRead(d *schema.ResourceData, m interface{}) error {
client := m.(*redis.Client)
key := d.Id()
// Check if the key exists
exists, err := client.Exists(key).Result()
if err != nil {
return fmt.Errorf("error checking if key exists: %v", err)
}
if exists == 0 {
// Key doesn't exist, so clear the value in state
d.Set("value", "")
return nil
}
// Key exists, get its value
value, err := client.Get(key).Result()
if err != nil {
return fmt.Errorf("error getting value for key %s: %v", key, err)
}
// Update resource state with retrieved value
d.Set("value", value)
return nil
}
func resourceRedisKeyUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*redis.Client)
key := d.Id()
newValue := d.Get("value").(string)
// Check if the new value is different from the existing value
if d.HasChange("value") {
// If there's a change, update the value in Redis
err := client.Set(key, newValue, 0).Err()
if err != nil {
return err
}
}
return nil
}
func resourceRedisKeyDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*redis.Client)
key := d.Id()
// Check if the key exists (optional)
exists, err := client.Exists(key).Result()
if err != nil {
return fmt.Errorf("error checking if key exists: %v", err)
}
if exists == 1 {
// If the key exists, delete it
if err := client.Del(key).Err(); err != nil {
return fmt.Errorf("error deleting key %s: %v", key, err)
}
}
// Clear the resource ID from the state regardless of whether the key was deleted
d.SetId("")
return nil
}
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() *schema.Provider {
return Provider()
},
})
}