From e3fd18a1f023cc803f963544ef605affd1eeddc0 Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Fri, 7 Sep 2012 18:40:10 +0400 Subject: [PATCH 1/4] Customize monitoring Url (protocol://host:port) --- include/erlcloud_aws.hrl | 2 ++ src/erlcloud_aws.erl | 21 ++++++++++++++++++--- src/erlcloud_mon.erl | 22 +++++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/include/erlcloud_aws.hrl b/include/erlcloud_aws.hrl index b79a7bb3..0120c326 100644 --- a/include/erlcloud_aws.hrl +++ b/include/erlcloud_aws.hrl @@ -7,6 +7,8 @@ sqs_host="queue.amazonaws.com"::string(), mturk_host="mechanicalturk.amazonaws.com"::string(), mon_host="monitoring.amazonaws.com"::string(), + mon_port=undefined::non_neg_integer()|undefined, + mon_prot=undefined::string()|undefined, access_key_id::string(), secret_access_key::string() }). diff --git a/src/erlcloud_aws.erl b/src/erlcloud_aws.erl index 52a66495..c5171e30 100644 --- a/src/erlcloud_aws.erl +++ b/src/erlcloud_aws.erl @@ -1,15 +1,22 @@ -module(erlcloud_aws). --export([aws_request/6, aws_request_xml/6, +-export([aws_request/6, + aws_request/8, + aws_request_xml/6, + aws_request_xml/8, param_list/2, default_config/0, format_timestamp/1]). -include_lib("erlcloud/include/erlcloud_aws.hrl"). aws_request_xml(Method, Host, Path, Params, AccessKeyID, SecretAccessKey) -> Body = aws_request(Method, Host, Path, Params, AccessKeyID, SecretAccessKey), - %io:format("Body = ~p~n", [Body]), + element(1, xmerl_scan:string(Body)). +aws_request_xml(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey) -> + Body = aws_request(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey), element(1, xmerl_scan:string(Body)). aws_request(Method, Host, Path, Params, AccessKeyID, SecretAccessKey) -> + aws_request(Method, undefined, Host, undefined, Path, Params, AccessKeyID, SecretAccessKey). +aws_request(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey) -> Timestamp = format_timestamp(erlang:universaltime()), QParams = lists:sort([{"Timestamp", Timestamp}, {"SignatureVersion", "2"}, @@ -23,7 +30,15 @@ aws_request(Method, Host, Path, Params, AccessKeyID, SecretAccessKey) -> Query = [QueryToSign, "&Signature=", erlcloud_http:url_encode(Signature)], - URL = ["https://", Host, Path], + case Prot of + undefined -> UProt = "https://"; + _ -> UProt = [Prot, "://"] + end, + + case Port of + undefined -> URL = [UProt, Host, Path]; + _ -> URL = [UProt, Host, $:, Port, Path] + end, Response = case Method of diff --git a/src/erlcloud_mon.erl b/src/erlcloud_mon.erl index 2bee45f9..80c5d20d 100644 --- a/src/erlcloud_mon.erl +++ b/src/erlcloud_mon.erl @@ -16,6 +16,7 @@ put_metric_data/2, put_metric_data/5, get_metric_statistics/8, + configure_host/3, test/0, test2/0 ]). @@ -251,14 +252,25 @@ mon_query(Config, Action, Params) -> mon_query(Config, Action, Params, ApiVersion) -> QParams = [{"Action", Action}, {"Version", ApiVersion}|Params], erlcloud_aws:aws_request_xml(get, - Config#aws_config.mon_host, - "/", - QParams, - Config#aws_config.access_key_id, - Config#aws_config.secret_access_key). + Config#aws_config.mon_prot, + Config#aws_config.mon_host, + Config#aws_config.mon_port, + "/", + QParams, + Config#aws_config.access_key_id, + Config#aws_config.secret_access_key). default_config() -> erlcloud_aws:default_config(). +configure_host(Host, Port, Protocol) -> + Config = default_config(), + NewConfig = Config#aws_config{mon_host=Host, + mon_port=Port, + mon_prot=Protocol}, + put(aws_config, NewConfig). + + + %%------------------------------------------------------------------------------ %% tests %% TODO : convert into e-unit tests From d44cdee74fb5ed992ecb95915da8f6b5597c6689 Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Tue, 9 Oct 2012 17:48:59 +0400 Subject: [PATCH 2/4] Added test for aws_request --- rebar.config | 2 ++ test/erlcloud_aws_tests.erl | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/erlcloud_aws_tests.erl diff --git a/rebar.config b/rebar.config index 0f5d40e6..6bd77283 100644 --- a/rebar.config +++ b/rebar.config @@ -1 +1,3 @@ {erl_opts, [debug_info]}. + +{deps, [{meck, ".*", {git, "https://github.com/eproxus/meck.git", "master"}}]}. diff --git a/test/erlcloud_aws_tests.erl b/test/erlcloud_aws_tests.erl new file mode 100644 index 00000000..ed00bc9d --- /dev/null +++ b/test/erlcloud_aws_tests.erl @@ -0,0 +1,26 @@ +-module(erlcloud_aws_tests). +-include_lib("eunit/include/eunit.hrl"). + +request_test() -> + meck:new(httpc, [unstick]), + meck:expect(httpc, request, fun(_) -> {ok, {{0, 200, 0}, 0, ok}} end), + ok = erlcloud_aws:aws_request(get, "host", "/", [], "id", "key"), + [{_, {httpc, request, [Url]}, _}] = meck:history(httpc), + test_url(https, "host", 443, "/", Url), + meck:unload(httpc). + + +request2_test() -> + meck:new(httpc, [unstick]), + meck:expect(httpc, request, fun(_) -> {ok, {{0, 200, 0}, 0, ok}} end), + ok = erlcloud_aws:aws_request(get, "http", "host1", "9999", "/path1", [], "id", "key"), + [{_, {httpc, request, [Url]}, _}] = meck:history(httpc), + test_url(http, "host1", 9999, "/path1", Url), + meck:unload(httpc). + +test_url(ExpScheme, ExpHost, ExpPort, ExpPath, Url) -> + {Scheme, _UserInfo, Host, Port, Path, _Query} = http_uri:parse(Url), + ?assertEqual(ExpScheme, Scheme), + ?assertEqual(ExpHost, Host), + ?assertEqual(ExpPort, Port), + ?assertEqual(ExpPath, Path). From eb2463ce85ec3af51f1375799079f52e7353c5ca Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Wed, 10 Oct 2012 17:46:19 +0400 Subject: [PATCH 3/4] Added tests for aws_request/6 and aws_request/8 (depends on meck). Change Makefile --- .gitignore | 2 ++ Makefile | 16 +++++++++--- src/erlcloud_aws.erl | 7 +++++- test/erlcloud_aws_tests.erl | 49 ++++++++++++++++++++++++++----------- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 86df3f28..a3db9623 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ _build ebin/*.beam ebin/*.app +.eunit/ +deps/ diff --git a/Makefile b/Makefile index cc3cb877..9d732e0c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,15 @@ -all: - rebar compile +REBAR=$(shell which rebar || echo ./rebar) + +get-deps: + @$(REBAR) get-deps + +all: compile clean: - rebar clean + @$(REBAR) clean + +compile: + @$(REBAR) compile + +eunit: compile + @$(REBAR) eunit skip_deps=true \ No newline at end of file diff --git a/src/erlcloud_aws.erl b/src/erlcloud_aws.erl index c5171e30..3f004980 100644 --- a/src/erlcloud_aws.erl +++ b/src/erlcloud_aws.erl @@ -37,7 +37,7 @@ aws_request(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey case Port of undefined -> URL = [UProt, Host, Path]; - _ -> URL = [UProt, Host, $:, Port, Path] + _ -> URL = [UProt, Host, $:, port_to_str(Port), Path] end, Response = @@ -98,3 +98,8 @@ default_config() -> Config -> Config end. + +port_to_str(Port) when is_integer(Port) -> + integer_to_list(Port); +port_to_str(Port) when is_list(Port) -> + Port. diff --git a/test/erlcloud_aws_tests.erl b/test/erlcloud_aws_tests.erl index ed00bc9d..257f9006 100644 --- a/test/erlcloud_aws_tests.erl +++ b/test/erlcloud_aws_tests.erl @@ -1,26 +1,47 @@ -module(erlcloud_aws_tests). -include_lib("eunit/include/eunit.hrl"). -request_test() -> +request_test_() -> + {foreach, + fun start/0, + fun stop/1, + [fun request_default_test/1, + fun request_prot_host_port_str_test/1, + fun request_prot_host_port_int_test/1]}. + +start() -> meck:new(httpc, [unstick]), meck:expect(httpc, request, fun(_) -> {ok, {{0, 200, 0}, 0, ok}} end), - ok = erlcloud_aws:aws_request(get, "host", "/", [], "id", "key"), - [{_, {httpc, request, [Url]}, _}] = meck:history(httpc), - test_url(https, "host", 443, "/", Url), + ok. + +stop(_) -> meck:unload(httpc). +request_default_test(_) -> + ok = erlcloud_aws:aws_request(get, "host", "/", [], "id", "key"), + Url = get_url_from_history(meck:history(httpc)), + test_url(https, "host", 443, "/", Url). -request2_test() -> - meck:new(httpc, [unstick]), - meck:expect(httpc, request, fun(_) -> {ok, {{0, 200, 0}, 0, ok}} end), +request_prot_host_port_str_test(_) -> ok = erlcloud_aws:aws_request(get, "http", "host1", "9999", "/path1", [], "id", "key"), - [{_, {httpc, request, [Url]}, _}] = meck:history(httpc), - test_url(http, "host1", 9999, "/path1", Url), - meck:unload(httpc). + Url = get_url_from_history(meck:history(httpc)), + test_url(http, "host1", 9999, "/path1", Url). + +request_prot_host_port_int_test(_) -> + ok = erlcloud_aws:aws_request(get, "http", "host1", 9999, "/path1", [], "id", "key"), + Url = get_url_from_history(meck:history(httpc)), + test_url(http, "host1", 9999, "/path1", Url). + +% ================== +% Internal functions +% ================== + +get_url_from_history([{_, {httpc, request, [Url]}, _}]) -> + Url. test_url(ExpScheme, ExpHost, ExpPort, ExpPath, Url) -> {Scheme, _UserInfo, Host, Port, Path, _Query} = http_uri:parse(Url), - ?assertEqual(ExpScheme, Scheme), - ?assertEqual(ExpHost, Host), - ?assertEqual(ExpPort, Port), - ?assertEqual(ExpPath, Path). + [?_assertEqual(ExpScheme, Scheme), + ?_assertEqual(ExpHost, Host), + ?_assertEqual(ExpPort, Port), + ?_assertEqual(ExpPath, Path)]. From b8ea45b44c184990b72acf80aa43274021ce8fa3 Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Fri, 12 Oct 2012 15:12:46 +0400 Subject: [PATCH 4/4] Change prot to protocol --- include/erlcloud_aws.hrl | 2 +- src/erlcloud_aws.erl | 16 ++++++++-------- src/erlcloud_mon.erl | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/erlcloud_aws.hrl b/include/erlcloud_aws.hrl index 0120c326..bd5deeb7 100644 --- a/include/erlcloud_aws.hrl +++ b/include/erlcloud_aws.hrl @@ -8,7 +8,7 @@ mturk_host="mechanicalturk.amazonaws.com"::string(), mon_host="monitoring.amazonaws.com"::string(), mon_port=undefined::non_neg_integer()|undefined, - mon_prot=undefined::string()|undefined, + mon_protocol=undefined::string()|undefined, access_key_id::string(), secret_access_key::string() }). diff --git a/src/erlcloud_aws.erl b/src/erlcloud_aws.erl index 3f004980..86bc9b1c 100644 --- a/src/erlcloud_aws.erl +++ b/src/erlcloud_aws.erl @@ -10,13 +10,13 @@ aws_request_xml(Method, Host, Path, Params, AccessKeyID, SecretAccessKey) -> Body = aws_request(Method, Host, Path, Params, AccessKeyID, SecretAccessKey), element(1, xmerl_scan:string(Body)). -aws_request_xml(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey) -> - Body = aws_request(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey), +aws_request_xml(Method, Protocol, Host, Port, Path, Params, AccessKeyID, SecretAccessKey) -> + Body = aws_request(Method, Protocol, Host, Port, Path, Params, AccessKeyID, SecretAccessKey), element(1, xmerl_scan:string(Body)). aws_request(Method, Host, Path, Params, AccessKeyID, SecretAccessKey) -> aws_request(Method, undefined, Host, undefined, Path, Params, AccessKeyID, SecretAccessKey). -aws_request(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey) -> +aws_request(Method, Protocol, Host, Port, Path, Params, AccessKeyID, SecretAccessKey) -> Timestamp = format_timestamp(erlang:universaltime()), QParams = lists:sort([{"Timestamp", Timestamp}, {"SignatureVersion", "2"}, @@ -30,14 +30,14 @@ aws_request(Method, Prot, Host, Port, Path, Params, AccessKeyID, SecretAccessKey Query = [QueryToSign, "&Signature=", erlcloud_http:url_encode(Signature)], - case Prot of - undefined -> UProt = "https://"; - _ -> UProt = [Prot, "://"] + case Protocol of + undefined -> UProtocol = "https://"; + _ -> UProtocol = [Protocol, "://"] end, case Port of - undefined -> URL = [UProt, Host, Path]; - _ -> URL = [UProt, Host, $:, port_to_str(Port), Path] + undefined -> URL = [UProtocol, Host, Path]; + _ -> URL = [UProtocol, Host, $:, port_to_str(Port), Path] end, Response = diff --git a/src/erlcloud_mon.erl b/src/erlcloud_mon.erl index 80c5d20d..f673230d 100644 --- a/src/erlcloud_mon.erl +++ b/src/erlcloud_mon.erl @@ -252,7 +252,7 @@ mon_query(Config, Action, Params) -> mon_query(Config, Action, Params, ApiVersion) -> QParams = [{"Action", Action}, {"Version", ApiVersion}|Params], erlcloud_aws:aws_request_xml(get, - Config#aws_config.mon_prot, + Config#aws_config.mon_protocol, Config#aws_config.mon_host, Config#aws_config.mon_port, "/", @@ -266,7 +266,7 @@ configure_host(Host, Port, Protocol) -> Config = default_config(), NewConfig = Config#aws_config{mon_host=Host, mon_port=Port, - mon_prot=Protocol}, + mon_protocol=Protocol}, put(aws_config, NewConfig).