This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Elasticity | ||
|
||
class AwsUtils | ||
|
||
# Escape a string according to Amazon's rules. | ||
# See: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?REST_RESTAuth.html | ||
def self.aws_escape(param) | ||
param.to_s.gsub(/([^a-zA-Z0-9._~-]+)/n) do | ||
'%' + $1.unpack('H2' * $1.size).join('%').upcase | ||
end | ||
end | ||
|
||
# Since we use the same structure as AWS, we can generate AWS param names | ||
# from the Ruby versions of those names (and the param nesting). | ||
def self.convert_ruby_to_aws(params) | ||
result = {} | ||
params.each do |key, value| | ||
case value | ||
when Array | ||
prefix = "#{camelize(key.to_s)}.member" | ||
value.each_with_index do |item, index| | ||
if item.is_a?(String) | ||
result["#{prefix}.#{index+1}"] = item | ||
else | ||
convert_ruby_to_aws(item).each do |nested_key, nested_value| | ||
result["#{prefix}.#{index+1}.#{nested_key}"] = nested_value | ||
end | ||
end | ||
end | ||
when Hash | ||
prefix = "#{camelize(key.to_s)}" | ||
convert_ruby_to_aws(value).each do |nested_key, nested_value| | ||
result["#{prefix}.#{nested_key}"] = nested_value | ||
end | ||
else | ||
result[camelize(key.to_s)] = value | ||
end | ||
end | ||
result | ||
end | ||
|
||
def self.camelize(word) | ||
word.to_s.gsub(/\/(.?)/) { '::' + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
describe Elasticity::AwsUtils do | ||
|
||
describe '.convert_ruby_to_aws' do | ||
it 'should convert the params' do | ||
add_jobflow_steps_params = { | ||
:job_flow_id => 'j-1', | ||
:steps => [ | ||
{ | ||
:action_on_failure => 'CONTINUE', | ||
:name => 'First New Job Step', | ||
:hadoop_jar_step => { | ||
:args => %w(arg1 arg2 arg3), | ||
:jar => 'first_step.jar', | ||
:main_class => 'first_class.jar' | ||
} | ||
}, | ||
{ | ||
:action_on_failure => 'CANCEL_AND_WAIT', | ||
:name => 'Second New Job Step', | ||
:hadoop_jar_step => { | ||
:args => %w(arg4 arg5 arg6), | ||
:jar => 'second_step.jar', | ||
:main_class => 'second_class.jar' | ||
} | ||
} | ||
] | ||
} | ||
expected_result = { | ||
'JobFlowId' => 'j-1', | ||
'Steps.member.1.Name' => 'First New Job Step', | ||
'Steps.member.1.ActionOnFailure' => 'CONTINUE', | ||
'Steps.member.1.HadoopJarStep.Jar' => 'first_step.jar', | ||
'Steps.member.1.HadoopJarStep.MainClass' => 'first_class.jar', | ||
'Steps.member.1.HadoopJarStep.Args.member.1' => 'arg1', | ||
'Steps.member.1.HadoopJarStep.Args.member.2' => 'arg2', | ||
'Steps.member.1.HadoopJarStep.Args.member.3' => 'arg3', | ||
'Steps.member.2.Name' => 'Second New Job Step', | ||
'Steps.member.2.ActionOnFailure' => 'CANCEL_AND_WAIT', | ||
'Steps.member.2.HadoopJarStep.Jar' => 'second_step.jar', | ||
'Steps.member.2.HadoopJarStep.MainClass' => 'second_class.jar', | ||
'Steps.member.2.HadoopJarStep.Args.member.1' => 'arg4', | ||
'Steps.member.2.HadoopJarStep.Args.member.2' => 'arg5', | ||
'Steps.member.2.HadoopJarStep.Args.member.3' => 'arg6' | ||
} | ||
Elasticity::AwsUtils.send(:convert_ruby_to_aws, add_jobflow_steps_params).should == expected_result | ||
end | ||
end | ||
|
||
end |