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 = {