Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from goura/r16_pmods
Browse files Browse the repository at this point in the history
R16 pmods
  • Loading branch information
goura committed May 31, 2013
2 parents 20acb3d + 7bda144 commit 5e449ba
Show file tree
Hide file tree
Showing 11 changed files with 1,141 additions and 286 deletions.
Binary file added rebar
Binary file not shown.
8 changes: 8 additions & 0 deletions src/erlaws.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%% -*- mode: erlang; -*-
{application, erlaws,
[{description, "Erlang interface to Amazon Web Service"},
{vsn, "0.0.1"},
{modules, []},
{registered, []},
{applications, [kernel, stdlib, inets, ssl, crypto]},
{env, []}]}.
2 changes: 1 addition & 1 deletion src/erlaws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

start() ->
application:start(sasl),
crypto:start(),
ssl:start(),
inets:start().

start(_Type, _Args) ->
Expand Down
55 changes: 30 additions & 25 deletions src/erlaws_ec2.erl
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
-module(erlaws_ec2, [AWS_KEY, AWS_SEC_KEY, SECURE]).
-module(erlaws_ec2).
-author (dieu).

-include_lib("xmerl/include/xmerl.hrl").

-export ([start_instances/1, run_instances/13, stop_instances/2, terminate_instances/1, describe_instances/0, describe_instances/1]).
-export ([new/3, start_instances/2, run_instances/14, stop_instances/3, terminate_instances/2, describe_instances/1, describe_instances/2]).

-define (AWS_EC2_HOST, "ec2.amazonaws.com").
-define (AWS_EC2_VERSION, "2009-11-30").


new(AWS_KEY, AWS_SEC_KEY, SECURE) ->
{?MODULE, [AWS_KEY, AWS_SEC_KEY, SECURE]}.

% API
describe_instances() ->
describe_instances([]).
describe_instances({?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}=THIS) ->
describe_instances([], THIS).

