class Fog::Storage::Rackspace::Mock

Constants

HeaderOptions

Public Instance Methods

delete_container(name) click to toggle source
# File lib/fog/rackspace/requests/storage/delete_container.rb, line 23
def delete_container(name)
  c = mock_container! name

  raise Excon::Errors::Conflict.new 'Conflict' unless c.empty?
  remove_container name

  response = Excon::Response.new
  response.status = 204
  response
end
delete_multiple_objects(container, object_names, options = {}) click to toggle source
# File lib/fog/rackspace/requests/storage/delete_multiple_objects.rb, line 73
def delete_multiple_objects(container, object_names, options = {})
  results = {
    "Number Not Found" => 0,
    "Response Status" => "200 OK",
    "Response Body" => "",
    "Errors" => [],
    "Number Deleted" => 0
  }

  object_names.each do |name|
    if container
      cname, oname = container, name
    else
      cname, oname = name.split('/', 2)
    end

    c = mock_container cname
    if c.nil?
      # Container not found
      results["Number Not Found"] += 1
      next
    end

    if oname.nil?
      # No object name specified; delete the container if it's nonempty
      unless c.empty?
        results["Response Status"] = "400 Bad Request"
        results["Errors"] << [cname, "409 Conflict"]
        next
      end

      remove_container cname
      results["Number Deleted"] += 1
      next
    end

    o = c.mock_object oname
    if o.nil?
      # Object not found.
      results["Number Not Found"] += 1
      next
    end

    c.remove_object oname
    results["Number Deleted"] += 1
  end

  response = Excon::Response.new
  response.status = 200
  response.body = results
  response
end
delete_object(container, object) click to toggle source
# File lib/fog/rackspace/requests/storage/delete_object.rb, line 24
def delete_object(container, object)
  c = mock_container! container
  c.mock_object! object
  c.remove_object object

  response = Excon::Response.new
  response.status = 204
  response
end
delete_static_large_object(container, object, options = {}) click to toggle source
# File lib/fog/rackspace/requests/storage/delete_static_large_object.rb, line 47
def delete_static_large_object(container, object, options = {})
  c = mock_container container
  return not_found(container) unless c

  o = c.mock_object object
  return not_found(object) unless o

  # What happens if o isn't a static large object?
  raise Fog::Storage::Rackspace::BadRequest.new unless o.static_manifest?

  segments = Fog::JSON.decode(o.body)
  paths = segments.map { |s| s['path'] }
  paths << "#{container}/#{object}"
  delete_multiple_objects(nil, paths)
end
get_container(container, options = {}) click to toggle source
# File lib/fog/rackspace/requests/storage/get_container.rb, line 46
def get_container(container, options = {})
  c = mock_container! container

  results = []
  c.objects.each do |key, mock_file|
    results << {
      "hash" => mock_file.hash,
      "last_modified" => mock_file.last_modified.strftime('%Y-%m-%dT%H:%M:%S.%L'),
      "bytes" => mock_file.bytes_used,
      "name" => key,
      "content_type" => mock_file.content_type
    }
  end

  response = Excon::Response.new
  response.status = 200
  response.headers = c.to_headers
  response.body = results
  response
end
get_containers(options = {}) click to toggle source
# File lib/fog/rackspace/requests/storage/get_containers.rb, line 35
def get_containers(options = {})
  results = data.map do |name, container|
    {
      "name" => name,
      "count" => container.objects.size,
      "bytes" => container.bytes_used
    }
  end
  response = Excon::Response.new
  response.status = 200
  response.body = results
  response
end
get_object(container, object, &block) click to toggle source
# File lib/fog/rackspace/requests/storage/get_object.rb, line 30
def get_object(container, object, &block)
  c = mock_container! container
  o = c.mock_object! object

  body, size = "", 0

  o.each_part do |part|
    body << part.body
    size += part.bytes_used
  end

  if block_given?
    # Just send it all in one chunk.
    block.call(body, 0, size)
  end

  response = Excon::Response.new
  response.body = body
  response.headers = o.to_headers
  response
end
head_container(container) click to toggle source
# File lib/fog/rackspace/requests/storage/head_container.rb, line 30
def head_container(container)
  c = mock_container! container

  response = Excon::Response.new
  response.status = 204
  response.headers = c.to_headers
  response
end
head_containers() click to toggle source
# File lib/fog/rackspace/requests/storage/head_containers.rb, line 27
def head_containers
  bytes_used = data.values.map { |c| c.bytes_used }.reduce(0) { |a, b| a + b }
  container_count = data.size
  object_count = data.values.map { |c| c.objects.size }.reduce(0) { |a, b| a + b }

  response = Excon::Response.new
  response.status = 204
  response.headers = {
    'X-Account-Bytes-Used' => bytes_used,
    'X-Account-Container-Count' => container_count,
    'X-Account-Object-Count' => object_count
  }.merge(account_meta)
  response
