From 7f89da9b44d37ce8a6b9e0bbf8c2a243ddb86236 Mon Sep 17 00:00:00 2001 From: Damian Lukowski Date: Thu, 11 Jul 2024 13:43:02 +0200 Subject: [PATCH] Change organization in datasource_by_name --- lib/puppet/provider/grafana_datasource/grafana.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/puppet/provider/grafana_datasource/grafana.rb b/lib/puppet/provider/grafana_datasource/grafana.rb index e6d13bec..152a5da5 100644 --- a/lib/puppet/provider/grafana_datasource/grafana.rb +++ b/lib/puppet/provider/grafana_datasource/grafana.rb @@ -52,6 +52,7 @@ def fetch_organization end def datasource_by_name + change_organization response = send_request('GET', format('%s/datasources/name/%s', resource[:grafana_api_path], ERB::Util.url_encode(resource[:name]))) return nil if response.code == '404' @@ -149,12 +150,15 @@ def secure_json_data {} end + def change_organization + 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' + end + def flush return if resource['ensure'] == :absent - # change organizations - 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' + change_organization # Build the `data` to POST/PUT by first creating a hash with some defaults which will be used if we're _creating_ a datasource data = {