describe_instances(InstanceIds) ->
describe_instances(InstanceIds, {?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}=THIS) ->
Fun = fun(AIM, [Inc, List]) -> [Inc + 1, lists:append(List, [{lists:flatten(io_lib:format("InstanceId.~p", [Inc])), AIM}])] end,
[_, Params] = lists:foldl(Fun, [1, []], InstanceIds),
try query_request("DescribeInstances", Params) of
try query_request("DescribeInstances", Params, THIS) of
{ok, Body} ->
{ResponseXML, _Rest} = xmerl_scan:string(Body),
Response = lists:foldl(
Expand All @@ -40,8 +43,8 @@ describe_instances(InstanceIds) ->
{error, Descr}
end.

terminate_instances(InstanceId) ->
try query_request("TerminateInstances", [{"InstanceId", InstanceId}]) of
terminate_instances(InstanceId, {?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}=THIS) ->
try query_request("TerminateInstances", [{"InstanceId", InstanceId}], THIS) of
{ok, Body} ->
{ResponseXML, _Rest} = xmerl_scan:string(Body),
Response = lists:foldl(
Expand All @@ -58,8 +61,8 @@ terminate_instances(InstanceId) ->
{error, Descr}
end.

stop_instances(InstanceId, Force) ->
try query_request("StopInstances", [{"InstanceId", InstanceId}, {"Force", Force}]) of
stop_instances(InstanceId, Force, {?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}=THIS) ->
try query_request("StopInstances", [{"InstanceId", InstanceId}, {"Force", Force}], THIS) of
{ok, Body} ->
{ResponseXML, _Rest} = xmerl_scan:string(Body),
Response = lists:foldl(
Expand All @@ -76,10 +79,10 @@ stop_instances(InstanceId, Force) ->
{error, Descr}
end.

start_instances(InstanceIds) ->
start_instances(InstanceIds, {?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}=THIS) ->
Fun = fun(AIM, [Inc, List]) -> [Inc + 1, lists:append(List, [{lists:flatten(io_lib:format("InstanceId.~p", [Inc])), AIM}])] end,
[_, Params] = lists:foldl(Fun, [1, []], InstanceIds),
try query_request("StartInstances", Params) of
try query_request("StartInstances", Params, THIS) of
{ok, Body} ->
{ResponseXML, _Rest} = xmerl_scan:string(Body),
Response = lists:foldl(
Expand All @@ -98,12 +101,14 @@ start_instances(InstanceIds) ->

%InstanceType Valid Values: m1.small | m1.large | m1.xlarge | c1.medium | c1.xlarge | m2.xlarge | m2.2xlarge | m2.4xlarge
run_instances(ImageId, MinCount, MaxCount, KeyName, SecurityGroups, AddressingType, InstanceType,
KernelId, RamdiskId, Monitoring, SubnetId, DisableApiTermination, InstanceInitiatedShutdownBehavior) ->
KernelId, RamdiskId, Monitoring, SubnetId, DisableApiTermination, InstanceInitiatedShutdownBehavior,
{?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}=THIS) ->
Fun = fun(Group, [Inc, List]) -> [Inc + 1, lists:append(List, [{lists:flatten(io_lib:format("SecurityGroup.~p", [Inc])), Group}])] end,
[_, SecGroups] = lists:foldl(Fun, [1, []], SecurityGroups),
try query_request("RunInstances", [{"ImageId", ImageId}, {"MinCount", lists:flatten(io_lib:format("~p", [MinCount]))}, {"KeyName", KeyName}] ++ SecGroups ++
[{"AddressingType", AddressingType}, {"InstanceType", InstanceType}, {"KernelId", KernelId}, {"RamdiskId", RamdiskId}, {"Monitoring", Monitoring},
{"SubnetId", SubnetId}, {"DisableApiTermination", DisableApiTermination}, {"InstanceInitiatedShutdownBehavior", InstanceInitiatedShutdownBehavior}, {"MaxCount", lists:flatten(io_lib:format("~p", [MaxCount]))}]) of
{"SubnetId", SubnetId}, {"DisableApiTermination", DisableApiTermination}, {"InstanceInitiatedShutdownBehavior", InstanceInitiatedShutdownBehavior}, {"MaxCount", lists:flatten(io_lib:format("~p", [MaxCount]))}],
THIS) of
{ok, Body} ->
{ResponseXML, _Rest} = xmerl_scan:string(Body),
Response = lists:foldl(
Expand All @@ -121,17 +126,17 @@ run_instances(ImageId, MinCount, MaxCount, KeyName, SecurityGroups, AddressingTy
{error, Descr}
end.

sign (Key,Data) ->
sign (Key, Data, {?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}) ->
binary_to_list( base64:encode( crypto:sha_mac(Key,Data) ) ).

query_request(Action, Parameters) ->
query_request(Action, Parameters, {?MODULE, [_AWS_KEY, _AWS_SEC_KEY, SECURE]}=THIS) ->
case SECURE of
true -> Prefix = "https://";
_ -> Prefix = "http://"
end,
query_request(Prefix ++ ?AWS_EC2_HOST ++ "/", Action, Parameters).
query_request(Prefix ++ ?AWS_EC2_HOST ++ "/", Action, Parameters, THIS).

query_request(Url, Action, Parameters) ->
query_request(Url, Action, Parameters, {?MODULE, [AWS_KEY, AWS_SEC_KEY, _SECURE]}=THIS) ->
Timestamp = lists:flatten(erlaws_util:get_timestamp()),
SignParams = [{"Action", Action}, {"AWSAccessKeyId", AWS_KEY}, {"Timestamp", Timestamp}] ++
Parameters ++ [{"SignatureVersion", "1"}, {"Version", ?AWS_EC2_VERSION}],
Expand All @@ -140,18 +145,18 @@ query_request(Url, Action, Parameters) ->
{KeyB, _} = B,
string:to_lower(KeyA) =< string:to_lower(KeyB) end,
SignParams)], ""),
Signature = sign(AWS_SEC_KEY, StringToSign),
Signature = sign(AWS_SEC_KEY, StringToSign, THIS),
FinalQueryParams = SignParams ++
[{"Signature", Signature}],
Result = mkReq(get, Url, [], FinalQueryParams, "", ""),
Result = mkReq(get, Url, [], FinalQueryParams, "", "", THIS),
case Result of
{ok, _Status, Body} ->
{ok, Body};
{error, {_Proto, Code, Reason}, Body} ->
throw({error, {integer_to_list(Code), Reason}, mkErr(Body)})
throw({error, {integer_to_list(Code), Reason}, mkErr(Body, THIS)})
end.

mkReq(Method, PreUrl, Headers, QueryParams, ContentType, ReqBody) ->
mkReq(Method, PreUrl, Headers, QueryParams, ContentType, ReqBody, {?MODULE, [_AWS_KEY, _AWS_SEC_KEY, _SECURE]}) ->
Url = PreUrl ++ erlaws_util:queryParams( QueryParams ),
Request = case Method of
get -> { Url, Headers };
Expand All @@ -160,13 +165,13 @@ mkReq(Method, PreUrl, Headers, QueryParams, ContentType, ReqBody) ->

HttpOptions = [{autoredirect, true}],
Options = [ {sync,true}, {headers_as_is,true}, {body_format, binary} ],
{ok, {Status, _ReplyHeaders, Body}} = http:request(Method, Request, HttpOptions, Options),
{ok, {Status, _ReplyHeaders, Body}} = httpc:request(Method, Request, HttpOptions, Options),
case Status of
{_, 200, _} -> {ok, Status, binary_to_list(Body)};
{_, _, _} -> {error, Status, binary_to_list(Body)}
end.

mkErr(Xml) ->
mkErr(Xml, _THIS) ->
{XmlDoc, _Rest} = xmerl_scan:string( Xml ),
[#xmlText{value=ErrorCode}|_] = xmerl_xpath:string("//Error/Code/text()", XmlDoc),
ErrorMessage =
Expand Down
Loading

0 comments on commit 5e449ba

Please sign in to comment.