Skip to content

Commit ae24d36

Browse files
authored
Merge pull request #798 from rickard-von-essen/redis-instance-conn
feat: redis instance - Store serverCaCerts.[].cert to connection details
2 parents 31b1754 + 9c66c80 commit ae24d36

4 files changed

Lines changed: 95 additions & 56 deletions

File tree

config/cluster/redis/config.go

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,53 @@ import (
1313
// Configure configures individual resources by adding custom
1414
// ResourceConfigurators.
1515
func Configure(p *config.Provider) {
16-
p.AddResourceConfigurator("google_redis_instance", func(r *config.Resource) {
17-
config.MarkAsRequired(r.TerraformResource, "region")
18-
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
19-
conn := map[string][]byte{}
20-
if a, ok := attr["host"].(string); ok {
21-
conn["host"] = []byte(a)
16+
p.AddResourceConfigurator("google_redis_instance", redisInstance)
17+
p.AddResourceConfigurator("google_redis_cluster", redisCluster)
18+
}
19+
20+
func redisInstance(r *config.Resource) {
21+
config.MarkAsRequired(r.TerraformResource, "region")
22+
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
23+
conn := map[string][]byte{}
24+
if host, ok := attr["host"].(string); ok {
25+
conn["host"] = []byte(host)
26+
}
27+
if port, ok := attr["port"].(float64); ok {
28+
conn["port"] = []byte(fmt.Sprintf("%g", port))
29+
}
30+
if caCerts, ok := attr["server_ca_certs"].([]any); ok {
31+
for i, ca := range caCerts {
32+
if caCerts, ok := ca.(map[string]any); ok && len(caCerts) > 0 {
33+
if cert, ok := caCerts["cert"].(string); ok {
34+
key := fmt.Sprintf("server_ca_certs_%d_cert", i)
35+
conn[key] = []byte(cert)
36+
}
37+
}
2238
}
23-
return conn, nil
2439
}
25-
})
40+
return conn, nil
41+
}
42+
}
2643

27-
p.AddResourceConfigurator("google_redis_cluster", func(r *config.Resource) {
28-
r.MarkAsRequired("region")
29-
r.UseAsync = true
30-
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
31-
conn := map[string][]byte{}
32-
if discoveryendpoints, ok := attr["discovery_endpoints"].([]any); ok {
33-
for i, de := range discoveryendpoints {
34-
if discoveryendpoints, ok := de.(map[string]any); ok && len(discoveryendpoints) > 0 {
35-
if address, ok := discoveryendpoints["address"].(string); ok {
36-
key := fmt.Sprintf("discovery_endpoints_%d_address", i)
37-
conn[key] = []byte(address)
38-
}
39-
if port, ok := discoveryendpoints["port"].(float64); ok {
40-
key := fmt.Sprintf("discovery_endpoints_%d_port", i)
41-
conn[key] = []byte(fmt.Sprintf("%g", port))
42-
}
44+
func redisCluster(r *config.Resource) {
45+
r.MarkAsRequired("region")
46+
r.UseAsync = true
47+
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
48+
conn := map[string][]byte{}
49+
if discoveryendpoints, ok := attr["discovery_endpoints"].([]any); ok {
50+
for i, de := range discoveryendpoints {
51+
if discoveryendpoints, ok := de.(map[string]any); ok && len(discoveryendpoints) > 0 {
52+
if address, ok := discoveryendpoints["address"].(string); ok {
53+
key := fmt.Sprintf("discovery_endpoints_%d_address", i)
54+
conn[key] = []byte(address)
55+
}
56+
if port, ok := discoveryendpoints["port"].(float64); ok {
57+
key := fmt.Sprintf("discovery_endpoints_%d_port", i)
58+
conn[key] = []byte(fmt.Sprintf("%g", port))
4359
}
4460
}
4561
}
46-
return conn, nil
4762
}
48-
})
63+
return conn, nil
64+
}
4965
}

config/namespaced/redis/config.go

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,53 @@ import (
1313
// Configure configures individual resources by adding custom
1414
// ResourceConfigurators.
1515
func Configure(p *config.Provider) {
16-
p.AddResourceConfigurator("google_redis_instance", func(r *config.Resource) {
17-
config.MarkAsRequired(r.TerraformResource, "region")
18-
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
19-
conn := map[string][]byte{}
20-
if a, ok := attr["host"].(string); ok {
21-
conn["host"] = []byte(a)
16+
p.AddResourceConfigurator("google_redis_instance", redisInstance)
17+
p.AddResourceConfigurator("google_redis_cluster", redisCluster)
18+
}
19+
20+
func redisInstance(r *config.Resource) {
21+
config.MarkAsRequired(r.TerraformResource, "region")
22+
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
23+
conn := map[string][]byte{}
24+
if host, ok := attr["host"].(string); ok {
25+
conn["host"] = []byte(host)
26+
}
27+
if port, ok := attr["port"].(float64); ok {
28+
conn["port"] = []byte(fmt.Sprintf("%g", port))
29+
}
30+
if caCerts, ok := attr["server_ca_certs"].([]any); ok {
31+
for i, ca := range caCerts {
32+
if caCerts, ok := ca.(map[string]any); ok && len(caCerts) > 0 {
33+
if cert, ok := caCerts["cert"].(string); ok {
34+
key := fmt.Sprintf("server_ca_certs_%d_cert", i)
35+
conn[key] = []byte(cert)
36+
}
37+
}
2238
}
23-
return conn, nil
2439
}
25-
})
40+
return conn, nil
41+
}
42+
}
2643

27-
p.AddResourceConfigurator("google_redis_cluster", func(r *config.Resource) {
28-
r.MarkAsRequired("region")
29-
r.UseAsync = true
30-
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
31-
conn := map[string][]byte{}
32-
if discoveryendpoints, ok := attr["discovery_endpoints"].([]any); ok {
33-
for i, de := range discoveryendpoints {
34-
if discoveryendpoints, ok := de.(map[string]any); ok && len(discoveryendpoints) > 0 {
35-
if address, ok := discoveryendpoints["address"].(string); ok {
36-
key := fmt.Sprintf("discovery_endpoints_%d_address", i)
37-
conn[key] = []byte(address)
38-
}
39-
if port, ok := discoveryendpoints["port"].(float64); ok {
40-
key := fmt.Sprintf("discovery_endpoints_%d_port", i)
41-
conn[key] = []byte(fmt.Sprintf("%g", port))
42-
}
44+
func redisCluster(r *config.Resource) {
45+
r.MarkAsRequired("region")
46+
r.UseAsync = true
47+
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
48+
conn := map[string][]byte{}
49+
if discoveryendpoints, ok := attr["discovery_endpoints"].([]any); ok {
50+
for i, de := range discoveryendpoints {
51+
if discoveryendpoints, ok := de.(map[string]any); ok && len(discoveryendpoints) > 0 {
52+
if address, ok := discoveryendpoints["address"].(string); ok {
53+
key := fmt.Sprintf("discovery_endpoints_%d_address", i)
54+
conn[key] = []byte(address)
55+
}
56+
if port, ok := discoveryendpoints["port"].(float64); ok {
57+
key := fmt.Sprintf("discovery_endpoints_%d_port", i)
58+
conn[key] = []byte(fmt.Sprintf("%g", port))
4359
}
4460
}
4561
}
46-
return conn, nil
4762
}
48-
})
63+
return conn, nil
64+
}
4965
}

examples/cluster/redis/v1beta1/cluster.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ metadata:
99
name: cluster-ha
1010
spec:
1111
forProvider:
12-
authorizationMode: AUTH_MODE_DISABLED
12+
authorizationMode: AUTH_MODE_IAM_AUTH
1313
deletionProtectionEnabled: false
1414
nodeType: REDIS_SHARED_CORE_NANO
1515
pscConfigs:
@@ -21,9 +21,12 @@ spec:
2121
region: us-central1
2222
replicaCount: 1
2323
shardCount: 3
24-
transitEncryptionMode: TRANSIT_ENCRYPTION_MODE_DISABLED
24+
transitEncryptionMode: TRANSIT_ENCRYPTION_MODE_SERVER_AUTHENTICATION
2525
zoneDistributionConfig:
2626
mode: MULTI_ZONE
27+
writeConnectionSecretToRef:
28+
name: example-redis-cluster-secret
29+
namespace: upbound-system
2730

2831
---
2932

examples/namespaced/redis/v1beta1/cluster.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ metadata:
1010
namespace: upbound-system
1111
spec:
1212
forProvider:
13-
authorizationMode: AUTH_MODE_DISABLED
13+
authorizationMode: AUTH_MODE_IAM_AUTH
1414
deletionProtectionEnabled: false
1515
nodeType: REDIS_SHARED_CORE_NANO
1616
pscConfigs:
@@ -22,9 +22,13 @@ spec:
2222
region: us-central1
2323
replicaCount: 1
2424
shardCount: 3
25-
transitEncryptionMode: TRANSIT_ENCRYPTION_MODE_DISABLED
25+
transitEncryptionMode: TRANSIT_ENCRYPTION_MODE_SERVER_AUTHENTICATION
2626
zoneDistributionConfig:
2727
mode: MULTI_ZONE
28+
writeConnectionSecretToRef:
29+
name: example-redis-cluster-secret
30+
namespace: upbound-system
31+
2832
---
2933
apiVersion: compute.gcp.m.upbound.io/v1beta1
3034
kind: Network

0 commit comments

Comments
 (0)