Class: Etna::Clients::Metis

Inherits:
BaseClient show all
Defined in:
lib/etna/clients/metis/client.rb,
lib/etna/clients/metis/models.rb,
lib/etna/clients/metis/workflows/walk_metis_workflow.rb,
lib/etna/clients/metis/workflows/metis_upload_workflow.rb,
lib/etna/clients/metis/workflows/metis_download_workflow.rb,
lib/etna/clients/metis/workflows/sync_metis_data_workflow.rb,
lib/etna/clients/metis/workflows/walk_metis_diff_workflow.rb,
lib/etna/clients/metis/workflows/ingest_metis_data_workflow.rb

Defined Under Namespace

Classes: AuthorizeDownloadRequest, AuthorizeUploadRequest, CopyFilesRequest, CopyRevision, CreateFolderRequest, DeleteFileRequest, DeleteFolderRequest, DownloadResponse, File, Files, FilesResponse, FindParam, FindRequest, Folder, FolderRequest, Folders, FoldersAndFilesResponse, FoldersResponse, GetByteCountByProjectRequest, GetFileCountByProjectRequest, IngestMetisDataWorkflow, ListFolderByIdRequest, ListFolderRequest, ListFoldersRequest, MetisDownloadWorkflow, MetisUploadWorkflow, RenameFileRequest, RenameFolderRequest, SyncMetisDataWorkflow, TailBucketRequest, TailNode, TailResponse, TouchFileRequest, TouchFolderRequest, UploadAction, UploadBlobRequest, UploadResponse, UploadStartRequest, WalkMetisDiffWorkflow, WalkMetisWorkflow

Instance Attribute Summary

Attributes inherited from BaseClient

#host, #ignore_ssl, #token

Instance Method Summary collapse

Methods inherited from BaseClient

#safe_parse, #token_expired?, #token_will_expire?

Constructor Details

#initialize(host:, token:, ignore_ssl: false, logger: nil) ⇒ Metis

Returns a new instance of Metis.



13
14
15
16
17
18
19
# File 'lib/etna/clients/metis/client.rb', line 13

def initialize(host:, token:, ignore_ssl: false, logger: nil)
  raise 'Metis client configuration is missing host.' unless host
  raise 'Metis client configuration is missing token.' unless token
  @etna_client = ::Etna::Client.new(host, token, ignore_ssl: ignore_ssl, logger: logger)

  @token = token
end

Instance Method Details

#authorize_download(authorize_download_request = AuthorizeDownloadRequest.new) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/etna/clients/metis/client.rb', line 135

def authorize_download(authorize_download_request = AuthorizeDownloadRequest.new)
  json = nil
  @etna_client.post("/authorize/download", authorize_download_request) do |res|
    json = JSON.parse(res.body)
  end

  DownloadResponse.new(json)
end

#authorize_upload(authorize_upload_request = AuthorizeUploadRequest.new) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/etna/clients/metis/client.rb', line 170

def authorize_upload(authorize_upload_request = AuthorizeUploadRequest.new)
  json = nil
  @etna_client.post("/authorize/upload", authorize_upload_request) do |res|
    json = JSON.parse(res.body)
  end

  UploadResponse.new(json)
end

#copy_files(copy_files_request) ⇒ Object



188
189
190
191
# File 'lib/etna/clients/metis/client.rb', line 188

def copy_files(copy_files_request)
  FilesResponse.new(
    @etna_client.file_bulk_copy(copy_files_request.to_h))
end

#create_folder(create_folder_request) ⇒ Object



92
93
94
95
# File 'lib/etna/clients/metis/client.rb', line 92

def create_folder(create_folder_request)
  FoldersResponse.new(
    @etna_client.folder_create(create_folder_request.to_h))
end

#delete_file(delete_file_request) ⇒ Object



102
103
104
105
# File 'lib/etna/clients/metis/client.rb', line 102

def delete_file(delete_file_request)
  FilesResponse.new(
    @etna_client.file_remove(delete_file_request.to_h))
end

#delete_folder(delete_folder_request) ⇒ Object



97
98
99
100
# File 'lib/etna/clients/metis/client.rb', line 97

def delete_folder(delete_folder_request)
  FoldersResponse.new(
    @etna_client.folder_remove(delete_folder_request.to_h))
end

