Class: BitsService::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bits_service_client/client.rb

Constant Summary collapse

ResourceTypeNotPresent =
Class.new(StandardError)
ConfigurationError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(bits_service_options:, resource_type:, vcap_request_id: '', request_timeout_in_seconds: 900, request_timeout_in_seconds_fast: 10) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bits_service_client/client.rb', line 13

def initialize(bits_service_options:, resource_type:, vcap_request_id: '', request_timeout_in_seconds: 900, request_timeout_in_seconds_fast: 10)
  @username = validated(bits_service_options, :username)
  @password = validated(bits_service_options, :password)
  @private_endpoint = validated_http_url(bits_service_options, :private_endpoint)
  @public_endpoint = validated_http_url(bits_service_options, :public_endpoint)

  raise ResourceTypeNotPresent.new('Must specify resource type') unless resource_type
  @resource_type = resource_type
  @vcap_request_id = vcap_request_id

  @private_http_client = create_logging_http_client(@private_endpoint, bits_service_options, request_timeout_in_seconds)
  @private_http_client_fast_timeout = create_logging_http_client(@private_endpoint, bits_service_options, request_timeout_in_seconds_fast)
  @public_http_client = create_logging_http_client(@public_endpoint, bits_service_options, request_timeout_in_seconds)
end

Instance Method Details

#blob(key) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bits_service_client/client.rb', line 104

def blob(key)
  Blob.new(
    key: key,
    private_http_client: @private_http_client,
    private_endpoint: @private_endpoint,
    vcap_request_id: @vcap_request_id,
    username: @username,
    password: @password,
    resource_type: @resource_type
  )
end

#cp_file_between_keys(source_key, destination_key) ⇒ Object



90
91
92
93
94
# File 'lib/bits_service_client/client.rb', line 90

def cp_file_between_keys(source_key, destination_key)
  temp_downloaded_file = Tempfile.new('foo')
  download_from_blobstore(source_key, temp_downloaded_file.path)
  cp_to_blobstore(temp_downloaded_file.path, destination_key)
end

#cp_to_blobstore(source_path, destination_key, resources: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bits_service_client/client.rb', line 39

def cp_to_blobstore(source_path, destination_key, resources: nil)
  if source_path.to_s.empty?
    file = Tempfile.new(['empty', '.zip'])
    source_path = file.path
    file.close!
    Dir.mktmpdir do |dir|
      output, error, status = Open3.capture3(%(/usr/bin/zip #{source_path} #{dir}))
      unless status.success?
        logger.error("Could not create a zip with no contents.\n STDOUT: \"#{output}\"\n STDERR: \"#{error}\"")
        raise Errors::Error.new('Could not create a zip with no contents')
      end
    end
  end

  body = { :"#{@resource_type.to_s.singularize}" => UploadIO.new(source_path, 'application/octet-stream') }

  if !resources.nil?
    body[:resources] = resources.to_json
  end

  response = @private_http_client.do_request(Net::HTTP::Put::Multipart.new(resource_path(destination_key), body), @vcap_request_id)
  validate_response_code!(201, response)
  if response.body.nil?
    logger.error('UnexpectedMissingBody: expected body with json payload. Got empty body.')

    fail BlobstoreError.new({
      response_code: response.code,
      response_body: response.body,
      response: response
    }.to_json)
  end
  shas = JSON.parse(response.body, symbolize_names: true)
  validate_keys_present!(i[sha1 sha256], shas, response)
  shas
end

#delete(key) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/bits_service_client/client.rb', line 96

def delete(key)
  response = @private_http_client_fast_timeout.delete(resource_path(key), @vcap_request_id)
  validate_response_code!([204, 404], response)
  if response.code.to_i == 404
    raise FileNotFound.new("Could not find object '#{key}', #{response.code}/#{response.body}")
  end
end

#delete_all(_ = nil) ⇒ Object

Raises:

  • (NotImplementedError)


120
121
122
123
124
125
126
# File 'lib/bits_service_client/client.rb', line 120

def delete_all(_=nil)
  raise NotImplementedError unless resource_type == :buildpack_cache

  @private_http_client.delete(resource_path(''), @vcap_request_id).tap do |response|
    validate_response_code!(204, response)
  end
end

#delete_all_in_path(path) ⇒ Object

Raises:

  • (NotImplementedError)


128
129
130
131
132
133
134
# File 'lib/bits_service_client/client.rb', line 128

def delete_all_in_path(path)
  raise NotImplementedError unless resource_type == :buildpack_cache

  @private_http_client.delete(resource_path(path.to_s), @vcap_request_id).tap do |response|
    validate_response_code!(204, response)
  end
end

#delete_blob(blob) ⇒ Object



116
117
118
# File 'lib/bits_service_client/client.rb', line 116

def delete_blob(blob)
  delete(blob.guid)
end

#download_from_blobstore(source_key, destination_path, mode: nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bits_service_client/client.rb', line 75

def download_from_blobstore(source_key, destination_path, mode: nil)
  FileUtils.mkdir_p(File.dirname(destination_path))
  File.open(destination_path, 'wb') do |file|
    response = @private_http_client.get(resource_path(source_key), @vcap_request_id)

    if response.code == '302'
      response = Net::HTTP.get_response(URI(response['location']))
    end

    validate_response_code!(200, response)
    file.write(response.body)
    file.chmod(mode) if mode
  end
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/bits_service_client/client.rb', line 32

def exists?(key)
  response = @private_http_client_fast_timeout.head(resource_path(key), @vcap_request_id)
  validate_response_code!([200, 302, 404], response)

  response.code.to_i != 404
end

#local? ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/bits_service_client/client.rb', line 28

def local?
  false
end