module Fog::AWS

Constants

VERSION

Public Class Methods

escape(string) click to toggle source
# File lib/fog/aws.rb, line 136
def self.escape(string)
  string.gsub(/([^a-zA-Z0-9_.\-~]+)/) {
    "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase
  }
end
indexed_filters(filters) click to toggle source
# File lib/fog/aws.rb, line 123
def self.indexed_filters(filters)
  params = {}
  filters.keys.each_with_index do |key, key_index|
    key_index += 1
    params[format('Filter.%d.Name', key_index)] = key
    [*filters[key]].each_with_index do |value, value_index|
      value_index += 1
      params[format('Filter.%d.Value.%d', key_index, value_index)] = value
    end
  end
  params
end
indexed_param(key, values) click to toggle source
# File lib/fog/aws.rb, line 80
def self.indexed_param(key, values)
  params = {}
  unless key.include?('%d')
    key << '.%d'
  end
  [*values].each_with_index do |value, index|
    if value.respond_to?('keys')
      k = format(key, index + 1)
      value.each do | vkey, vvalue |
        params["#{k}.#{vkey}"] = vvalue
      end
    else
      params[format(key, index + 1)] = value
    end
  end
  params
end
indexed_request_param(name, values) click to toggle source
# File lib/fog/aws.rb, line 115
def self.indexed_request_param(name, values)
  idx = -1
  Array(values).reduce({}) do |params, value|
    params["#{name}.#{idx += 1}"] = value
    params
  end
end
json_response?(response) click to toggle source
# File lib/fog/aws.rb, line 216
def self.json_response?(response)
  return false unless response && response.headers
  response.get_header('Content-Type') =~ %r{application/.*json.*}i ? true : false
end
parse_security_group_options(group_name, options) click to toggle source
# File lib/fog/aws.rb, line 194
def self.parse_security_group_options(group_name, options)
  options ||= Hash.new
  if group_name.is_a?(Hash)
    options = group_name
  elsif group_name
    if options.key?('GroupName')
      raise Fog::AWS::Compute::Error, 'Arguments specified both group_name and GroupName in options'
    end
    options = options.clone
    options['GroupName'] = group_name
  end
  name_specified = options.key?('GroupName') && !options['GroupName'].nil?
  group_id_specified = options.key?('GroupId') && !options['GroupId'].nil?
  unless name_specified || group_id_specified
    raise Fog::AWS::Compute::Error, 'Neither GroupName nor GroupId specified'
  end
  if name_specified && group_id_specified
    options.delete('GroupName')
  end
  options
end
regions() click to toggle source
# File lib/fog/aws.rb, line 221
def self.regions
  @regions ||= [
    'af-south-1',
    'ap-east-1',
    'ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3',
    'ap-south-1',
    'ap-southeast-1', 'ap-southeast-2',
    'ca-central-1',
    'cn-north-1',
    'cn-northwest-1',
    'eu-central-1',
    'eu-north-1',
    'eu-west-1', 'eu-west-2', 'eu-west-3', 'eu-south-1',
    'me-south-1',
    'us-east-1', 'us-east-2',
    'us-west-1', 'us-west-2',
    'sa-east-1',
    'us-gov-east-1',
    'us-gov-west-1'
  ]
end
serialize_keys(key, value, options = {}) click to toggle source
# File lib/fog/aws.rb, line 98
def self.serialize_keys(key, value, options = {})
  case value
  when Hash
    value.each do | k, v |
      options.merge!(serialize_keys("#{key}.#{k}", v))
    end
    return options
  when Array
    value.each_with_index do | it, idx |
      options.merge!(serialize_keys("#{key}.member.#{(idx + 1)}", it))
    end
    return options
  else
    return {key => value}
  end
end
signed_params(params, options = {}) click to toggle source
# File lib/fog/aws.rb, line 168
def self.signed_params(params, options = {})
  params.merge!({
    'AWSAccessKeyId'    => options[:aws_access_key_id],
    'SignatureMethod'   => 'HmacSHA256',
    'SignatureVersion'  => '2',
    'Timestamp'         => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
    'Version'           => options[:version]
  })

  params.merge!({
    'SecurityToken'     => options[:aws_session_token]
  }) if options[:aws_session_token]

  body = ''
  for key in params.keys.sort
    unless (value = params[key]).nil?
      body << "#{key}=#{escape(value.to_s)}&"
    end
  end
  string_to_sign = "POST\n#{options[:host]}:#{options[:port]}\n#{options[:path]}\n" << body.chop
  signed_string = options[:hmac].sign(string_to_sign)
  body << "Signature=#{escape(Base64.encode64(signed_string).chomp!)}"

  body
end
signed_params_v4(params, headers, options={}) click to toggle source
# File lib/fog/aws.rb, line 142
def self.signed_params_v4(params, headers, options={})
  date = Fog::Time.now

  params = params.merge('Version' => options[:version])

  headers = headers.merge('Host' => options[:host], 'x-amz-date' => date.to_iso8601_basic)
  headers['x-amz-security-token'] = options[:aws_session_token] if options[:aws_session_token]
  query = options[:query] || {}

  if !options[:body]
    body = ''
    for key in params.keys.sort
      unless (value = params[key]).nil?
        body << "#{key}=#{escape(value.to_s)}&"
      end
    end
    body.chop!
  else
    body = options[:body]
  end

  headers['Authorization'] = options[:signer].sign({:method => options[:method], :headers => headers, :body => body, :query => query, :path => options[:path]}, date)

  return body, headers
end
validate_region!(region, host=nil) click to toggle source
# File lib/fog/aws.rb, line 243
def self.validate_region!(region, host=nil)
  if (!host || host.end_with?('.amazonaws.com')) && !regions.include?(region)
    raise ArgumentError, "Unknown region: #{region.inspect}"
  end
end