Skip to content

Commit

Permalink
Create subpaths for provisioning_datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianrakel committed Jun 10, 2024
1 parent b6218f9 commit 18f6551
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -251,6 +252,14 @@ Plugins to be passed to `create_resources`, wraps around the

Default value: `{}`

##### <a name="-grafana--provisioning_dir"></a>`provisioning_dir`

Data type: `String`

Path to the grafana provisioning dir e.g /etc/grafana/provisioning

Default value: `'/etc/grafana/provisioning/'`

##### <a name="-grafana--provisioning_dashboards"></a>`provisioning_dashboards`

Data type: `Hash`
Expand Down
1 change: 1 addition & 0 deletions data/family/FreeBSD.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
45 changes: 45 additions & 0 deletions spec/acceptance/class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions spec/classes/grafana_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 18f6551

Please sign in to comment.