|
| 1 | +# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +defmodule DiffoExample.Access.Cable do |
| 6 | + @moduledoc """ |
| 7 | + Diffo - TMF Service and Resource Management with a difference |
| 8 | +
|
| 9 | + Cable - Cable Resource Instance |
| 10 | + """ |
| 11 | + |
| 12 | + alias Diffo.Provider.BaseInstance |
| 13 | + alias Diffo.Provider.Instance.Specification |
| 14 | + alias Diffo.Provider.Instance.Relationship |
| 15 | + alias Diffo.Provider.Instance.Feature |
| 16 | + alias Diffo.Provider.Instance.Characteristic |
| 17 | + alias Diffo.Provider.Instance.Place |
| 18 | + alias Diffo.Provider.Instance.Party |
| 19 | + alias Diffo.Provider.Assigner |
| 20 | + alias Diffo.Provider.Assignment |
| 21 | + |
| 22 | + alias DiffoExample.Access |
| 23 | + |
| 24 | + use Ash.Resource, |
| 25 | + fragments: [BaseInstance], |
| 26 | + domain: Access |
| 27 | + |
| 28 | + resource do |
| 29 | + description "An Ash Resource representing a Cable" |
| 30 | + plural_name :Cables |
| 31 | + end |
| 32 | + |
| 33 | + specification do |
| 34 | + id "ce0a567a-6abb-4862-9e33-851fd79fa595" |
| 35 | + name "cable" |
| 36 | + type :resourceSpecification |
| 37 | + description "A Cable Resource Instance" |
| 38 | + category "Network Resource" |
| 39 | + end |
| 40 | + |
| 41 | + characteristics do |
| 42 | + characteristic :cable, DiffoExample.Access.CableValue |
| 43 | + characteristic :pairs, Diffo.Provider.AssignableValue |
| 44 | + end |
| 45 | + |
| 46 | + actions do |
| 47 | + create :build do |
| 48 | + description "creates a new Cable resource instance for build" |
| 49 | + accept [:id, :name, :type, :which] |
| 50 | + argument :specified_by, :uuid, public?: false |
| 51 | + argument :relationships, {:array, :struct} |
| 52 | + argument :features, {:array, :uuid}, public?: false |
| 53 | + argument :characteristics, {:array, :uuid}, public?: false |
| 54 | + argument :places, {:array, :struct} |
| 55 | + argument :parties, {:array, :struct} |
| 56 | + |
| 57 | + change set_attribute(:type, :resource) |
| 58 | + |
| 59 | + change before_action(fn changeset, _context -> |
| 60 | + changeset |
| 61 | + |> Specification.set_specified_by_argument() |
| 62 | + |> Feature.set_features_argument() |
| 63 | + |> Characteristic.set_characteristics_argument() |
| 64 | + end) |
| 65 | + |
| 66 | + change after_action(fn changeset, result, _context -> |
| 67 | + with {:ok, with_specification} <- Specification.relate_instance(result, changeset), |
| 68 | + {:ok, with_relationships} <- |
| 69 | + Relationship.relate_instance(with_specification, changeset), |
| 70 | + {:ok, with_features} <- |
| 71 | + Feature.relate_instance(with_relationships, changeset), |
| 72 | + {:ok, with_characteristics} <- |
| 73 | + Characteristic.relate_instance(with_features, changeset), |
| 74 | + {:ok, with_places} <- Place.relate_instance(with_characteristics, changeset), |
| 75 | + {:ok, _with_parties} <- Party.relate_instance(with_places, changeset), |
| 76 | + {:ok, cable} <- Access.get_cable_by_id(result.id), |
| 77 | + do: {:ok, cable} |
| 78 | + end) |
| 79 | + |
| 80 | + change load [:href] |
| 81 | + upsert? false |
| 82 | + end |
| 83 | + |
| 84 | + update :define do |
| 85 | + description "defines the cable" |
| 86 | + argument :characteristic_value_updates, {:array, :term} |
| 87 | + |
| 88 | + change after_action(fn changeset, result, _context -> |
| 89 | + with {:ok, _result} <- Characteristic.update_values(result, changeset), |
| 90 | + {:ok, cable} <- Access.get_cable_by_id(result.id), |
| 91 | + do: {:ok, cable} |
| 92 | + end) |
| 93 | + end |
| 94 | + |
| 95 | + update :relate do |
| 96 | + description "relates the cable with other instances" |
| 97 | + argument :relationships, {:array, :struct} |
| 98 | + |
| 99 | + change after_action(fn changeset, result, _context -> |
| 100 | + with {:ok, _cable} <- Relationship.relate_instance(result, changeset), |
| 101 | + {:ok, cable} <- Access.get_cable_by_id(result.id), |
| 102 | + do: {:ok, cable} |
| 103 | + end) |
| 104 | + end |
| 105 | + |
| 106 | + update :assign_pair do |
| 107 | + description "relates the cable with an instance by assigning a pair" |
| 108 | + argument :assignment, :struct, constraints: [instance_of: Assignment] |
| 109 | + |
| 110 | + change after_action(fn changeset, result, _context -> |
| 111 | + with {:ok, _cable} <- Assigner.assign(result, changeset, :pairs, :pair), |
| 112 | + {:ok, cable} <- Access.get_cable_by_id(result.id), |
| 113 | + do: {:ok, cable} |
| 114 | + end) |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments