Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grafana_user password idempotency #211

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/puppet/provider/grafana_user/grafana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def theme=(value)
end

def password
user[:password]
nil
end

def password=(value)
Expand Down Expand Up @@ -126,6 +126,24 @@ def save_user
self.user = nil
end

def check_password
uri = URI.parse format('%s://%s:%d%s/dashboards/home', grafana_scheme, grafana_host, grafana_port, resource[:grafana_api_path])
request = Net::HTTP::Get.new(uri.to_s)
request.content_type = 'application/json'
request.basic_auth resource[:name], resource[:password]
response = Net::HTTP.start(grafana_host, grafana_port,
use_ssl: grafana_scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
http.request(request)
end

if response.code == '200'
true
else
false
end
end

def delete_user
response = send_request('DELETE', format('%s/admin/users/%s', resource[:grafana_api_path], user[:id]))

Expand Down
3 changes: 3 additions & 0 deletions lib/puppet/type/grafana_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@

newproperty(:password) do
desc 'The password for the user'
def insync?(_is)
provider.check_password
end
end

newproperty(:email) do
Expand Down
35 changes: 35 additions & 0 deletions spec/acceptance/grafana_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper_acceptance'

describe 'grafana_user' do
context 'setup grafana server' do
it 'runs successfully' do
pp = <<-EOS
class { 'grafana':
cfg => {
security => {
admin_user => 'admin',
admin_password => 'admin'
}
}
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end
end
it 'runs successfully' do
pp = <<-EOS

grafana_user { 'user1':
grafana_url => 'http://localhost:3000',
grafana_user => 'admin',
grafana_password => 'admin',
full_name => 'John Doe',
password => 'Us3r5ecret',
email => 'john@example.com',
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end
end