Skip to content

Commit bfd11ed

Browse files
authored
Merge pull request #47 from aws-beam/25-missing-services
[#25] Generate all missing services
2 parents 781c487 + 0dfa7a8 commit bfd11ed

File tree

8 files changed

+215
-312
lines changed

8 files changed

+215
-312
lines changed

lib/aws_codegen.ex

Lines changed: 71 additions & 257 deletions
Large diffs are not rendered by default.

lib/aws_codegen/post_service.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ defmodule AWS.CodeGen.PostService do
5757
that can be used to generate code for an AWS service. `language` must be
5858
`:elixir` or `:erlang`.
5959
"""
60-
def load_context(language, module_name, endpoints_spec, api_spec, doc_spec, _options) do
60+
def load_context(language, module_name, endpoints_spec, api_spec, doc_spec) do
6161
actions = collect_actions(language, api_spec, doc_spec)
6262
endpoint_prefix = api_spec["metadata"]["endpointPrefix"]
6363
endpoint_info = endpoints_spec["services"][endpoint_prefix]
64-
is_global = not Map.get(endpoint_info, "isRegionalized", true)
64+
is_global = not is_nil(endpoint_info) and not Map.get(endpoint_info, "isRegionalized", true)
6565
credential_scope = if is_global do
6666
endpoint_info["endpoints"]["aws-global"]["credentialScope"]["region"]
6767
else

lib/aws_codegen/rest_service.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ defmodule AWS.CodeGen.RestService do
1313
is_global: false,
1414
json_version: nil,
1515
module_name: nil,
16-
options: [],
1716
protocol: nil,
1817
signing_name: nil,
1918
target_prefix: nil
@@ -107,12 +106,12 @@ defmodule AWS.CodeGen.RestService do
107106
that can be used to generate code for an AWS service. `language` must be
108107
`:elixir` or `:erlang`.
109108
"""
110-
def load_context(language, module_name, endpoints_spec, api_spec, doc_spec, options) do
109+
def load_context(language, module_name, endpoints_spec, api_spec, doc_spec) do
111110
actions = collect_actions(language, api_spec, doc_spec)
112111
protocol = api_spec["metadata"]["protocol"]
113112
endpoint_prefix = api_spec["metadata"]["endpointPrefix"]
114113
endpoint_info = endpoints_spec["services"][endpoint_prefix]
115-
is_global = not Map.get(endpoint_info, "isRegionalized", true)
114+
is_global = not is_nil(endpoint_info) and not Map.get(endpoint_info, "isRegionalized", true)
116115
credential_scope = if is_global do
117116
endpoint_info["endpoints"]["aws-global"]["credentialScope"]["region"]
118117
else
@@ -132,7 +131,6 @@ defmodule AWS.CodeGen.RestService do
132131
is_global: is_global,
133132
json_version: api_spec["metadata"]["jsonVersion"],
134133
module_name: module_name,
135-
options: options,
136134
protocol: api_spec["metadata"]["protocol"],
137135
signing_name: signing_name,
138136
target_prefix: api_spec["metadata"]["targetPrefix"]}

