class Fog::AWS::Storage::Mock

Public Class Methods

acls(type) click to toggle source
# File lib/fog/aws/storage.rb, line 380
def self.acls(type)
  case type
  when 'private'
    {
      "AccessControlList" => [
        {
          "Permission" => "FULL_CONTROL",
          "Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
        }
      ],
      "Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  when 'public-read'
    {
      "AccessControlList" => [
        {
          "Permission" => "FULL_CONTROL",
          "Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
        },
        {
          "Permission" => "READ",
          "Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AllUsers"}
        }
      ],
      "Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  when 'public-read-write'
    {
      "AccessControlList" => [
        {
          "Permission" => "FULL_CONTROL",
          "Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
        },
        {
          "Permission" => "READ",
          "Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AllUsers"}
        },
        {
          "Permission" => "WRITE",
          "Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AllUsers"}
        }
      ],
      "Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  when 'authenticated-read'
    {
      "AccessControlList" => [
        {
          "Permission" => "FULL_CONTROL",
          "Grantee" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
        },
        {
          "Permission" => "READ",
          "Grantee" => {"URI" => "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"}
        }
      ],
      "Owner" => {"DisplayName" => "me", "ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  end
end
data() click to toggle source
# File lib/fog/aws/storage.rb, line 441
def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :acls => {
          :bucket => {},
          :object => {}
        },
        :buckets => {},
        :cors => {
          :bucket => {}
        },
        :bucket_notifications => {},
        :bucket_tagging => {},
        :multipart_uploads => {}
      }
    end
  end
end
new(options={}) click to toggle source
# File lib/fog/aws/storage.rb, line 465
def initialize(options={})
  require_mime_types

  @use_iam_profile = options[:use_iam_profile]

  @region = options[:region] || DEFAULT_REGION

  if @endpoint = options[:endpoint]
    endpoint = URI.parse(@endpoint)
    @host = endpoint.host
    @scheme = endpoint.scheme
    @port = endpoint.port
  else
    @host       = options[:host]        || region_to_host(@region)
    @scheme     = options[:scheme]      || DEFAULT_SCHEME
    @port       = options[:port]        || DEFAULT_SCHEME_PORT[@scheme]
  end


  @path_style = options[:path_style] || false

  init_max_put_chunk_size!(options)
  init_max_copy_chunk_size!(options)

  @signature_version = options.fetch(:aws_signature_version, 4)
  validate_signature_version!
  setup_credentials(options)
end
reset() click to toggle source
# File lib/fog/aws/storage.rb, line 461
def self.reset
  @data = nil
end

Public Instance Methods

data() click to toggle source
# File lib/fog/aws/storage.rb, line 494
def data
  self.class.data[@region][@aws_access_key_id]
end
delete_bucket_policy(bucket_name) click to toggle source
# File lib/fog/aws/requests/storage/delete_bucket_policy.rb, line 26
def delete_bucket_policy(bucket_name)
   if bucket = data[:buckets][bucket_name]
     bucket[:policy] = nil

     Excon::Response.new.tap do |response|
       response.body = { 'RequestId' => Fog::AWS::Mock.request_id }
       response.status = 200
     end
   else
     raise(Excon::Errors.status_error({:expects => 200}, response))
   end
end
get_bucket_notification(bucket_name) click to toggle source
# File lib/fog/aws/requests/storage/get_bucket_notification.rb, line 45
def get_bucket_notification(bucket_name)
  response = Excon::Response.new

  if self.data[:buckets][bucket_name] && self.data[:bucket_notifications][bucket_name]
    response.status = 200
    response.body = self.data[:bucket_notifications][bucket_name]
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end
get_bucket_object_versions(bucket_name, options = {}) click to toggle source
# File lib/fog/aws/requests/storage/get_bucket_object_versions.rb, line 66
def get_bucket_object_versions(bucket_name, options = {})
  delimiter, key_marker, max_keys, prefix, version_id_marker = \
    options['delimiter'], options['key-marker'], options['max-keys'],options['prefix'],options['version-id-marker']

  unless bucket_name
    raise ArgumentError.new('bucket_name is required')
  end

  response = Excon::Response.new

  # Invalid arguments.
  if version_id_marker && !key_marker
    response.status = 400
    response.body = {
      'Error' => {
        'Code' => 'InvalidArgument',
        'Message' => 'A version-id marker cannot be specified without a key marker.',
        'ArgumentValue' => version_id_marker,
        'RequestId' => Fog::Mock.random_hex(16),
        'HostId' => Fog::Mock.random_base64(65)
      }
    }
    raise(Excon::Errors.status_error({:expects => 200}, response))

  # Valid case.
  # TODO: (nirvdrum 12/15/11) It's not clear to me how to actually use version-id-marker, so I didn't implement it below.
  elsif bucket = self.data[:buckets][bucket_name]
    # We need to order results by S3 key, but since our data store is key => [versions], we want to ensure the integrity
    # of the versions as well.  So, sort the keys, then fetch the versions, and then combine them all as a sorted list by
    # flattening the results.
    contents = bucket[:objects].keys.sort.map { |key| bucket[:objects][key] }.flatten.reject do |object|
        (prefix      && object['Key'][0...prefix.length] != prefix) ||
        (key_marker  && object['Key'] <= key_marker) ||
        (delimiter   && object['Key'][(prefix ? prefix.length : 0)..-1].include?(delimiter) \
                     && common_prefixes << object['Key'].sub(/^(#{prefix}[^#{delimiter}]+.).*/, '\1'))
      end.map do |object|
        if object.key?(:delete_marker)
          tag_name = 'DeleteMarker'
          extracted_attrs = ['Key', 'VersionId']
        else
          tag_name = 'Version'
          extracted_attrs = ['ETag', 'Key', 'StorageClass', 'VersionId']
        end

        data = {}
        data[tag_name] = object.reject { |key, value| !extracted_attrs.include?(key) }
        data[tag_name].merge!({
          'LastModified' => Time.parse(object['Last-Modified']),
          'Owner'        => bucket['Owner'],
          'IsLatest'     => object == bucket[:objects][object['Key']].first
        })

        data[tag_name]['Size'] = object['Content-Length'].to_i if tag_name == 'Version'
      data
    end

    max_keys = max_keys || 1000
    size = [max_keys, 1000].min
    truncated_contents = contents[0...size]

    response.status = 200
    response.body = {
      'Versions'        => truncated_contents,
      'IsTruncated'     => truncated_contents.size != contents.size,
      'KeyMarker'       => key_marker,
      'VersionIdMarker' => version_id_marker,
      'MaxKeys'         => max_keys,
      'Name'            => bucket['Name'],
      'Prefix'          => prefix
    }
    if max_keys && max_keys < response.body['Versions'].length
        response.body['IsTruncated'] = true
        response.body['Versions'] = response.body['Versions'][0...max_keys]
    end

  # Missing bucket case.
  else
    response.status = 404
    response.body = {
      'Error' => {
        'Code' => 'NoSuchBucket',
        'Message' => 'The specified bucket does not exist',
        'BucketName' => bucket_name,
        'RequestId' => Fog::Mock.random_hex(16),
        'HostId' => Fog::Mock.random_base64(65)
      }
    }

    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end
get_bucket_versioning(bucket_name) click to toggle source
# File lib/fog/aws/requests/storage/get_bucket_versioning.rb, line 35
def get_bucket_versioning(bucket_name)
  response = Excon::Response.new
  bucket = self.data[:buckets][bucket_name]

  if bucket
    response.status = 200

    if bucket[:versioning]
      response.body = { 'VersioningConfiguration' => { 'Status' => bucket[:versioning] } }
    else
      response.body = { 'VersioningConfiguration' => {} }
    end

  else
    response.status = 404
    response.body = {
      'Error' => {
        'Code' => 'NoSuchBucket',
        'Message' => 'The specified bucket does not exist',
        'BucketName' => bucket_name,
        'RequestId' => Fog::Mock.random_hex(16),
        'HostId' => Fog::Mock.random_base64(65)
      }
    }

    raise(Excon::Errors.status_error({:expects => 200}, response))
  end

  response
end
put_bucket_acl(bucket_name, acl) click to toggle source
# File lib/fog/aws/requests/storage/put_bucket_acl.rb, line 56
def put_bucket_acl(bucket_name, acl)
  if acl.is_a?(Hash)
    self.data[:acls][:bucket][bucket_name] = Fog::AWS::Storage.hash_to_acl(acl)
  else
    if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
      raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
    end
    self.data[:acls][:bucket][bucket_name] = acl
  end
end
put_bucket_cors(bucket_name, cors) click to toggle source
# File lib/fog/aws/requests/storage/put_bucket_cors.rb, line 41
def put_bucket_cors(bucket_name, cors)
  self.data[:cors][:bucket][bucket_name] = Fog::AWS::Storage.hash_to_cors(cors)
end
put_bucket_notification(bucket_name, notification) click to toggle source
# File lib/fog/aws/requests/storage/put_bucket_notification.rb, line 67
def put_bucket_notification(bucket_name, notification)
  response = Excon::Response.new

  if self.data[:buckets][bucket_name]
    self.data[:bucket_notifications][bucket_name] = notification
    response.status = 204
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 204}, response))
  end

  response
end
put_bucket_policy(bucket_name, policy) click to toggle source

FIXME: You can't actually use the credentials for anything elsewhere in Fog FIXME: Doesn't do any validation on the policy

# File lib/fog/aws/requests/storage/put_bucket_policy.rb, line 27
def put_bucket_policy(bucket_name, policy)
  if bucket = data[:buckets][bucket_name]
    bucket[:policy] = policy

    Excon::Response.new.tap do |response|
      response.body = { 'RequestId' => Fog::AWS::Mock.request_id }
      response.status = 200
    end
  else
    raise Fog::AWS::IAM::NotFound.new("The bucket with name #{bucket_name} cannot be found.")
  end
end
put_bucket_versioning(bucket_name, status) click to toggle source
# File lib/fog/aws/requests/storage/put_bucket_versioning.rb, line 32
def put_bucket_versioning(bucket_name, status)
  response = Excon::Response.new
  bucket = self.data[:buckets][bucket_name]

  if bucket
    if ['Enabled', 'Suspended'].include?(status)
      bucket[:versioning] = status

      response.status = 200
    else
      response.status = 400
      response.body = {
        'Error' => {
          'Code' => 'MalformedXML',
          'Message' => 'The XML you provided was not well-formed or did not validate against our published schema',
          'RequestId' => Fog::Mock.random_hex(16),
          'HostId' => Fog::Mock.random_base64(65)
        }
      }

      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  else
    response.status = 404
    response.body = {
      'Error' => {
        'Code' => 'NoSuchBucket',
        'Message' => 'The specified bucket does not exist',
        'BucketName' => bucket_name,
        'RequestId' => Fog::Mock.random_hex(16),
        'HostId' => Fog::Mock.random_base64(65)
      }
    }

    raise(Excon::Errors.status_error({:expects => 200}, response))
  end

  response
end
put_object_acl(bucket_name, object_name, acl, options = {}) click to toggle source
# File lib/fog/aws/requests/storage/put_object_acl.rb, line 65
def put_object_acl(bucket_name, object_name, acl, options = {})
  if acl.is_a?(Hash)
    self.data[:acls][:object][bucket_name][object_name] = Fog::AWS::Storage.hash_to_acl(acl)
  else
    if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
      raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
    end
    self.data[:acls][:object][bucket_name][object_name] = acl
  end
end
reset_data() click to toggle source
# File lib/fog/aws/storage.rb, line 498
def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end
setup_credentials(options) click to toggle source
# File lib/fog/aws/storage.rb, line 502
def setup_credentials(options)
  @aws_credentials_refresh_threshold_seconds = options[:aws_credentials_refresh_threshold_seconds]

  @aws_access_key_id = options[:aws_access_key_id]
  @aws_secret_access_key = options[:aws_secret_access_key]
  @aws_session_token     = options[:aws_session_token]
  @aws_credentials_expire_at = options[:aws_credentials_expire_at]

  @signer = Fog::AWS::SignatureV4.new( @aws_access_key_id, @aws_secret_access_key, @region, 's3')
end
signature_v2(params, expires) click to toggle source
# File lib/fog/aws/storage.rb, line 513
def signature_v2(params, expires)
  'foo'
end