Skip to content

Commit

Permalink
feat(containers): implement create volume request
Browse files Browse the repository at this point in the history
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
lusergit committed Nov 6, 2024
1 parent d76991e commit da9b0f0
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 0 deletions.
49 changes: 49 additions & 0 deletions backend/lib/edgehog/astarte/device/create_volume_request.ex
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
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
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
13 changes: 13 additions & 0 deletions backend/lib/edgehog/devices/device/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule Edgehog.Devices.Device do
alias Edgehog.Containers.Deployment
alias Edgehog.Containers.Image
alias Edgehog.Containers.Release
alias Edgehog.Containers.Volume
alias Edgehog.Devices.Device.BatterySlot
alias Edgehog.Devices.Device.Calculations
alias Edgehog.Devices.Device.Changes
Expand Down Expand Up @@ -209,6 +210,18 @@ defmodule Edgehog.Devices.Device do
manual ManualActions.SendCreateDeployment
end

update :send_create_volume_request do
description "Send a create volume request to the device."

argument :volume, :struct do
constraints instance_of: Volume
description "The new volume for the device."
allow_nil? false
end

manual ManualActions.SendCreateVolume
end

update :remove_tags do
description "Remove tags from a device."

Expand Down
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
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."
}
]
}

0 comments on commit da9b0f0

Please sign in to comment.