-
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.
Merge pull request #240 from rbino/base-images
Add Base Images
- Loading branch information
Showing
27 changed files
with
1,758 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
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,83 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2023 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.BaseImages.BaseImage do | ||
use Ecto.Schema | ||
use I18nHelpers.Ecto.TranslatableFields | ||
import Ecto.Changeset | ||
import Edgehog.Localization.Validation | ||
|
||
alias Edgehog.BaseImages.BaseImageCollection | ||
|
||
@type t :: Ecto.Schema.t() | ||
|
||
schema "base_images" do | ||
field :tenant_id, :integer, autogenerate: {Edgehog.Repo, :get_tenant_id, []} | ||
translatable_field :description | ||
translatable_field :release_display_name | ||
field :starting_version_requirement, :string | ||
field :url, :string | ||
field :version, :string | ||
translatable_belongs_to :base_image_collection, BaseImageCollection | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def create_changeset(base_image, attrs) do | ||
base_image | ||
|> cast(attrs, [:version, :release_display_name, :description, :starting_version_requirement]) | ||
|> validate_required([:version]) | ||
|> unique_constraint([:version, :base_image_collection_id, :tenant_id]) | ||
|> validate_change(:version, &validate_version/2) | ||
|> validate_change(:starting_version_requirement, &validate_version_requirement/2) | ||
|> validate_change(:description, &validate_locale/2) | ||
|> validate_change(:release_display_name, &validate_locale/2) | ||
end | ||
|
||
@doc false | ||
def update_changeset(base_image, attrs) do | ||
base_image | ||
|> cast(attrs, [:release_display_name, :description, :starting_version_requirement]) | ||
|> validate_change(:starting_version_requirement, &validate_version_requirement/2) | ||
|> validate_change(:description, &validate_locale/2) | ||
|> validate_change(:release_display_name, &validate_locale/2) | ||
end | ||
|
||
defp validate_version(field, value) do | ||
case Version.parse(value) do | ||
{:ok, _version} -> | ||
[] | ||
|
||
:error -> | ||
[{field, "is not a valid version"}] | ||
end | ||
end | ||
|
||
defp validate_version_requirement(field, value) do | ||
case Version.parse_requirement(value) do | ||
{:ok, _version} -> | ||
[] | ||
|
||
:error -> | ||
[{field, "is not a valid version requirement"}] | ||
end | ||
end | ||
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
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,41 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2023 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.BaseImages.BucketStorage do | ||
@behaviour Edgehog.BaseImages.Storage | ||
|
||
alias Edgehog.BaseImages.BaseImage | ||
alias Edgehog.BaseImages.Uploaders | ||
|
||
@impl true | ||
def store(%BaseImage{} = scope, %Plug.Upload{} = upload) do | ||
with {:ok, file_name} <- Uploaders.BaseImage.store({upload, scope}) do | ||
# TODO: investigate URL signing instead of public access | ||
file_url = Uploaders.BaseImage.url({file_name, scope}) | ||
{:ok, file_url} | ||
end | ||
end | ||
|
||
@impl true | ||
def delete(%BaseImage{} = scope) do | ||
%BaseImage{url: url} = scope | ||
Uploaders.BaseImage.delete({url, scope}) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2023 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.BaseImages.Storage do | ||
alias Edgehog.BaseImages.BaseImage | ||
|
||
@type upload :: %Plug.Upload{} | ||
|
||
@callback store(scope :: BaseImage.t(), file :: upload()) :: | ||
{:ok, file_url :: String.t()} | {:error, reason :: any} | ||
|
||
@callback delete(base_image :: BaseImage.t()) :: | ||
:ok | {:error, reason :: any} | ||
end |
Oops, something went wrong.