Class: Fog::Radosgw::Provisioning::Real

Inherits:
Object
  • Object
show all
Includes:
MultipartUtils, 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

Instance Method Summary collapse

Methods included from UserUtils

#update_mock_user, #update_radosgw_user

Methods included from MultipartUtils

#extract_boundary, #parse

Methods included from Utils

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

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/radosgw/provisioning.rb', line 48

def initialize(options = {})
  configure_uri_options(options)
  @radosgw_access_key_id     = options[:radosgw_access_key_id]
  @radosgw_secret_access_key = options[:radosgw_secret_access_key]
  @connection_options       = options[:connection_options] || {}
  @persistent               = options[:persistent]         || false
  @path_style               = options[:path_style]         || false

  @connection = Fog::XML::Connection.new(radosgw_uri, @persistent, @connection_options)
end

Instance Method Details

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



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

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

  path         = "admin/user"
  user_id      = escape(user_id)
  display_name = escape(display_name)
  email        = escape(email)
  query        = "?uid=#{user_id}&display-name=#{display_name}&email=#{email}&format=json"
  params       = {
    :method => 'PUT',
    :path => path,
  }

  begin
    response = Excon.put("#{@scheme}://#{@host}/#{path}#{query}",
                         :headers => signed_headers(params))
    if !response.body.empty?
      case response.headers['Content-Type']
      when 'application/json'
        response.body = Fog::JSON.decode(response.body)
      end
    end
    response
  rescue Excon::Errors::Conflict => e
    raise Fog::Radosgw::Provisioning::UserAlreadyExists.new
  rescue Excon::Errors::BadRequest => e
    raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
  end
end

#delete_user(user_id) ⇒ Object



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

def delete_user(user_id)
  path         = "admin/user"
  user_id      = escape(user_id)
  query        = "?uid=#{user_id}&format=json"
  params       = {
    :method => 'DELETE',
    :path => path,
  }

  begin
    response = Excon.delete("#{@scheme}://#{@host}/#{path}#{query}",
                         :headers => signed_headers(params))
    if !response.body.empty?
      case response.headers['Content-Type']
      when 'application/json'
        response.body = Fog::JSON.decode(response.body)
      end
    end
    response
  rescue Excon::Errors::NotFound => e
    raise Fog::Radosgw::Provisioning::NoSuchUser.new
  rescue Excon::Errors::BadRequest => e
    raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
  end
end

#disable_user(user_id) ⇒ Object



9
10
11
# File 'lib/fog/radosgw/requests/provisioning/disable_user.rb', line 9

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

#enable_user(user_id) ⇒ Object



9
10
11
# File 'lib/fog/radosgw/requests/provisioning/enable_user.rb', line 9

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

#get_user(user_id) ⇒ Object



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

def get_user(user_id)
  path = "admin/user"
  user_id = escape(user_id)
  query = "?uid=#{user_id}&format=json"
  params = { 
    :method => 'GET',
    :path => path,
  }

  begin
    response = Excon.get("#{@scheme}://#{@host}/#{path}#{query}",
                         :headers => signed_headers(params))
    if !response.body.empty?
      case response.headers['Content-Type']
      when 'application/json'
        response.body = Fog::JSON.decode(response.body)
      end
    end
    response
  rescue Excon::Errors::NotFound => e
    raise Fog::Radosgw::Provisioning::NoSuchUser.new
  rescue Excon::Errors::BadRequest => e
    raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
  end
end

#list_user_idsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/radosgw/requests/provisioning/list_users.rb', line 8

def list_user_ids()
  path = "admin/metadata/user"
  query = "?format=json"
  params = { 
    :method => 'GET',
    :path => path,
  }

  begin
    response = Excon.get("#{@scheme}://#{@host}/#{path}#{query}",
                         :headers => signed_headers(params))
    if !response.body.empty?
      case response.headers['Content-Type']
      when 'application/json'
        response.body = Fog::JSON.decode(response.body)
      end
    else
      response.body = []
    end
    response
  rescue Excon::Errors::BadRequest => e
    raise Fog::Radosgw::Provisioning::ServiceUnavailable.new
  end
end

#list_users(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/fog/radosgw/requests/provisioning/list_users.rb', line 33

def list_users(options = {})
  response = list_user_ids
  response.body = response.body.map { |user_id| get_user(user_id).body }
  if options[:suspended]
    response.body = response.body.select { |user| user[:suspended] == options[:suspended] }
  end
  response
end

#request(params, parse_response = true, &block) ⇒ Object



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

def request(params, parse_response = true, &block)
  begin
    response = @connection.request(params.merge({
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}",
    }), &block)
  rescue Excon::Errors::HTTPStatusError => error
    if match = error.message.match(/<Code>(.*?)<\/Code>(?:.*<Message>(.*?)<\/Message>)?/m)
      case match[1]
      when 'UserAlreadyExists'
        raise Fog::Radosgw::Provisioning.const_get(match[1]).new
      when 'ServiceUnavailable'
        raise Fog::Radosgw::Provisioning.const_get(match[1]).new
      else
        raise error
      end
    else
      raise error
    end
  end
  if !response.body.empty? && parse_response
    response.body = Fog::JSON.decode(response.body)
  end
  response
end

#update_user(key_id, user) ⇒ Object



9
10
11
# File 'lib/fog/radosgw/requests/provisioning/update_user.rb', line 9

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