Skip to content

Commit 8b18189

Browse files
authored
feat: add datastream maximum storage retention to realms (#36)
Add support for the datastream_maximum_storage_retention configuration option when creating or updating realms via the Housekeeping client. Include tests to verify proper handling of integer, nil, and undefined values. Signed-off-by: Davide Briani <davide.briani@secomind.com>
1 parent 492b354 commit 8b18189

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

lib/astarte/client/housekeeping/realms.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ defmodule Astarte.Client.Housekeeping.Realms do
4949
query = Keyword.get(opts, :query, [])
5050
device_registration_limit = Keyword.get(opts, :device_registration_limit)
5151

52+
datastream_maximum_storage_retention =
53+
Keyword.get(opts, :datastream_maximum_storage_retention)
54+
5255
data = %{
5356
realm_name: realm_name,
5457
jwt_public_key_pem: public_key_pem,
55-
device_registration_limit: device_registration_limit
58+
device_registration_limit: device_registration_limit,
59+
datastream_maximum_storage_retention: datastream_maximum_storage_retention
5660
}
5761

5862
with {:ok, replication_data} <- fetch_replication(opts),
@@ -141,7 +145,7 @@ defmodule Astarte.Client.Housekeeping.Realms do
141145
tesla_client = client.http_client
142146

143147
realm_data =
144-
[:jwt_public_key_pem, :device_registration_limit]
148+
[:jwt_public_key_pem, :device_registration_limit, :datastream_maximum_storage_retention]
145149
|> Enum.reduce(%{}, fn key, acc ->
146150
case Keyword.fetch(opts, key) do
147151
{:ok, value} -> Map.put(acc, key, value)

test/astarte/client/housekeeping/realms_test.exs

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,64 @@ defmodule Astarte.Client.Housekeeping.RealmsTest do
248248
assert :ok = Realms.create(client, @realm_name, @realm_public_key, replication_factor: 1)
249249
end
250250

251+
test "specifies integer datastream maximum storage retention", %{client: client} do
252+
opts = [datastream_maximum_storage_retention: 1]
253+
254+
Tesla.Mock.mock(fn %{body: body} ->
255+
assert %{
256+
"data" => %{
257+
"datastream_maximum_storage_retention" => 1
258+
}
259+
} = Jason.decode!(body)
260+
261+
Tesla.Mock.json(realm_response(), status: 201)
262+
end)
263+
264+
assert :ok =
265+
Realms.create(
266+
client,
267+
@realm_name,
268+
@realm_public_key,
269+
[replication_factor: 1] ++ opts
270+
)
271+
end
272+
273+
test "specifies nil datastream maximum storage retention", %{client: client} do
274+
opts = [datastream_maximum_storage_retention: nil]
275+
276+
Tesla.Mock.mock(fn %{body: body} ->
277+
assert %{
278+
"data" => %{
279+
"datastream_maximum_storage_retention" => nil
280+
}
281+
} = Jason.decode!(body)
282+
283+
Tesla.Mock.json(realm_response(), status: 201)
284+
end)
285+
286+
assert :ok =
287+
Realms.create(
288+
client,
289+
@realm_name,
290+
@realm_public_key,
291+
[replication_factor: 1] ++ opts
292+
)
293+
end
294+
295+
test "specifies nil without a defined datastream maximum storage retention", %{client: client} do
296+
Tesla.Mock.mock(fn %{body: body} ->
297+
assert %{
298+
"data" => %{
299+
"datastream_maximum_storage_retention" => nil
300+
}
301+
} = Jason.decode!(body)
302+
303+
Tesla.Mock.json(realm_response(), status: 201)
304+
end)
305+
306+
assert :ok = Realms.create(client, @realm_name, @realm_public_key, replication_factor: 1)
307+
end
308+
251309
test "specifies replication with simple strategy", %{client: client} do
252310
opts = [replication_factor: 1]
253311

@@ -380,6 +438,55 @@ defmodule Astarte.Client.Housekeeping.RealmsTest do
380438
assert :ok = Realms.update(client, @realm_name, [])
381439
end
382440

441+
test "specifies integer datastream maximum storage retention", %{client: client} do
442+
datastream_maximum_storage_retention = 1
443+
444+
Tesla.Mock.mock(fn %{body: body} ->
445+
assert %{
446+
"data" => %{
447+
"datastream_maximum_storage_retention" => ^datastream_maximum_storage_retention
448+
}
449+
} = Jason.decode!(body)
450+
451+
Tesla.Mock.json(realm_response(), status: 200)
452+
end)
453+
454+
assert :ok =
455+
Realms.update(client, @realm_name,
456+
datastream_maximum_storage_retention: datastream_maximum_storage_retention
457+
)
458+
end
459+
460+
test "specifies nil datastream maximum storage retention", %{client: client} do
461+
datastream_maximum_storage_retention = nil
462+
463+
Tesla.Mock.mock(fn %{body: body} ->
464+
assert %{
465+
"data" => %{
466+
"datastream_maximum_storage_retention" => ^datastream_maximum_storage_retention
467+
}
468+
} = Jason.decode!(body)
469+
470+
Tesla.Mock.json(realm_response(), status: 200)
471+
end)
472+
473+
assert :ok =
474+
Realms.update(client, @realm_name,
475+
datastream_maximum_storage_retention: datastream_maximum_storage_retention
476+
)
477+
end
478+
479+
test "does not specify datastream maximum storage retention if undefined", %{client: client} do
480+
Tesla.Mock.mock(fn %{body: body} ->
481+
assert %{"data" => data} = Jason.decode!(body)
482+
refute Map.has_key?(data, "datastream_maximum_storage_retention")
483+
484+
Tesla.Mock.json(realm_response(), status: 200)
485+
end)
486+
487+
assert :ok = Realms.update(client, @realm_name, [])
488+
end
489+
383490
test "retuns APIError on error", %{client: client} do
384491
error_data = %{"errors" => %{"detail" => "Forbidden"}}
385492
error_status = 403
@@ -500,7 +607,7 @@ defmodule Astarte.Client.Housekeeping.RealmsTest do
500607
%{
501608
"data" =>
502609
%{
503-
"datastream_maximum_storage_retention" => nil,
610+
"datastream_maximum_storage_retention" => opts[:datastream_maximum_storage_retention],
504611
"device_registration_limit" => opts[:device_registration_limit],
505612
"jwt_public_key_pem" => opts[:jwt_public_key_pem] || @realm_public_key,
506613
"realm_name" => opts[:realm_name] || @realm_name

0 commit comments

Comments
 (0)