Skip to content

Commit be342b0

Browse files
Merge pull request #64 from diffo-dev/63-update-to-refactored-ashneo4j
63 update to refactored ashneo4j
2 parents 7e6d5ff + a510f7c commit be342b0

49 files changed

Lines changed: 1182 additions & 252 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

config/config.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ config :spark,
3636
]
3737
]
3838

39+
config :ash, :custom_expressions, [Diffo.Unwrap.AshCustomExpression]
40+
3941
config :diffo, ash_domains: [Diffo.Provider]
4042
import_config "#{config_env()}.exs"

config/dev.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ level =
2323
end
2424

2525
config :logger, :console,
26-
level: level,
26+
# level: level,
27+
level: :debug,
2728
format: "$date $time [$level] $metadata$message\n"

config/test.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import Config
66

7-
config :logger, level: :warning
87
config :ash, disable_async?: true
98
config :ash, :missed_notifications, :ignore
109

@@ -16,8 +15,8 @@ config :bolty, Bolt,
1615
max_overflow: 3,
1716
prefix: :default,
1817
name: Bolt,
19-
log: true,
20-
log_hex: true
18+
log: false,
19+
log_hex: false
2120

2221
level =
2322
if System.get_env("DEBUG") do
@@ -26,6 +25,8 @@ level =
2625
:info
2726
end
2827

28+
config :logger, level: level
29+
2930
config :logger, :console,
3031
level: level,
3132
format: "$date $time [$level] $metadata$message\n"

