Skip to content

Commit

Permalink
Put all discovery endpoints in AdditionalConnectionDetails
Browse files Browse the repository at this point in the history
Signed-off-by: Rickard von Essen <[email protected]>
  • Loading branch information
rickard-von-essen committed Oct 25, 2024
1 parent 793ee09 commit 9cc2c51
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions config/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
package redis

import (
"strconv"
"fmt"

"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/upjet/pkg/config"
"github.com/pkg/errors"

"github.com/upbound/provider-gcp/config/common"
)

// Configure configures individual resources by adding custom
Expand All @@ -33,34 +29,21 @@ func Configure(p *config.Provider) {
r.UseAsync = true
r.Sensitive.AdditionalConnectionDetailsFn = func(attr map[string]any) (map[string][]byte, error) {
conn := map[string][]byte{}

address, err := common.GetField(attr, "discovery_endpoints[0].address")
if err != nil {
return nil, err
}
conn["address"] = []byte(address)

port, err := GetFloat(attr, "discovery_endpoints[0].port")
if err != nil {
return nil, err
if discoveryendpoints, ok := attr["discovery_endpoints"].([]any); ok {
for i, de := range discoveryendpoints {
if discoveryendpoints, ok := de.(map[string]any); ok && len(discoveryendpoints) > 0 {
if address, ok := discoveryendpoints["address"].(string); ok {
key := fmt.Sprintf("discovery_endpoints_%d_address", i)
conn[key] = []byte(address)
}
if port, ok := discoveryendpoints["port"].(float64); ok {
key := fmt.Sprintf("discovery_endpoints_%d_port", i)
conn[key] = []byte(fmt.Sprintf("%g", port))
}
}
}
}
conn["port"] = []byte(strconv.FormatFloat(port, 'f', -1, 64))

return conn, nil
}
})
}

// GetFloat value of the supplied field path.
func GetFloat(from map[string]interface{}, path string) (float64, error) {
v, err := fieldpath.Pave(from).GetValue(path)
if err != nil {
return 0, err
}

f, ok := v.(float64)
if !ok {
return 0, errors.Errorf("%s: not a (float64) number", path)
}
return f, nil
}

0 comments on commit 9cc2c51

Please sign in to comment.