Skip to content

Commit eba79fa

Browse files
committed
AstarteImport: Add support for db_host_port argument
Updated Astarte.Import to accept db_host_and_port as the argument Updated CLI.main/3 to receive and handle db_host_and_port Ensured db_host_and_port is properly passed to populate/4 instead of relying on Application.get_env/1 Improved flexibility in database configuration by allowing direct host and port specification Added @db_host_and_port to test cases, including "Test import into Cassandra database Signed-off-by: Jusuf <jusuf.avdic@secomind.com>
1 parent 5d399ec commit eba79fa

File tree

5 files changed

+9
-9
lines changed

5 files changed

+9
-9
lines changed

tools/astarte_import/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ docker run -e CASSANDRA_DB_HOST=127.0.0.1 -e CASSANDRA_DB_PORT=9042 \
1111
Command to run data import:
1212

1313
```bash
14-
mix astarte.import <realm> <xml file>
14+
mix astarte.import <db_host_and_port> <realm> <xml file>
1515
```
1616

1717
```xml

tools/astarte_import/lib/astarte/import/cli.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule Astarte.Import.CLI do
2222

2323
@chunk_size 4096
2424

25-
def main([realm, file_name]) do
25+
def main([db_host_and_port, realm, file_name]) do
2626
with {:started, {:ok, _}} <- {:started, Application.ensure_all_started(:astarte_import)},
2727
true <- String.valid?(realm),
2828
true <- String.valid?(file_name),
@@ -38,7 +38,7 @@ defmodule Astarte.Import.CLI do
3838
end
3939

4040
# Call the populate function with the necessary arguments
41-
PopulateDB.populate(realm, data, more_data)
41+
PopulateDB.populate(db_host_and_port, realm, data, more_data)
4242
else
4343
error ->
4444
Logger.error("Failed to start import: #{inspect(error)}")

tools/astarte_import/lib/astarte/import/populatedb.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ defmodule Astarte.Import.PopulateDB do
4141
]
4242
end
4343

44-
def populate(realm, xml, continuation_fun \\ :undefined) do
44+
def populate(db_host_and_port, realm, xml, continuation_fun \\ :undefined) do
4545
Logger.info("Import started.", realm: realm)
4646

47-
nodes = Application.get_env(:cqerl, :cassandra_nodes)
48-
{host, port} = Enum.random(nodes)
47+
[host, port] = String.split(db_host_and_port, ":")
4948
Logger.info("Connecting to #{host}:#{port} cassandra database.", realm: realm)
5049

5150
{:ok, xandra_conn} = Xandra.start_link(nodes: ["#{host}:#{port}"])

tools/astarte_import/lib/mix/tasks/astarte_import.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Mix.Tasks.Astarte.Import do
1111

1212
# Process the arguments
1313
case args do
14-
[realm, file_name] ->
14+
[db_host_and_port, realm, file_name] ->
1515
Config.validate!()
1616

1717
xandra_options = Config.xandra_options!()
@@ -28,7 +28,7 @@ defmodule Mix.Tasks.Astarte.Import do
2828
opts = [strategy: :one_for_one, name: AstarteImport.Supervisor]
2929
Supervisor.start_link(children, opts)
3030

31-
CLI.main([realm, file_name])
31+
CLI.main([db_host_and_port, realm, file_name])
3232

3333
_ ->
3434
Logger.info("Usage: mix astarte.import <realm> <file_name>")

tools/astarte_import/test/astarte/populatedb_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule Astarte.PopulateDBTest do
2121
alias Astarte.Import.PopulateDB
2222

2323
@realm "test"
24+
@db_host_and_port "localhost:9042"
2425

2526
@xml """
2627
<?xml version="1.0" encoding="UTF-8"?>
@@ -77,6 +78,6 @@ defmodule Astarte.PopulateDBTest do
7778
"""
7879

7980
test "Test import into Cassandra database" do
80-
PopulateDB.populate(@realm, @xml)
81+
PopulateDB.populate(@db_host_and_port, @realm, @xml)
8182
end
8283
end

0 commit comments

Comments
 (0)