end
head_object(container, object) click to toggle source
# File lib/fog/rackspace/requests/storage/head_object.rb, line 24
def head_object(container, object)
  c = mock_container! container
  o = c.mock_object! object

  headers = o.to_headers

  hashes, length = [], 0
  o.each_part do |part|
    hashes << part.hash
    length += part.bytes_used
  end

  headers['Etag'] = "\"#{Digest::MD5.hexdigest(hashes.join)}\""
  headers['Content-Length'] = length.to_s
  headers['X-Static-Large-Object'] = "True" if o.static_manifest?

  response = Excon::Response.new
  response.status = 200
  response.headers = headers
  response
end
not_found(path) click to toggle source
# File lib/fog/rackspace/requests/storage/delete_static_large_object.rb, line 63
def not_found(path)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "Number Not Found" => 1,
    "Response Status" => "200 OK",
    "Response Body" => "",
    "Errors" => [[path, "404 Not Found"]],
    "Number Deleted" => 0
  }
  response
end
post_set_meta_temp_url_key(key) click to toggle source
# File lib/fog/rackspace/requests/storage/post_set_meta_temp_url_key.rb, line 36
def post_set_meta_temp_url_key(key)
  account_meta['X-Account-Meta-Temp-Url-Key'] = key

  response = Excon::Response.new
  response.status = 204
  response
end
put_container(name, options={}) click to toggle source
# File lib/fog/rackspace/requests/storage/put_container.rb, line 24
def put_container(name, options={})
  existed = ! mock_container(name).nil?
  container = add_container(name)
  options.keys.each do |k|
    container.meta[k] = options[k].to_s if k =~ /^X-Container-Meta/
  end

  response = Excon::Response.new
  response.status = existed ? 202 : 201
  response
end
put_dynamic_obj_manifest(container, object, options = {}) click to toggle source
# File lib/fog/rackspace/requests/storage/put_dynamic_obj_manifest.rb, line 42
def put_dynamic_obj_manifest(container, object, options = {})
  path = "#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object)}"

  # Escape the X-Object-Manifest header to match.
  explicit_manifest = options['X-Object-Manifest']
  if explicit_manifest
    parts = explicit_manifest.split('/', 2)
    explicit_manifest = parts.map { |p| Fog::Rackspace.escape p }.join('/')
  end

  c = mock_container! container
  o = c.add_object object, ''
  o.meta['X-Object-Manifest'] = explicit_manifest || path

  response = Excon::Response.new
  response.status = 201
  response
end
put_object(container, object, data, options = {}) { || ... } click to toggle source
# File lib/fog/rackspace/requests/storage/put_object.rb, line 45
def put_object(container, object, data, options = {}, &block)
  c = mock_container! container

  if block_given?
    data = ""
    loop do
      chunk = yield
      break if chunk.empty?
      data << chunk
    end
  end

  o = c.add_object object, data
  options.keys.each do |k|
    o.meta[k] = options[k].to_s if k =~ /^X-Object-Meta/
    o.meta[k] = options[k] if HeaderOptions.include? k
  end

  # Validate the provided Etag
  etag = o.meta['Etag']
  if etag && etag != o.hash
    c.remove_object object
    raise Fog::Storage::Rackspace::ServiceError.new
  end

  response = Excon::Response.new
  response.status = 201
  response
end
put_static_obj_manifest(container, object, segments, options = {}) click to toggle source
# File lib/fog/rackspace/requests/storage/put_static_obj_manifest.rb, line 56
def put_static_obj_manifest(container, object, segments, options = {})
  c = mock_container! container

  # Verify paths.
  errors = []
  segments.each do |segment|
    cname, oname = segment[:path].split('/', 2)
    target_container = mock_container(cname)

    raise Fog::Storage::Rackspace::NotFound.new unless target_container

    target_object = target_container.mock_object oname
    unless target_object
      errors << [segment[:path], '404 Not Found']
      next
    end

    unless target_object.hash == segment[:etag]
      errors << [segment[:path], 'Etag Mismatch']
    end

    unless target_object.bytes_used == segment[:size_bytes]
      errors << [segment[:path], 'Size Mismatch']
    end
  end

  unless errors.empty?
    response = Excon::Response.new
    response.status = 400
    response.body = Fog::JSON.encode({ 'Errors' => errors })

    error = Excon::Errors.status_error({}, response)
    raise Fog::Storage::Rackspace::BadRequest.slurp(error)
  end

  data = Fog::JSON.encode(segments)
  o = c.add_object object, data
  o.static_manifest = true

  response = Excon::Response.new
  response.status = 201
  response
end