Skip to content

Commit 607ed7f

Browse files
committed
Use S3Config in cloud tests
1 parent f281f44 commit 607ed7f

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

lib/explorer/fss.ex

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,42 @@ defmodule Explorer.FSS do
55
# Public APIs accept URIs like "s3://bucket/key", and this module
66
# parses them into triplets {:backend, path, config} for internal use.
77

8+
defmodule S3Config do
9+
@moduledoc false
10+
@derive {Inspect, only: [:bucket, :region, :endpoint]}
11+
defstruct [
12+
:access_key_id,
13+
:secret_access_key,
14+
:region,
15+
:endpoint,
16+
:bucket,
17+
:token
18+
]
19+
20+
@type t :: %__MODULE__{
21+
access_key_id: String.t(),
22+
secret_access_key: String.t(),
23+
region: String.t(),
24+
endpoint: String.t(),
25+
bucket: String.t() | nil,
26+
token: String.t() | nil
27+
}
28+
end
29+
830
@doc """
931
Parses an S3 URL in the format `s3://bucket/key` and returns a triplet.
1032
1133
## Options
1234
13-
* `:config` - A map with S3 configuration keys, or `nil` to read from env vars:
35+
* `:config` - A map/keyword list with S3 configuration keys, or `nil` to read from env vars:
1436
- `AWS_ACCESS_KEY_ID`
1537
- `AWS_SECRET_ACCESS_KEY`
1638
- `AWS_REGION` or `AWS_DEFAULT_REGION`
1739
- `AWS_SESSION_TOKEN`
1840
1941
## Returns
2042
21-
`{:ok, {:s3, key, config}}` where config is a map with:
43+
`{:ok, {:s3, key, %S3Config{}}}` where config is a struct with:
2244
- `:access_key_id`
2345
- `:secret_access_key`
2446
- `:endpoint`
@@ -27,7 +49,7 @@ defmodule Explorer.FSS do
2749
- `:token` (optional)
2850
"""
2951
@spec parse_s3(String.t(), Keyword.t()) ::
30-
{:ok, {:s3, String.t(), map()}} | {:error, Exception.t()}
52+
{:ok, {:s3, String.t(), S3Config.t()}} | {:error, Exception.t()}
3153
def parse_s3(url, opts \\ []) do
3254
opts = Keyword.validate!(opts, config: nil)
3355

@@ -84,12 +106,14 @@ defmodule Explorer.FSS do
84106

85107
defp normalize_s3_config!(nil), do: s3_config_from_env()
86108

109+
defp normalize_s3_config!(%S3Config{} = config), do: config
110+
87111
defp normalize_s3_config!(config) when is_map(config) do
88-
Map.merge(s3_config_from_env(), config)
112+
struct!(s3_config_from_env(), config)
89113
end
90114

91115
defp normalize_s3_config!(config) when is_list(config) do
92-
Map.merge(s3_config_from_env(), Map.new(config))
116+
struct!(s3_config_from_env(), config)
93117
end
94118

95119
defp normalize_s3_config!(other) do
@@ -105,7 +129,7 @@ defmodule Explorer.FSS do
105129
end
106130

107131
defp s3_config_from_env do
108-
%{
132+
%S3Config{
109133
access_key_id: System.get_env("AWS_ACCESS_KEY_ID"),
110134
secret_access_key: System.get_env("AWS_SECRET_ACCESS_KEY"),
111135
region: System.get_env("AWS_REGION", System.get_env("AWS_DEFAULT_REGION")),

native/explorer/src/datatypes.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,12 @@ impl TryFrom<ExParquetCompression> for ParquetCompression {
803803
}
804804

805805
// =========================
806-
// ====== FSS Structs ======
806+
// ====== S3 Entry ======
807807
// =========================
808808

809+
// Represents Explorer.FSS.S3Config struct from Elixir
809810
#[derive(NifStruct, Clone, Debug)]
810-
#[module = "FSS.S3.Config"]
811+
#[module = "Explorer.FSS.S3Config"]
811812
pub struct ExS3Config {
812813
pub access_key_id: String,
813814
pub secret_access_key: String,
@@ -817,13 +818,31 @@ pub struct ExS3Config {
817818
pub token: Option<String>,
818819
}
819820

820-
#[derive(NifStruct, Clone, Debug)]
821-
#[module = "FSS.S3.Entry"]
821+
// Represents {:s3, key, config} triplet from Elixir
822+
#[derive(Clone, Debug)]
822823
pub struct ExS3Entry {
823824
pub key: String,
824825
pub config: ExS3Config,
825826
}
826827

828+
impl<'a> rustler::Decoder<'a> for ExS3Entry {
829+
fn decode(term: rustler::Term<'a>) -> rustler::NifResult<Self> {
830+
use rustler::*;
831+
832+
// Expecting a tuple {:s3, key, %S3Config{}}
833+
let tuple: (Atom, String, ExS3Config) = term.decode()?;
834+
835+
if tuple.0 != atoms::s3() {
836+
return Err(rustler::Error::BadArg);
837+
}
838+
839+
Ok(ExS3Entry {
840+
key: tuple.1,
841+
config: tuple.2,
842+
})
843+
}
844+
}
845+
827846
impl fmt::Display for ExS3Entry {
828847
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
829848
if let Some(bucket_name) = &self.config.bucket {

native/explorer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ mod atoms {
6464
coef,
6565
exp,
6666
sign,
67+
s3,
6768
}
6869
}
6970

0 commit comments

Comments
 (0)