Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
zerth committed Nov 20, 2014
2 parents c49d448 + 7468b0e commit 7ce119d
Show file tree
Hide file tree
Showing 6 changed files with 670 additions and 13 deletions.
2 changes: 0 additions & 2 deletions Makefile
@@ -1,4 +1,3 @@

PREFIX:=../
DEST:=$(PREFIX)$(PROJECT)

Expand Down Expand Up @@ -26,4 +25,3 @@ dialyzer:

app:
@$(REBAR) create template=mochiwebapp dest=$(DEST) appid=$(PROJECT)

15 changes: 14 additions & 1 deletion README.md
@@ -1,6 +1,20 @@
Here's how you use this thing without using any macro help:

dinerl:setup("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "us-east-1b").

or (use a specific endpoint by specifying the zone, and obtain
security tokens from the local instance metadata server):

dinerl:setup("us-east-1b").

or (obtain security tokens from the local instance metadata server,
and use the zone returned from the instance metadata server to
determine the endpoint):

dinerl:setup().

then:

dinerl:create_table(<<"TestTable">>, [{<<"HashKeyElement">>, [{<<"AttributeName">>, <<"Key">>}, {<<"AttributeType">>, <<"S">>}]}], 50, 50).
dinerl:list_tables().
dinerl:put_item(<<"TestTable">>, [{<<"Key">>, [{<<"S">>, <<"jello">>}]}], []).
Expand Down Expand Up @@ -80,4 +94,3 @@ Here's how you use this thing without using any macro help:
pytime(erlang:now()).
pytime({MegaSecs, Secs, MicroSecs}) ->
erlang:trunc((1.0e+6 * MegaSecs) + Secs + (1.0e-6 * MicroSecs)).

49 changes: 40 additions & 9 deletions src/dinerl.erl
Expand Up @@ -5,7 +5,8 @@

-include("dinerl_types.hrl").

-export([setup/3, api/1, api/2, api/3]).
-export([setup/3, setup/1, setup/0,
api/1, api/2, api/3]).

-export([create_table/4, create_table/5, delete_table/1, delete_table/2]).
-export([describe_table/1, describe_table/2, update_table/3, update_table/4]).
Expand All @@ -15,14 +16,34 @@
-export([update_item/3, update_item/4]).
-export([query_item/3, query_item/4]).

-export([update_data/3]).
-export([update_data/3, update_data/2]).

-spec setup(access_key_id(), secret_access_key(), zone()) ->
{ok, clientarguments()}.
setup(AccessKeyId, SecretAccessKey, Zone) ->
Q = fun () ->
iam:get_session_token(AccessKeyId, SecretAccessKey)
end,
setup_(Q, Zone).

-spec setup(zone()) -> {ok, clientarguments()}.
setup(Zone) ->
Q = fun () ->
imds:get_session_token()
end,
setup_(Q, Zone).

-spec setup() -> {ok, clientarguments()}.
setup() ->
{ok, Zone} = imds:zone(),
setup(Zone).


-spec setup_(function(), string()) -> {ok, clientarguments()}.
setup_(Q, Zone) ->
ets:new(?DINERL_DATA, [named_table, public]),
R = update_data(AccessKeyId, SecretAccessKey, Zone),
timer:apply_interval(1000, ?MODULE, update_data, [AccessKeyId, SecretAccessKey, Zone]),
R = update_data(Q, Zone),
timer:apply_interval(1000, ?MODULE, update_data, [Q, Zone]),
R.


Expand Down Expand Up @@ -235,6 +256,15 @@ query_item(T, K, [{attrs, V}|Rest], Acc, Timeout) ->
-spec update_data(access_key_id(), secret_access_key(), zone()) ->
{ok, clientarguments()}.
update_data(AccessKeyId, SecretAccessKey, Zone) ->
update_data(fun () ->
iam:get_session_token(AccessKeyId, SecretAccessKey)
end,
Zone).


-spec update_data(function(), zone()) ->
{ok, clientarguments()}.
update_data(GetToken, Zone) ->
case catch(ets:lookup_element(?DINERL_DATA, ?ARGS_KEY, 2)) of
{'EXIT', {badarg, _}} ->
CurrentApiAccessKeyId = "123",
Expand All @@ -252,32 +282,33 @@ update_data(AccessKeyId, SecretAccessKey, Zone) ->
CurrentExpirationSeconds} = Result

end,

NewDate = httpd_util:rfc1123_date(),
NowSeconds = calendar:datetime_to_gregorian_seconds(erlang:universaltime()),
SecondsToExpire = CurrentExpirationSeconds - NowSeconds,

case SecondsToExpire < 120 of
true ->
NewToken = iam:get_session_token(AccessKeyId, SecretAccessKey),
NewToken = GetToken(),

ExpirationString = proplists:get_value(expiration, NewToken),
ApiAccessKeyId = proplists:get_value(access_key_id, NewToken),
ApiSecretAccessKey = proplists:get_value(secret_access_key, NewToken),
ApiToken = proplists:get_value(token, NewToken),
ExpirationSeconds = calendar:datetime_to_gregorian_seconds(iso8601:parse(ExpirationString)),

NewArgs = {ApiAccessKeyId, ApiSecretAccessKey, Zone, ApiToken, NewDate, ExpirationSeconds};

false ->
NewArgs = {CurrentApiAccessKeyId, CurrentApiSecretAccessKey,
Zone, CurrentApiToken, NewDate, CurrentExpirationSeconds}
end,

ets:insert(?DINERL_DATA, {?ARGS_KEY, NewArgs}),
{ok, NewArgs}.



expected([], Acc) ->
Acc;
expected([{Option, Value}|Rest], Acc) ->
Expand Down
2 changes: 1 addition & 1 deletion src/dmochijson2.erl
Expand Up @@ -140,7 +140,7 @@ json_encode(null, _State) ->
json_encode(I, _State) when is_integer(I) ->
integer_to_list(I);
json_encode(F, _State) when is_float(F) ->
mochinum:digits(F);
dmochinum:digits(F);
json_encode(S, State) when is_binary(S); is_atom(S) ->
json_encode_string(S, State);
json_encode([{K, _}|_] = Props, State) when (K =/= struct andalso
Expand Down

0 comments on commit 7ce119d

Please sign in to comment.