-
Notifications
You must be signed in to change notification settings - Fork 17
/
user.rb
85 lines (71 loc) · 2.44 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
property :username, String, name_property: true
property :password, String, sensitive: true, required: true
property :first_name, String, default: ''
property :last_name, String, default: ''
property :email, String, default: ''
property :roles, Array, default: lazy { [] }, coerce: proc { |r| r.sort }
property :api_client, ::Nexus3::Api, identity: true, desired_state: false, default: lazy { ::Nexus3::Api.default(node) }
load_current_value do |desired|
begin
config = ::JSON.parse(api_client.run_script('get_user', username))
current_value_does_not_exist! if config.nil?
::Chef::Log.debug "User config is #{config}"
first_name config['first_name']
last_name config['last_name']
email config['email']
roles config['roles']
# Check if we need to change the password.
if self.class.properties[:password].is_set?(desired)
begin
::Nexus3::Api.new(api_client.endpoint, username, desired.password).request(:get, 'status')
password desired.password
rescue ::Nexus3::ApiError
password 'Supercalifragilisticexpialidocious-that-does-not-exist-so-maybe-the-resource-will-need-to-converge'
end
end
rescue LoadError, ::Nexus3::ApiError => e
::Chef::Log.warn "A '#{e.class}' occurred: #{e.message}"
current_value_does_not_exist!
end
end
action :create do
init
converge_if_changed do
nexus3_api "upsert_user #{new_resource.username}" do
script_name 'upsert_user'
args username: new_resource.username,
password: new_resource.password,
first_name: new_resource.first_name,
last_name: new_resource.last_name,
email: new_resource.email,
roles: new_resource.roles
action %i[create run]
api_client new_resource.api_client
content ::Nexus3::Scripts.groovy_content('upsert_user', node)
end
end
end
action :delete do
init
nexus3_api "delete_user #{new_resource.username}" do
script_name 'delete_user'
args new_resource.username
action %i[create run]
api_client new_resource.api_client
content ::Nexus3::Scripts.groovy_content('delete_user', node)
not_if { current_resource.nil? }
end
end
action_class do
def init
nexus3_api "get_user #{new_resource.username}" do
action :create
script_name 'get_user'
api_client new_resource.api_client
content ::Nexus3::Scripts.groovy_content('get_user', node)
end
end
def whyrun_supported?
true
end
end