Skip to content

Commit d48f561

Browse files
committed
[CLIENT] Allow a port to be set with a Cloud id and use default if no port is provided
1 parent 01fff96 commit d48f561

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class Client
4848
# @since 7.0.0
4949
DEFAULT_HOST = 'localhost:9200'.freeze
5050

51+
# The default port to use if connecting using a Cloud ID.
52+
#
53+
# @since 7.2.0
54+
DEFAULT_CLOUD_PORT = 9243
55+
5156
# Returns the transport object.
5257
#
5358
# @see Elasticsearch::Transport::Transport::Base
@@ -162,10 +167,11 @@ def perform_request(method, path, params={}, body=nil, headers=nil)
162167
def extract_cloud_creds(arguments)
163168
return unless arguments[:cloud_id]
164169
cloud_url, elasticsearch_instance = Base64.decode64(arguments[:cloud_id].gsub('name:', '')).split('$')
165-
[ { :scheme => 'https',
166-
:user => arguments[:user],
167-
:password => arguments[:password],
168-
:host => "#{elasticsearch_instance}.#{cloud_url}" } ]
170+
[ { scheme: 'https',
171+
user: arguments[:user],
172+
password: arguments[:password],
173+
host: "#{elasticsearch_instance}.#{cloud_url}",
174+
port: arguments[:port] || DEFAULT_CLOUD_PORT } ]
169175
end
170176

171177
# Normalizes and returns hosts configuration.

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,30 @@
285285
expect(hosts[0][:protocol]).to eq('https')
286286
expect(hosts[0][:user]).to eq('elastic')
287287
expect(hosts[0][:password]).to eq('changeme')
288+
expect(hosts[0][:port]).to eq(9243)
288289
end
289290

290291
it 'creates the correct full url' do
291-
expect(client.transport.__full_url(client.transport.hosts[0])).to eq('https://elastic:changeme@abcd.localhost')
292+
expect(client.transport.__full_url(client.transport.hosts[0])).to eq('https://elastic:changeme@abcd.localhost:9243')
293+
end
294+
295+
context 'when a port is specified' do
296+
297+
let(:client) do
298+
described_class.new(cloud_id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==', user: 'elastic', password: 'changeme', port: 9200 )
299+
end
300+
301+
it 'sets the specified port along with the cloud credentials' do
302+
expect(hosts[0][:host]).to eq('abcd.localhost')
303+
expect(hosts[0][:protocol]).to eq('https')
304+
expect(hosts[0][:user]).to eq('elastic')
305+
expect(hosts[0][:password]).to eq('changeme')
306+
expect(hosts[0][:port]).to eq(9200)
307+
end
308+
309+
it 'creates the correct full url' do
310+
expect(client.transport.__full_url(client.transport.hosts[0])).to eq('https://elastic:changeme@abcd.localhost:9200')
311+
end
292312
end
293313
end
294314

0 commit comments

Comments
 (0)