#download_file(file_or_url = File.new, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/etna/clients/metis/client.rb', line 112

def download_file(file_or_url = File.new, &block)
  if file_or_url.instance_of?(File)
    download_url = file_or_url.download_url

    if !download_url.present?
      download = authorize_download(AuthorizeDownloadRequest.new(
        project_name: file_or_url.project_name,
        bucket_name: file_or_url.bucket_name,
        file_path: file_or_url.file_path
      ))
      download_url = download.download_url
    end
  else
    download_url = file_or_url
  end

  download_path = download_url.sub(%r!^https://[^/]*?/!, '/')

  @etna_client.get(download_path) do |response|
    response.read_body(&block)
  end
end

#ensure_parent_folder_exists(project_name:, bucket_name:, path:) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/etna/clients/metis/client.rb', line 61

def ensure_parent_folder_exists(project_name:, bucket_name:, path:)
  create_folder_request = CreateFolderRequest.new(
    project_name: project_name,
    bucket_name: bucket_name,
    folder_path: parent_folder_path(path)
  )
  create_folder(create_folder_request) if !folder_exists?(create_folder_request)
end

#file_metadata(file_or_url = File.new) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/etna/clients/metis/client.rb', line 144

def (file_or_url = File.new)
  if file_or_url.instance_of?(File)
    download_path =  file_or_url.download_path
  else
    download_path = file_or_url.sub(%r!^https://[^/]*?/!, '/')
  end

  # Do not actually consume the data, however.
  # TODO: implement HEAD requests in metis through apache.
  @etna_client.get(download_path) do |response|
    return {
        etag: response['ETag'].gsub(/"/, ''),
        size: response['Content-Length'].to_i,
    }
  end
end

#find(find_request) ⇒ Object



107
108
109
110
# File 'lib/etna/clients/metis/client.rb', line 107

def find(find_request)
  FoldersAndFilesResponse.new(
    @etna_client.bucket_find(find_request.to_h))
end

#folder_exists?(create_folder_request) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/etna/clients/metis/client.rb', line 193

def folder_exists?(create_folder_request)
  # NOTE: this doesn't test if the folder_path itself exists
  #   This can be confusing for root folders, because
  #       they have no parents, so you don't need
  #       to create anything.
  return true if create_folder_request.folder_path.empty? # root folder

  # returns 422 if the folder_path does not exist
  begin
    list_folder(
        Etna::Clients::Metis::ListFolderRequest.new(
          project_name: create_folder_request.project_name,
          bucket_name: create_folder_request.bucket_name,
          folder_path: create_folder_request.folder_path
      ))
  rescue Etna::Error => e
      return false if e.status == 422
      raise
  end
  return true
end

#folders(project_name:, bucket_name:) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/etna/clients/metis/client.rb', line 215

def folders(project_name:, bucket_name:)
  @folders ||= Hash.new { |h, key|
    h[key] = list_all_folders(
      Etna::Clients::Metis::ListFoldersRequest.new(
        project_name: project_name,
        bucket_name: key
      )).folders.all
  }

  @folders[bucket_name]
end

#get_byte_count_by_project(get_byte_count_by_project_request = GetByteCountByProjectRequest.new) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/etna/clients/metis/client.rb', line 376

def get_byte_count_by_project(get_byte_count_by_project_request = GetByteCountByProjectRequest.new)
  query = ""
  unless get_byte_count_by_project_request.project_names.nil?
    query = "?#{create_query_list_str('projects', get_byte_count_by_project_request.project_names)}"
  end

  json = nil
  @etna_client.get("/api/stats/bytes#{query}") do |res|
    json = JSON.parse(res.body, symbolize_names: true)
  end

  json
end

#get_file_count_by_project(get_file_count_by_project_request = GetFileCountByProjectRequest.new) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/etna/clients/metis/client.rb', line 362

def get_file_count_by_project(get_file_count_by_project_request = GetFileCountByProjectRequest.new)
  query = ""
  unless get_file_count_by_project_request.project_names.nil?
    query = "?#{create_query_list_str('projects', get_file_count_by_project_request.project_names)}"
  end

  json = nil
  @etna_client.get("/api/stats/files#{query}") do |res|
    json = JSON.parse(res.body, symbolize_names: true)
  end

  json
end

#list_all_folders(list_all_folders_request = ListFoldersRequest.new) ⇒ Object



21
22
23
24
# File 'lib/etna/clients/metis/client.rb', line 21

def list_all_folders(list_all_folders_request = ListFoldersRequest.new)
  FoldersResponse.new(
    @etna_client.folder_list_all_folders(list_all_folders_request.to_h))
end

#list_folder(list_folder_request = ListFolderRequest.new) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/etna/clients/metis/client.rb', line 36

def list_folder(list_folder_request = ListFolderRequest.new)
  if list_folder_request.folder_path != ''
    FoldersAndFilesResponse.new(
      @etna_client.folder_list(list_folder_request.to_h))
  else
    FoldersAndFilesResponse.new(
      @etna_client.bucket_list(list_folder_request.to_h))
  end
end

#list_folder_by_id(list_folder_by_id_request = ListFolderByIdRequest.new) ⇒ Object



46
47
48
49
# File 'lib/etna/clients/metis/client.rb', line 46

def list_folder_by_id(list_folder_by_id_request = ListFolderByIdRequest.new)
  FoldersAndFilesResponse.new(
    @etna_client.folder_list_by_id(list_folder_by_id_request.to_h))
end

#recursively_rename_folder(project_name:, source_bucket:, dest_bucket:, folder:) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/etna/clients/metis/client.rb', line 318

def recursively_rename_folder(project_name:, source_bucket:, dest_bucket:, folder:)
  folder_contents = list_folder(
    Etna::Clients::Metis::ListFolderRequest.new(
      project_name: project_name,
      bucket_name: source_bucket,
      folder_path: folder.folder_path
  ))

  folder_contents.folders.all.each do |sub_folder|
    recursively_rename_folder(
      project_name: project_name,
      source_bucket: source_bucket,
      dest_bucket: dest_bucket,
      folder: sub_folder
    )
  end

  folder_contents.files.all.each do |file|
    should_rename = resolve_conflicts_and_verify_rename(
      project_name: project_name,
      source_bucket: source_bucket,
      dest_bucket: dest_bucket,
      folder: folder,
      file: file)

    rename_file(Etna::Clients::Metis::RenameFileRequest.new(
      bucket_name: source_bucket,
      project_name: project_name,
      file_path: file.file_path,
      new_bucket_name: dest_bucket,
      new_file_path: file.file_path,
      create_parent: true)
    ) if should_rename
  end

  # Now delete the source folder
  delete_folder(
    Etna::Clients::Metis::DeleteFolderRequest.new(
      project_name: project_name,
      bucket_name: source_bucket,
      folder_path: folder.folder_path
  ))
end

#rename_file(rename_file_request) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/etna/clients/metis/client.rb', line 81

def rename_file(rename_file_request)
  ensure_parent_folder_exists(
    project_name: rename_file_request.project_name,
    bucket_name: rename_file_request.new_bucket_name,
    path: rename_file_request.new_file_path # ensure_parent_folder_exists() parses this for the parent path
  ) if rename_file_request.create_parent

  FilesResponse.new(
    @etna_client.file_rename(rename_file_request.to_h))
end

#rename_folder(rename_folder_request) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/etna/clients/metis/client.rb', line 70

def rename_folder(rename_folder_request)
  ensure_parent_folder_exists(
    project_name: rename_folder_request.project_name,
    bucket_name: rename_folder_request.new_bucket_name,
    path: rename_folder_request.new_folder_path
  ) if rename_folder_request.create_parent

  FoldersResponse.new(
    @etna_client.folder_rename(rename_folder_request.to_h))
end

#rename_folders(project_name:, source_bucket:, source_folders:, dest_bucket:) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/etna/clients/metis/client.rb', line 242

def rename_folders(project_name:, source_bucket:, source_folders:, dest_bucket:)
  source_folders.each { |folder|
    # If the destination folder already exists, we need to copy the files
    #   over to it and delete the source folder.
    create_folder_request = CreateFolderRequest.new(
      project_name: project_name,
      bucket_name: dest_bucket,
      folder_path: folder.folder_path
    )

    if folder_exists?(create_folder_request)
      recursively_rename_folder(
        project_name: project_name,
        source_bucket: source_bucket,
        dest_bucket: dest_bucket,
        folder: folder
      )
    else
      rename_folder(Etna::Clients::Metis::RenameFolderRequest.new(
        bucket_name: source_bucket,
        project_name: project_name,
        folder_path: folder.folder_path,
        new_bucket_name: dest_bucket,
        new_folder_path: folder.folder_path,
        create_parent: true)
      )
    end
  }
end

#rename_folders_by_regex(project_name:, source_bucket:, source_folders:, dest_bucket:, regex:) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/etna/clients/metis/client.rb', line 227

def rename_folders_by_regex(project_name:, source_bucket:, source_folders:, dest_bucket:, regex:)
  found_folders = source_folders.select { |folder|
      folder.folder_path =~ regex
  }

  return if found_folders.length == 0

  rename_folders(
    project_name: project_name,
    source_bucket: source_bucket,
    source_folders: found_folders,
    dest_bucket: dest_bucket
  )
end

#resolve_conflicts_and_verify_rename(project_name:, source_bucket:, dest_bucket:, folder:, file:) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/etna/clients/metis/client.rb', line 272

def resolve_conflicts_and_verify_rename(project_name:, source_bucket:, dest_bucket:, folder:, file:)
  parent_folder = ::File.dirname(file.file_path)

  should_rename_original = true

  # If the destination folder already exists, check to see if
  #   the file also exists, otherwise we risk a
  #   rename conflict.
  create_folder_request = CreateFolderRequest.new(
    project_name: project_name,
    bucket_name: dest_bucket,
    folder_path: parent_folder
  )

  if folder_exists?(create_folder_request)
    # If file exists in destination, delete the older file.
    list_dest_folder_request = Etna::Clients::Metis::ListFolderRequest.new(
      bucket_name: dest_bucket,
      project_name: project_name,
      folder_path: parent_folder
    )

    dest_file = list_folder(list_dest_folder_request).files.all.find { |f| f.file_name == file.file_name }

    if (dest_file && file.updated_at <= dest_file.updated_at)
      # Delete source file if it's out of date
      delete_file(Etna::Clients::Metis::DeleteFileRequest.new(
        bucket_name: source_bucket,
        project_name: project_name,
        file_path: file.file_path,
      ))

      should_rename_original = false
    elsif (dest_file && file.updated_at > dest_file.updated_at)
      # Delete dest file if it's out of date
      delete_file(Etna::Clients::Metis::DeleteFileRequest.new(
        bucket_name: dest_bucket,
        project_name: project_name,
        file_path: dest_file.file_path,
      ))
    end
  end

  should_rename_original
end

#tail_bucket(tail_bucket_request = TailBucketRequest.new) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/etna/clients/metis/client.rb', line 26

def tail_bucket(tail_bucket_request = TailBucketRequest.new)
  TailResponse.new(
    tail_bucket_request.project_name,
    tail_bucket_request.bucket_name,
    @etna_client.bucket_tail(tail_bucket_request.to_h).split("\n").map do |line|
      JSON.parse(line, symbolize_names: true)
    end
  )
end

#touch_file(touch_file_request = TouchFileRequest.new) ⇒ Object



56
57
58
59
# File 'lib/etna/clients/metis/client.rb', line 56

def touch_file(touch_file_request = TouchFileRequest.new)
  FilesResponse.new(
    @etna_client.file_touch(touch_file_request.to_h))
end

#touch_folder(touch_folder_request = TouchFolderRequest.new) ⇒ Object



51
52
53
54
# File 'lib/etna/clients/metis/client.rb', line 51

def touch_folder(touch_folder_request = TouchFolderRequest.new)
  FoldersResponse.new(
    @etna_client.folder_touch(touch_folder_request.to_h))
end

#upload_blob(upload_blob_request = UploadBlobRequest.new) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/etna/clients/metis/client.rb', line 179

def upload_blob(upload_blob_request = UploadBlobRequest.new)
  json = nil
  @etna_client.multipart_post(upload_blob_request.upload_path, upload_blob_request.encode_multipart_content) do |res|
    json = JSON.parse(res.body)
  end

  UploadResponse.new(json)
end

#upload_start(upload_start_request = UploadStartRequest.new) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/etna/clients/metis/client.rb', line 161

def upload_start(upload_start_request = UploadStartRequest.new)
  json = nil
  @etna_client.post(upload_start_request.upload_path, upload_start_request) do |res|
    json = JSON.parse(res.body)
  end

  UploadResponse.new(json)
end