Class: Miasma::Models::Storage::OpenStack

Inherits:
Miasma::Models::Storage show all
Includes:
Contrib::OpenStackApiCore::ApiCommon
Defined in:
lib/miasma/contrib/open_stack/storage.rb

Instance Method Summary collapse

Methods included from Contrib::OpenStackApiCore::ApiCommon

#connection, #endpoint, included, #open_stack_api, #token

Instance Method Details

#bucket_allArray<Models::Storage::Bucket>

Return all buckets

Returns:

  • (Array<Models::Storage::Bucket>)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/miasma/contrib/open_stack/storage.rb', line 84

def bucket_all
  result = request(
    :path => '/',
    :expects => [200, 204],
    :params => {
      :format => 'json'
    }
  )
  [result[:body]].flatten.compact.map do |bkt|
    Bucket.new(
      self,
      :id => bkt['name'],
      :name => bkt['name']
    ).valid_state
  end
end

#bucket_destroy(bucket) ⇒ TrueClass, FalseClass

Destroy bucket

Parameters:

  • bucket (Models::Storage::Bucket)

Returns:

  • (TrueClass, FalseClass)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/miasma/contrib/open_stack/storage.rb', line 32

def bucket_destroy(bucket)
  if(bucket.persisted?)
    request(
      :path => full_path(bucket),
      :method => :delete,
      :expects => 204
    )
    true
  else
    false
  end
end

#bucket_path(bucket) ⇒ String

Returns escaped bucket name.

Returns:

  • (String)

    escaped bucket name



304
305
306
# File 'lib/miasma/contrib/open_stack/storage.rb', line 304

def bucket_path(bucket)
  uri_escape(bucket.name)
end

#bucket_reload(bucket) ⇒ Models::Storage::Bucket

Reload the bucket

Parameters:

  • bucket (Models::Storage::Bucket)

Returns:

  • (Models::Storage::Bucket)


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
76
77
78
79
# File 'lib/miasma/contrib/open_stack/storage.rb', line 49

def bucket_reload(bucket)
  if(bucket.persisted?)
    begin
      result = request(
        :path => full_path(bucket),
        :method => :head,
        :expects => 204,
        :params => {
          :format => 'json'
        }
      )
      meta = Smash.new.tap do |m|
        result[:response].headers.each do |k,v|
          if(k.to_s.start_with?('X-Container-Meta-'))
            m[k.sub('X-Container-Meta-', '')] = v
          end
        end
      end
      bucket. = meta unless meta.empty?
      bucket.valid_state
    rescue Error::ApiError::RequestError => e
      if(e.response.status == 404)
        bucket.data.clear
        bucket.dirty.clear
      else
        raise
      end
    end
  end
  bucket
end

#bucket_save(bucket) ⇒ Models::Storage::Bucket

Save bucket

Parameters:

  • bucket (Models::Storage::Bucket)

Returns:

  • (Models::Storage::Bucket)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/miasma/contrib/open_stack/storage.rb', line 15

def bucket_save(bucket)
  unless(bucket.persisted?)
    request(
      :path => full_path(bucket),
      :method => :put,
      :expects => [201, 204]
    )
    bucket.id = bucket.name
    bucket.valid_state
  end
  bucket
end

#file_all(bucket) ⇒ Array<File>

TODO:

pagination auto-follow

Return all files within bucket

Parameters:

  • bucket (Bucket)

Returns:

  • (Array<File>)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/miasma/contrib/open_stack/storage.rb', line 130

def file_all(bucket)
  result = request(
    :path => full_path(bucket),
    :expects => [200, 204],
    :params => {
      :format => :json
    }
  )
  [result[:body]].flatten.compact.map do |file|
    File.new(
      bucket,
      :id => ::File.join(bucket.name, file[:name]),
      :name => file[:name],
      :updated => file[:last_modified],
      :size => file[:bytes].to_i
    ).valid_state
  end
end

#file_body(file) ⇒ IO, HTTP::Response::Body

Fetch the contents of the file

Parameters:

  • file (Models::Storage::File)

Returns:

  • (IO, HTTP::Response::Body)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/miasma/contrib/open_stack/storage.rb', line 282

def file_body(file)
  if(file.persisted?)
    result = request(:path => full_path(file))
    content = result[:body]
    begin
      if(content.is_a?(String))
        StringIO.new(content)
      else
        if(content.respond_to?(:stream!))
          content.stream!
        end
        content
      end
    rescue HTTP::StateError
      StringIO.new(content.to_s)
    end
  else
    StringIO.new('')
  end
end

#file_destroy(file) ⇒ TrueClass, FalseClass

Destroy file

Parameters:

  • file (Models::Storage::File)

Returns:

  • (TrueClass, FalseClass)


222
223
224
225
226
227
228
229
230
231
232
# File 'lib/miasma/contrib/open_stack/storage.rb', line 222

def file_destroy(file)
  if(file.persisted?)
    request(
      :path => full_path(file),
      :method => :delete
    )
    true
  else
    false
  end
end

#file_filter(bucket, args) ⇒ Array<Models::Storage::File>

Return filtered files

Parameters:

  • args (Hash)

    filter options

Returns:

  • (Array<Models::Storage::File>)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/miasma/contrib/open_stack/storage.rb', line 105

