Class: Puppet::Provider::NexusUser::NexusUser
- Inherits:
-
ResourceApi::SimpleProvider
- Object
- ResourceApi::SimpleProvider
- Puppet::Provider::NexusUser::NexusUser
- Defined in:
- lib/puppet/provider/nexus_user/nexus_user.rb
Overview
Implementation for the nexus_user type using the Resource API.
Instance Method Summary collapse
-
#create(context, name, should) ⇒ Object
Creates new user if they not exist yet.
-
#delete(context, name) ⇒ Object
Delete existing user if they set to absent.
-
#get(context) ⇒ Object
Return all existing users as resources.
-
#initialize ⇒ NexusUser
constructor
Init connection to the rest api.
-
#insync?(context, name, property_name, _is_hash, should_hash) ⇒ Boolean
Check function if user password is correct.
-
#keys_to_camelcase(hash) ⇒ Object
convert keys of the given hash to camelCase.
-
#keys_to_snake_case(hash) ⇒ Object
convert keys of the given hash to snake_case.
-
#update(context, name, should) ⇒ Object
Update already existing user.
Constructor Details
#initialize ⇒ NexusUser
Init connection to the rest api
9 10 11 12 13 14 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 9 def initialize super local_device = Puppet::Util::NetworkDevice::Config.devices['localhost_nexus_rest_api'] config_file = local_device['url'] unless local_device.nil? Puppet::ResourceApi::Transport.inject_device('nexus_rest_api', config_file) unless File.exist?(config_file) end |
Instance Method Details
#create(context, name, should) ⇒ Object
Creates new user if they not exist yet
72 73 74 75 76 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 72 def create(context, name, should) res = context.transport.post_request(context, 'security/users', keys_to_camelcase(should)) context.err(name, res.body) unless res.success? end |
#delete(context, name) ⇒ Object
Delete existing user if they set to absent
86 87 88 89 90 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 86 def delete(context, name) res = context.transport.delete_request(context, "security/users/#{name}") context.err(name, res.body) unless res.success? end |
#get(context) ⇒ Object
Return all existing users as resources
61 62 63 64 65 66 67 68 69 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 61 def get(context) res = context.transport.get_request(context, 'security/users') context.err(res.body) unless res.success? Puppet::Util::Json.load(res.body).map do |user| keys_to_snake_case(user.merge({ 'ensure' => 'present' })) end end |
#insync?(context, name, property_name, _is_hash, should_hash) ⇒ Boolean
Check function if user password is correct
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 17 def insync?(context, name, property_name, _is_hash, should_hash) context.debug("Checking whether #{property_name} is out of sync") case property_name when :password res = Puppet.runtime[:http].get( context.transport.build_uri('status'), options: { basic_auth: { user: name, password: should_hash[property_name] } }, ) unless res.success? res = context.transport.put_request_text(context, "security/users/#{name}/change-password", should_hash[property_name]) end res.success? end end |
#keys_to_camelcase(hash) ⇒ Object
convert keys of the given hash to camelCase
51 52 53 54 55 56 57 58 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 51 def keys_to_camelcase(hash) hash.transform_keys do |key| key.to_s .gsub(%r{(?:_+)([a-z])}) { Regexp.last_match(1).upcase } .gsub(%r{(\A|\s)([A-Z])}) { Regexp.last_match(1) + Regexp.last_match(2).downcase } .to_sym end end |
#keys_to_snake_case(hash) ⇒ Object
convert keys of the given hash to snake_case
41 42 43 44 45 46 47 48 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 41 def keys_to_snake_case(hash) hash.transform_keys do |key| key.gsub(%r{([A-Z]+)([A-Z][a-z])}, '\1_\2') .gsub(%r{([a-z\d])([A-Z])}, '\1_\2') .downcase .to_sym end end |
#update(context, name, should) ⇒ Object
Update already existing user
79 80 81 82 83 |
# File 'lib/puppet/provider/nexus_user/nexus_user.rb', line 79 def update(context, name, should) res = context.transport.put_request(context, "security/users/#{name}", keys_to_camelcase(should)) context.err(name, res.body) unless res.success? end |