Skip to content

Commit 3351e5b

Browse files
authored
Merge pull request #129 from vulkoingim/respect-aws_endpoint_url
Honor `AWS_ENDPOINT_URL_<SERVICE>` env vars in generated Erlang clients
2 parents cb3cdf4 + 7bc5fcd commit 3351e5b

6 files changed

Lines changed: 15 additions & 6 deletions

File tree

lib/aws_codegen.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ defmodule AWS.CodeGen do
2626
decode: nil,
2727
encode: nil,
2828
endpoint_prefix: nil,
29+
# Name of the AWS-canonical service-specific endpoint env var
30+
# (e.g. "AWS_ENDPOINT_URL_DYNAMODB"). Derived from `service_id`
31+
# per the AWS CLI/SDK convention: spaces -> "_", uppercased.
32+
# See https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-endpoints.html
33+
endpoint_url_env_var: nil,
2934
is_global: false,
3035
hostname: nil,
3136
json_version: nil,

lib/aws_codegen/post_service.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ defmodule AWS.CodeGen.PostService do
111111
signing_name: signing_name,
112112
signature_version: AWS.CodeGen.Util.get_signature_version(service),
113113
service_id: AWS.CodeGen.Util.get_service_id(service),
114+
endpoint_url_env_var: "AWS_ENDPOINT_URL_" <> spec.module_name |> String.upcase(),
114115
target_prefix: target_prefix(spec.api)
115116
}
116117
end

lib/aws_codegen/rest_service.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ defmodule AWS.CodeGen.RestService do
157157
signing_name: signing_name,
158158
signature_version: AWS.CodeGen.Util.get_signature_version(service),
159159
service_id: AWS.CodeGen.Util.get_service_id(service),
160+
endpoint_url_env_var: "AWS_ENDPOINT_URL_" <> spec.module_name |> String.upcase(),
160161
## TODO: metadata["targetPrefix"],
161162
target_prefix: nil,
162163
shapes: Shapes.collect_shapes(language, spec.api)

priv/post.erl.eex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ request(Client, Action, Input, Options) ->
7575
<% else %>do_request(Client, Action, Input0, Options) ->
7676
<% end %> Client1 = Client#{service => <<"<%= context.signing_name %>">><%= if context.is_global do %>,
7777
region => <<"<%= context.credential_scope %>">><% end %>},
78-
Host = build_host(<<"<%= context.endpoint_prefix %>">>, Client1),
79-
URL = build_url(Host, Client1),
78+
DefaultHost = build_host(<<"<%= context.endpoint_prefix %>">>, Client1),
79+
{URL, Host} = aws_util:apply_endpoint_url_override(build_url(DefaultHost, Client1), DefaultHost, <<"/">>, <<"<%= context.endpoint_url_env_var %>">>),
8080
Headers = [
8181
{<<"Host">>, Host},
8282
{<<"Content-Type">>, <<"<%= context.content_type %>">>}<%= if context.protocol == "json" do %>,

priv/rest.erl.eex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,11 @@ do_request(Client, Method, Path, Query, Headers0, Input, Options, SuccessStatusC
209209
Client1 = Client#{service => <<"<%= context.signing_name %>">><%= if context.is_global do %>,
210210
region => <<"<%= context.credential_scope %>">><% end %>},
211211
<%= if context.endpoint_prefix == "s3-control" do %>AccountId = proplists:get_value(<<"x-amz-account-id">>, Headers0),
212-
Host = build_host(AccountId, <<"<%= context.endpoint_prefix %>">>, Client1),<% else %><%= if AWS.CodeGen.RestService.Context.s3_context?(context) do %>Host = build_host(<<"<%= context.endpoint_prefix %>">>, Client1, Bucket),<%else %>Host = build_host(<<"<%= context.endpoint_prefix %>">>, Client1),<% end %><% end %>
213-
URL0 = build_url(Host, Path, Client1<%= if AWS.CodeGen.RestService.Context.s3_context?(context) do %>, Bucket<% end %>),
214-
URL = aws_request:add_query(URL0, Query),
212+
DefaultHost = build_host(AccountId, <<"<%= context.endpoint_prefix %>">>, Client1),<% else %><%= if AWS.CodeGen.RestService.Context.s3_context?(context) do %>DefaultHost = build_host(<<"<%= context.endpoint_prefix %>">>, Client1, Bucket),<%else %>DefaultHost = build_host(<<"<%= context.endpoint_prefix %>">>, Client1),<% end %><% end %>
213+
URL0 = build_url(DefaultHost, Path, Client1<%= if AWS.CodeGen.RestService.Context.s3_context?(context) do %>, Bucket<% end %>),
214+
PathBin = erlang:iolist_to_binary(Path),
215+
{URL1, Host} = aws_util:apply_endpoint_url_override(URL0, DefaultHost, PathBin, <<"<%= context.endpoint_url_env_var %>">>),
216+
URL = aws_request:add_query(URL1, Query),
215217
AdditionalHeaders1 = [ {<<"Host">>, Host}
216218
, {<<"Content-Type">>, <<"<%= context.content_type %>">>}
217219
],
@@ -332,7 +334,6 @@ build_host(EndpointPrefix, #{endpoint := Endpoint}) ->
332334
aws_util:binary_join([EndpointPrefix, Endpoint], <<".">>).<% else %>
333335
build_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
334336
aws_util:binary_join([EndpointPrefix, Region, Endpoint], <<".">>).<% end %><% end %><% end %><% end %>
335-
336337
<%= if AWS.CodeGen.RestService.Context.s3_context?(context) do %>build_url(Host0, Path0, Client, Bucket) ->
337338
Proto = aws_client:proto(Client),
338339
%% Mocks are notoriously bad with host-style requests, just skip it and use path-style for anything local

test/aws_codegen/rest_service_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ defmodule AWS.CodeGen.RestServiceTest do
5151
module_name: "AWS.CloudTrailData",
5252
protocol: "rest-json",
5353
service_id: "CloudTrail Data",
54+
endpoint_url_env_var: "AWS_ENDPOINT_URL_AWS.CLOUDTRAILDATA", # For Erlang this would be: AWS_ENDPOINT_URL_CLOUDTRAILDATA
5455
signature_version: "v4",
5556
signing_name: "cloudtrail-data",
5657
target_prefix: nil

0 commit comments

Comments
 (0)