def file_filter(bucket, args)
  result = request(
    :path => full_path(bucket),
    :expects => [200, 204],
    :params => {
      :prefix => args[:prefix],
      :format => :json
    }
  )
  [result[:body]].flatten.compact.map do |file|
    File.new(
      bucket,
      :id => ::File.join(bucket.name, file[:name]),
      :name => file[:name],
      :updated => file[:last_modified],
      :size => file[:bytes].to_i
    ).valid_state
  end
end

#file_path(file) ⇒ String

Returns escaped file path.

Returns:

  • (String)

    escaped file path



309
310
311
312
313
# File 'lib/miasma/contrib/open_stack/storage.rb', line 309

def file_path(file)
  file.name.split('/').map do |part|
    uri_escape(part)
  end.join('/')
end

#file_reload(file) ⇒ Models::Storage::File

Reload the file

Parameters:

  • file (Models::Storage::File)

Returns:

  • (Models::Storage::File)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/miasma/contrib/open_stack/storage.rb', line 238

def file_reload(file)
  if(file.persisted?)
    result = request(
      :path => full_path(file),
      :method => :head
    )
    info = result[:headers]
    new_info = Smash.new.tap do |data|
      data[:updated] = info[:last_modified]
      data[:etag] = info[:etag]
      data[:size] = info[:content_length].to_i
      data[:content_type] = info[:content_type]
      meta = Smash.new.tap do |m|
        result[:response].headers.each do |k, v|
          if(k.to_s.start_with?('X-Object-Meta-'))
            m[k.sub('X-Object-Meta-', '')] = v
          end
        end
      end
      data[:metadata] = meta unless meta.empty?
    end
    file.load_data(file.attributes.deep_merge(new_info))
    file.valid_state
  end
  file
end

#file_save(file) ⇒ Models::Storage::File

Save file

Parameters:

  • file (Models::Storage::File)

Returns:

  • (Models::Storage::File)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/miasma/contrib/open_stack/storage.rb', line 153

def file_save(file)
  if(file.dirty?)
    file.load_data(file.attributes)
    args = Smash.new
    args[:headers] = Smash[
      Smash.new(
        :content_type => 'Content-Type',
        :content_disposition => 'Content-Disposition',
        :content_encoding => 'Content-Encoding'
      ).map do |attr, key|
        if(file.attributes[attr])
          [key, file.attributes[attr]]
        end
      end.compact
    ]
    if(file.attributes[:body].is_a?(IO) && file.body.size >= Storage::MAX_BODY_SIZE_FOR_STRINGIFY)
      parts = []
      file.body.rewind
      while(content = file.body.read(Storage::READ_BODY_CHUNK_SIZE))
        data = Smash.new(
          :path => "segments/#{full_path(file)}-#{SecureRandom.uuid}",
          :etag => Digest::MD5.hexdigest(content),
          :size_bytes => content.length
        )
        request(
          :path => data[:path],
          :method => :put,
          :expects => 201,
          :headers => {
            'Content-Length' => data[:size_bytes],
            'Etag' => data[:etag]
          }
        )
        parts << data
      end
      result = request(
        :path => full_path(file),
        :method => :put,
        :expects => 201,
        :params => {
          'multipart-manifest' => :put
        },
        :json => parts
      )
    else
      if(file.attributes[:body].is_a?(IO) || file.attributes[:body].is_a?(StringIO))
        args[:headers]['Content-Length'] = file.body.size.to_s
        file.body.rewind
        args[:body] = file.body.read
        file.body.rewind
      end
      result = request(
        args.merge(
          :method => :put,
          :expects => 201,
          :path => full_path(file)
        )
      )
    end
    file.id = ::File.join(file.bucket.name, file.name)
    file.reload
  end
  file
end

#file_url(file, timeout_secs) ⇒ String

TODO:

where is this in swift?

Create publicly accessible URL

Parameters:

  • timeout_secs (Integer)

    seconds available

Returns:

  • (String)

    URL



270
271
272
273
274
275
276
# File 'lib/miasma/contrib/open_stack/storage.rb', line 270

def file_url(file, timeout_secs)
  if(file.persisted?)
    raise NotImplementedError
  else
    raise Error::ModelPersistError.new "#{file} has not been saved!"
  end
end

#full_path(file_or_bucket) ⇒ String

Provide full path for object

Parameters:

  • file_or_bucket (File, Bucket)

Returns:

  • (String)


319
320
321
322
323
324
325
326
# File 'lib/miasma/contrib/open_stack/storage.rb', line 319

def full_path(file_or_bucket)
  path = ''
  if(file_or_bucket.respond_to?(:bucket))
    path << '/' << bucket_path(file_or_bucket.bucket)
  end
  path << '/' << file_path(file_or_bucket)
  path
end

#uri_escape(string) ⇒ String

TODO:

move this to common module

URL string escape

Parameters:

  • string (String)

    string to escape

Returns:

  • (String)

    escaped string



333
334
335
336
337
# File 'lib/miasma/contrib/open_stack/storage.rb', line 333

def uri_escape(string)
  string.to_s.gsub(/([^a-zA-Z0-9_.\-~])/) do
    '%' << $1.unpack('H2' * $1.bytesize).join('%').upcase
  end
end