Skip to content

Commit

Permalink
Merge pull request #12 from BencoXX/master
Browse files Browse the repository at this point in the history
Add connectionstring to client hosts config
  • Loading branch information
neilcook authored Oct 21, 2024
2 parents b663a3d + f16dc49 commit 3e37ab6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
ES_PASSWORD: "Changeme12456!"
ES_TEST_PROTOCOL: http
ES_TEST_PORT: 9200
ES_TEST_CONNECTIONSTRING: "http://localhost:9200"
CACERT_PATH:
ELASTIC_CREDS:
PROTOCOL: HTTP
Expand Down Expand Up @@ -61,13 +62,15 @@ jobs:
env:
ES_TEST_PROTOCOL: https
ES_TEST_PORT: 19200
ES_TEST_CONNECTIONSTRING: "https://localhost:19200"
ELASTIC_CREDS: -u ${{env.ES_USERNAME}}:${{env.ES_PASSWORD}}
PROTOCOL: https
run: cd tests && luarocks path >lpath.sh && source lpath.sh && lua run-tests.lua
- name: run the tests for opensearch2
if: ${{ matrix.elastic == 'os2' }}
env:
ES_TEST_PORT: 29200
ES_TEST_CONNECTIONSTRING: "http://localhost:29200"
run: cd tests && luarocks path >lpath.sh && source lpath.sh && lua run-tests.lua
- name: run the tests for el7
if: ${{ matrix.elastic == 'el7' }}
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"github-actions.workflows.pinned.workflows": [
".github/workflows/test-all.yml"
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The complete documetation is [here](http://elasticsearch-lua.readthedocs.io/).
hosts = {
{ -- Ignoring any of the following hosts parameters is allowed.
-- The default shall be set
connectionstring = "", -- Ignore host, port and protocol and parse this string instead
protocol = "http",
host = "localhost",
port = 9200,
Expand Down
4 changes: 4 additions & 0 deletions src/elasticsearch/Settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ local Settings = {}
-- Initial seed of hosts
Settings.hosts = {
{
connectionstring = "",
protocol = "http",
host = "localhost",
port = 9200,
Expand Down Expand Up @@ -131,6 +132,7 @@ function Settings:setParameters()
-- Checking hosts
if self.user_hosts ~= nil then
local default_host = {
connectionstring = self.hosts[1].connectionstring,
protocol = self.hosts[1].protocol,
host = self.hosts[1].host,
port = self.hosts[1].port,
Expand All @@ -142,6 +144,7 @@ function Settings:setParameters()
self.hosts = {}
for i, v in pairs(self.user_hosts) do
self.hosts[i] = {
connectionstring = default_host.connectionstring,
protocol = default_host.protocol,
host = default_host.host,
port = default_host.port,
Expand Down Expand Up @@ -172,6 +175,7 @@ function Settings:setConnectionSettings()
self.connections = {}
for key, host in pairs(self.hosts) do
table.insert(self.connections, Connection:new{
connectionstring = host.connectionstring,
protocol = host.protocol,
host = host.host,
port = host.port,
Expand Down
9 changes: 7 additions & 2 deletions src/elasticsearch/connection/Connection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ local Connection = {}
-------------------------------------------------------------------------------
-- Declaring instance variables
-------------------------------------------------------------------------------

-- The FULL connection string should be use
Connection.connectionstring = nil
-- The protocol of the connection
Connection.protocol = nil
-- The host where the connection should be made
Expand Down Expand Up @@ -83,7 +84,7 @@ function Connection:request(method, uri, params, body, timeout)
request.source = ltn12.source.string(body)
end
-- Adding auth to request
if self.username ~= nil and self.password ~= nil then
if self.username and self.username ~= "" and self.password and self.password ~= "" then
local authStr, foo = mime.b64(self.username .. ':' .. self.password)
request.headers['Authorization'] = 'Basic ' .. authStr
end
Expand Down Expand Up @@ -171,6 +172,10 @@ function Connection:buildURI(uri, params)
port = self.port,
path = uri
}
if self.connectionstring and self.connectionstring ~= "" then
urlComponents = url.parse(self.connectionstring)
urlComponents.path = uri
end
if params ~= nil then
urlComponents.query = self:buildQuery(params)
end
Expand Down
14 changes: 14 additions & 0 deletions tests/connection/ConnectionTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ function buildURITest()
con.protocol .. "://" .. con.host .. ":" .. con.port .. "/path?b=b_s&a=a_s" == url)
url = con:buildURI("/path", {a="a_s"})
assert_not_equal(con.protocol .. "://" .. con.host .. ":" .. con.port .. "/path?a=a_s&b=b_s", url)
local connectionstring = os.getenv("ES_TEST_CONNECTIONSTRING")
if connectionstring == nil then connectionstring = "http://localhost:9200" end
local con = Connection:new{
connectionstring = connectionstring,
username = username,
password = password,
pingTimeout = 1,
logger = logger,
requestEngine = "LuaSocket"
}
local url = con:buildURI("/path", {a="a_s", b="b_s"})
assert_true(con.connectionstring .. "/path?a=a_s&b=b_s" == url or con.connectionstring .. "/path?b=b_s&a=a_s" == url)
url = con:buildURI("/path", {a="a_s"})
assert_not_equal(con.connectionstring .. "/path?a=a_s&b=b_s", url)
end

-- Testing the request function
Expand Down

0 comments on commit 3e37ab6

Please sign in to comment.