Skip to content

Commit 26750cf

Browse files
committed
load registry settings from appcfg
1 parent c34deea commit 26750cf

6 files changed

Lines changed: 110 additions & 13 deletions

File tree

lib/oci/pagination.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule OCI.Pagination do
2+
@moduledoc """
3+
Provides pagination functionality for OCI registry operations, particularly for listing tags.
4+
5+
This module handles the pagination parameters and results for operations that return
6+
large sets of data, such as listing repository tags. It supports the OCI Distribution
7+
Specification's pagination model using `n` (number of results) and `last` (last seen value)
8+
parameters.
9+
"""
10+
use TypedStruct
11+
12+
typedstruct do
13+
field :n, pos_integer(), enforce: false
14+
field :last, String.t(), enforce: false
15+
end
16+
end

lib/oci/plug/handler.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule OCI.Plug.Handler do
99
"""
1010
import Plug.Conn
1111
alias OCI.Registry
12-
alias OCI.Registry.Pagination
12+
alias OCI.Pagination
1313

1414
def handle(%{halted: true} = conn), do: conn
1515

lib/oci/registry.ex

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ defmodule OCI.Registry do
1919
field :repo_name_pattern, Regex.t(), enforce: false, default: @repo_name_pattern
2020
end
2121

22-
typedstruct module: Pagination do
23-
field :n, pos_integer(), enforce: false
24-
field :last, String.t(), enforce: false
25-
end
26-
2722
@doc """
2823
Returns the API version string used in OCI registry paths.
2924
@@ -38,9 +33,7 @@ defmodule OCI.Registry do
3833
@spec api_version() :: String.t()
3934
def api_version, do: "v2"
4035

41-
def load_from_env() do
42-
oci_cfg = Application.get_all_env(:oci)
43-
36+
def load_from_keywordlist(oci_cfg) do
4437
auth_cfg = Keyword.fetch!(oci_cfg, :auth)
4538
{:ok, auth} = auth_cfg.adapter.init(auth_cfg.config)
4639

@@ -53,6 +46,12 @@ defmodule OCI.Registry do
5346
registry
5447
end
5548

49+
def load_from_env() do
50+
:oci
51+
|> Application.get_all_env()
52+
|> load_from_keywordlist
53+
end
54+
5655
def authenticate(%{auth: auth}, authorization) do
5756
adapter(auth).authenticate(auth, authorization)
5857
end
@@ -83,7 +82,15 @@ defmodule OCI.Registry do
8382
def init(config) do
8483
storage = Keyword.fetch!(config, :storage)
8584
auth = Keyword.fetch!(config, :auth)
86-
{:ok, %__MODULE__{storage: storage, auth: auth}}
85+
reg = %__MODULE__{storage: storage, auth: auth}
86+
87+
# Update struct with any additional configuration options
88+
reg =
89+
Enum.reduce(config, reg, fn
90+
{key, value}, acc -> Map.put(acc, key, value)
91+
end)
92+
93+
{:ok, reg}
8794
end
8895

8996
def repo_exists?(%{storage: storage}, repo, ctx) do

lib/oci/storage/adapter.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ defmodule OCI.Storage.Adapter do
177177
@callback list_tags(
178178
storage :: t(),
179179
repo :: String.t(),
180-
pagination :: OCI.Registry.Pagination.t(),
180+
pagination :: OCI.Pagination.t(),
181181
ctx :: OCI.Context.t()
182182
) ::
183183
{:ok, tags :: [String.t()]}

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule OCI.MixProject do
44
def project do
55
[
66
app: :oci,
7-
version: "0.0.5",
7+
version: "0.0.6",
88
elixir: "~> 1.15",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
start_permanent: Mix.env() == :prod,

test/registry_test.exs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule OCI.RegistryTest do
22
use ExUnit.Case, async: true
3-
doctest OCI.Registry
3+
# doctest OCI.Registry
44

55
defp tmp_storage() do
66
OCI.Storage.Local.init(%{path: "./tmp/"})
@@ -17,6 +17,80 @@ defmodule OCI.RegistryTest do
1717
registry
1818
end
1919

20+
defmodule NoAuth do
21+
defstruct [:none]
22+
23+
def init(map) do
24+
{:ok, struct(__MODULE__, map)}
25+
end
26+
end
27+
28+
defmodule VoidStorage do
29+
defstruct [:where]
30+
def init(map), do: {:ok, struct(__MODULE__, map)}
31+
end
32+
33+
describe "load_from_keywordlist/1" do
34+
test "defaults if not present" do
35+
config = [
36+
auth: %{
37+
adapter: NoAuth,
38+
config: %{none: "nope"}
39+
},
40+
storage: %{
41+
adapter: VoidStorage,
42+
config: %{where: "nowhere"}
43+
}
44+
]
45+
46+
registry = OCI.Registry.load_from_keywordlist(config)
47+
48+
assert %OCI.Registry{
49+
enable_blob_deletion: true,
50+
enable_manifest_deletion: true,
51+
max_blob_upload_chunk_size: 10_485_760,
52+
max_manifest_size: 4_194_304,
53+
realm: "Registry",
54+
repo_name_pattern:
55+
~r/^([a-z0-9]+(?:[._-][a-z0-9]+)*)(\/[a-z0-9]+(?:[._-][a-z0-9]+)*)*$/,
56+
auth: %OCI.RegistryTest.NoAuth{none: "nope"},
57+
storage: %OCI.RegistryTest.VoidStorage{where: "nowhere"}
58+
} = registry
59+
end
60+
61+
test "should include the settings" do
62+
config = [
63+
realm: "My Registry",
64+
max_manifest_size: 1,
65+
max_blob_upload_chunk_size: 2,
66+
enable_blob_deletion: false,
67+
enable_manifest_deletion: false,
68+
repo_name_pattern: ~r/^[a-z0-9]+\/[a-z0-9]+$/,
69+
auth: %{
70+
adapter: NoAuth,
71+
config: %{none: "nope"}
72+
},
73+
storage: %{
74+
adapter: VoidStorage,
75+
config: %{where: "nowhere"}
76+
}
77+
]
78+
79+
registry = OCI.Registry.load_from_keywordlist(config)
80+
81+
assert %OCI.Registry{
82+
realm: "My Registry",
83+
enable_blob_deletion: false,
84+
enable_manifest_deletion: false,
85+
repo_name_pattern: ~r/^[a-z0-9]+\/[a-z0-9]+$/,
86+
max_manifest_size: 1,
87+
max_blob_upload_chunk_size: 2,
88+
auth: %OCI.RegistryTest.NoAuth{none: "nope"},
89+
storage: %OCI.RegistryTest.VoidStorage{where: "nowhere"}
90+
} = registry
91+
end
92+
end
93+
2094
describe "authenticate" do
2195
test "returns {:ok, ctx} when authentication is successful" do
2296
registry = registry_with_user("myuser", "mypass")

0 commit comments

Comments
 (0)