Sha256: a1f185574adfae9df071f4fb07bd97c3042f4c87de390b1043118943cd1b9941

Contents?: true

Size: 1.92 KB

Versions: 118

Compression:

Stored size: 1.92 KB

Contents

# Copyright (c) 2009-2012 VMware, Inc.

require 'base64'
require 'httpclient'
require 'digest/sha1'

module Bosh
  module Blobstore
    class DavBlobstoreClient < BaseClient

      def initialize(options)
        super(options)
        @client = HTTPClient.new

        if @options[:ssl_no_verify]
          @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
          @client.ssl_config.verify_callback = proc {}
        end

        @endpoint = @options[:endpoint]
        @headers = {}
        user = @options[:user]
        password = @options[:password]
        if user && password
          @headers['Authorization'] = 'Basic ' +
            Base64.strict_encode64("#{user}:#{password}").strip
        end
      end

      def url(id)
        prefix = Digest::SHA1.hexdigest(id)[0, 2]

        [@endpoint, prefix, id].compact.join('/')
      end

      def create_file(id, file)
        id ||= generate_object_id

        response = @client.put(url(id), file, @headers)

        raise BlobstoreError, "Could not create object, #{response.status}/#{response.content}" if response.status != 201

        id
      end

      def get_file(id, file)
        response = @client.get(url(id), {}, @headers) do |block|
          file.write(block)
        end

        raise BlobstoreError, "Could not fetch object, #{response.status}/#{response.content}" if response.status != 200
      end

      def delete_object(id)
        response = @client.delete(url(id), header: @headers)

        raise BlobstoreError, "Could not delete object, #{response.status}/#{response.content}" if response.status != 204
      end

      def object_exists?(id)
        response = @client.head(url(id), header: @headers)
        if response.status == 200
          true
        elsif response.status == 404
          false
        else
          raise BlobstoreError, "Could not get object existence, #{response.status}/#{response.content}"
        end
      end
    end
  end
end

Version data entries

118 entries across 118 versions & 1 rubygems

Version Path
blobstore_client-1.2798.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2797.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2792.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2789.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2788.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2787.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2786.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2785.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2781.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2780.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2778.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2776.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2768.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2765.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2754.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2751.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2749.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2748.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2745.0 lib/blobstore_client/dav_blobstore_client.rb
blobstore_client-1.2744.0 lib/blobstore_client/dav_blobstore_client.rb