diff --git a/lib/puppet/provider/grafana_organization/grafana.rb b/lib/puppet/provider/grafana_organization/grafana.rb index c0fe8e9..9cf0239 100644 --- a/lib/puppet/provider/grafana_organization/grafana.rb +++ b/lib/puppet/provider/grafana_organization/grafana.rb @@ -1,102 +1,107 @@ require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) Puppet::Type.type(:grafana_organization).provide(:grafana, parent: Puppet::Provider::Grafana) do desc 'Support for Grafana organizations' defaultfor kernel: 'Linux' def organizations response = send_request('GET', format('%s/orgs', resource[:grafana_api_path])) if response.code != '200' raise format('Failed to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) end begin organizations = JSON.parse(response.body) organizations.map { |x| x['id'] }.map do |id| response = send_request 'GET', format('%s/orgs/%s', resource[:grafana_api_path], id) if response.code != '200' raise format('Failed to retrieve organization %d (HTTP response: %s/%s)', id, response.code, response.body) end organization = JSON.parse(response.body) { id: organization['id'], name: organization['name'], address: organization['address'] } end rescue JSON::ParserError raise format('Failed to parse response: %s', response.body) end end def organization unless @organization @organization = organizations.find { |x| x[:name] == resource[:name] } end @organization end attr_writer :organization # rubocop:enable Style/PredicateName def id organization[:id] end def id=(value) resource[:id] = value save_organization end def address organization[:json_data] end def address=(value) resource[:address] = value save_organization end def save_organization data = { id: resource[:id], name: resource[:name], address: resource[:address] } response = send_request('POST', format('%s/orgs', resource[:grafana_api_path]), data) if organization.nil? if response.code != '200' raise format('Failed to create save %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) end self.organization = nil end def delete_organization response = send_request 'DELETE', format('%s/orgs/%s', resource[:grafana_api_path], organization[:id]) if response.code != '200' raise format('Failed to delete organization %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) end + # change back to default organization + response = send_request 'POST', format('%s/user/using/1', resource[:grafana_api_path]) + unless response.code == '200' + raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) + end self.organization = nil end def create save_organization end def destroy delete_organization end def exists? organization end end diff --git a/lib/puppet/type/grafana_organization.rb b/lib/puppet/type/grafana_organization.rb index 01a521a..5c60e96 100644 --- a/lib/puppet/type/grafana_organization.rb +++ b/lib/puppet/type/grafana_organization.rb @@ -1,59 +1,60 @@ Puppet::Type.newtype(:grafana_organization) do @doc = 'Manage organizations in Grafana' - ensurable do - defaultvalues - defaultto :present - end + ensurable newparam(:name, namevar: true) do desc 'The name of the organization.' + + validate do |value| + raise ArgumentError, format('Unable to modify default organization') if value == 'Main Org.' + end end newparam(:grafana_api_path) do desc 'The absolute path to the API endpoint' defaultto '/api' validate do |value| unless value =~ %r{^/.*/?api$} raise ArgumentError, format('%s is not a valid API path', value) end end end newparam(:grafana_url) do desc 'The URL of the Grafana server' defaultto '' validate do |value| unless value =~ %r{^https?://} raise ArgumentError, format('%s is not a valid URL', value) end end end newparam(:grafana_user) do desc 'The username for the Grafana server' end newparam(:grafana_password) do desc 'The password for the Grafana server' end newproperty(:id) do desc 'The ID of the organization' end newproperty(:address) do desc 'Additional JSON data to configure the organization address (optional)' validate do |value| unless value.nil? || value.is_a?(Hash) raise ArgumentError, 'address should be a Hash!' end end end autorequire(:service) do 'grafana-server' end end