lib/elasticsearch/transport/client.rb in elasticsearch-transport-7.1.0 vs lib/elasticsearch/transport/client.rb in elasticsearch-transport-7.2.0
- old
+ new
@@ -13,10 +13,12 @@
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
+require 'base64'
+
module Elasticsearch
module Transport
# Handles communication with an Elasticsearch cluster.
#
@@ -44,10 +46,15 @@
# The default host and port to use if not otherwise specified.
#
# @since 7.0.0
DEFAULT_HOST = 'localhost:9200'.freeze
+ # The default port to use if connecting using a Cloud ID.
+ #
+ # @since 7.2.0
+ DEFAULT_CLOUD_PORT = 9243
+
# Returns the transport object.
#
# @see Elasticsearch::Transport::Transport::Base
# @see Elasticsearch::Transport::Transport::HTTP::Faraday
#
@@ -99,10 +106,13 @@
# @option arguments [Constant] :selector An instance of selector strategy implemented with
# {Elasticsearch::Transport::Transport::Connections::Selector::Base}.
#
# @option arguments [String] :send_get_body_as Specify the HTTP method to use for GET requests with a body.
# (Default: GET)
+ # @option arguments [true, false] :compression Whether to compress requests. Gzip compression will be used.
+ # The default is false. Responses will automatically be inflated if they are compressed.
+ # If a custom transport object is used, it must handle the request compression and response inflation.
#
# @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
#
def initialize(arguments={}, &block)
@options = arguments.each_with_object({}){ |(k,v), args| args[k.to_sym] = v }
@@ -115,11 +125,12 @@
@arguments[:randomize_hosts] ||= false
@arguments[:transport_options] ||= {}
@arguments[:http] ||= {}
@options[:http] ||= {}
- @seeds = __extract_hosts(@arguments[:hosts] ||
+ @seeds = extract_cloud_creds(@arguments)
+ @seeds ||= __extract_hosts(@arguments[:hosts] ||
@arguments[:host] ||
@arguments[:url] ||
@arguments[:urls] ||
ENV['ELASTICSEARCH_URL'] ||
DEFAULT_HOST)
@@ -128,16 +139,10 @@
if @arguments[:request_timeout]
@arguments[:transport_options][:request] = { :timeout => @arguments[:request_timeout] }
end
- @arguments[:transport_options][:headers] ||= {}
-
- unless @arguments[:transport_options][:headers].keys.any? {|k| k.to_s.downcase =~ /content\-?\_?type/}
- @arguments[:transport_options][:headers]['Content-Type'] = 'application/json'
- end
-
if @arguments[:transport]
@transport = @arguments[:transport]
else
transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
if transport_class == Transport::HTTP::Faraday
@@ -159,9 +164,19 @@
method = @send_get_body_as if 'GET' == method && body
transport.perform_request(method, path, params, body, headers)
end
private
+
+ def extract_cloud_creds(arguments)
+ return unless arguments[:cloud_id]
+ cloud_url, elasticsearch_instance = Base64.decode64(arguments[:cloud_id].gsub('name:', '')).split('$')
+ [ { scheme: 'https',
+ user: arguments[:user],
+ password: arguments[:password],
+ host: "#{elasticsearch_instance}.#{cloud_url}",
+ port: arguments[:port] || DEFAULT_CLOUD_PORT } ]
+ end
# Normalizes and returns hosts configuration.
#
# Arrayifies the `hosts_config` argument and extracts `host` and `port` info from strings.
# Performs shuffling when the `randomize_hosts` option is set.