Skip to content

Commit

Permalink
Merge pull request erlcloud#14 from couchemar/master
Browse files Browse the repository at this point in the history
Customization in aws_request(xml) and erlcloud_mon
  • Loading branch information
gleber committed Oct 30, 2012
2 parents cbd6bc8 + b8ea45b commit dd42966
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@
_build
ebin/*.beam
ebin/*.app
.eunit/
deps/
16 changes: 13 additions & 3 deletions 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
2 changes: 2 additions & 0 deletions include/erlcloud_aws.hrl
Expand Up @@ -8,6 +8,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_protocol=undefined::string()|undefined,
access_key_id::string(),
secret_access_key::string()
}).
Expand Down
2 changes: 2 additions & 0 deletions rebar.config
@@ -1 +1,3 @@
{erl_opts, [debug_info]}.

{deps, [{meck, ".*", {git, "https://github.com/eproxus/meck.git", "master"}}]}.
26 changes: 23 additions & 3 deletions 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, 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, Protocol, Host, Port, Path, Params, AccessKeyID, SecretAccessKey) ->
Timestamp = format_timestamp(erlang:universaltime()),
QParams = lists:sort([{"Timestamp", Timestamp},
{"SignatureVersion", "2"},
Expand All @@ -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 Protocol of
undefined -> UProtocol = "https://";
_ -> UProtocol = [Protocol, "://"]
end,

case Port of
undefined -> URL = [UProtocol, Host, Path];
_ -> URL = [UProtocol, Host, $:, port_to_str(Port), Path]
end,

Response =
case Method of
Expand Down Expand Up @@ -83,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.
22 changes: 17 additions & 5 deletions src/erlcloud_mon.erl
Expand Up @@ -16,6 +16,7 @@
put_metric_data/2,
put_metric_data/5,
get_metric_statistics/8,
configure_host/3,
test/0,
test2/0
]).
Expand Down Expand Up @@ -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_protocol,
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_protocol=Protocol},
put(aws_config, NewConfig).



%%------------------------------------------------------------------------------
%% tests
%% TODO : convert into e-unit tests
Expand Down
47 changes: 47 additions & 0 deletions test/erlcloud_aws_tests.erl
@@ -0,0 +1,47 @@
-module(erlcloud_aws_tests).
-include_lib("eunit/include/eunit.hrl").

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.

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).

request_prot_host_port_str_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).

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)].

0 comments on commit dd42966

Please sign in to comment.