Skip to content

Commit

Permalink
ruby1.9 support: replace: 'to_a' by 'Array()'
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin committed Mar 31, 2010
1 parent 0f5901d commit 27eeb7b
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 93 deletions.
4 changes: 2 additions & 2 deletions lib/acf/right_acf_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def merge_headers(hash) # :nodoc:
def config_to_xml(config) # :nodoc:
cnames = ''
unless config[:cnames].blank?
config[:cnames].to_a.each { |cname| cnames += " <CNAME>#{cname}</CNAME>\n" }
Array(config[:cnames]).each { |cname| cnames += " <CNAME>#{cname}</CNAME>\n" }
end
# logging
logging = ''
Expand Down Expand Up @@ -291,7 +291,7 @@ def create_distribution(origin, comment='', enabled=true, cnames=[], caller_refe
config = { :origin => origin,
:comment => comment,
:enabled => enabled,
:cnames => cnames.to_a,
:cnames => Array(cnames),
:caller_reference => caller_reference }
config[:logging] = logging unless logging.blank?
create_distribution_by_config(config)
Expand Down
4 changes: 2 additions & 2 deletions lib/acw/right_acw_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def get_metric_statistics(options={})
# Period (60 sec by default)
period = (options[:period] && options[:period].to_i) || 60
# Statistics ('Average' by default)
statistics = options[:statistics].to_a.flatten
statistics = Array(options[:statistics]).flatten
statistics = statistics.blank? ? ['Average'] : statistics.map{|statistic| statistic.to_s.capitalize }
# Times (5.min.ago up to now by default)
start_time = options[:start_time] || (Time.now.utc - 5*60)
Expand All @@ -167,7 +167,7 @@ def get_metric_statistics(options={})
# dimentions
dim = []
dimentions.each do |key, values|
values.to_a.each { |value| dim << [key, value] }
Array(values).each { |value| dim << [key, value] }
end
request_hash.merge!(amazonize_list(['Dimensions.member.?.Name', 'Dimensions.member.?.Value'], dim))
#
Expand Down
8 changes: 4 additions & 4 deletions lib/as/right_as_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def create_auto_scaling_group(auto_scaling_group_name, launch_configuration_name
options[:min_size] ||= 1
options[:max_size] ||= 20
options[:cooldown] ||= 0
request_hash = amazonize_list('AvailabilityZones.member', availability_zones.to_a)
request_hash.merge!( amazonize_list('LoadBalancerNames', options[:load_balancer_names].to_a) )
request_hash = amazonize_list('AvailabilityZones.member', availability_zones)
request_hash.merge!( amazonize_list('LoadBalancerNames', options[:load_balancer_names]) )
request_hash.merge!( 'AutoScalingGroupName' => auto_scaling_group_name,
'LaunchConfigurationName' => launch_configuration_name,
'MinSize' => options[:min_size],
Expand Down Expand Up @@ -204,7 +204,7 @@ def set_desired_capacity(auto_scaling_group_name, desired_capacity)
# as.update_auto_scaling_group('CentOS.5.1-c', :min_size => 1, :max_size => 4) #=> true
#
def update_auto_scaling_group(auto_scaling_group_name, options={})
request_hash = amazonize_list('AvailabilityZones.member', options[:availability_zones].to_a)
request_hash = amazonize_list('AvailabilityZones.member', options[:availability_zones])
request_hash['AutoScalingGroupName'] = auto_scaling_group_name
request_hash['LaunchConfigurationName'] = options[:launch_configuration_name] if options[:launch_configuration_name]
request_hash['MinSize'] = options[:min_size] if options[:min_size]
Expand Down Expand Up @@ -486,7 +486,7 @@ def create_or_update_scaling_trigger(trigger_name, auto_scaling_group_name, opti
request_hash['CustomUnit'] = options[:custom_unit] if options[:custom_unit]
dimentions = []
(options[:dimentions] || {}).each do |key, values|
values.to_a.each { |value| dimentions << [key, value] }
Array(values).each { |value| dimentions << [key, value] }
end
request_hash.merge!(amazonize_list(['Dimensions.member.?.Name', 'Dimensions.member.?.Value'], dimentions))
link = generate_request("CreateOrUpdateScalingTrigger", request_hash)
Expand Down
8 changes: 4 additions & 4 deletions lib/awsbase/right_awsbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def self.caller_method
end

def self.split_items_and_params(array)
items = array.to_a.flatten.compact
items = Array(array).flatten.compact
params = items.last.kind_of?(Hash) ? items.pop : {}
[items, params]
end
Expand Down Expand Up @@ -505,11 +505,11 @@ def last_request_id
# "Filter.2.Value.2"=>"bb"}
def amazonize_list(masks, list) #:nodoc:
groups = {}
list.to_a.each_with_index do |list_item, i|
masks.to_a.each_with_index do |mask, mask_idx|
Array(list).each_with_index do |list_item, i|
Array(masks).each_with_index do |mask, mask_idx|
key = mask[/\?/] ? mask.dup : mask.dup + '.?'
key.sub!('?', (i+1).to_s)
value = list_item.to_a[mask_idx]
value = Array(list_item)[mask_idx]
if value.is_a?(Array)
groups.merge!(amazonize_list(key, value))
else
Expand Down
32 changes: 16 additions & 16 deletions lib/ec2/right_ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ def request_info(request, parser) #:nodoc:
request_info_impl(:ec2_connection, @@bench, request, parser)
end


#-----------------------------------------------------------------
# Keys
#-----------------------------------------------------------------
Expand All @@ -152,9 +151,10 @@ def request_info(request, parser) #:nodoc:
# {:aws_fingerprint=> "1e:29:30:47:58:6d:7b:8c:9f:08:11:20:3c:44:52:69:74:80:97:08", :aws_key_name=>"key-2"},
# ..., {...} ]
#
def describe_key_pairs(list=[])
link = generate_request("DescribeKeyPairs", amazonize_list('KeyName',list.to_a))
request_cache_or_info :describe_key_pairs, link, QEc2DescribeKeyPairParser, @@bench, list.blank?
def describe_key_pairs(*key_pairs)
key_pairs = key_pairs.flatten
link = generate_request("DescribeKeyPairs", amazonize_list('KeyName', key_pairs))
request_cache_or_info :describe_key_pairs, link, QEc2DescribeKeyPairParser, @@bench, key_pairs.blank?
rescue Exception
on_exception
end
Expand Down Expand Up @@ -224,10 +224,10 @@ def associate_address(instance_id, public_ip)
#
# ec2.describe_addresses('75.101.154.140') #=> [{:instance_id=>"i-d630cbbf", :public_ip=>"75.101.154.140"}]
#
def describe_addresses(list=[])
link = generate_request("DescribeAddresses",
amazonize_list('PublicIp',list.to_a))
request_cache_or_info :describe_addresses, link, QEc2DescribeAddressesParser, @@bench, list.blank?
def describe_addresses(*addresses)
addresses = addresses.flatten
link = generate_request("DescribeAddresses", amazonize_list('PublicIp', addresses))
request_cache_or_info :describe_addresses, link, QEc2DescribeAddressesParser, @@bench, addresses.blank?
rescue Exception
on_exception
end
Expand Down Expand Up @@ -273,10 +273,10 @@ def release_address(public_ip)
# :zone_state=>"available",
# :zone_name=>"us-east-1c"}]
#
def describe_availability_zones(list=[])
link = generate_request("DescribeAvailabilityZones",
amazonize_list('ZoneName',list.to_a))
request_cache_or_info :describe_availability_zones, link, QEc2DescribeAvailabilityZonesParser, @@bench, list.blank?
def describe_availability_zones(*availability_zones)
availability_zones = availability_zones.flatten
link = generate_request("DescribeAvailabilityZones", amazonize_list('ZoneName', availability_zones))
request_cache_or_info :describe_availability_zones, link, QEc2DescribeAvailabilityZonesParser, @@bench, availability_zones.blank?
rescue Exception
on_exception
end
Expand All @@ -289,10 +289,10 @@ def describe_availability_zones(list=[])
#
# ec2.describe_regions #=> ["eu-west-1", "us-east-1"]
#
def describe_regions(list=[])
link = generate_request("DescribeRegions",
amazonize_list('RegionName',list.to_a))
request_cache_or_info :describe_regions, link, QEc2DescribeRegionsParser, @@bench, list.blank?
def describe_regions(*regions)
regions = regions.flatten
link = generate_request("DescribeRegions", amazonize_list('RegionName', regions))
request_cache_or_info :describe_regions, link, QEc2DescribeRegionsParser, @@bench, regions.blank?
rescue Exception
on_exception
end
Expand Down
20 changes: 10 additions & 10 deletions lib/ec2/right_ec2_ebs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class Ec2
# :aws_id => "vol-58957031",
# :aws_created_at => Wed Jun 18 08:19:21 UTC 2008,}, ... ]
#
def describe_volumes(list=[])
link = generate_request("DescribeVolumes",
amazonize_list('VolumeId',list.to_a))
request_cache_or_info :describe_volumes, link, QEc2DescribeVolumesParser, @@bench, list.blank?
def describe_volumes(*volumes)
volumes = volumes.flatten
link = generate_request("DescribeVolumes", amazonize_list('VolumeId', volumes))
request_cache_or_info :describe_volumes, link, QEc2DescribeVolumesParser, @@bench, volumes.blank?
rescue Exception
on_exception
end
Expand Down Expand Up @@ -158,10 +158,10 @@ def detach_volume(volume_id, instance_id=nil, device=nil, force=nil)
# :aws_volume_size=>180,
# :aws_status=>"completed"}, ...]
#
def describe_snapshots(list=[])
link = generate_request("DescribeSnapshots",
amazonize_list('SnapshotId',list.to_a))
request_cache_or_info :describe_snapshots, link, QEc2DescribeSnapshotsParser, @@bench, list.blank?
def describe_snapshots(*snapshots)
snapshots = snapshots.flatten
link = generate_request("DescribeSnapshots", amazonize_list('SnapshotId', snapshots))
request_cache_or_info :describe_snapshots, link, QEc2DescribeSnapshotsParser, @@bench, snapshots.blank?
rescue Exception
on_exception
end
Expand Down Expand Up @@ -270,8 +270,8 @@ def modify_snapshot_attribute(snapshot_id, attribute='createVolumePermission', o
params = {'SnapshotId' => snapshot_id,
'Attribute' => attribute,
'OperationType' => operation_type}
params.update(amazonize_list('UserId', vars[:user_id].to_a)) if vars[:user_id]
params.update(amazonize_list('UserGroup', vars[:user_group].to_a)) if vars[:user_group]
params.update(amazonize_list('UserId', Array(vars[:user_id]))) if vars[:user_id]
params.update(amazonize_list('UserGroup', Array(vars[:user_group]))) if vars[:user_group]
link = generate_request("ModifySnapshotAttribute", params)
request_info(link, RightBoolResponseParser.new(:logger => @logger))
rescue Exception
Expand Down
42 changes: 21 additions & 21 deletions lib/ec2/right_ec2_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Ec2
def ec2_describe_images(params={}, image_type=nil, cache_for=nil) #:nodoc:
request_hash = {}
params.each do |list_by, list|
request_hash.merge! amazonize_list(list_by, list.to_a)
request_hash.merge! amazonize_list(list_by, Array(list))
end
request_hash['ImageType'] = image_type if image_type
link = generate_request("DescribeImages", request_hash)
Expand Down Expand Up @@ -80,21 +80,21 @@ def ec2_describe_images(params={}, image_type=nil, cache_for=nil) #:nodoc:
#
# ec2.describe_images(['ami-5aa1f74c'])
#
def describe_images(list=[], image_type=nil)
list = list.to_a
cache_for = list.empty? && !image_type ? :describe_images : nil
ec2_describe_images({ 'ImageId' => list }, image_type, cache_for)
def describe_images(images=[], image_type=nil)
images = Array(images)
cache_for = images.empty? && !image_type ? :describe_images : nil
ec2_describe_images({ 'ImageId' => images }, image_type, cache_for)
end

# Example:
#
# ec2.describe_images_by_owner('522821470517')
# ec2.describe_images_by_owner('self')
#
def describe_images_by_owner(list=['self'], image_type=nil)
list = list.to_a
cache_for = list==['self'] && !image_type ? :describe_images_by_owner : nil
ec2_describe_images({ 'Owner' => list }, image_type, cache_for)
def describe_images_by_owner(owners=['self'], image_type=nil)
owners = Array(owners)
cache_for = (owners == ['self']) && (!image_type ? :describe_images_by_owner : nil)
ec2_describe_images({ 'Owner' => owners }, image_type, cache_for)
end

# Example:
Expand All @@ -103,10 +103,10 @@ def describe_images_by_owner(list=['self'], image_type=nil)
# ec2.describe_images_by_executable_by('self')
# ec2.describe_images_by_executable_by('all')
#
def describe_images_by_executable_by(list=['self'], image_type=nil)
list = list.to_a
cache_for = list==['self'] && !image_type ? :describe_images_by_executable_by : nil
ec2_describe_images({ 'ExecutableBy' => list }, image_type, cache_for)
def describe_images_by_executable_by(executable_by=['self'], image_type=nil)
executable_by = Array(executable_by)
cache_for = (executable_by==['self']) && (!image_type ? :describe_images_by_executable_by : nil)
ec2_describe_images({ 'ExecutableBy' => executable_by }, image_type, cache_for)
end


Expand Down Expand Up @@ -209,9 +209,9 @@ def modify_image_attribute(image_id, attribute, operation_type = nil, vars = {})
params = {'ImageId' => image_id,
'Attribute' => attribute}
params['OperationType'] = operation_type if operation_type
params.update(amazonize_list('UserId', vars[:user_id].to_a)) if vars[:user_id]
params.update(amazonize_list('UserGroup', vars[:user_group].to_a)) if vars[:user_group]
params.update(amazonize_list('ProductCode', vars[:product_code])) if vars[:product_code]
params.update(amazonize_list('UserId', vars[:user_id])) if vars[:user_id]
params.update(amazonize_list('UserGroup', vars[:user_group])) if vars[:user_group]
params.update(amazonize_list('ProductCode', vars[:product_code])) if vars[:product_code]
link = generate_request("ModifyImageAttribute", params)
request_info(link, RightBoolResponseParser.new(:logger => @logger))
rescue Exception
Expand All @@ -224,15 +224,15 @@ def modify_image_attribute(image_id, attribute, operation_type = nil, vars = {})
#
# ec2.modify_image_launch_perm_add_users('ami-e444444d',['000000000777','000000000778']) #=> true
def modify_image_launch_perm_add_users(image_id, user_id=[])
modify_image_attribute(image_id, 'launchPermission', 'add', :user_id => user_id.to_a)
modify_image_attribute(image_id, 'launchPermission', 'add', :user_id => user_id)
end

# Revokes image launch permissions for users. +user_id+ is a list of users AWS accounts ids. Returns +true+ or an exception.
#
# ec2.modify_image_launch_perm_remove_users('ami-e444444d',['000000000777','000000000778']) #=> true
#
def modify_image_launch_perm_remove_users(image_id, user_id=[])
modify_image_attribute(image_id, 'launchPermission', 'remove', :user_id => user_id.to_a)
modify_image_attribute(image_id, 'launchPermission', 'remove', :user_id => user_id)
end

# Add image launch permissions for users groups (currently only 'all' is supported, which gives public launch permissions).
Expand All @@ -241,23 +241,23 @@ def modify_image_launch_perm_remove_users(image_id, user_id=[])
# ec2.modify_image_launch_perm_add_groups('ami-e444444d') #=> true
#
def modify_image_launch_perm_add_groups(image_id, user_group=['all'])
modify_image_attribute(image_id, 'launchPermission', 'add', :user_group => user_group.to_a)
modify_image_attribute(image_id, 'launchPermission', 'add', :user_group => user_group)
end

# Remove image launch permissions for users groups (currently only 'all' is supported, which gives public launch permissions).
#
# ec2.modify_image_launch_perm_remove_groups('ami-e444444d') #=> true
#
def modify_image_launch_perm_remove_groups(image_id, user_group=['all'])
modify_image_attribute(image_id, 'launchPermission', 'remove', :user_group => user_group.to_a)
modify_image_attribute(image_id, 'launchPermission', 'remove', :user_group => user_group)
end

# Add product code to image
#
# ec2.modify_image_product_code('ami-e444444d','0ABCDEF') #=> true
#
def modify_image_product_code(image_id, product_code=[])
modify_image_attribute(image_id, 'productCodes', nil, :product_code => product_code.to_a)
modify_image_attribute(image_id, 'productCodes', nil, :product_code => product_code)
end

# Create a new image.
Expand Down
21 changes: 12 additions & 9 deletions lib/ec2/right_ec2_instances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def get_desc_instances(instances) # :nodoc:
# :ebs_volume_id=>"vol-f900f990"}],
# :aws_instance_id=>"i-8ce84ae4"} , ... ]
#
def describe_instances(list=[])
link = generate_request("DescribeInstances", amazonize_list('InstanceId',list.to_a))
request_cache_or_info(:describe_instances, link, QEc2DescribeInstancesParser, @@bench, list.blank?) do |parser|
def describe_instances(*instances)
instances = instances.flatten
link = generate_request("DescribeInstances", amazonize_list('InstanceId', instances))
request_cache_or_info(:describe_instances, link, QEc2DescribeInstancesParser, @@bench, instances.blank?) do |parser|
get_desc_instances(parser.result)
end
rescue Exception
Expand Down Expand Up @@ -191,7 +192,7 @@ def run_instances(image_id, min_count, max_count, group_ids, key_name, user_data
#
def launch_instances(image_id, options={})
@logger.info("Launching instance of image #{image_id} for #{@aws_access_key_id}, " +
"key: #{options[:key_name]}, groups: #{(options[:group_ids]).to_a.join(',')}")
"key: #{options[:key_name]}, groups: #{Array(options[:group_ids]).join(',')}")
options[:image_id] = image_id
options[:min_count] ||= 1
options[:max_count] ||= options[:min_count]
Expand All @@ -204,7 +205,7 @@ def launch_instances(image_id, options={})
end

def prepare_instance_launch_params(options={}) # :nodoc:
params = amazonize_list('SecurityGroup', options[:group_ids].to_a)
params = amazonize_list('SecurityGroup', Array(options[:group_ids]))
params['InstanceType'] = options[:instance_type] || DEFAULT_INSTANCE_TYPE
params['ImageId'] = options[:image_id] unless options[:image_id].blank?
params['AddressingType'] = options[:addressing_type] unless options[:addressing_type].blank?
Expand Down Expand Up @@ -299,8 +300,9 @@ def get_console_output(instance_id)
#
# ec2.reboot_instances(['i-f222222d','i-f222222e']) #=> true
#
def reboot_instances(list)
link = generate_request("RebootInstances", amazonize_list('InstanceId', list.to_a))
def reboot_instances(*instances)
instances = instances.flatten
link = generate_request("RebootInstances", amazonize_list('InstanceId', instances))
request_info(link, RightBoolResponseParser.new(:logger => @logger))
rescue Exception
on_exception
Expand Down Expand Up @@ -495,8 +497,9 @@ def bundle_instance(instance_id, s3_bucket, s3_prefix,
# :aws_state => "failed",
# :aws_instance_id => "i-e3e24e8a"}]
#
def describe_bundle_tasks(list=[])
link = generate_request("DescribeBundleTasks", amazonize_list('BundleId', list.to_a))
def describe_bundle_tasks(*tasks)
tasks = tasks.flatten
link = generate_request("DescribeBundleTasks", amazonize_list('BundleId', tasks))
request_info(link, QEc2DescribeBundleTasksParser.new)
rescue Exception
on_exception
Expand Down
Loading

0 comments on commit 27eeb7b

Please sign in to comment.