-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(containers): implement create volume request
closes #627 , the application can send the appropriate astarte calls to the device to create a new volume trough the `send_create_volume_request` action Signed-off-by: Luca Zaninotto <luca.zaninotto@secomind.com>
- Loading branch information
Showing
6 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
backend/lib/edgehog/astarte/device/create_volume_request.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
defmodule Edgehog.Astarte.Device.CreateVolumeRequest do | ||
@moduledoc false | ||
@behaviour Edgehog.Astarte.Device.CreateVolumeRequest.Behaviour | ||
|
||
alias Astarte.Client.AppEngine | ||
alias Edgehog.Error.AstarteAPIError | ||
|
||
@interface "io.edgehog.devicemanager.apps.CreateVolumeRequest" | ||
|
||
@impl Edgehog.Astarte.Device.CreateVolumeRequest.Behaviour | ||
def send_create_volume_request(%AppEngine{} = client, device_id, request_data) do | ||
request_data = Map.from_struct(request_data) | ||
|
||
api_call = | ||
AppEngine.Devices.send_datastream( | ||
client, | ||
device_id, | ||
@interface, | ||
"/volume", | ||
request_data | ||
) | ||
|
||
with {:error, api_error} <- api_call do | ||
reason = AstarteAPIError.exception(status: api_error.status, response: api_error.response) | ||
|
||
{:error, reason} | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
backend/lib/edgehog/astarte/device/create_volume_request/behaviour.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
defmodule Edgehog.Astarte.Device.CreateVolumeRequest.Behaviour do | ||
@moduledoc false | ||
alias Astarte.Client.AppEngine | ||
alias Edgehog.Astarte.Device.CreateVolumeRequest.RequestData | ||
|
||
@callback send_create_volume_request( | ||
client :: AppEngine.t(), | ||
device_id :: String.t(), | ||
request_data :: RequestData.t() | ||
) :: :ok | {:error, term()} | ||
end |
35 changes: 35 additions & 0 deletions
35
backend/lib/edgehog/astarte/device/create_volume_request/request_data.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
defmodule Edgehog.Astarte.Device.CreateVolumeRequest.RequestData do | ||
@moduledoc false | ||
|
||
defstruct [ | ||
:id, | ||
:driver, | ||
:options | ||
] | ||
|
||
@type t() :: %__MODULE__{ | ||
id: String.t(), | ||
driver: String.t(), | ||
options: list(String.t()) | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
backend/lib/edgehog/devices/device/manual_actions/send_create_volume.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule Edgehog.Devices.Device.ManualActions.SendCreateVolume do | ||
@moduledoc false | ||
use Ash.Resource.ManualUpdate | ||
|
||
alias Edgehog.Astarte.Device.CreateVolumeRequest.RequestData | ||
|
||
@send_create_volume_request_behaviour Application.compile_env( | ||
:edgehog, | ||
:astarte_creater_volume_request_module, | ||
Edgehog.Astarte.Device.CreateVolumeRequest | ||
) | ||
|
||
@impl Ash.Resource.ManualUpdate | ||
def update(changeset, _opts, _context) do | ||
device = changeset.data | ||
|
||
with {:ok, volume} <- Ash.Changeset.fetch_argument(changeset, :volume), | ||
{:ok, volume} <- Ash.load(volume, :options_encoding), | ||
{:ok, device} <- Ash.load(device, :appengine_client) do | ||
data = %RequestData{ | ||
id: volume.id, | ||
driver: volume.driver, | ||
options: volume.options_encoding | ||
} | ||
|
||
with :ok <- | ||
@send_create_volume_request_behaviour.send_create_volume_request( | ||
device.appengine_client, | ||
device.device_id, | ||
data | ||
) do | ||
{:ok, device} | ||
end | ||
end | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
.../priv/astarte_resources/interfaces/io.edgehog.devicemanager.apps.CreateVolumeRequest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"interface_name": "io.edgehog.devicemanager.apps.CreateVolumeRequest", | ||
"version_major": 0, | ||
"version_minor": 1, | ||
"type": "datastream", | ||
"ownership": "server", | ||
"aggregation": "object", | ||
"mappings": [ | ||
{ | ||
"endpoint": "/volume/id", | ||
"type": "string", | ||
"database_retention_policy": "use_ttl", | ||
"database_retention_ttl": 31556952, | ||
"description": "Create Volume Request id", | ||
"doc": "Unique id for the volume." | ||
}, | ||
{ | ||
"endpoint": "/volume/driver", | ||
"type": "string", | ||
"database_retention_policy": "use_ttl", | ||
"database_retention_ttl": 31556952, | ||
"description": "Volume driver name", | ||
"doc": "Name of the volume driver to use." | ||
}, | ||
{ | ||
"endpoint": "/volume/options", | ||
"type": "stringarray", | ||
"database_retention_policy": "use_ttl", | ||
"database_retention_ttl": 31556952, | ||
"description": "Volume driver options", | ||
"doc": "An array of key=value options to set for the driver." | ||
} | ||
] | ||
} |