fog-radosgw 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,83 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Provisioning
4
+ class Real
5
+ include Utils
6
+
7
+ def create_user(user_id, display_name, email, options = {})
8
+ if get_user(user_id).status != 404
9
+ raise Fog::Radosgw::Provisioning::UserAlreadyExists, "User with user_id #{user_id} already exists."
10
+ end
11
+
12
+ path = "admin/user"
13
+ user_id = Fog::AWS.escape(user_id)
14
+ display_name = Fog::AWS.escape(display_name)
15
+ email = Fog::AWS.escape(email)
16
+ query = "?uid=#{user_id}&display-name=#{display_name}&email=#{email}&format=json"
17
+ params = {
18
+ :method => 'PUT',
19
+ :path => path,
20
+ }
21
+
22
+ begin
23
+ response = Excon.put("#{@scheme}://#{@host}/#{path}#{query}",
24
+ :headers => signed_headers(params))
25
+ if !response.body.empty?
26
+ case response.headers['Content-Type']
27
+ when 'application/json'
28
+ response.body = Fog::JSON.decode(response.body)
29
+ end
30
+ end
31
+ response
32
+ rescue Excon::Errors::Conflict => e
33
+ raise Fog::Radosgw::Provisioning::UserAlreadyExists.new
34
+ rescue Excon::Errors::BadRequest => e
35
+ raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
36
+ end
37
+ end
38
+ end
39
+
40
+ class Mock
41
+ def user_exists?(user_id)
42
+ data.find do |key, value|
43
+ value[:user_id] == user_id
44
+ end
45
+ end
46
+
47
+ def create_user(user_id, display_name, email, options = {})
48
+ if user_exists?(user_id)
49
+ raise Fog::Radosgw::Provisioning::UserAlreadyExists, "User with user_id #{user_id} already exists."
50
+ end
51
+
52
+ secret_key = rand(1000).to_s
53
+ data[user_id] = {
54
+ :email => email,
55
+ :user_id => user_id,
56
+ :display_name => display_name,
57
+ :suspended => 0,
58
+ :secret_key => secret_key,
59
+ }
60
+
61
+ Excon::Response.new.tap do |response|
62
+ response.status = 200
63
+ response.headers['Content-Type'] = 'application/json'
64
+ response.body = {
65
+ "email" => email,
66
+ "user_id" => user_id,
67
+ "display_name" => display_name,
68
+ "suspended" => 0,
69
+ "keys" =>
70
+ [
71
+ {
72
+ "access_key" => "XXXXXXXXXXXXXXXXXXXX",
73
+ "secret_key" => secret_key,
74
+ "user" => user_id,
75
+ }
76
+ ],
77
+ }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,57 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Provisioning
4
+ class Real
5
+ include Utils
6
+
7
+ def delete_user(user_id)
8
+ path = "admin/user"
9
+ user_id = Fog::AWS.escape(user_id)
10
+ query = "?uid=#{user_id}&format=json"
11
+ params = {
12
+ :method => 'DELETE',
13
+ :path => path,
14
+ }
15
+
16
+ begin
17
+ response = Excon.delete("#{@scheme}://#{@host}/#{path}#{query}",
18
+ :headers => signed_headers(params))
19
+ if !response.body.empty?
20
+ case response.headers['Content-Type']
21
+ when 'application/json'
22
+ response.body = Fog::JSON.decode(response.body)
23
+ end
24
+ end
25
+ response
26
+ rescue Excon::Errors::NotFound => e
27
+ raise Fog::Radosgw::Provisioning::NoSuchUser.new
28
+ rescue Excon::Errors::BadRequest => e
29
+ raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
30
+ end
31
+ end
32
+ end
33
+
34
+ class Mock
35
+ def user_exists?(user_id)
36
+ data.find do |key, value|
37
+ value[:user_id] == user_id
38
+ end
39
+ end
40
+
41
+ def delete_user(user_id)
42
+ if !user_exists?(user_id)
43
+ raise Fog::Radosgw::Provisioning::NoSuchUser, "No user with user_id #{user_id} exists."
44
+ end
45
+
46
+ data.delete(user_id)
47
+
48
+ Excon::Response.new.tap do |response|
49
+ response.status = 200
50
+ response.headers['Content-Type'] = 'application/json'
51
+ response.body = ""
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Provisioning
4
+ class Real
5
+ include Utils
6
+ include UserUtils
7
+ include MultipartUtils
8
+
9
+ def disable_user(user_id)
10
+ update_radosgw_user(user_id, { :suspended => 1 })
11
+ end
12
+ end
13
+
14
+ class Mock
15
+ include UserUtils
16
+
17
+ def disable_user(user_id)
18
+ update_mock_user(user_id, { :suspended => 1 })
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Provisioning
4
+ class Real
5
+ include Utils
6
+ include UserUtils
7
+ include MultipartUtils
8
+
9
+ def enable_user(user_id)
10
+ update_radosgw_user(user_id, { :suspended => 0 })
11
+ end
12
+ end
13
+
14
+ class Mock
15
+ include UserUtils
16
+
17
+ def enable_user(user_id)
18
+ update_mock_user(user_id, { :suspended => 0 })
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,66 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Provisioning
4
+ class Real
5
+ include Utils
6
+ include MultipartUtils
7
+
8
+ def get_user(user_id)
9
+ path = "admin/user"
10
+ user_id = Fog::AWS.escape(user_id)
11
+ query = "?uid=#{user_id}&format=json"
12
+ params = {
13
+ :method => 'GET',
14
+ :path => path,
15
+ }
16
+
17
+ begin
18
+ response = Excon.get("#{@scheme}://#{@host}/#{path}#{query}",
19
+ :headers => signed_headers(params))
20
+ if !response.body.empty?
21
+ case response.headers['Content-Type']
22
+ when 'application/json'
23
+ response.body = Fog::JSON.decode(response.body)
24
+ end
25
+ end
26
+ response
27
+ rescue Excon::Errors::NotFound => e
28
+ raise Fog::Radosgw::Provisioning::NoSuchUser.new
29
+ rescue Excon::Errors::BadRequest => e
30
+ raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
31
+ end
32
+ end
33
+ end
34
+
35
+ class Mock
36
+ def get_user(user_id)
37
+ if value = data[user_id]
38
+ Excon::Response.new.tap do |response|
39
+ response.status = 200
40
+ response.headers['Content-Type'] = 'application/json'
41
+ response.body = {
42
+ "email" => value[:email],
43
+ "user_id" => value[:user_id],
44
+ "display_name" => value[:display_name],
45
+ "suspended" => value[:suspended],
46
+ "keys" =>
47
+ [
48
+ {
49
+ "access_key" => "XXXXXXXXXXXXXXXXXXXX",
50
+ "secret_key" => value[:secret_key],
51
+ "user" => value[:user_id],
52
+ }
53
+ ],
54
+ }
55
+ end
56
+ else
57
+ Excon::Response.new.tap do |response|
58
+ response.status = 404
59
+ response.headers['Content-Type'] = 'application/json'
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,70 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Provisioning
4
+ class Real
5
+ include Utils
6
+ include MultipartUtils
7
+
8
+ def list_user_ids()
9
+ path = "admin/metadata/user"
10
+ query = "?format=json"
11
+ params = {
12
+ :method => 'GET',
13
+ :path => path,
14
+ }
15
+
16
+ begin
17
+ response = Excon.get("#{@scheme}://#{@host}/#{path}#{query}",
18
+ :headers => signed_headers(params))
19
+ if !response.body.empty?
20
+ case response.headers['Content-Type']
21
+ when 'application/json'
22
+ response.body = Fog::JSON.decode(response.body)
23
+ end
24
+ else
25
+ response.body = []
26
+ end
27
+ response
28
+ rescue Excon::Errors::BadRequest => e
29
+ raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
30
+ end
31
+ end
32
+
33
+ def list_users(options = {})
34
+ response = list_user_ids
35
+ response.body = response.body.map { |user_id| get_user(user_id).body }
36
+ if options[:suspended]
37
+ response.body = response.body.select { |user| user[:suspended] == options[:suspended] }
38
+ end
39
+ response
40
+ end
41
+ end
42
+
43
+ class Mock
44
+ def list_users(options = {})
45
+ filtered_data = options[:suspended] ? data.select { |key, value| value[:suspended] == options[:suspended] } : data
46
+
47
+ Excon::Response.new.tap do |response|
48
+ response.status = 200
49
+ response.body = filtered_data.map do |key, value|
50
+ {
51
+ "email" => value[:email],
52
+ "display_name" => value[:user_id],
53
+ "user_id" => value[:user_id],
54
+ "suspended" => value[:suspended],
55
+ "keys" =>
56
+ [
57
+ {
58
+ "access_key" => "XXXXXXXXXXXXXXXXXXXX",
59
+ "secret_key" => value[:secret_key],
60
+ "user" => value[:user_id],
61
+ }
62
+ ],
63
+ }
64
+ end.compact
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,23 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Provisioning
4
+ class Real
5
+ include Utils
6
+ include UserUtils
7
+ include MultipartUtils
8
+
9
+ def update_user(key_id, user)
10
+ update_radosgw_user(key_id, user)
11
+ end
12
+ end
13
+
14
+ class Mock
15
+ include UserUtils
16
+
17
+ def update_user(key_id, user)
18
+ update_mock_user(key_id, user)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ {
2
+ "keys" : [
3
+ {
4
+ "access_key" : "UPET5548S0CU9112ID33",
5
+ "secret_key" : "aaw1AZ7+OlEEy6FrXkGy0oP2p4BvE/Eeg0L9ucmj",
6
+ "user" : "Fog User"
7
+ }
8
+ ],
9
+ "suspended" : 0,
10
+ "swift_keys" : [],
11
+ "subusers" : [],
12
+ "user_id" : "Fog User",
13
+ "caps" : [],
14
+ "display_name" : "Fog User",
15
+ "email" : "",
16
+ "max_buckets" : 1000
17
+ }
@@ -0,0 +1,60 @@
1
+ module Fog
2
+ module Radosgw
3
+ class Usage
4
+ module Utils
5
+
6
+ def sanitize_and_convert_time(time)
7
+ fmt = '%Y-%m-%d %H:%M:%S'
8
+ Fog::AWS.escape(time.strftime(fmt))
9
+ end
10
+
11
+ end
12
+
13
+ class Real
14
+ include Utils
15
+
16
+ def get_usage(access_key_id, options = {})
17
+ path = "admin/usage"
18
+ t_now = Fog::Time.now
19
+ start_time = sanitize_and_convert_time(options[:start_time] || t_now - 86400)
20
+ end_time = sanitize_and_convert_time(options[:end_time] || t_now)
21
+
22
+ query = "?format=json&start=#{start_time}&end=#{end_time}"
23
+ params = {
24
+ :method => 'GET',
25
+ :path => path,
26
+ }
27
+
28
+ begin
29
+ response = Excon.get("#{@scheme}://#{@host}/#{path}#{query}",
30
+ :headers => signed_headers(params))
31
+ if !response.body.empty?
32
+ case response.headers['Content-Type']
33
+ when 'application/json'
34
+ response.body = Fog::JSON.decode(response.body)
35
+ end
36
+ end
37
+ response
38
+ rescue Excon::Errors::BadRequest => e
39
+ raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
40
+ end
41
+ end
42
+ end
43
+
44
+ class Mock
45
+ include Utils
46
+
47
+ def get_usage(access_key, options = {})
48
+ Excon::Response.new.tap do |response|
49
+ response.status = 200
50
+ response.headers['Content-Type'] = 'application/json'
51
+ response.body = {
52
+ 'entries' => [],
53
+ 'summary' => []
54
+ }
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,62 @@
1
+ require 'fog/radosgw/core'
2
+ require 'time'
3
+
4
+ module Fog
5
+ module Radosgw
6
+ class Usage < Fog::Service
7
+ requires :radosgw_access_key_id, :radosgw_secret_access_key
8
+ recognizes :host, :path, :port, :scheme, :persistent
9
+
10
+ request_path 'fog/radosgw/requests/usage'
11
+ request :get_usage
12
+
13
+ class Mock
14
+ include Utils
15
+
16
+ def self.data
17
+ @data ||= Hash.new do |hash, key|
18
+ hash[key] = {}
19
+ end
20
+ end
21
+
22
+ def self.reset
23
+ @data = nil
24
+ end
25
+
26
+ def initialize(options = {})
27
+ configure_uri_options(options)
28
+ end
29
+
30
+ def data
31
+ self.class.data[radosgw_uri]
32
+ end
33
+
34
+ def reset_data
35
+ self.class.data.delete(radosgw_uri)
36
+ end
37
+ end
38
+
39
+ class Real
40
+ include Utils
41
+
42
+ def initialize(options = {})
43
+ configure_uri_options(options)
44
+ @radosgw_access_key_id = options[:radosgw_access_key_id]
45
+ @radosgw_secret_access_key = options[:radosgw_secret_access_key]
46
+ @connection_options = options[:connection_options] || {}
47
+ @persistent = options[:persistent] || false
48
+
49
+ @s3_connection = Fog::Storage.new(
50
+ :provider => 'AWS',
51
+ :aws_access_key_id => @radosgw_access_key_id,
52
+ :aws_secret_access_key => @radosgw_secret_access_key,
53
+ :host => @host,
54
+ :port => @port,
55
+ :scheme => @scheme,
56
+ :connection_options => @connection_options
57
+ )
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end