Skip to content

Commit

Permalink
Ash 3.0 update
Browse files Browse the repository at this point in the history
- Update Ash and all Ash components to 3.0 compatible versions
- Rename Api to Domain
- Remove legacy breaking-change compatibility options
- Explicitly accept attributes in actions
- Mark all public attributes, calculations and relationships
- Add the context argument to module based validations and calculations
- Explicitly load relationship fields in calculations
- Change Ash.Calculation to Ash.Resource.Calculation
- Remove hide_fields, rely instead on the public? option
- Mark non atomic actions with require_atomic? false
- Remove unnecessary domain from cross-domain relationships
- Replace Ash.Query.expr with Ash.Expr.expr
- Implement Ash.ToTenant for the Tenant resource
- Replace calls to domain modules with calls to the Ash module
- Handle new nested errors in JSON API tests

Signed-off-by: Riccardo Binetti <[email protected]>
  • Loading branch information
rbino committed Apr 10, 2024
1 parent 42a4a5c commit 02e2644
Show file tree
Hide file tree
Showing 45 changed files with 257 additions and 133 deletions.
5 changes: 1 addition & 4 deletions backend/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ config :edgehog, Edgehog.PromEx,
grafana: :disabled,
metrics_server: :disabled

config :edgehog, :ash_apis, [
config :edgehog, :ash_domains, [
Edgehog.Astarte,
Edgehog.Devices,
Edgehog.Groups,
Expand All @@ -98,9 +98,6 @@ config :edgehog, :ash_apis, [

config :ash, :default_belongs_to_type, :integer
config :ash, :custom_types, id: Edgehog.Types.Id
config :ash, :utc_datetime_type, :datetime
config :ash_graphql, :default_managed_relationship_type_name_template, :action_name
config :ash_graphql, :allow_non_null_mutation_arguments?, true

resource_section_order = [
:resource,
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/edgehog/astarte/astarte.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#

defmodule Edgehog.Astarte do
use Ash.Api
use Ash.Domain

resources do
resource Edgehog.Astarte.Cluster
Expand Down
13 changes: 10 additions & 3 deletions backend/lib/edgehog/astarte/cluster/cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

defmodule Edgehog.Astarte.Cluster do
use Ash.Resource,
api: Edgehog.Astarte,
domain: Edgehog.Astarte,
data_layer: AshPostgres.DataLayer

alias Edgehog.Astarte.Cluster
Expand All @@ -34,6 +34,7 @@ defmodule Edgehog.Astarte.Cluster do

create :create do
primary? true
accept [:base_api_url, :name]
upsert? true
upsert_identity :url
upsert_fields [:updated_at]
Expand All @@ -45,8 +46,14 @@ defmodule Edgehog.Astarte.Cluster do
attributes do
integer_primary_key :id

attribute :base_api_url, :string, allow_nil?: false
attribute :name, :string
attribute :base_api_url, :string do
public? true
allow_nil? false
end

attribute :name, :string do
public? true
end

create_timestamp :inserted_at
update_timestamp :updated_at
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/edgehog/astarte/cluster/validations/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Edgehog.Astarte.Cluster.Validations.URL do
end

@impl true
def validate(changeset, opts) do
def validate(changeset, opts, _context) do
case Ash.Changeset.fetch_argument_or_change(changeset, opts[:attribute]) do
{:ok, url} when is_binary(url) ->
%URI{scheme: scheme, host: maybe_host} = URI.parse(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#

defmodule Edgehog.Astarte.Realm.Calculations.RealmManagementClient do
use Ash.Calculation
use Ash.Resource.Calculation

@impl true
def load(_query, _opts, _context) do
[:cluster]
[cluster: [:base_api_url]]
end

@impl true
Expand Down
18 changes: 10 additions & 8 deletions backend/lib/edgehog/astarte/realm/realm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

defmodule Edgehog.Astarte.Realm do
use Edgehog.MultitenantResource,
api: Edgehog.Astarte
domain: Edgehog.Astarte

alias Edgehog.Astarte.Realm
alias Edgehog.Validations
Expand All @@ -40,19 +40,20 @@ defmodule Edgehog.Astarte.Realm do

create :create do
primary? true

argument :cluster_id, :integer, allow_nil?: false

change manage_relationship(:cluster_id, :cluster, type: :append)
accept [:name, :private_key, :cluster_id]
end
end

attributes do
integer_primary_key :id

attribute :name, :string, allow_nil?: false
attribute :name, :string do
public? true
allow_nil? false
end

attribute :private_key, :string do
public? true
allow_nil? false
constraints trim?: false
end
Expand All @@ -62,13 +63,14 @@ defmodule Edgehog.Astarte.Realm do
end

relationships do
belongs_to :cluster, Edgehog.Astarte.Cluster
belongs_to :cluster, Edgehog.Astarte.Cluster do
allow_nil? false
end
end

calculations do
calculate :realm_management_client, :struct, Realm.Calculations.RealmManagementClient do
constraints instance_of: Astarte.Client.RealmManagement
private? true
filterable? false
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
#

defmodule Edgehog.Devices.Device.Calculations.AppEngineClient do
use Ash.Calculation
use Ash.Resource.Calculation

@impl true
def load(_query, _opts, _context) do
[realm: [:cluster]]
# TODO: `:cluster_id` should not be necessary but this currently doesn't work without it,
# remove it when this gets fixed upstream
[realm: [:name, :private_key, :cluster_id, cluster: [:base_api_url]]]
end

@impl true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#

defmodule Edgehog.Devices.Device.Calculations.AstarteInterfaceValue do
use Ash.Calculation
use Ash.Resource.Calculation

@impl true
def load(_query, _opts, _context) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#

defmodule Edgehog.Devices.Device.Calculations.Capabilities do
use Ash.Calculation
use Ash.Resource.Calculation

alias Edgehog.Capabilities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#

defmodule Edgehog.Devices.Device.Calculations.CellularConnection do
use Ash.Calculation
use Ash.Resource.Calculation

alias Edgehog.Devices.Device.Types.Modem

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#

defmodule Edgehog.Devices.Device.Calculations.DeviceStatus do
use Ash.Calculation
use Ash.Resource.Calculation

@device_status Application.compile_env(
:edgehog,
Expand Down
Loading

0 comments on commit 02e2644

Please sign in to comment.