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

Inherits:
Service
  • 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

Methods inherited from Service

#endpoint_uri, #region, #request, #service_name

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



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

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.



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

def auth_token
  @auth_token
end

#service_catalogObject (readonly)

Returns the value of attribute service_catalog.



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

def service_catalog
  @service_catalog
end

Instance Method Details

#authenticate(options = {}) ⇒ Object



66
67
68
69
70
# File 'lib/fog/rackspace/identity.rb', line 66

def authenticate(options={})
  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

#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