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: AuthorizeUploadRequest, CopyFilesRequest, CopyRevision, CreateFolderRequest, DeleteFileRequest, DeleteFolderRequest, File, Files, FilesResponse, FindParam, FindRequest, Folder, FolderRequest, Folders, FoldersAndFilesResponse, FoldersResponse, IngestMetisDataWorkflow, ListFolderByIdRequest, ListFolderRequest, ListFoldersRequest, MetisDownloadWorkflow, MetisUploadWorkflow, RenameFileRequest, RenameFolderRequest, SyncMetisDataWorkflow, 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

#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_upload(authorize_upload_request = AuthorizeUploadRequest.new) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/etna/clients/metis/client.rb', line 140

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



158
159
160
161
# File 'lib/etna/clients/metis/client.rb', line 158

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



82
83
84
85
# File 'lib/etna/clients/metis/client.rb', line 82

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

#delete_file(delete_file_request) ⇒ Object



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

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

#delete_folder(delete_folder_request) ⇒ Object



87
88
89
90
# File 'lib/etna/clients/metis/client.rb', line 87

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



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/etna/clients/metis/client.rb', line 102

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

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

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



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

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



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

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



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

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

#folder_exists?(create_folder_request) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/etna/clients/metis/client.rb', line 163

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



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/etna/clients/metis/client.rb', line 185

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

#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



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

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



36
37
38
39
# File 'lib/etna/clients/metis/client.rb', line 36

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



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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/etna/clients/metis/client.rb', line 288

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



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

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



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

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/etna/clients/metis/client.rb', line 212

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/etna/clients/metis/client.rb', line 197

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



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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/etna/clients/metis/client.rb', line 242

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

#touch_file(touch_file_request = TouchFileRequest.new) ⇒ Object



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

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



41
42
43
44
# File 'lib/etna/clients/metis/client.rb', line 41

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



149
150
151
152
153
154
155
156
# File 'lib/etna/clients/metis/client.rb', line 149

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



131
132
133
134
135
136
137
138
# File 'lib/etna/clients/metis/client.rb', line 131

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