Class: SwiftClient

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authurl, user, key, retries = 5, preauthurl = nil, preauthtoken = nil, snet = false, starting_backoff = 1) ⇒ SwiftClient

Returns a new instance of SwiftClient.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fog/rackspace/client.rb', line 95

def initialize(authurl, user, key, retries=5, preauthurl=nil, preauthtoken=nil, snet=false, starting_backoff=1)
  @authurl = authurl
  @user = user
  @key = key
  @retries = retries
  @http_conn = nil
  @url = preauthurl
  @token = preauthtoken
  @attempts = 0
  @snet = snet
  @starting_backoff = starting_backoff
end

Class Method Details

.delete_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/fog/rackspace/client.rb', line 421

def self.delete_container(url, token, container, headers={}, http_conn=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  
  conn.start if !conn.started?
  parsed.path += "/#{quote(container)}"
  headers['x-auth-token'] = token
  resp = conn.delete(parsed.request_uri, headers)
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Container DELETE failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
end

.delete_object(url, token = nil, container = nil, name = nil, http_conn = nil, headers = {}, proxy = nil) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/fog/rackspace/client.rb', line 597

def self.delete_object(url, token=nil, container=nil, name=nil, http_conn=nil, headers={}, proxy=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  
  conn.start if !conn.started?
  parsed.path += "/#{quote(container)}" if container
  parsed.path += "/#{quote(name)}" if name
  headers['x-auth-token'] = token if token
  resp = conn.delete(parsed.request_uri, headers)
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Object DELETE failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
end

.get_account(url, token, marker = nil, limit = nil, prefix = nil, http_conn = nil, full_listing = false) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
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
241
242
243
244
# File 'lib/fog/rackspace/client.rb', line 201

def self.(url, token, marker=nil, limit=nil, prefix=nil, 
    http_conn=nil, full_listing=false)
  #todo: add in rest of functionality
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  if full_listing
    rv = (url, token, marker, limit, prefix, http_conn)
    listing = rv[1]
    while listing.length > 0
      marker = listing[-1]['name']
      listing = (url, token, marker, limit, prefix, http_conn)[1]
      if listing.length > 0
        rv[1] += listing
      end
    end
    return rv
  end
  query = Query.new(parsed.query)
  query.add('format', 'json')
  query.add('marker', quote(marker.to_s)) if marker
  query.add('limit', quote(limit.to_s)) if limit
  query.add('prefix', quote(prefix.to_s)) if prefix
  parsed.query = query.to_url_params
  conn.start if !conn.started?
  resp = conn.get(parsed.request_uri, {'x-auth-token' => token})
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Account GET failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
  resp_headers = {}
  resp.header.each do |k,v|
    resp_headers[k.downcase] = v
  end
  if resp.code.to_i == 204
    [resp_headers, []]
  else
    [resp_headers, JSON.parse(resp.body)]
  end
end

.get_auth(url, user, key, snet = false) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/fog/rackspace/client.rb', line 180

def self.get_auth(url, user, key, snet=false)
  parsed, conn = http_connection(url)
  conn.start if not conn.started?
  resp = conn.get(URI.encode(parsed.request_uri), {"x-auth-user" => user, "x-auth-key" => key })
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Account GET failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
  url = URI::parse(resp.header['x-storage-url'])
  if snet
    url.host = "snet-#{url.host}"
  end
  [url.to_s, resp.header['x-auth-token'], resp.header]
end

.get_container(url, token, container, marker = nil, limit = nil, prefix = nil, delimiter = nil, http_conn = nil, full_listing = nil) ⇒ Object



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
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/fog/rackspace/client.rb', line 296

def self.get_container(url, token, container, marker=nil, limit=nil, 
      prefix=nil, delimiter=nil, http_conn=nil, full_listing=nil)
  #todo: add in rest of functionality
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  
  if full_listing
    rv = (url, token, marker, limit, prefix, http_conn)
    listing = rv[1]
    while listing.length > 0
      marker = listing[-1]['name']
      listing = (url, token, marker, limit, prefix, http_conn)[1]
      if listing.length > 0
        rv[1] += listing
      end
    end
    return rv
  end
  query = Query.new(parsed.query)
  query.add('format', 'json')
  query.add('marker', quote(marker.to_s)) if marker
  query.add('limit', quote(limit.to_s)) if limit
  query.add('prefix', quote(prefix.to_s)) if prefix
  parsed.query = query.to_url_params
  conn.start if !conn.started?
  parsed.path += "/#{quote(container)}"
  resp = conn.get(parsed.request_uri, {'x-auth-token' => token})
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Container GET failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
  resp_headers = {}
  resp.header.each do |k,v|
    resp_headers[k.downcase] = v
  end
  if resp.code.to_i == 204
    [resp_headers, []]
  else
    [resp_headers, JSON.parse(resp.body())]
  end
end

.get_object(url, token, container, name, http_conn = nil, resp_chunk_size = nil, &block) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/fog/rackspace/client.rb', line 444

def self.get_object(url, token, container, name, http_conn=nil, resp_chunk_size=nil, &block)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  

  parsed.path += "/#{quote(container)}/#{quote(name)}"
  conn.start if not conn.started?
  headers = {'x-auth-token' => token}
  if block_given?
    resp = conn.request_get(parsed.request_uri, headers) do |r|
      r.read_body do |b|
        yield b
      end
    end
    object_body = nil
  else
    resp = conn.request_get(parsed.request_uri, headers)
    object_body = resp.body  
  end
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Object GET failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
  resp_headers = {}
  resp.header.each do |k,v|
    resp_headers[k.downcase] = v
  end
  [resp_headers, object_body]
end

.head_account(url, token, http_conn = nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/fog/rackspace/client.rb', line 250

def self.(url, token, http_conn=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  conn.start if !conn.started?
  resp = conn.head(parsed.request_uri, {'x-auth-token' => token})
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Account HEAD failed', :http_scheme=>parsed.scheme,
            :http_host=>conn.address, :http_port=>conn.port,
            :http_path=>parsed.path, :http_status=>resp.code,
            :http_reason=>resp.message)
  end
  resp_headers = {}
  resp.header.each do |k,v|
    resp_headers[k.downcase] = v
  end
  resp_headers
end

.head_container(url, token, container, http_conn = nil) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/fog/rackspace/client.rb', line 347

def self.head_container(url, token, container, http_conn=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  
  conn.start if !conn.started?
  parsed.path += "/#{quote(container)}"
  resp = conn.head(parsed.request_uri, {'x-auth-token' => token})
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Container HEAD failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
  resp_headers = {}
  resp.header.each do |k,v|
    resp_headers[k.downcase] = v
  end
  resp_headers
end

.head_object(url, token, container, name, http_conn = nil) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/fog/rackspace/client.rb', line 483

def self.head_object(url, token, container, name, http_conn=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  

  parsed.path += "/#{quote(container)}/#{quote(name)}"
  conn.start if not conn.started?
  resp = conn.head(parsed.request_uri, {'x-auth-token' => token})
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Object HEAD failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
  resp_headers = {}
  resp.header.each do |k,v|
    resp_headers[k.downcase] = v
  end
  resp_headers
end

.http_connection(url, proxy_host = nil, proxy_port = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fog/rackspace/client.rb', line 153

def self.http_connection(url, proxy_host=nil, proxy_port=nil)
  parsed = URI::parse(url)
  
  if parsed.scheme == 'http'
    require 'net/http'
    conn = Net::HTTP::Proxy(proxy_host, proxy_port).new(parsed.host, parsed.port)
    [parsed, conn]
  elsif parsed.scheme == 'https'
    require 'net/https'
    conn = Net::HTTP::Proxy(proxy_host, proxy_port).new(parsed.host, parsed.port)
    conn.use_ssl = true
    conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
    [parsed, conn]
  else
    raise ClientException.new(
      "Cannot handle protocol scheme #{parsed.scheme} for #{url} %s")
  end
end

.post_account(url, token, headers, http_conn = nil) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/fog/rackspace/client.rb', line 275

def self.(url, token, headers, http_conn=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  headers['x-auth-token'] = token
  conn.start if !conn.started?
  resp = conn.post(parsed.request_uri, nil, headers)
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Account POST failed', :http_scheme=>parsed.scheme,
            :http_host=>conn.address, :http_port=>conn.port,
            :http_path=>parsed.path, :http_status=>resp.code,
            :http_reason=>resp.message)
  end
  resp.body
end

.post_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/fog/rackspace/client.rb', line 398

def self.post_container(url, token, container, headers={}, http_conn=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  
  conn.start if !conn.started?
  parsed.path += "/#{quote(container)}"
  headers['x-auth-token'] = token
  resp = conn.post(parsed.request_uri, nil, headers)
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Container POST failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
end

.post_object(url, token = nil, container = nil, name = nil, headers = {}, http_conn = nil) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/fog/rackspace/client.rb', line 574

def self.post_object(url, token=nil, container=nil, name=nil, headers={}, http_conn=nil)
  if not http_conn
     http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  
  parsed.path += "/#{quote(container)}" if container
  parsed.path += "/#{quote(name)}" if name
  headers['x-auth-token'] = token if token
  resp = conn.post(parsed.request_uri, nil, headers)
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Object POST failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
end

.put_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/fog/rackspace/client.rb', line 374

def self.put_container(url, token, container, headers={}, http_conn=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
  
  conn.start if !conn.started?
  parsed.path += "/#{quote(container)}"
  headers['x-auth-token'] = token
  # headers['content-length'] = 0
  resp = conn.put(parsed.request_uri, nil, headers)
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Container PUT failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)  
  end
end

.put_object(url, token = nil, container = nil, name = nil, contents = nil, content_length = nil, etag = nil, chunk_size = 65536, content_type = nil, headers = {}, http_conn = nil, proxy = nil) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/fog/rackspace/client.rb', line 511

def self.put_object(url, token=nil, container=nil, name=nil, contents=nil,
               content_length=nil, etag=nil, chunk_size=65536,
               content_type=nil, headers={}, http_conn=nil, proxy=nil)
  if not http_conn
    http_conn = http_connection(url)
  end
  parsed = http_conn[0].clone
  conn = http_conn[1]
                
  parsed.path += "/#{quote(container)}" if container
  parsed.path += "/#{quote(name)}" if name
  headers['x-auth-token'] = token if token
  headers['etag'] = etag if etag
  if content_length != nil
    headers['content-length'] = content_length.to_s
  else
    headers.each do |k,v|
      if k.downcase == 'content-length'
        content_length = v.to_i
      end
    end
  end
  headers['content-type'] = content_type if content_type
  headers['content-length'] = '0' if not contents  
  if contents.respond_to? :read
    request = Net::HTTP::Put.new(parsed.request_uri, headers)
    chunked = ChunkedConnectionWrapper.new(contents, chunk_size)
    if content_length == nil
      request['Transfer-Encoding'] = 'chunked'
      request.delete('content-length')
    end
    request.body_stream = chunked
    resp = conn.start do |http|
      http.request(request)
    end
  else
    conn.start if not conn.started?
    resp = conn.put(parsed.request_uri, contents, headers)
  end
  if resp.code.to_i < 200 or resp.code.to_i > 300
    raise ClientException.new('Object PUT failed', :http_scheme=>parsed.scheme,
                :http_host=>conn.address, :http_port=>conn.port,
                :http_path=>parsed.path, :http_status=>resp.code,
                :http_reason=>resp.message)
  end
  resp.header['etag']
end

Instance Method Details

#delete_container(container) ⇒ Object



440
441
442
# File 'lib/fog/rackspace/client.rb', line 440

def delete_container(container)
  _retry(nil, :delete_container, [container])
end

#delete_object(container, name, headers = {}) ⇒ Object



617
618
619
# File 'lib/fog/rackspace/client.rb', line 617

def delete_object(container, name, headers={})
  _retry(nil, :delete_object, [container, name, headers])
end

#get_account(marker = nil, limit = nil, prefix = nil, full_listing = false) ⇒ Object



246
247
248
# File 'lib/fog/rackspace/client.rb', line 246

def (marker=nil, limit=nil, prefix=nil, full_listing=false)
  _retry(nil, :get_account, [marker, limit, prefix, @http_conn, full_listing])
end

#get_authObject



197
198
199
# File 'lib/fog/rackspace/client.rb', line 197

def get_auth
  @url, @token = SwiftClient.get_auth(@authurl, @user, @key, @snet)
end

#get_container(container, marker = nil, limit = nil, prefix = nil, delimiter = nil, full_listing = nil) ⇒ Object



343
344
345
# File 'lib/fog/rackspace/client.rb', line 343

def get_container(container, marker=nil, limit=nil, prefix=nil, delimiter=nil, full_listing=nil)
  _retry(nil, :get_container, [container, marker, limit, prefix, delimiter, full_listing])
end

#get_object(container, name, resp_chunk_size = nil) ⇒ Object



479
480
481
# File 'lib/fog/rackspace/client.rb', line 479

def get_object(container, name, resp_chunk_size=nil)
  _retry(nil, :get_object, [container, name, resp_chunk_size])
end

#head_accountObject



271
272
273
# File 'lib/fog/rackspace/client.rb', line 271

def 
  _retry(nil, :head_account, [@http_conn])
end

#head_container(container) ⇒ Object



370
371
372
# File 'lib/fog/rackspace/client.rb', line 370

def head_container(container)
  _retry(nil, :head_container, [container, @http_conn])
end

#head_object(container, name) ⇒ Object



507
508
509
# File 'lib/fog/rackspace/client.rb', line 507

def head_object(container, name)
  _retry(nil, :head_object, [container, name])
end

#http_connectionObject



172
173
174
175
176
177
178
# File 'lib/fog/rackspace/client.rb', line 172

def http_connection
  if !@http_conn
    @http_conn = SwiftClient.http_connection(@url)
  else
    @http_conn
  end
end

#post_account(headers = nil) ⇒ Object



292
293
294
# File 'lib/fog/rackspace/client.rb', line 292

def (headers=nil)
  _retry(nil, :post_account, [headers, @http_conn])
end

#post_container(container, headers = {}) ⇒ Object



417
418
419
# File 'lib/fog/rackspace/client.rb', line 417

def post_container(container, headers={})
  _retry(nil, :post_container, [container, headers])
end

#post_object(container, name, headers = {}) ⇒ Object



593
594
595
# File 'lib/fog/rackspace/client.rb', line 593

def post_object(container, name, headers={})
  _retry(nil, :post_object, [container, name, headers])
end

#put_container(container, headers = {}) ⇒ Object



394
395
396
# File 'lib/fog/rackspace/client.rb', line 394

def put_container(container, headers={})
  _retry(nil, :put_container, [container, headers, @http_conn])
end

#put_object(container, obj, contents, content_length = nil, etag = nil, chunk_size = 65536, content_type = nil, headers = {}) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/fog/rackspace/client.rb', line 559

def put_object(container, obj, contents, content_length=nil, etag=nil, chunk_size=65536, content_type=nil, headers={})
  
  _default_reset = Proc.new do |args|
    raise ClientException("put_object(#{container}, #{obj}, ...) failure and no ability to reset contents for reupload.")
  end
  reset_func = _default_reset
  if (contents.respond_to? :seek) and (contents.respond_to? :tell)
    orig_pos = contents.tell
    reset_func = Proc.new {|a| contents.seek(orig_pos)}
  elsif !contents
    reset_func = Proc.new {|a| nil }
  end
  _retry(reset_func, :put_object, [container, obj, contents, content_length, etag, chunk_size, content_type, headers])
end