documentation/how_to/use_diffo_provider_instance_extension.livemd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ We need to define each GPU instance, in this case defining the gpu Characteristi
535535
```elixir
536536
updates = [
537537
gpu: [family: :nvidia, model: "GeForce RTX5090", technology: :blackwell],
538-
cores: [first: 1, last: 680, free: 680, type: "tensor"]
538+
cores: [first: 1, last: 680, free: 680, assignable_type: "tensor"]
539539
]
540540

541541
gpu_1 = Compute.define_gpu!(gpu_1, %{characteristic_value_updates: updates})

lib/diffo/helpers/uuid.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ defmodule Diffo.Uuid do
102102
@doc """
103103
Returns trailing uuid4 if valid, or nil.
104104
## Examples
105-
iex> Diffo.Uuid.trailing_uuid4("serviceInventoryManagement/v4/service/accessEvc/d2566874-d5ee-400b-9983-10d63ec52f32")
105+
iex> Diffo.Uuid.trailing_uuid4("serviceInventoryManagement/v4/service/d2566874-d5ee-400b-9983-10d63ec52f32")
106106
"d2566874-d5ee-400b-9983-10d63ec52f32"
107107
108-
iex> Diffo.Uuid.trailing_uuid4("serviceInventoryManagement/v4/service/accessEvc/d2566874-d5ee-400b-9983")
108+
iex> Diffo.Uuid.trailing_uuid4("serviceInventoryManagement/v4/service/d2566874-d5ee-400b-9983")
109109
nil
110110
111111
iex> Diffo.Uuid.trailing_uuid4("d2566874-d5ee-400b-9983-10d63ec52f32")
@@ -136,7 +136,7 @@ defmodule Diffo.Uuid do
136136
iex> Diffo.Uuid.expect_uuid4("d2566874-d5ee-400b-9983-10d63ec52f32")
137137
nil
138138
139-
iex> Diffo.Uuid.expect_uuid4("serviceInventoryManagement/v4/service/accessEvc/d2566874-d5ee-400b-9983-10d63ec52f32")
139+
iex> Diffo.Uuid.expect_uuid4("serviceInventoryManagement/v4/service/d2566874-d5ee-400b-9983-10d63ec52f32")
140140
:uuid4
141141
142142
iex> Diffo.Uuid.expect_uuid4(nil)
@@ -154,10 +154,10 @@ defmodule Diffo.Uuid do
154154
@doc """
155155
Callback function for outstanding to expect trailing uuid4.
156156
## Examples
157-
iex> Diffo.Uuid.expect_trailing_uuid4("serviceInventoryManagement/v4/service/accessEvc/d2566874-d5ee-400b-9983-10d63ec52f32")
157+
iex> Diffo.Uuid.expect_trailing_uuid4("serviceInventoryManagement/v4/service/d2566874-d5ee-400b-9983-10d63ec52f32")
158158
nil
159159
160-
iex> Diffo.Uuid.expect_trailing_uuid4("serviceInventoryManagement/v4/service/accessEvc/d2566874-d5ee-400b-9983")
160+
iex> Diffo.Uuid.expect_trailing_uuid4("serviceInventoryManagement/v4/service/d2566874-d5ee-400b-9983")
161161
:trailing_uuid4
162162
163163
iex> Diffo.Uuid.expect_trailing_uuid4("d2566874-d5ee-400b-9983-10d63ec52f32")

lib/diffo/provider/assigner/assignable_value.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ defmodule Diffo.Provider.AssignableValue do
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct]
1212

1313
jason do
14-
pick [:first, :last, :free, :type, :algorithm]
14+
pick [:first, :last, :free, :assignable_type, :algorithm]
1515
compact true
16+
rename assignable_type: :type
1617
end
1718

1819
typed_struct do
@@ -31,7 +32,7 @@ defmodule Diffo.Provider.AssignableValue do
3132
default: 1,
3233
constraints: [min: 0]
3334

34-
field :type, :atom, description: "the type of the assignable thing"
35+
field :assignable_type, :string, description: "the type of the assignable thing"
3536

3637
field :algorithm, :atom,
3738
description: "the assignment algorithm",

lib/diffo/provider/assigner/assigner.ex

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Diffo.Provider.Assigner do
1010
"""
1111

1212
alias Diffo.Provider.AssignableValue
13+
alias Diffo.Type.Value
1314

1415
@doc """
1516
Assign a thing using the instance changeset assignment
@@ -53,7 +54,11 @@ defmodule Diffo.Provider.Assigner do
5354
defp relate_is_assigned(result, things, thing, value, assignee_id)
5455
when is_struct(result) and is_atom(things) and is_atom(thing) and is_integer(value) and
5556
is_bitstring(assignee_id) do
56-
case Diffo.Provider.create_characteristic(%{name: thing, value: value, type: :relationship}) do
57+
case Diffo.Provider.create_characteristic(%{
58+
name: thing,
59+
value: Value.primitive("integer", value),
60+
type: :relationship
61+
}) do
5762
{:ok, characteristic} ->
5863
case Diffo.Provider.create_relationship(%{
5964
type: :assignedTo,
@@ -62,7 +67,6 @@ defmodule Diffo.Provider.Assigner do
6267
characteristics: [characteristic.id]
6368
}) do
6469
{:ok, _relationship} ->
65-
# we haven't refreshed the result there will be a new forward_relationship and an updated things characteristic
6670
case decrement_free(result, things) do
6771
:ok ->
6872
{:ok, result}
@@ -91,7 +95,7 @@ defmodule Diffo.Provider.Assigner do
9195
} ->
9296
type == :assignedTo and target_id == assignee_id and
9397
Enum.any?(characteristics, fn %{name: name, value: v} ->
94-
name == thing and v == value
98+
name == thing and Diffo.Unwrap.unwrap(v) == value
9599
end)
96100
end)
97101

@@ -105,16 +109,13 @@ defmodule Diffo.Provider.Assigner do
105109
characteristic =
106110
Enum.find(relationship.characteristics, fn %{name: n} -> n == thing end)
107111

108-
# unrelate the relationship characterisitic
109112
relationship =
110113
Diffo.Provider.unrelate_relationship_characteristics!(relationship, %{
111114
characteristics: [characteristic.id]
112115
})
113116

114-
# delete the relationship characteristic
115117
Diffo.Provider.delete_characteristic(characteristic.id)
116118

117-
# delete the relationship
118119
case Diffo.Provider.delete_relationship(relationship.id) do
119120
:ok ->
120121
case increment_free(result, things) do
@@ -148,8 +149,8 @@ defmodule Diffo.Provider.Assigner do
148149
if characteristic do
149150
assignment =
150151
struct(Diffo.Provider.Assignment, %{
151-
id: characteristic.value,
152-
type: thing,
152+
id: Diffo.Unwrap.unwrap(characteristic.value),
153+
assignable_type: thing,
153154
assignee_id: target_id
154155
})
155156

@@ -168,9 +169,10 @@ defmodule Diffo.Provider.Assigner do
168169
defp next(instance, things, thing)
169170
when is_struct(instance) and is_atom(things) and is_atom(thing) do
170171
characteristic = Enum.find(instance.characteristics, fn %{name: name} -> name == things end)
171-
algorithm = Map.get(characteristic.value, :algorithm)
172+
assignable_value = Diffo.Unwrap.unwrap(characteristic.value)
173+
algorithm = Map.get(assignable_value, :algorithm)
172174

173-
case free = free(instance, thing, characteristic.value) do
175+
case free = free(instance, thing, assignable_value) do
174176
[] ->
175177
{:error, "all things are assigned"}
176178

@@ -191,7 +193,8 @@ defmodule Diffo.Provider.Assigner do
191193
defp assignable?(instance, things, thing, value)
192194
when is_struct(instance) and is_atom(things) and is_atom(thing) and is_integer(value) do
193195
characteristic = Enum.find(instance.characteristics, fn %{name: name} -> name == things end)
194-
free = free(instance, thing, characteristic.value)
196+
assignable_value = Diffo.Unwrap.unwrap(characteristic.value)
197+
free = free(instance, thing, assignable_value)
195198

196199
value in free
197200
end
@@ -200,10 +203,15 @@ defmodule Diffo.Provider.Assigner do
200203
characteristic =
201204
Enum.find(instance.characteristics, fn %{name: name} -> name == things end)
202205

203-
{_free, assignable_value} =
204-
Map.get_and_update(characteristic.value, :free, fn free -> {free - 1, free - 1} end)
206+
assignable_value = Diffo.Unwrap.unwrap(characteristic.value)
205207

206-
case Diffo.Provider.update_characteristic(characteristic, %{value: assignable_value}) do
208+
{_free, updated} =
209+
Map.get_and_update(assignable_value, :free, fn free -> {free - 1, free - 1} end)
210+
211+
{:ok, new_struct} = Ash.Type.cast_input(AssignableValue, Map.from_struct(updated), AssignableValue.subtype_constraints())
212+
new_value = Value.dynamic(AssignableValue, new_struct)
213+
214+
case Diffo.Provider.update_characteristic(characteristic, %{value: new_value}) do
207215
{:ok, _characteristic} ->
208216
:ok
209217

@@ -216,10 +224,15 @@ defmodule Diffo.Provider.Assigner do
216224
characteristic =
217225
Enum.find(instance.characteristics, fn %{name: name} -> name == things end)
218226

219-
{_free, assignable_value} =
220-
Map.get_and_update(characteristic.value, :free, fn free -> {free + 1, free + 1} end)
227+
assignable_value = Diffo.Unwrap.unwrap(characteristic.value)
228+
229+
{_free, updated} =
230+
Map.get_and_update(assignable_value, :free, fn free -> {free + 1, free + 1} end)
231+
232+
{:ok, new_struct} = Ash.Type.cast_input(AssignableValue, Map.from_struct(updated), AssignableValue.subtype_constraints())
233+
new_value = Value.dynamic(AssignableValue, new_struct)
221234

222-
case Diffo.Provider.update_characteristic(characteristic, %{value: assignable_value}) do
235+
case Diffo.Provider.update_characteristic(characteristic, %{value: new_value}) do
223236
{:ok, _characteristic} ->
224237
:ok
225238

lib/diffo/provider/assigner/assignment.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ defmodule Diffo.Provider.Assignment do
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct]
1212

1313
jason do
14-
pick [:id, :assignee_id, :operation]
14+
pick [:id, :assignee_id, :assignable_type, :operation]
1515
compact true
16+
rename assignable_type: :type
1617
end
1718

1819
typed_struct do
1920
field :id, :integer,
2021
constraints: [min: 0],
2122
description: "the id of the assigned thing"
2223

23-
field :type, :atom, description: "the type of the assigned thing"
24+
field :assignable_type, :string, description: "the type of the assigned thing"
2425

2526
field :assignee_id, :uuid, description: "the id of the assignee Ash resource"
2627

lib/diffo/provider/components/base_instance.ex

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -504,18 +504,7 @@ defmodule Diffo.Provider.BaseInstance do
504504
end
505505

506506
calculations do
507-
calculate :href,
508-
:string,
509-
expr(
510-
type <>
511-
"InventoryManagement/v" <>
512-
specification.tmf_version <>
513-
"/" <>
514-
type <>
515-
"/" <>
516-
specification.name <>
517-
"/" <> id
518-
) do
507+
calculate :href, :string, Diffo.Provider.Calculations.InstanceHref do
519508
description "the inventory href of the service or resource instance"
520509
end
521510
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2025 diffo contributors <https://github.com/diffo-dev/diffo/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule Diffo.Provider.Calculations.InstanceHref do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
InstanceHref - Ash Resource Calculation for generating instance href
10+
11+
"""
12+
use Ash.Resource.Calculation
13+
14+
@impl true
15+
def load(_query, _opts, _context), do: [:specification]
16+
17+
@impl true
18+
def calculate(records, _opts, _context) do
19+
Enum.map(records, fn record ->
20+
case record.specification do
21+
%{tmf_version: tmf_version, type: :serviceSpecification} ->
22+
"serviceInventoryManagement/v#{tmf_version}/service/#{record.id}"
23+
24+
%{tmf_version: tmf_version, type: :resourceSpecification} ->
25+
"resourceInventoryManagement/v#{tmf_version}/resource/#{record.id}"
26+
27+
_ ->
28+
nil
29+
end
30+
end)
31+
end
32+
end

0 commit comments

Comments
 (0)