From 18f655183d75f57e183cb7e4ff1df34671575a41 Mon Sep 17 00:00:00 2001 From: Sebastian Rakel Date: Fri, 7 Jun 2024 13:46:57 +0200 Subject: [PATCH] Create subpaths for provisioning_datasources --- REFERENCE.md | 9 +++++++ data/family/FreeBSD.yaml | 1 + manifests/config.pp | 10 ++++++++ manifests/init.pp | 4 ++++ spec/acceptance/class_spec.rb | 45 +++++++++++++++++++++++++++++++++++ spec/classes/grafana_spec.rb | 30 +++++++++++++++++++++++ 6 files changed, 99 insertions(+) diff --git a/REFERENCE.md b/REFERENCE.md index b60eb643..14eadb96 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -82,6 +82,7 @@ The following parameters are available in the `grafana` class: * [`repo_release`](#-grafana--repo_release) * [`repo_url`](#-grafana--repo_url) * [`plugins`](#-grafana--plugins) +* [`provisioning_dir`](#-grafana--provisioning_dir) * [`provisioning_dashboards`](#-grafana--provisioning_dashboards) * [`provisioning_datasources`](#-grafana--provisioning_datasources) * [`provisioning_dashboards_file`](#-grafana--provisioning_dashboards_file) @@ -251,6 +252,14 @@ Plugins to be passed to `create_resources`, wraps around the Default value: `{}` +##### `provisioning_dir` + +Data type: `String` + +Path to the grafana provisioning dir e.g /etc/grafana/provisioning + +Default value: `'/etc/grafana/provisioning/'` + ##### `provisioning_dashboards` Data type: `Hash` diff --git a/data/family/FreeBSD.yaml b/data/family/FreeBSD.yaml index 40e56354..e9e68fc2 100644 --- a/data/family/FreeBSD.yaml +++ b/data/family/FreeBSD.yaml @@ -1,5 +1,6 @@ --- grafana::cfg_location: '/usr/local/etc/grafana.ini' +grafana::provisioning_dir: '/usr/local/etc/grafana/provisioning' grafana::data_dir: '/var/db/grafana' grafana::install_method: 'repo' grafana::manage_package_repo: false diff --git a/manifests/config.pp b/manifests/config.pp index 74400ca1..6b8ac1ae 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -161,6 +161,16 @@ if (length($pdatasources) >= 1) { # template uses: # - pdatasources + + $datasources_provisioning_dir = "${grafana::provisioning_dir}/datasources," + + file { $datasources_provisioning_dir: + ensure => directory, + owner => 'grafana', + group => 'grafana', + mode => '0750', + } + file { $grafana::provisioning_datasources_file: ensure => file, owner => 'grafana', diff --git a/manifests/init.pp b/manifests/init.pp index 5ba39388..db661da7 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -68,6 +68,9 @@ # Plugins to be passed to `create_resources`, wraps around the # `grafana_plugin` resource. # +# @param provisioning_dir +# Path to the grafana provisioning dir e.g /etc/grafana/provisioning +# # @param provisioning_dashboards # Dashboards to provision into grafana. grafana > v5.0.0 # required. Will be converted into YAML and used by grafana to @@ -167,6 +170,7 @@ Hash $plugins = {}, Hash $provisioning_dashboards = {}, Hash $provisioning_datasources = {}, + String $provisioning_dir = '/etc/grafana/provisioning/', String $provisioning_dashboards_file = '/etc/grafana/provisioning/dashboards/puppetprovisioned.yaml', String $provisioning_datasources_file = '/etc/grafana/provisioning/datasources/puppetprovisioned.yaml', Boolean $create_subdirs_provisioning = false, diff --git a/spec/acceptance/class_spec.rb b/spec/acceptance/class_spec.rb index c6c2cd24..f1f5e09d 100644 --- a/spec/acceptance/class_spec.rb +++ b/spec/acceptance/class_spec.rb @@ -118,6 +118,51 @@ class { 'grafana': apply_manifest(pp, catch_changes: true) end end + + context 'with fancy dashboard config and custom target file and create_subdirs_provisioning' do + it 'works idempotently with no errors' do + pp = <<-EOS + class { 'grafana': + version => "#{grafana_version}", + create_subdirs_provisioning => true, + provisioning_datasources => { + apiVersion => 1, + datasources => [ + { + name => 'Prometheus', + type => 'prometheus', + access => 'proxy', + url => 'http://localhost:9090/prometheus', + isDefault => false, + }, + ], + }, + provisioning_dashboards => { + apiVersion => 1, + providers => [ + { + name => 'default', + orgId => 1, + folder => '', + type => 'file', + disableDeletion => true, + options => { + path => '/var/lib/grafana/dashboards', + puppetsource => 'puppet:///modules/my_custom_module/dashboards', + }, + }, + ], + }, + provisioning_dashboards_file => '/etc/grafana/provisioning/dashboards/dashboard.yaml', + provisioning_datasources_file => '/etc/grafana/provisioning/datasources/datasources.yaml' + } + EOS + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + end end end diff --git a/spec/classes/grafana_spec.rb b/spec/classes/grafana_spec.rb index aa89b0e2..17b5f2ea 100644 --- a/spec/classes/grafana_spec.rb +++ b/spec/classes/grafana_spec.rb @@ -532,6 +532,36 @@ end end + context 'provisioning_datasources defined and create_subdirs_provisioning is true' do + let(:params) do + { + version: '11.0.0', + create_subdirs_provisioning: true, + provisioning_datasources: { + apiVersion: 1, + datasources: [ + { + name: 'Prometheus', + type: 'prometheus', + access: 'proxy', + url: 'http://localhost:9090/', + isDefault: true, + } + ] + } + } + end + + it do + expect(subject).to contain_file('/etc/grafana/provisioning/datasources/puppetprovisioned.yaml').with( + ensure: 'file', + owner: 'grafana', + group: 'grafana', + mode: '0640' + ) + end + end + context 'sysconfig environment variables' do let(:params) do {