@@ -12,20 +12,48 @@ defmodule Sequin.Consumers.ElasticsearchSink do
1212 field :type , Ecto.Enum , values: [ :elasticsearch ] , default: :elasticsearch
1313 field :endpoint_url , :string
1414 field :index_name , :string
15- field :auth_type , Ecto.Enum , values: [ :api_key , :basic , :bearer ] , default: :api_key
15+ field :auth_type , Ecto.Enum , values: [ :none , : api_key, :basic , :bearer ] , default: :api_key
1616 field :auth_value , Sequin.Encrypted.Binary
1717 field :batch_size , :integer , default: 100
18+ field :routing_mode , Ecto.Enum , values: [ :dynamic , :static ]
1819 end
1920
2021 def changeset ( struct , params ) do
2122 struct
22- |> cast ( params , [ :endpoint_url , :index_name , :auth_type , :auth_value , :batch_size ] )
23- |> validate_required ( [ :endpoint_url , :index_name , :auth_type , :auth_value ] )
23+ |> cast ( params , [ :endpoint_url , :index_name , :auth_type , :auth_value , :batch_size , :routing_mode ] )
24+ |> validate_required ( [ :endpoint_url , :auth_type ] )
25+ |> validate_auth ( )
26+ |> validate_routing ( )
2427 |> validate_endpoint_url ( )
2528 |> validate_length ( :index_name , max: 1024 )
2629 |> validate_number ( :batch_size , greater_than: 0 , less_than_or_equal_to: 10_000 )
2730 end
2831
32+ defp validate_auth ( changeset ) do
33+ auth_type = get_field ( changeset , :auth_type )
34+
35+ if auth_type == :none do
36+ put_change ( changeset , :auth_value , nil )
37+ else
38+ validate_required ( changeset , [ :auth_value ] )
39+ end
40+ end
41+
42+ defp validate_routing ( changeset ) do
43+ routing_mode = get_field ( changeset , :routing_mode )
44+
45+ cond do
46+ routing_mode == :dynamic ->
47+ put_change ( changeset , :index_name , nil )
48+
49+ routing_mode == :static ->
50+ validate_required ( changeset , [ :index_name ] )
51+
52+ true ->
53+ add_error ( changeset , :routing_mode , "is required" )
54+ end
55+ end
56+
2957 defp validate_endpoint_url ( changeset ) do
3058 changeset
3159 |> validate_change ( :endpoint_url , fn :endpoint_url , url ->
0 commit comments