Class: Fog::Rackspace::Identity::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/identity.rb,
lib/fog/rackspace/requests/identity/list_users.rb,
lib/fog/rackspace/requests/identity/create_user.rb,
lib/fog/rackspace/requests/identity/delete_user.rb,
lib/fog/rackspace/requests/identity/update_user.rb,
lib/fog/rackspace/requests/identity/create_token.rb,
lib/fog/rackspace/requests/identity/list_tenants.rb,
lib/fog/rackspace/requests/identity/get_user_by_id.rb,
lib/fog/rackspace/requests/identity/list_user_roles.rb,
lib/fog/rackspace/requests/identity/get_user_by_name.rb,
lib/fog/rackspace/requests/identity/list_credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fog/rackspace/identity.rb', line 47

def initialize(options={})
  @rackspace_username = options[:rackspace_username]
  @rackspace_api_key = options[:rackspace_api_key]
  @rackspace_region = options[:rackspace_region]
  @rackspace_auth_url = options[:rackspace_auth_url] || US_ENDPOINT

  uri = URI.parse(@rackspace_auth_url)
  @host = uri.host
  @path = uri.path
  @port = uri.port
  @scheme = uri.scheme
  @persistent = options[:persistent] || false
  @connection_options = options[:connection_options] || {}
  @connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)

  authenticate
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



45
46
47
# File 'lib/fog/rackspace/identity.rb', line 45

def auth_token
  @auth_token
end

#service_catalogObject (readonly)

Returns the value of attribute service_catalog.



45
46
47
# File 'lib/fog/rackspace/identity.rb', line 45

def service_catalog
  @service_catalog
end

Instance Method Details

#authenticateObject



82
83
84
85
86
# File 'lib/fog/rackspace/identity.rb', line 82

def authenticate
  data = self.create_token(@rackspace_username, @rackspace_api_key).body
  @service_catalog = ServiceCatalog.from_response(self, data)
  @auth_token = data['access']['token']['id']
end

#create_token(username, api_key) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/rackspace/requests/identity/create_token.rb', line 5

def create_token(username, api_key)
  data = {
    'auth' => {
      'RAX-KSKEY:apiKeyCredentials' => {
        'username' => username,
        'apiKey' => api_key
      }
    }
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [200, 203],
    :method => 'POST',
    :path => 'tokens'
  )
end

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/rackspace/requests/identity/create_user.rb', line 5

def create_user(username, email, enabled, options = {})
  data = {
    'user' => {
      'username' => username,
      'email' => email,
      'enabled' => enabled
    }
  }
  data['user']['OS-KSADM:password'] = options[:password] unless options[:password].nil?

  request(
    :body => Fog::JSON.encode(data),
    :expects => [201],
    :method => 'POST',
    :path => 'users'
  )
end

#delete_user(user_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/identity/delete_user.rb', line 5

def delete_user(user_id)
  request(
    :expects => [204],
    :method => 'DELETE',
    :path => "users/#{user_id}"
  )
end

#get_user_by_id(user_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/identity/get_user_by_id.rb', line 5

def get_user_by_id(user_id)
  request(
    :expects => [200, 203],
    :method => 'GET',
    :path => "users/#{user_id}"
  )
end

#get_user_by_name(username) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/identity/get_user_by_name.rb', line 5

def get_user_by_name(username)
  request(
    :expects => [200, 203],
    :method => 'GET',
    :path => "users?name=#{username}"
  )
end

#list_credentials(user_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/identity/list_credentials.rb', line 5

def list_credentials(user_id)
  response = request(
    :expects => [200, 203],
    :method => 'GET',
    :path => "users/#{user_id}/OS-KSADM/credentials"
  )

  unless response.body['credentials'].is_a?(Array)
    response.body['credentials'] = [response.body['credential']]
    response.body.delete('credential')
  end

  response
end

#list_tenantsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/identity/list_tenants.rb', line 5

def list_tenants()
  response = request(
    :expects => [200, 203],
    :method => 'GET',
    :path => 'tenants'
  )

  unless response.body['tenants'].is_a?(Array)
    response.body['tenants'] = [response.body['tenant']]
    response.body.delete('tenant')
  end

  response
end

#list_user_roles(user_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/identity/list_user_roles.rb', line 5

def list_user_roles(user_id)
  response = request(
    :expects => [200, 203],
    :method => 'GET',
    :path => "users/#{user_id}/roles"
  )

  unless response.body['roles'].is_a?(Array)
    response.body['roles'] = [response.body['role']]
    response.body.delete('role')
  end

  response
end

#list_usersObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/identity/list_users.rb', line 5

def list_users()
  response = request(
    :expects => [200, 203],
    :method => 'GET',
    :path => 'users'
  )

  unless response.body['users'].is_a?(Array)
    response.body['users'] = [response.body['user']]
    response.body.delete('user')
  end

  response
end

#request(params) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fog/rackspace/identity.rb', line 65

def request(params)
  begin
    parameters = params.merge!({
      :headers => {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
        'X-Auth-Token' => @auth_token
      },
      :host => @host,
      :path => "#{@path}/#{params[:path]}"
    })
    response = @connection.request(parameters)
    response.body = Fog::JSON.decode(response.body) unless response.body.empty?
    response
  end
end

#update_user(user_id, username, email, enabled, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/rackspace/requests/identity/update_user.rb', line 5

def update_user(user_id, username, email, enabled, options = {})
  data = {
    'user' => {
      'username' => username,
      'email' => email,
      'enabled' => enabled
    }
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [200, 203],
    :method => 'POST',
    :path => "users/#{user_id}"
  )
end