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

Instance Attribute Summary

Attributes inherited from Resource

#api_version, #client, #data, #logger

Instance Method Summary collapse

Methods inherited from Resource

#==, #[], #[]=, build_query, #create!, #delete, #each, #eql?, #exists?, find_by, from_file, get_all, #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

Instance Method Details

#createResource

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

Returns:

Raises:


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

def create
  ensure_client
  response = @client.rest_post(self.class::BASE_URI, { '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:


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

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:


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

def update(attributes = {})
  set_all(attributes)
  ensure_client && ensure_uri
  new_data = @data.select { |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