Class: Fog::RiakCS::Provisioning::Real

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

Instance Method Summary collapse

Methods included from UserUtils

#update_mock_user, #update_riakcs_user

Methods included from MultipartUtils

#extract_boundary, #parse

Methods included from Utils

#configure_uri_options, #riakcs_uri

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/riakcs/provisioning.rb', line 49

def initialize(options = {})
  configure_uri_options(options)
  @riakcs_access_key_id     = options[:riakcs_access_key_id]
  @riakcs_secret_access_key = options[:riakcs_secret_access_key]
  @connection_options       = options[:connection_options] || {}
  @persistent               = options[:persistent]         || false

  @raw_connection = Fog::Connection.new(riakcs_uri, @persistent, @connection_options)

  @s3_connection  = Fog::Storage.new(
    :provider              => 'AWS',
    :aws_access_key_id     => @riakcs_access_key_id,
    :aws_secret_access_key => @riakcs_secret_access_key,
    :host                  => @host,
    :port                  => @port,
    :scheme                => @scheme,
    :connection_options    => @connection_options
  )
end

Instance Method Details

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



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

def create_user(email, name, options = {})
  payload = Fog::JSON.encode({ :email => email, :name => name })
  headers = { 'Content-Type' => 'application/json' }

  if(options[:anonymous])
    request(
      :expects => [201],
      :method  => 'POST',
      :path    => 'user',
      :body    => payload,
      :headers => headers
    )
  else
    begin
      response = @s3_connection.put_object('riak-cs', 'user', payload, headers)
      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::RiakCS::Provisioning::UserAlreadyExists.new
    rescue Excon::Errors::BadRequest => e
      raise Fog::RiakCS::Provisioning::ServiceUnavailable.new
    end
  end
end

#disable_user(key_id) ⇒ Object



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

def disable_user(key_id)
  update_riakcs_user(key_id, { :status => 'disabled' })
end

#enable_user(key_id) ⇒ Object



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

def enable_user(key_id)
  update_riakcs_user(key_id, { :status => 'enabled' })
end

#get_user(key_id) ⇒ Object



8
9
10
11
12
# File 'lib/fog/riakcs/requests/provisioning/get_user.rb', line 8

def get_user(key_id)
  response = @s3_connection.get_object('riak-cs', "user/#{key_id}", { 'Accept' => 'application/json' })
  response.body = Fog::JSON.decode(response.body)
  response
end

#list_users(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/riakcs/requests/provisioning/list_users.rb', line 8

def list_users(options = {})
  response      = @s3_connection.get_object('riak-cs', 'users', { 'Accept' => 'application/json', 'query' => options })

  boundary      = extract_boundary(response.headers['Content-Type'])
  parts         = parse(response.body, boundary)
  decoded       = parts.map { |part| Fog::JSON.decode(part[:body]) }

  response.body = decoded.flatten

  response
end

#regrant_secret(key_id) ⇒ Object



9
10
11
# File 'lib/fog/riakcs/requests/provisioning/regrant_secret.rb', line 9

def regrant_secret(key_id)
  update_riakcs_user(key_id, { :new_key_secret => true })
end

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



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

def request(params, parse_response = true, &block)
  begin
    response = @raw_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::RiakCS::Provisioning.const_get(match[1]).new
      when 'ServiceUnavailable'
        raise Fog::RiakCS::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/riakcs/requests/provisioning/update_user.rb', line 9

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