Skip to content

Commit

Permalink
Merge pull request #30 from davidebriani/housekeeping-realm-configura…
Browse files Browse the repository at this point in the history
…tion

Housekeeping: support updating realms configuration
  • Loading branch information
rbino authored Jul 30, 2024
2 parents 8286db6 + 3cd3a3d commit e7e6696
Show file tree
Hide file tree
Showing 2 changed files with 482 additions and 3 deletions.
40 changes: 38 additions & 2 deletions lib/astarte/client/housekeeping/realms.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2021 SECO Mind
# Copyright 2021-2024 SECO Mind
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,10 +47,12 @@ defmodule Astarte.Client.Housekeeping.Realms do
request_path = "realms"
tesla_client = client.http_client
query = Keyword.get(opts, :query, [])
device_registration_limit = Keyword.get(opts, :device_registration_limit)

data = %{
realm_name: realm_name,
jwt_public_key_pem: public_key_pem
jwt_public_key_pem: public_key_pem,
device_registration_limit: device_registration_limit
}

with {:ok, replication_data} <- fetch_replication(opts),
Expand Down Expand Up @@ -123,6 +125,40 @@ defmodule Astarte.Client.Housekeeping.Realms do
end
end

@doc """
Updates a realm.
## Examples
Astarte.Client.Housekeeping.Realms.update(client, realm_name, [jwt_public_key_pem: "..."])
Astarte.Client.Housekeeping.Realms.update(client, realm_name, [device_registration_limit: 100])
"""
def update(%Housekeeping{} = client, realm_name, opts)
when is_binary(realm_name) and is_list(opts) do
request_path = "realms/#{realm_name}"
tesla_client = client.http_client

realm_data =
[:jwt_public_key_pem, :device_registration_limit]
|> Enum.reduce(%{}, fn key, acc ->
case Keyword.fetch(opts, key) do
{:ok, value} -> Map.put(acc, key, value)
:error -> acc
end
end)

with {:ok, %Tesla.Env{} = result} <-
Tesla.patch(tesla_client, request_path, %{data: realm_data}) do
if result.status == 200 do
:ok
else
{:error, %APIError{status: result.status, response: result.body}}
end
end
end

@doc """
Deletes a realm.
Expand Down
Loading

0 comments on commit e7e6696

Please sign in to comment.