-
Notifications
You must be signed in to change notification settings - Fork 0
/
dds_swift.rb
114 lines (101 loc) · 2.79 KB
/
dds_swift.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/local/bin/ruby
require 'httparty'
class SwiftException < StandardError
end
class DdsSwift
def call_auth_uri
begin
@auth_uri_resp ||= HTTParty.get(
"#{ENV['SWIFT_PROVIDER_URL_ROOT']}#{ENV['SWIFT_PROVIDER_AUTH_URI']}",
headers: {
'X-Auth-User' => ENV['SWIFT_USER'],
'X-Auth-Key' => ENV['SWIFT_PASS']
}
)
rescue Exception => e
raise SwiftException, "Unexpected StorageProvider Error #{e.message}"
end
unless @auth_uri_resp.response.code.to_i == 200
raise SwiftException, "Auth Failure: #{ @auth_uri_resp.body }"
end
@auth_uri_resp.headers
end
def auth_token
call_auth_uri['x-auth-token']
end
def storage_url
call_auth_uri['x-storage-url']
end
def auth_header
{'X-Auth-Token' => auth_token}
end
def get_account_info
resp = HTTParty.get(
"#{storage_url}",
headers: auth_header
)
([200,204].include?(resp.response.code.to_i)) ||
raise(SwiftException, resp.body)
resp.headers
end
def get_containers
resp = HTTParty.get(
"#{storage_url}",
headers: auth_header
)
return [] if resp.response.code.to_i == 404
([200,204].include?(resp.response.code.to_i)) ||
raise(SwiftException, resp.body)
return resp.body ? resp.body.split("\n") : []
end
def get_container_meta(container)
resp = HTTParty.head(
"#{storage_url}/#{container}",
headers: auth_header
)
return if resp.response.code.to_i == 404
([200,204].include?(resp.response.code.to_i)) ||
raise(SwiftException, resp.body)
resp.headers
end
def get_container_objects(container)
resp = HTTParty.get(
"#{storage_url}/#{container}",
headers: auth_header
)
return [] if resp.response.code.to_i == 404
([200,204].include?(resp.response.code.to_i)) ||
raise(SwiftException, resp.body)
return resp.body ? resp.body.split("\n") : []
end
def get_object_metadata(container, object)
resp = HTTParty.head(
"#{storage_url}/#{container}/#{object}",
headers: auth_header
)
([200,204].include?(resp.response.code.to_i)) ||
raise(SwiftException, resp.body)
resp.headers
end
def get_object_manifest(container, object)
resp = HTTParty.get(
"#{storage_url}/#{container}/#{object}?multipart-manifest=get",
headers: auth_header
)
([200,204].include?(resp.response.code.to_i)) ||
raise(SwiftException, resp.body)
resp.parsed_response
end
def get_object(container, object)
get_data("/#{container}/#{object}")
end
def get_data(path)
resp = HTTParty.get(
"#{storage_url}#{path}",
headers: auth_header
)
([200,204].include?(resp.response.code.to_i)) ||
raise(SwiftException, resp.body)
resp.body
end
end