Class: Fog::Radosgw::Provisioning::Mock

Inherits:
Object
  • Object
show all
Includes:
UserUtils, Utils
Defined in:
lib/fog/radosgw/provisioning.rb,
lib/fog/radosgw/requests/provisioning/get_user.rb,
lib/fog/radosgw/requests/provisioning/list_users.rb,
lib/fog/radosgw/requests/provisioning/create_user.rb,
lib/fog/radosgw/requests/provisioning/delete_user.rb,
lib/fog/radosgw/requests/provisioning/enable_user.rb,
lib/fog/radosgw/requests/provisioning/update_user.rb,
lib/fog/radosgw/requests/provisioning/disable_user.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserUtils

#update_mock_user, #update_radosgw_user

Methods included from Utils

#configure_uri_options, #escape, #radosgw_uri, #signature, #signed_headers

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



32
33
34
# File 'lib/fog/radosgw/provisioning.rb', line 32

def initialize(options = {})
  configure_uri_options(options)
end

Class Method Details

.dataObject



24
25
26
# File 'lib/fog/radosgw/provisioning.rb', line 24

def self.data
  @data ||= Hash.new({})
end

.resetObject



28
29
30
# File 'lib/fog/radosgw/provisioning.rb', line 28

def self.reset
  @data = nil
end

Instance Method Details

#create_user(user_id, display_name, email, options = {}) ⇒ Object



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
# File 'lib/fog/radosgw/requests/provisioning/create_user.rb', line 47

def create_user(user_id, display_name, email, options = {})
  if user_exists?(user_id)
    raise Fog::Radosgw::Provisioning::UserAlreadyExists, "User with user_id #{user_id} already exists."
  end

  secret_key   = rand(1000).to_s
  data[user_id] = {
    :email        => email, 
    :user_id      => user_id, 
    :display_name => display_name,
    :suspended    => 0, 
    :secret_key   => secret_key,
  }

  Excon::Response.new.tap do |response|
    response.status = 200
    response.headers['Content-Type'] = 'application/json'
    response.body = {
      "email"        => email,
      "user_id"      => user_id,
      "display_name" => display_name,
      "suspended"    => 0,
      "keys"         =>
      [
       {
         "access_key" => "XXXXXXXXXXXXXXXXXXXX",
         "secret_key" => secret_key,
         "user"       => user_id,
       }
      ],
    }
  end
end

#dataObject



36
37
38
# File 'lib/fog/radosgw/provisioning.rb', line 36

def data
  self.class.data[radosgw_uri]
end

#delete_user(user_id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/radosgw/requests/provisioning/delete_user.rb', line 41

def delete_user(user_id)
  if !user_exists?(user_id)
    raise Fog::Radosgw::Provisioning::NoSuchUser, "No user with user_id #{user_id} exists."
  end

  data.delete(user_id)

  Excon::Response.new.tap do |response|
    response.status = 200
    response.headers['Content-Type'] = 'application/json'
    response.body = ""
  end
end

#disable_user(user_id) ⇒ Object



17
18
19
# File 'lib/fog/radosgw/requests/provisioning/disable_user.rb', line 17

def disable_user(user_id)
  update_mock_user(user_id, { :suspended => 1 })
end

#enable_user(user_id) ⇒ Object



17
18
19
# File 'lib/fog/radosgw/requests/provisioning/enable_user.rb', line 17

def enable_user(user_id)
  update_mock_user(user_id, { :suspended => 0 })
end

#get_user(user_id) ⇒ Object



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
# File 'lib/fog/radosgw/requests/provisioning/get_user.rb', line 36

def get_user(user_id)
  if value = data[user_id]
    Excon::Response.new.tap do |response|
      response.status = 200
      response.headers['Content-Type'] = 'application/json'
      response.body = {
        "email"        => value[:email],
        "user_id"      => value[:user_id],
        "display_name" => value[:display_name],
        "suspended"    => value[:suspended],
        "keys"         =>
        [
         {
           "access_key" => "XXXXXXXXXXXXXXXXXXXX",
           "secret_key" => value[:secret_key],
           "user"       => value[:user_id],
         }
        ],
      }
    end
  else
    Excon::Response.new.tap do |response|
      response.status = 404
      response.headers['Content-Type'] = 'application/json'
    end
  end
end

#list_users(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/radosgw/requests/provisioning/list_users.rb', line 44

def list_users(options = {})
  filtered_data = options[:suspended] ? data.select { |key, value| value[:suspended] == options[:suspended] } : data

  Excon::Response.new.tap do |response|
    response.status = 200
    response.body   = filtered_data.map do |key, value|
      {
        "email"        => value[:email],
        "display_name" => value[:user_id],
        "user_id"      => value[:user_id],
        "suspended"    => value[:suspended],
        "keys"         =>
        [
         {
           "access_key" => "XXXXXXXXXXXXXXXXXXXX",
           "secret_key" => value[:secret_key],
           "user"       => value[:user_id],
         }
        ],
      }
    end.compact
  end
end

#reset_dataObject



40
41
42
# File 'lib/fog/radosgw/provisioning.rb', line 40

def reset_data
  self.class.data.delete(radosgw_uri)
end

#update_user(key_id, user) ⇒ Object



17
18
19
# File 'lib/fog/radosgw/requests/provisioning/update_user.rb', line 17

def update_user(key_id, user)
  update_mock_user(key_id, user)
end

#user_exists?(user_id) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/fog/radosgw/requests/provisioning/create_user.rb', line 41

def user_exists?(user_id)
  data.find do |key, value|
    value[:user_id] == user_id
  end
end