Skip to content

Commit

Permalink
fix: Only set a default address if cloud_id is not configured (#16892)
Browse files Browse the repository at this point in the history
This was preventing the connection to Elastic Cloud from working as the client does not allow both `addresses` and `cloud_id` to be configured.
  • Loading branch information
mnorbury committed Feb 27, 2024
1 parent 3e3eae5 commit b5b0ce7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
32 changes: 32 additions & 0 deletions plugins/destination/elasticsearch/client/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion plugins/destination/elasticsearch/client/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package client
import (
_ "embed"
"runtime"

"github.com/invopop/jsonschema"
orderedmap "github.com/wk8/go-ordered-map/v2"
)

const (
Expand Down Expand Up @@ -50,7 +53,7 @@ type Spec struct {
var JSONSchema string

func (s *Spec) SetDefaults() {
if len(s.Addresses) == 0 {
if len(s.Addresses) == 0 && s.CloudID == "" {
s.Addresses = []string{"http://localhost:9200"}
}
if s.Concurrency == 0 {
Expand All @@ -67,3 +70,25 @@ func (s *Spec) SetDefaults() {
func (*Spec) Validate() error {
return nil
}

func (Spec) JSONSchemaExtend(sc *jsonschema.Schema) {
sc.Not = &jsonschema.Schema{
Description: "Either addresses or cloud_id must be set, but not both.",
Properties: func() *orderedmap.OrderedMap[string, *jsonschema.Schema] {
one := uint64(1)
properties := jsonschema.NewProperties()

addresses := *sc.Properties.Value("addresses")
addresses.MinLength = &one
properties.Set("addresses", &addresses)

cloudID := *sc.Properties.Value("cloud_id")
cloudID.MinLength = &one

properties.Set("cloud_id", &cloudID)

return properties
}(),
Required: []string{"addresses", "cloud_id"},
}
}
9 changes: 9 additions & 0 deletions plugins/destination/elasticsearch/client/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,14 @@ func TestJSONSchema(t *testing.T) {
Spec: `{"unknown": "test"}`,
Err: true,
},
{
Name: "spec with both addresses and cloud_id",
Spec: `{"addresses": ["address"], "cloud_id": "cloud_id"}`,
Err: true,
},
{
Name: "spec with valid cloud_id",
Spec: `{"cloud_id": "cloud_id"}`,
},
})
}
12 changes: 8 additions & 4 deletions plugins/destination/elasticsearch/docs/_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ spec:
version: "VERSION_DESTINATION_ELASTICSEARCH"
write_mode: "overwrite-delete-stale"
spec:
# Optional parameters
# Elastic Cloud configuration parameters
cloud_id: "${ELASTICSEARCH_CLOUD_ID}"
api_key: "${ELASTICSEARCH_API_KEY}"

# Self-hosted Elasticsearch configuration parameters
# addresses: ["http://localhost:9200"]
# username: ""
# password: ""
# cloud_id: ""
# api_key: ""
# service_token: ""
# certificate_fingerprint: ""
# ca_cert: ""

# Optional parameters
# concurrency: 5 # default: number of CPUs
# batch_size: 1000
# batch_size_bytes: 5242880 # 5 MiB
```
```

0 comments on commit b5b0ce7

Please sign in to comment.