Skip to content

Commit

Permalink
User provider update
Browse files Browse the repository at this point in the history
- replace create_if_missing with create action
- Remove mandatory login information. Use resource name by default.
  If present and is different from resource name, means that we have to update
  user's login.
  • Loading branch information
Jean Baptiste Favre committed Oct 6, 2015
1 parent 3e05191 commit 04cd530
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions providers/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,38 @@ def whyrun_supported?
true
end

action :create_if_missing do
action :create do
grafana_options = {
host: new_resource.host,
port: new_resource.port,
user: new_resource.admin_user,
password: new_resource.admin_password
}
# If login is not provided as variable,
# Let's use resource name for it
unless new_resource.user.key?(:login)
new_resource.user[:login] = new_resource.name
end

users_list = get_user_list(grafana_options)
exists = false

# Find wether user already exists
users_list.each do |user|
if user['login'] == new_resource.user[:login]
exists = true
end
break if exists
end

# If not found, let's create it and set its permissions
unless exists
new_resource.user[:name] = new_resource.name
converge_by("Creating user #{new_resource.user[:login]}") do
add_user(new_resource.user, grafana_options)
end
converge_by("Setting permissions #{new_resource.user[:login]}") do
update_user_permission(new_resource.user, grafana_options)
update_user_permissions(new_resource.user, grafana_options)
end

end
Expand All @@ -43,15 +52,25 @@ def whyrun_supported?
user: new_resource.admin_user,
password: new_resource.admin_password
}
# If login is not provided as variable,
# Let's use resource name for it
unless new_resource.user.key?(:login)
new_resource.user[:login] = new_resource.name
end

users_list = get_user_list(grafana_options)
exists = false

# Check wether we have to update user's login
if new_resource.user[:login] != new_resource.name
old_login = new_resource.name
new_login = new_resource.user[:login]
else
old_login = new_login = new_resource.user[:login]
end

# Find wether user already exists
# If found, update all informations we have to
users_list.each do |user|
if user['login'] == old_login
exists = true
Expand Down Expand Up @@ -79,9 +98,17 @@ def whyrun_supported?
user: new_resource.admin_user,
password: new_resource.admin_password
}
# If login is not provided as variable,
# Let's use resource name for it
unless new_resource.user.key?(:login)
new_resource.user[:login] = new_resource.name
end

users_list = get_user_list(grafana_options)
exists = false

# Find wether use already exists
# If found, just delete it
users_list.each do |user|
if user['login'] == new_resource.user[:login]
exists = true
Expand Down

0 comments on commit 04cd530

Please sign in to comment.