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
27
28
# 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)
  @signing_key_secret = validated(bits_service_options, :signing_key_secret)
  @signing_key_id = validated(bits_service_options, :signing_key_id)

  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



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

def blob(key)
  Blob.new(
    key: key,
    private_http_client: @private_http_client,
    private_endpoint: @private_endpoint,
    vcap_request_id: @vcap_request_id,
    resource_type: @resource_type,
    public_endpoint: @public_endpoint,
    signing_key_secret: @signing_key_secret,
    signing_key_id: @signing_key_id,
  )
end

#cp_file_between_keys(source_key, destination_key) ⇒ Object



92
93
94
95
96
# File 'lib/bits_service_client/client.rb', line 92

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



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
74
75
# File 'lib/bits_service_client/client.rb', line 41

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



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

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)


123
124
125
126
127
128
129
# File 'lib/bits_service_client/client.rb', line 123

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)


131
132
133
134
135
136
137
# File 'lib/bits_service_client/client.rb', line 131

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



119
120
121
# File 'lib/bits_service_client/client.rb', line 119

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

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



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

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)


34
35
36
37
38
39
# File 'lib/bits_service_client/client.rb', line 34

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)


30
31
32
# File 'lib/bits_service_client/client.rb', line 30

def local?
  false
end