Current section
Files
Jump to
Current section
Files
lib/diffo/provider/components/base_relationship.ex
# SPDX-FileCopyrightText: 2025 diffo contributors <https://github.com/diffo-dev/diffo/graphs.contributors>
#
# SPDX-License-Identifier: MIT
defmodule Diffo.Provider.BaseRelationship do
@moduledoc """
Ash Resource Fragment which is the shared foundation for TMF Relationship resources.
Provides the common attributes, relationships, validations, and actions shared
between `Diffo.Provider.Relationship` (TMF service/resource relationships) and
`Diffo.Provider.AssignedToRelationship` (pool assignment relationships).
## Common attributes
- `id` — uuid4 primary key
- `type` — relationship type atom
- `target_href` — denormalised target href (set by the `DetailRelationship` change)
- `target_type` — denormalised target type (`:service` or `:resource`)
- `created_at`, `updated_at` — timestamps
## Common Ash relationships
- `belongs_to :source, Diffo.Provider.Instance`
- `belongs_to :target, Diffo.Provider.Instance`
"""
use Spark.Dsl.Fragment,
of: Ash.Resource,
otp_app: :diffo,
domain: Diffo.Provider,
data_layer: AshNeo4j.DataLayer,
extensions: [AshJason.Resource]
attributes do
uuid_primary_key :id do
description "a uuid4, unique to this relationship, generated by default"
public? true
end
attribute :type, :atom do
description "the type of the relationship from the source to the target"
allow_nil? false
public? true
end
attribute :target_href, :string do
description "the target href, denormalised from the target instance"
allow_nil? true
writable? false
public? true
end
attribute :target_type, :atom do
description "the target type, denormalised from the target instance"
allow_nil? true
writable? false
public? true
end
create_timestamp :created_at
update_timestamp :updated_at
end
relationships do
belongs_to :source, Diffo.Provider.Instance do
description "the source instance which originates this relationship"
allow_nil? false
public? true
end
belongs_to :target, Diffo.Provider.Instance do
description "the target instance which is the destination of this relationship"
allow_nil? false
public? true
end
end
validations do
validate {Diffo.Validations.IsUuid4OrNil, attribute: :source_id}, on: :create
validate {Diffo.Validations.IsUuid4OrNil, attribute: :target_id}, on: :create
end
actions do
defaults [:read, :destroy]
end
end