lib/aws_codegen/spec.ex

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
defmodule AWS.CodeGen.Spec do
2+
@moduledoc """
3+
Parse and process the specfication for an AWS service.
4+
"""
5+
defstruct protocol: nil,
6+
module_name: nil,
7+
filename: nil,
8+
api: nil,
9+
doc: nil
10+
11+
def parse(path, language) do
12+
api = path |> Path.join("api-2.json") |> parse_json
13+
protocol = api["metadata"]["protocol"]
14+
|> String.replace("-", "_")
15+
|> String.to_atom
16+
module_name = module_name(api, language)
17+
filename = filename(module_name, language)
18+
%AWS.CodeGen.Spec{
19+
protocol: protocol,
20+
module_name: module_name,
21+
filename: filename,
22+
api: path |> Path.join("api-2.json") |> parse_json,
23+
doc: path |> Path.join("docs-2.json") |> parse_json
24+
}
25+
end
26+
27+
def parse_json(path) do
28+
if File.exists?(path) do
29+
path |> File.read! |> Poison.Parser.parse!(%{})
30+
end
31+
end
32+
33+
defp module_name(api, language) do
34+
service_name = case api["metadata"]["serviceId"] do
35+
nil -> api["metadata"]["endpointPrefix"]
36+
service_id -> service_id
37+
end
38+
|> String.replace("-sync", "Sync")
39+
|> String.replace("Route 53", "Route53")
40+
|> String.replace(~r/ Service$/, "")
41+
|> String.replace("dynamodb", "DynamoDB")
42+
|> String.replace("api.pricing", "API.Pricing")
43+
|> String.replace("entitlement.marketplace", "Entitlement.Marketplace")
44+
case language do
45+
:elixir ->
46+
service_name = service_name
47+
|> String.replace(" connections", " Connections")
48+
|> String.replace(" ", "")
49+
|> AWS.CodeGen.Name.upcase_first
50+
"AWS.#{service_name}"
51+
:erlang ->
52+
service_name = service_name
53+
|> String.replace(" ", "_")
54+
|> String.replace(".", "_")
55+
|> String.downcase
56+
|> AWS.CodeGen.Name.to_snake_case
57+
"aws_#{service_name}"
58+
end
59+
end
60+
61+
defp filename(module_name, :elixir) do
62+
module_name = module_name
63+
|> String.replace("AWS.", "")
64+
|> String.replace("SMS", "Sms")
65+
|> String.replace("WAF", "Waf")
66+
|> String.replace("API", "Api")
67+
|> String.replace("SES", "Ses")
68+
|> String.replace("HSM", "Hsm")
69+
|> String.replace("EC2", "Ec2")
70+
|> String.replace("DynamoDB", "Dynamodb")
71+
|> String.replace("ElastiCache", "Elasticache")
72+
|> String.replace("FSx", "Fsx")
73+
|> String.replace("IoT", "Iot")
74+
|> String.replace("MTurk", "Mturk")
75+
|> String.replace("QLDB", "Qldb")
76+
|> String.replace("DB", "Db")
77+
|> String.replace("RDS", "Rds")
78+
|> String.replace("A2I", "A2i")
79+
|> String.replace("XRay", "Xray")
80+
|> String.replace(".", "")
81+
name = if module_name == String.upcase(module_name) do
82+
String.downcase(module_name)
83+
else
84+
AWS.CodeGen.Name.to_snake_case(module_name)
85+
end
86+
"#{name}.ex"
87+
end
88+
defp filename(module_name, :erlang) do
89+
"#{module_name}.erl"
90+
end
91+
end

