Skip to content

Commit

Permalink
Merge pull request #616 from noaccOS/chore/container-image
Browse files Browse the repository at this point in the history
chore(containers): init images
  • Loading branch information
rbino authored Oct 11, 2024
2 parents f91f2ed + 3f61271 commit c0bad26
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/lib/edgehog/containers/containers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule Edgehog.Containers do
end

resources do
resource Edgehog.Containers.Image
resource Edgehog.Containers.ImageCredentials
end
end
54 changes: 54 additions & 0 deletions backend/lib/edgehog/containers/image.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# 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.Containers.Image do
@moduledoc false
use Edgehog.MultitenantResource,
domain: Edgehog.Containers

actions do
defaults [:read, :destroy, create: [:reference, :image_credentials_id]]
end

attributes do
uuid_primary_key :id

attribute :reference, :string do
allow_nil? false
end

timestamps()
end

relationships do
belongs_to :credentials, Edgehog.Containers.ImageCredentials do
source_attribute :image_credentials_id
attribute_type :uuid
end
end

identities do
identity :reference, [:reference]
end

postgres do
table "images"
end
end
4 changes: 4 additions & 0 deletions backend/lib/edgehog/containers/image_credentials.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ defmodule Edgehog.Containers.ImageCredentials do
update_timestamp :updated_at
end

relationships do
has_many :images, Edgehog.Containers.Image
end

identities do
identity :name, [:name]
end
Expand Down
82 changes: 82 additions & 0 deletions backend/priv/repo/migrations/20241011135928_create_images.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#
# 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.Repo.Migrations.CreateImages do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
create table(:images, primary_key: false) do
add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
add :reference, :text, null: false

add :inserted_at, :utc_datetime_usec,
null: false,
default: fragment("(now() AT TIME ZONE 'utc')")

add :updated_at, :utc_datetime_usec,
null: false,
default: fragment("(now() AT TIME ZONE 'utc')")

add :tenant_id,
references(:tenants,
column: :tenant_id,
name: "images_tenant_id_fkey",
type: :bigint,
prefix: "public",
on_delete: :delete_all
),
null: false

add :image_credentials_id,
references(:image_credentials,
column: :id,
name: "images_image_credentials_id_fkey",
type: :uuid,
prefix: "public"
)
end

create index(:images, [:tenant_id])

create index(:images, [:id, :tenant_id], unique: true)

create unique_index(:images, [:tenant_id, :reference], name: "images_reference_index")
end

def down do
drop_if_exists unique_index(:images, [:tenant_id, :reference], name: "images_reference_index")

drop constraint(:images, "images_tenant_id_fkey")

drop constraint(:images, "images_image_credentials_id_fkey")

drop_if_exists index(:images, [:id, :tenant_id])

drop_if_exists index(:images, [:tenant_id])

drop table(:images)
end
end
182 changes: 182 additions & 0 deletions backend/priv/resource_snapshots/repo/images/20241011135928.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "fragment(\"gen_random_uuid()\")",
"generated?": false,
"primary_key?": true,
"references": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "reference",
"type": "text"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "inserted_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "updated_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "tenant_id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "images_tenant_id_fkey",
"on_delete": "delete",
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "tenants"
},
"size": null,
"source": "tenant_id",
"type": "bigint"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": "tenant_id",
"global": false,
"strategy": "attribute"
},
"name": "images_image_credentials_id_fkey",
"on_delete": null,
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "image_credentials"
},
"size": null,
"source": "image_credentials_id",
"type": "uuid"
}
],
"base_filter": null,
"check_constraints": [],
"custom_indexes": [
{
"all_tenants?": true,
"concurrently": false,
"error_fields": [
"id",
"tenant_id"
],
"fields": [
{
"type": "atom",
"value": "id"
},
{
"type": "atom",
"value": "tenant_id"
}
],
"include": null,
"message": null,
"name": null,
"nulls_distinct": true,
"prefix": null,
"table": null,
"unique": true,
"using": null,
"where": null
},
{
"all_tenants?": true,
"concurrently": false,
"error_fields": [
"tenant_id"
],
"fields": [
{
"type": "atom",
"value": "tenant_id"
}
],
"include": null,
"message": null,
"name": null,
"nulls_distinct": true,
"prefix": null,
"table": null,
"unique": false,
"using": null,
"where": null
}
],
"custom_statements": [],
"has_create_action": true,
"hash": "6DA67D38C7711DD727BE4563765E88E78AF0E64413CFEE56E9F3E19B2DF3781A",
"identities": [
{
"all_tenants?": false,
"base_filter": null,
"index_name": "images_reference_index",
"keys": [
{
"type": "atom",
"value": "reference"
}
],
"name": "reference",
"nils_distinct?": true,
"where": null
}
],
"multitenancy": {
"attribute": "tenant_id",
"global": false,
"strategy": "attribute"
},
"repo": "Elixir.Edgehog.Repo",
"schema": null,
"table": "images"
}

0 comments on commit c0bad26

Please sign in to comment.