Packages
erlcloud
3.5.5
3.8.3
3.8.2
3.8.1
3.7.6
3.7.4
3.7.3
3.7.2
3.7.1
3.7.0
3.6.8
3.6.7
3.6.5
3.6.4
3.6.3
3.6.2
3.6.1
3.6.0
3.5.16
3.5.15
3.5.14
3.5.13
3.5.12
3.5.11
3.5.10
3.5.9
3.5.8
3.5.7
3.5.6
3.5.5
3.5.4
3.5.3
3.5.2
3.5.1
3.5.0
3.4.5
3.4.3
3.4.1
3.4.0
3.3.9
3.3.8
3.3.7
3.3.6
3.3.5
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.18
3.2.17
3.2.16
3.2.15
3.2.14
3.2.13
3.2.12
3.2.11
3.2.10
3.2.7
3.2.6
3.2.5
3.2.4
3.2.3
3.2.2
3.2.1
3.2.0
3.1.17
3.1.16
3.1.14
3.1.13
3.1.12
3.1.11
3.1.9
3.1.8
3.1.7
3.1.6
3.1.5
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
2.2.16
2.2.15
2.2.14
2.2.13
2.2.12
2.2.11
2.2.10
2.2.9
2.2.8
2.2.7
2.2.6
2.2.5
2.2.4
2.2.2
2.2.1
2.2.0
2.1.0
2.0.5
2.0.4
2.0.3
2.0.0
0.13.10
0.13.9
0.13.8
0.13.6
0.13.5
0.13.4
0.13.3
0.13.2
0.13.0
0.12.0
0.11.0
0.9.2
0.9.2-rc.1
0.9.1
0.9.0
AWS APIs library for Erlang
Current section
Files
Jump to
Current section
Files
src/erlcloud_emr.erl
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
-module(erlcloud_emr).
-include("erlcloud_aws.hrl").
%% Library initialization
-export([configure/2, configure/3, configure/4, configure/5,
new/2, new/3, new/4, new/5]).
%% ElasticMapReduce API
-export([add_job_flow_steps/2, add_job_flow_steps/3, add_job_flow_steps/4]).
-export([describe_step/2, describe_step/3, describe_step/4]).
-export([run_job_flow/1, run_job_flow/2, run_job_flow/3]).
-export([terminate_job_flows/1, terminate_job_flows/2, terminate_job_flows/3]).
-export([set_termination_protection/2, set_termination_protection/3,
set_termination_protection/4]).
-export_type([emr_opts/0, emr_return/0]).
%%------------------------------------------------------------------------------
%% Library initialization
%%------------------------------------------------------------------------------
-spec new(string(), string()) -> aws_config().
new(AccessKeyID, SecretAccessKey) ->
#aws_config{access_key_id=AccessKeyID,
secret_access_key=SecretAccessKey}.
-spec new(string(), string(), string()) -> aws_config().
new(AccessKeyID, SecretAccessKey, Host) ->
#aws_config{access_key_id=AccessKeyID,
secret_access_key=SecretAccessKey,
emr_host=Host}.
-spec new(string(), string(), string(), non_neg_integer()) -> aws_config().
new(AccessKeyID, SecretAccessKey, Host, Port) ->
#aws_config{access_key_id=AccessKeyID,
secret_access_key=SecretAccessKey,
emr_host=Host,
emr_port=Port}.
-spec new(string(), string(), string(), non_neg_integer(), string()) -> aws_config().
new(AccessKeyID, SecretAccessKey, Host, Port, Scheme) ->
#aws_config{access_key_id=AccessKeyID,
secret_access_key=SecretAccessKey,
emr_host=Host,
emr_port=Port,
emr_scheme=Scheme}.
-spec configure(string(), string()) -> ok.
configure(AccessKeyID, SecretAccessKey) ->
erlcloud_config:configure(AccessKeyID, SecretAccessKey, fun new/2).
-spec configure(string(), string(), string()) -> ok.
configure(AccessKeyID, SecretAccessKey, Host) ->
erlcloud_config:configure(AccessKeyID, SecretAccessKey, Host, fun new/3).
-spec configure(string(), string(), string(), non_neg_integer()) -> ok.
configure(AccessKeyID, SecretAccessKey, Host, Port) ->
erlcloud_config:configure(AccessKeyID, SecretAccessKey, Host, Port, fun new/4).
-spec configure(string(), string(), string(), non_neg_integer(), string()) -> ok.
configure(AccessKeyID, SecretAccessKey, Host, Port, Scheme) ->
erlcloud_config:configure(AccessKeyID, SecretAccessKey, Host, Port, Scheme, fun new/5).
%%------------------------------------------------------------------------------
%% ElasticMapReduce API
%%------------------------------------------------------------------------------
-type emr_opts() :: [{out, json | raw}].
-type emr_return() :: {ok, jsx:json_term() | binary()} |
{error, {aws_error, jsx:json_term()}} |
{error, {socket_error, any()}} |
{error, tuple()}.
%% --- AddJobFlowSteps ---
%% http://docs.aws.amazon.com/ElasticMapReduce/latest/API/API_AddJobFlowSteps.html
-spec add_job_flow_steps(binary(), jsx:json_term()) -> emr_return().
add_job_flow_steps(JobFlowId, Steps) ->
add_job_flow_steps(JobFlowId, Steps, [], erlcloud_aws:default_config()).
-spec add_job_flow_steps(binary(), jsx:json_term(), aws_config() | emr_opts()) -> emr_return().
add_job_flow_steps(JobFlowId, Steps, Config = #aws_config{}) ->
add_job_flow_steps(JobFlowId, Steps, [], Config);
add_job_flow_steps(JobFlowId, Steps, Opts) when is_list(Opts) ->
add_job_flow_steps(JobFlowId, Steps, Opts, erlcloud_aws:default_config()).
-spec add_job_flow_steps(binary(), jsx:json_term(), emr_opts(), aws_config()) -> emr_return().
add_job_flow_steps(JobFlowId, Steps, Opts, Config) ->
emr_request("ElasticMapReduce.AddJobFlowSteps",
[{'JobFlowId', JobFlowId}, {'Steps', Steps}], Opts, Config).
%% --- DescribeStep ---
%% http://docs.aws.amazon.com/ElasticMapReduce/latest/API/API_DescribeStep.html
-spec describe_step(binary(), binary()) -> emr_return().
describe_step(ClusterId, StepId) ->
describe_step(ClusterId, StepId, [], erlcloud_aws:default_config()).
-spec describe_step(binary(), binary(), aws_config() | emr_opts()) -> emr_return().
describe_step(ClusterId, StepId, Config = #aws_config{}) ->
describe_step(ClusterId, StepId, [], Config);
describe_step(ClusterId, StepId, Opts) when is_list(Opts) ->
describe_step(ClusterId, StepId, Opts, erlcloud_aws:default_config()).
-spec describe_step(binary(), binary(), emr_opts(), aws_config()) -> emr_return().
describe_step(ClusterId, StepId, Opts, Config) ->
emr_request("ElasticMapReduce.DescribeStep",
[{'ClusterId', ClusterId}, {'StepId', StepId}], Opts, Config).
%% --- RunJobFlow ---
%% http://docs.aws.amazon.com/ElasticMapReduce/latest/API/API_RunJobFlow.html
-spec run_job_flow(jsx:json_term()) -> emr_return().
run_job_flow(Params) ->
run_job_flow(Params, [], erlcloud_aws:default_config()).
-spec run_job_flow(jsx:json_term(), aws_config() | emr_opts()) -> emr_return().
run_job_flow(Params, Config = #aws_config{}) ->
run_job_flow(Params, [], Config);
run_job_flow(Params, Opts) when is_list(Opts) ->
run_job_flow(Params, Opts, erlcloud_aws:default_config()).
-spec run_job_flow(jsx:json_term(), emr_opts(), aws_config()) -> emr_return().
run_job_flow(Params, Opts, Config) ->
emr_request("ElasticMapReduce.RunJobFlow", Params, Opts, Config).
%% --- TerminateJobFlows ---
%% http://docs.aws.amazon.com/ElasticMapReduce/latest/API/API_TerminateJobFlows.html
-spec terminate_job_flows([binary()]) -> emr_return().
terminate_job_flows(JobFlowIds) ->
terminate_job_flows(JobFlowIds, [], erlcloud_aws:default_config()).
-spec terminate_job_flows([binary()], aws_config() | emr_opts()) -> emr_return().
terminate_job_flows(JobFlowIds, Config = #aws_config{}) ->
terminate_job_flows(JobFlowIds, [], Config);
terminate_job_flows(JobFlowIds, Opts) when is_list(Opts) ->
terminate_job_flows(JobFlowIds, Opts, erlcloud_aws:default_config()).
-spec terminate_job_flows([binary()], emr_opts(), aws_config()) -> emr_return().
terminate_job_flows(JobFlowIds, Opts, Config) ->
emr_request("ElasticMapReduce.TerminateJobFlows",
[{'JobFlowIds', JobFlowIds}], Opts, Config).
%% --- SetTerminationProtection ---
%% http://docs.aws.amazon.com/ElasticMapReduce/latest/API/API_SetTerminationProtection.html
-spec set_termination_protection([binary()], boolean()) -> emr_return().
set_termination_protection(JobFlowIds, TerminationProtected) ->
set_termination_protection(JobFlowIds, TerminationProtected,
[], erlcloud_aws:default_config()).
-spec set_termination_protection([binary()], boolean(),
aws_config() | emr_opts()) -> emr_return().
set_termination_protection(JobFlowIds, TerminationProtected,
Config = #aws_config{}) ->
set_termination_protection(JobFlowIds, TerminationProtected,
[], Config);
set_termination_protection(JobFlowIds, TerminationProtected,
Opts) when is_list(Opts) ->
set_termination_protection(JobFlowIds, TerminationProtected,
Opts, erlcloud_aws:default_config()).
-spec set_termination_protection([binary()], boolean(),
emr_opts(), aws_config()) -> emr_return().
set_termination_protection(JobFlowIds, TerminationProtected, Opts, Config) ->
emr_request("ElasticMapReduce.SetTerminationProtection",
[{'JobFlowIds', JobFlowIds},
{'TerminationProtected', TerminationProtected}], Opts, Config).
%%------------------------------------------------------------------------------
%% Internal functions
%%------------------------------------------------------------------------------
emr_request(Action, Json, Opts, Cfg) ->
{Scheme, Host, Port, Service} = emr_endpoint(Cfg),
request(Action, Json, Scheme, Host, Port, Service, Opts, Cfg).
emr_endpoint(Cfg) ->
%% Scheme, Host, Port, Service
{Cfg#aws_config.emr_scheme,
Cfg#aws_config.emr_host,
Cfg#aws_config.emr_port,
"elasticmapreduce"}.
request(Action, Json, Scheme, Host, Port, Service, Opts, Cfg0) ->
case erlcloud_aws:update_config(Cfg0) of
{ok, Cfg} -> request_no_update(Action, Json, Scheme, Host, Port,
Service, Opts, Cfg);
Err -> Err
end.
request_no_update(Action, Json, Scheme, Host, Port, Service, Opts, Cfg) ->
ReqBody = jsx:encode(if Json == [] -> [{}];
true -> Json end),
H1 = [{"host", Host}],
H2 = [{"content-type", "application/x-amz-json-1.1"},
{"x-amz-target", Action}],
Region = erlcloud_aws:aws_region_from_host(Host),
Headers = erlcloud_aws:sign_v4_headers(Cfg, H1, ReqBody, Region, Service) ++ H2,
case erlcloud_aws:aws_request_form_raw(post, Scheme, Host, Port,
"/", ReqBody, Headers, [], Cfg) of
{ok, Body} -> case proplists:get_value(out, Opts, json) of
raw -> {ok, Body};
_ -> case Body of
<<>> -> {ok, <<>>};
_ -> {ok, jsx:decode(Body, [{return_maps, false}])}
end
end;
{error, {http_error, _Code, _StatusLine, ErrBody}} ->
{error, {aws_error, jsx:decode(ErrBody, [{return_maps, false}])}};
{error, {socket_error, Reason}} ->
{error, {socket_error, Reason}}
end.