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 idempotent issue in grafana_datasource provider #297

Closed
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
19 changes: 17 additions & 2 deletions lib/puppet/provider/grafana_datasource/grafana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def datasources

datasource = JSON.parse(response.body)

basic_auth_password = if datasource.key?('secureJsonData') && datasource['secureJsonData'].key?('basicAuthPassword')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secureJsonData is never part of the response. There's secureJsonFields but this just returns booleans. See grafana/grafana#20274 (comment) :(

Copy link
Author

@tobias-urdin tobias-urdin Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see :(

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use basic auth so I didn't verify that part, but that's bummer. We are using this PR internally until something can be figured out here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#301 might be that something. Still not much use for Grafana 9 users who need to configure prometheus basic auth or influxdb password, but if you're not using any auth, should remove the problem for you.

datasource['secureJsonData']['basicAuthPassword']
else
datasource.fetch('basicAuthPassword', '')
end

{
id: datasource['id'],
name: datasource['name'],
Expand All @@ -76,7 +82,7 @@ def datasources
with_credentials: datasource['withCredentials'] ? :true : :false,
basic_auth: datasource['basicAuth'] ? :true : :false,
basic_auth_user: datasource['basicAuthUser'],
basic_auth_password: datasource['basicAuthPassword'],
basic_auth_password: basic_auth_password,
json_data: datasource['jsonData'],
secure_json_data: datasource['secureJsonData']
}
Expand Down Expand Up @@ -217,6 +223,14 @@ def save_datasource
response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], fetch_organization[:id])
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) unless response.code == '200'

secure_json_data = if resource[:secure_json_data]
resource[:secure_json_data].clone
else
{}
end

secure_json_data[:basicAuthPassword] = resource[:basic_auth_password] if !secure_json_data.key?('basicAuthPassword') && resource[:basic_auth_password]

data = {
name: resource[:name],
type: resource[:type],
Expand All @@ -228,10 +242,11 @@ def save_datasource
isDefault: (resource[:is_default] == :true),
basicAuth: (resource[:basic_auth] == :true),
basicAuthUser: resource[:basic_auth_user],
# Deprecated field, use only secureJsonData later
basicAuthPassword: resource[:basic_auth_password],
withCredentials: (resource[:with_credentials] == :true),
jsonData: resource[:json_data],
secureJsonData: resource[:secure_json_data]
secureJsonData: secure_json_data
}

if datasource.nil?
Expand Down