priv/post.erl.eex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
request(Client, Action, Input0, Options) ->
3434
Client1 = Client#{service => <<"<%= context.signing_name %>">><%= if context.is_global do %>,
3535
region => <<"<%= context.credential_scope %>">><% end %>},
36-
Host = get_host(<<"<%= context.endpoint_prefix %>">>, Client1),
37-
URL = get_url(Host, Client1),
36+
Host = build_host(<<"<%= context.endpoint_prefix %>">>, Client1),
37+
URL = build_url(Host, Client1),
3838
Headers = [
3939
{<<"Host">>, Host},
4040
{<<"Content-Type">>, <<"<%= context.content_type %>">>}<%= if context.protocol == "json" do %>,
@@ -67,15 +67,15 @@ handle_response({ok, StatusCode, ResponseHeaders, Client}) ->
6767
handle_response({error, Reason}) ->
6868
{error, Reason}.
6969

70-
get_host(_EndpointPrefix, #{region := <<"local">>}) ->
70+
build_host(_EndpointPrefix, #{region := <<"local">>}) ->
7171
<<"localhost">>;<%= if context.is_global do %>
72-
get_host(EndpointPrefix, #{endpoint := Endpoint}) ->
72+
build_host(EndpointPrefix, #{endpoint := Endpoint}) ->
7373
aws_util:binary_join([EndpointPrefix, Endpoint], <<".">>).
7474
<% else %>
75-
get_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
75+
build_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
7676
aws_util:binary_join([EndpointPrefix, Region, Endpoint], <<".">>).
7777
<% end %>
78-
get_url(Host, Client) ->
78+
build_url(Host, Client) ->
7979
Proto = maps:get(proto, Client),
8080
Port = maps:get(port, Client),
8181
aws_util:binary_join([Proto, <<"://">>, Host, <<":">>, Port, <<"/">>], <<"">>).

priv/post.ex.eex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ defmodule <%= context.module_name %> do
2020
defp request(client, action, input, options) do
2121
client = %{client | service: "<%= context.signing_name %>"<%= if context.is_global do %>,
2222
region: "<%= context.credential_scope %>"<% end %>}
23-
host = get_host("<%= context.endpoint_prefix %>", client)
24-
url = get_url(host, client)
23+
host = build_host("<%= context.endpoint_prefix %>", client)
24+
url = build_url(host, client)
2525

2626
headers = [
2727
{"Host", host},
@@ -49,18 +49,18 @@ defmodule <%= context.module_name %> do
4949
end
5050
end
5151

52-
defp get_host(_endpoint_prefix, %{region: "local"}) do
52+
defp build_host(_endpoint_prefix, %{region: "local"}) do
5353
"localhost"
5454
end<%= if context.is_global do %>
55-
defp get_host(endpoint_prefix, %{endpoint: endpoint}) do
55+
defp build_host(endpoint_prefix, %{endpoint: endpoint}) do
5656
"#{endpoint_prefix}.#{endpoint}"
5757
end
5858
<% else %>
59-
defp get_host(endpoint_prefix, %{region: region, endpoint: endpoint}) do
59+
defp build_host(endpoint_prefix, %{region: region, endpoint: endpoint}) do
6060
"#{endpoint_prefix}.#{region}.#{endpoint}"
6161
end
6262
<% end %>
63-
defp get_url(host, %{:proto => proto, :port => port}) do
63+
defp build_url(host, %{:proto => proto, :port => port}) do
6464
"#{proto}://#{host}:#{port}/"
6565
end
6666
end

priv/rest.erl.eex

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
<% else %>
3131
Headers = [],
3232
<% end %><%= if length(action.query_parameters) > 0 do %>
33-
Query0 =
33+
Query0_ =
3434
[<%= for parameter <- Enum.drop(action.query_parameters, -1) do %>
3535
{<<"<%= parameter.location_name %>">>, <%= parameter.code_name %>},<% end %><%= for parameter <- Enum.slice action.query_parameters, -1..-1 do %>
3636
{<<"<%= parameter.location_name %>">>, <%= parameter.code_name %>}
3737
<% end %>],
38-
Query = [H || {_, V} = H <- Query0, V =/= undefined],
38+
Query_ = [H || {_, V} = H <- Query0_, V =/= undefined],
3939
<% else %>
40-
Query = [],
40+
Query_ = [],
4141
<% end %><%= if length(action.response_header_parameters) > 0 do %>
42-
case request(Client, get, Path, Query, Headers, undefined, Options, SuccessStatusCode) of
42+
case request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode) of
4343
{ok, Body0, {_, ResponseHeaders, _} = Response} ->
4444
ResponseHeadersParams =
4545
[<%= for parameter <- Enum.drop action.response_header_parameters, -1 do %>
@@ -57,7 +57,7 @@
5757
Result ->
5858
Result
5959
end.<% else %>
60-
request(Client, get, Path, Query, Headers, undefined, Options, SuccessStatusCode).<% end %>
60+
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).<% end %>
6161
<% else %>
6262
<%= action.function_name %>(Client<%= AWS.CodeGen.RestService.function_parameters(action) %>, Input) ->
6363
<%= action.function_name %>(Client<%= AWS.CodeGen.RestService.function_parameters(action) %>, Input, []).
@@ -79,11 +79,11 @@
7979
{<<"<%= parameter.location_name %>">>, <<"<%= parameter.name %>">>},<% end %><%= for parameter <- Enum.slice action.query_parameters, -1..-1 do %>
8080
{<<"<%= parameter.location_name %>">>, <<"<%= parameter.name %>">>}
8181
<% end %>],
82-
{Query, Input} = aws_request:build_headers(QueryMapping, Input1),<% else %>
83-
Query = [],
82+
{Query_, Input} = aws_request:build_headers(QueryMapping, Input1),<% else %>
83+
Query_ = [],
8484
Input = Input1,
8585
<% end %><%= if length(action.response_header_parameters) > 0 do %>
86-
case request(Client, Method, Path, Query, Headers, Input, Options, SuccessStatusCode) of
86+
case request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode) of
8787
{ok, Body0, {_, ResponseHeaders, _} = Response} ->
8888
ResponseHeadersParams =
8989
[<%= for parameter <- Enum.drop action.response_header_parameters, -1 do %>
@@ -101,7 +101,7 @@
101101
Result ->
102102
Result
103103
end.<% else %>
104-
request(Client, Method, Path, Query, Headers, Input, Options, SuccessStatusCode).<% end %>
104+
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).<% end %>
105105
<% end %><% end %>
106106
%%====================================================================
107107
%% Internal functions
@@ -118,8 +118,8 @@ request(Client, Method, Path, Query, Headers0, Input, Options, SuccessStatusCode
118118
Client1 = Client#{service => <<"<%= context.signing_name %>">><%= if context.is_global do %>,
119119
region => <<"<%= context.credential_scope %>">><% end %>},
120120
<%= if context.endpoint_prefix == "s3-control" do %>AccountId = proplists:get_value(<<"x-amz-account-id">>, Headers0),
121-
Host = get_host(AccountId, <<"<%= context.endpoint_prefix %>">>, Client1),<% else %>Host = get_host(<<"<%= context.endpoint_prefix %>">>, Client1),<% end %>
122-
URL0 = get_url(Host, Path, Client1),
121+
Host = build_host(AccountId, <<"<%= context.endpoint_prefix %>">>, Client1),<% else %>Host = build_host(<<"<%= context.endpoint_prefix %>">>, Client1),<% end %>
122+
URL0 = build_url(Host, Path, Client1),
123123
URL = aws_request:add_query(URL0, Query),
124124
AdditionalHeaders = [ {<<"Host">>, Host}
125125
, {<<"Content-Type">>, <<"<%= context.content_type %>">>}
@@ -151,22 +151,22 @@ handle_response({ok, StatusCode, ResponseHeaders, Client}, _) ->
151151
handle_response({error, Reason}, _) ->
152152
{error, Reason}.
153153
<%= if context.endpoint_prefix == "s3-control" do %>
154-
get_host(_AccountId, _EndpointPrefix, #{region := <<"local">>}) ->
154+
build_host(_AccountId, _EndpointPrefix, #{region := <<"local">>}) ->
155155
<<"localhost">>;
156-
get_host(undefined, _EndpointPrefix, _Client) ->
156+
build_host(undefined, _EndpointPrefix, _Client) ->
157157
error(missing_account_id);
158-
get_host(AccountId, EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
158+
build_host(AccountId, EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
159159
aws_util:binary_join([AccountId, EndpointPrefix, Region, Endpoint],
160160
<<".">>).<% else %>
161-
get_host(_EndpointPrefix, #{region := <<"local">>}) ->
161+
build_host(_EndpointPrefix, #{region := <<"local">>}) ->
162162
<<"localhost">>;<%= if context.is_global do %>
163-
get_host(EndpointPrefix, #{endpoint := Endpoint}) ->
163+
build_host(EndpointPrefix, #{endpoint := Endpoint}) ->
164164
aws_util:binary_join([EndpointPrefix, Endpoint], <<".">>).
165165
<% else %>
166-
get_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
166+
build_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
167167
aws_util:binary_join([EndpointPrefix, Region, Endpoint], <<".">>).
168168
<% end %><% end %>
169-
get_url(Host, Path0, Client) ->
169+
build_url(Host, Path0, Client) ->
170170
Proto = maps:get(proto, Client),
171171
Path = erlang:iolist_to_binary(Path0),
172172
Port = maps:get(port, Client),

priv/rest.ex.eex

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ defmodule <%= context.module_name %> do
1717
else
1818
headers
1919
end<% end %>
20-
query = []<%= for parameter <- Enum.reverse(action.query_parameters) do %>
21-
query = if !is_nil(<%= parameter.code_name %>) do
22-
[{"<%= parameter.location_name %>", <%= parameter.code_name %>} | query]
20+
query_ = []<%= for parameter <- Enum.reverse(action.query_parameters) do %>
21+
query_ = if !is_nil(<%= parameter.code_name %>) do
22+
[{"<%= parameter.location_name %>", <%= parameter.code_name %>} | query_]
2323
else
24-
query
24+
query_
2525
end<% end %><%= if length(action.response_header_parameters) > 0 do %>
26-
case request(client, :get, path_, query, headers, nil, options, <%= inspect(action.success_status_code) %>) do
26+
case request(client, :get, path_, query_, headers, nil, options, <%= inspect(action.success_status_code) %>) do
2727
{:ok, body, response} ->
2828
body =
2929
[<%= for parameter <- action.response_header_parameters do %>
@@ -41,7 +41,7 @@ defmodule <%= context.module_name %> do
4141
result ->
4242
result
4343
end<% else %>
44-
request(client, :get, path_, query, headers, nil, options, <%= inspect(action.success_status_code) %>)<% end %>
44+
request(client, :get, path_, query_, headers, nil, options, <%= inspect(action.success_status_code) %>)<% end %>
4545
end<% else %>
4646
def <%= action.function_name %>(client<%= AWS.CodeGen.RestService.function_parameters(action) %>, input, options \\ []) do
4747
path_ = "<%= AWS.CodeGen.RestService.Action.url_path(action) %>"<%= if length(action.request_header_parameters) > 0 do %>
@@ -51,13 +51,13 @@ defmodule <%= context.module_name %> do
5151
]
5252
|> AWS.Request.build_params(input)<% else %>
5353
headers = []<% end %><%= if length(action.query_parameters) > 0 do %>
54-
{query, input} =
54+
{query_, input} =
5555
[<%= for parameter <- action.query_parameters do %>
5656
{"<%= parameter.name %>", "<%= parameter.location_name %>"},<% end %>
5757
]
5858
|> AWS.Request.build_params(input)<% else %>
59-
query = []<% end %><%= if length(action.response_header_parameters) > 0 do %>
60-
case request(client, <%= AWS.CodeGen.RestService.Action.method(action) %>, path_, query, headers, input, options, <%= inspect(action.success_status_code) %>) do
59+
query_ = []<% end %><%= if length(action.response_header_parameters) > 0 do %>
60+
case request(client, <%= AWS.CodeGen.RestService.Action.method(action) %>, path_, query_, headers, input, options, <%= inspect(action.success_status_code) %>) do
6161
{:ok, body, response} ->
6262
body =
6363
[<%= for parameter <- action.response_header_parameters do %>
@@ -75,7 +75,7 @@ defmodule <%= context.module_name %> do
7575
result ->
7676
result
7777
end<% else %>
78-
request(client, <%= AWS.CodeGen.RestService.Action.method(action) %>, path_, query, headers, input, options, <%= inspect(action.success_status_code) %>)<% end %>
78+
request(client, <%= AWS.CodeGen.RestService.Action.method(action) %>, path_, query_, headers, input, options, <%= inspect(action.success_status_code) %>)<% end %>
7979
end<% end %><% end %>
8080

8181
@spec request(AWS.Client.t(), binary(), binary(), list(), list(), map(), list(), pos_integer()) ::
@@ -86,9 +86,9 @@ defmodule <%= context.module_name %> do
8686
client = %{client | service: "<%= context.signing_name %>"<%= if context.is_global do %>,
8787
region: "<%= context.credential_scope %>"<% end %>}
8888
<%= if context.endpoint_prefix == "s3-control" do %>account_id = :proplists.get_value("x-amz-account-id", headers)
89-
host = get_host(account_id, "<%= context.endpoint_prefix %>", client)<% else %>host = get_host("<%= context.endpoint_prefix %>", client)<% end %>
89+
host = build_host(account_id, "<%= context.endpoint_prefix %>", client)<% else %>host = build_host("<%= context.endpoint_prefix %>", client)<% end %>
9090
url = host
91-
|> get_url(path, client)
91+
|> build_url(path, client)
9292
|> add_query(query)
9393

9494
additional_headers = [{"Host", host}, {"Content-Type", "<%= context.content_type %>"}]
@@ -134,28 +134,28 @@ defmodule <%= context.module_name %> do
134134
end
135135
end
136136
<%= if context.endpoint_prefix == "s3-control" do %>
137-
defp get_host(_account_id, _endpoint_prefix, %{region: "local"}) do
137+
defp build_host(_account_id, _endpoint_prefix, %{region: "local"}) do
138138
"localhost"
139139
end
140-
defp get_host(:undefined, _endpoint_prefix, _client) do
140+
defp build_host(:undefined, _endpoint_prefix, _client) do
141141
raise "missing account_id"
142142
end
143-
defp get_host(account_id, endpoint_prefix, %{region: region, endpoint: endpoint}) do
143+
defp build_host(account_id, endpoint_prefix, %{region: region, endpoint: endpoint}) do
144144
"#{account_id}.#{endpoint_prefix}.#{region}.#{endpoint}"
145145
end
146146
<% else %>
147-
defp get_host(_endpoint_prefix, %{region: "local"}) do
147+
defp build_host(_endpoint_prefix, %{region: "local"}) do
148148
"localhost"
149149
end<%= if context.is_global do %>
150-
defp get_host(endpoint_prefix, %{endpoint: endpoint}) do
150+
defp build_host(endpoint_prefix, %{endpoint: endpoint}) do
151151
"#{endpoint_prefix}.#{endpoint}"
152152
end
153153
<% else %>
154-
defp get_host(endpoint_prefix, %{region: region, endpoint: endpoint}) do
154+
defp build_host(endpoint_prefix, %{region: region, endpoint: endpoint}) do
155155
"#{endpoint_prefix}.#{region}.#{endpoint}"
156156
end
157157
<% end %><% end %>
158-
defp get_url(host, path, %{:proto => proto, :port => port}) do
158+
defp build_url(host, path, %{:proto => proto, :port => port}) do
159159
"#{proto}://#{host}:#{port}#{path}"
160160
end
161161

0 commit comments

Comments
 (0)