Class: PushmiPullyu::SwiftDepositer

Inherits:
Object
  • Object
show all
Defined in:
lib/pushmi_pullyu/swift_depositer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ SwiftDepositer

Returns a new instance of SwiftDepositer.


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pushmi_pullyu/swift_depositer.rb', line 8

def initialize(connection)
  @swift_connection = OpenStack::Connection.create(
    username: connection[:username],
    api_key: connection[:password],
    auth_method: 'password',
    auth_url: connection[:auth_url],
    project_name: connection[:project_name],
    project_domain_name: connection[:project_domain_name],
    authtenant_name: connection[:tenant],
    service_type: 'object-store'
  )
end

Instance Attribute Details

#swift_connectionObject (readonly)

Returns the value of attribute swift_connection


6
7
8
# File 'lib/pushmi_pullyu/swift_depositer.rb', line 6

def swift_connection
  @swift_connection
end

Instance Method Details

#deposit_file(file_name, swift_container) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pushmi_pullyu/swift_depositer.rb', line 21

def deposit_file(file_name, swift_container)
  file_base_name = File.basename(file_name, '.*')

  checksum = Digest::MD5.file(file_name).hexdigest

  era_container = swift_connection.container(swift_container)

  # Add swift metadata with in accordance to AIP spec:
  # https://docs.google.com/document/d/154BqhDPAdGW-I9enrqLpBYbhkF9exX9lV3kMaijuwPg/edit#
   = {
    project: 'ERA',
    project_id: file_base_name,
    promise: 'bronze',
    aip_version: '1.0'
  }

  # ruby-openstack wants all keys of the metadata to be named like "X-Object-Meta-{{Key}}", so update them
  .transform_keys! { |key| "X-Object-Meta-#{key}" }

  # ruby-openstack expects the header hash in different structure for write vs create_object methods
  # details see: https://github.com/ualbertalib/pushmi_pullyu/issues/105
  if era_container.object_exists?(file_base_name)
    # temporary solution until fixed in upstream:
    # for update: construct hash for key/value pairs as strings,
    # and metadata as additional key/value string pairs in the hash
    headers = { 'etag' => checksum,
                'content-type' => 'application/x-tar' }.merge()
    deposited_file = era_container.object(file_base_name)
    deposited_file.write(File.open(file_name), headers)
  else
    # for creating new: construct hash with symbols as keys, add metadata as a hash within the header hash
    headers = { etag: checksum,
                content_type: 'application/x-tar',
                metadata:  }
    deposited_file = era_container.create_object(file_base_name, headers, File.open(file_name))
  end

  deposited_file
end