Skip to content

Commit f45194b

Browse files
committed
AstarteExport: Add Cassandra host/port and update README
- Modified export_realm_data/3 to accept db_host_and_port as an argument. - Set Cassandra host and port as env variables - Updated the run/1 function to reflect the new parameters for exporting. - Enhanced documentation to include the new db_host_and_port argument. - Updated README to reflect the changes in function usage and parameters. Signed-off-by: Jusuf <jusuf.avdic@secomind.com>
1 parent 5926acc commit f45194b

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

tools/astarte_export/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Astarte Export is an easy to use tool that allows to exporting all the devices and data from an existing Astarte realm to XML format.
44

55
```iex
6-
iex(astarte_export@127.0.0.1)6> Astarte.Export.export_realm_data("test", "test.xml")
6+
iex(astarte_export@127.0.0.1)6> Astarte.Export.export_realm_data("localhost:9042" "test", "test.xml")
77
level=info ts=2020-02-03T03:57:21.412+00:00 msg="Export started." module=Astarte.Export function=generate_xml/2 realm=test tag=export_started
88
level=info ts=2020-02-03T03:57:21.413+00:00 msg="Connecting to \"172.23.0.3\":\"9042\" cassandra database." module=Astarte.Export.FetchData.Queries function=get_connection/0
99
level=info ts=2020-02-03T03:57:21.414+00:00 msg="Connected to database." module=Astarte.Export.FetchData.Queries function=get_connection/0

tools/astarte_export/lib/astarte/export.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,25 @@ defmodule Astarte.Export do
2929
"""
3030

3131
@doc """
32-
The export_realm_data/2 function required 2 arguments to export
32+
The export_realm_data/2 function required 3 arguments to export
3333
the realm data into XML format.
3434
the arguments are
35+
- db-host-and-port -> -> A string in the format "hostname:port",
36+
used to set the environment variables for Cassandra database connection.
3537
- realm-name -> This is a string format of input
3638
- file -> file where to export the realm data.
3739
- options -> options to export the realm data.
3840
"""
3941

40-
@spec export_realm_data(String.t(), String.t(), keyword()) ::
42+
@spec export_realm_data(String.t(), String.t(), String.t(), keyword()) ::
4143
:ok | {:error, :invalid_parameters} | {:error, any()}
4244

43-
def export_realm_data(realm, file, opts \\ []) do
45+
def export_realm_data(db_host_and_port, realm, file, opts \\ []) do
46+
[db_host, db_port] = String.split(db_host_and_port, ":")
47+
48+
System.put_env("CASSANDRA_DB_HOST", db_host)
49+
System.put_env("CASSANDRA_DB_PORT", db_port)
50+
4451
file = Path.expand(file) |> Path.absname()
4552

4653
with {:ok, fd} <- File.open(file, [:write]) do

tools/astarte_export/lib/mix/tasks/astarte_export.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ defmodule Mix.Tasks.Astarte.Export do
88
@shortdoc "export data from an existing Astarte realm"
99
def run(args) do
1010
case args do
11-
[realm, file_name] ->
11+
[db_host_and_port, realm, file_name] ->
1212
Logger.info("Exporting data from realm #{realm} to file #{file_name}")
1313

1414
case Application.ensure_all_started(:astarte_export) do
1515
{:ok, _} ->
16-
Export.export_realm_data(realm, file_name)
16+
Export.export_realm_data(db_host_and_port, realm, file_name)
1717

1818
{:error, reason} ->
1919
Logger.error("Cannot start applications: #{inspect(reason)}")
2020
end
2121

22-
[realm, file_name, device_id] ->
22+
[db_host_and_port, realm, file_name, device_id] ->
2323
Logger.info(
2424
"Exporting data for device #{device_id} from realm #{realm} to file #{file_name}"
2525
)
@@ -28,7 +28,7 @@ defmodule Mix.Tasks.Astarte.Export do
2828

2929
case Application.ensure_all_started(:astarte_export) do
3030
{:ok, _} ->
31-
Export.export_realm_data(realm, file_name, options)
31+
Export.export_realm_data(db_host_and_port, realm, file_name, options)
3232

3333
{:error, reason} ->
3434
Logger.error("Cannot start applications: #{inspect(reason)}")

tools/astarte_export/test/astarte_export_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Astarte.ExportTest do
55
alias Astarte.Export.FetchData
66
alias Astarte.DatabaseTestdata
77
@realm "test"
8+
@db_host_and_port "localhost:9042"
89

910
@expected_xml """
1011
<?xml version="1.0" encoding="UTF-8"?>
@@ -72,14 +73,14 @@ defmodule Astarte.ExportTest do
7273

7374
test "export realm data to xmlfile" do
7475
DatabaseTestdata.initialize_database()
75-
assert {:ok, :export_completed} == Export.export_realm_data(@realm, "test.xml")
76+
assert {:ok, :export_completed} == Export.export_realm_data(@db_host_and_port, @realm, "test.xml")
7677
file = Path.expand("test.xml") |> Path.absname()
7778
assert @expected_xml == File.read!(file)
7879
end
7980

8081
test "export realm data to xmlfile in a absolute path " do
8182
file = File.cwd!() <> "/test.xml"
82-
assert {:ok, :export_completed} == Export.export_realm_data(@realm, file)
83+
assert {:ok, :export_completed} == Export.export_realm_data(@db_host_and_port, @realm, file)
8384
assert @expected_xml == File.read!(file)
8485
end
8586

0 commit comments

Comments
 (0)