Class: OneviewSDK::API200::User

Inherits:
Resource
  • Object
show all
Defined in:
lib/oneview-sdk/resource/api200/user.rb

Overview

User resource implementation

Constant Summary collapse

BASE_URI =
'/rest/users'.freeze
UNIQUE_IDENTIFIERS =
%w[userName uri].freeze

Constants inherited from Resource

Resource::DEFAULT_REQUEST_HEADER

Instance Attribute Summary

Attributes inherited from Resource

#api_version, #client, #data, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #[], #[]=, build_query, #create!, #deep_merge!, #delete, #each, #eql?, #exists?, find_by, find_with_pagination, from_file, get_all, get_all_with_query, #like?, #refresh, #retrieve!, schema, #schema, #set, #set_all, #to_file

Constructor Details

#initialize(client, params = {}, api_ver = nil) ⇒ User

Create a resource object, associate it with a client, and set its properties.

Parameters:

  • client (OneviewSDK::Client)

    The client object for the OneView appliance

  • params (Hash) (defaults to: {})

    The options for this resource (key-value pairs)

  • api_ver (Integer) (defaults to: nil)

    The api version to use when interracting with this resource.



25
26
27
28
29
30
31
# File 'lib/oneview-sdk/resource/api200/user.rb', line 25

def initialize(client, params = {}, api_ver = nil)
  super
  # Default values:
  @data['type'] ||= 'UserAndRoles'
  @data['enabled'] ||= true
  @data['roles'] ||= ['Read only']
end

Class Method Details

.validate_full_name(client, full_name) ⇒ Boolean

Checks for the existence of a user with the specified full name in the appliance.

Parameters:

  • client (OneviewSDK::Client)

    The client object for the OneView appliance

  • full_name (String)

    The full name to validate

Returns:

  • (Boolean)

    return true if the user already exists



92
93
94
95
# File 'lib/oneview-sdk/resource/api200/user.rb', line 92

def self.validate_full_name(client, full_name)
  response = client.rest_post("#{BASE_URI}/validateUserName/#{full_name}")
  client.response_handler(response)
end

.validate_user_name(client, user_name) ⇒ Boolean

Validates the existence of a user with the given user name in the appliance.

Parameters:

  • client (OneviewSDK::Client)

    The client object for the OneView appliance

  • user_name (String)

    The user name to validate

Returns:

  • (Boolean)

    return true if the user already exists



83
84
85
86
# File 'lib/oneview-sdk/resource/api200/user.rb', line 83

def self.validate_user_name(client, user_name)
  response = client.rest_post("#{BASE_URI}/validateLoginName/#{user_name}")
  client.response_handler(response)
end

Instance Method Details

#create(header = {}) ⇒ Resource

Note:

Calls the refresh method to set additional data

Note:

Removes the password attribute after creation

Create the resource on OneView using the current data

Parameters:

  • header (Hash) (defaults to: {})

    The header options for the request (key-value pairs)

Returns:

Raises:



40
41
42
43
44
45
46
47
# File 'lib/oneview-sdk/resource/api200/user.rb', line 40

def create(header = {})
  ensure_client
  response = @client.rest_post(BASE_URI, DEFAULT_REQUEST_HEADER.merge(header).merge('body' => @data), @api_version)
  body = @client.response_handler(response)
  @data.delete('password')
  set_all(body)
  self
end

#set_roles(roles) ⇒ Resource

Set data and save to OneView

Parameters:

  • roles (Array)

    The names of the roles to set for this user

Returns:

Raises:



69
70
71
72
73
74
75
76
77
# File 'lib/oneview-sdk/resource/api200/user.rb', line 69

def set_roles(roles)
  ensure_client && ensure_uri
  data = roles.map { |r| { roleName: r, type: 'RoleNameDtoV2' } }
  response = @client.rest_put("#{@data['uri']}/roles?multiResource=true", { 'body' => data }, @api_version)
  r = @client.response_handler(response)
  new_roles = r.map { |i| i['roleName'] }
  set('roles', new_roles)
  self
end

#update(attributes = {}) ⇒ Resource

Set data and save to OneView

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to add/change for this resource (key-value pairs)

Returns:

Raises:



54
55
56
57
58
59
60
61
62
# File 'lib/oneview-sdk/resource/api200/user.rb', line 54

def update(attributes = {})
  set_all(attributes)
  ensure_client && ensure_uri
  new_data = @data.reject { |k, _v| k.to_s == 'roles' } # This cannot be updated here. It is updated below
  response = @client.rest_put(self.class::BASE_URI, { 'body' => new_data }, @api_version)
  d = @client.response_handler(response)
  set_roles(@data['roles']) if @data['roles'] && @data['roles'].sort != d['roles'].sort
  self
end