Skip to content

Commit

Permalink
Create grafana_plugin resource type, and create grafana::plugins pass…
Browse files Browse the repository at this point in the history
…through
  • Loading branch information
William Yardley committed Sep 20, 2017
1 parent 7878f13 commit 15caac0
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 37 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ Grafana repositories are enabled on your host. If true, the official Grafana
repositories will be enabled. If false, the module assumes you are managing your
own package repository and will not set one up for you. Defaults to true.

##### `plugins`

Hash. This is a passthrough to call `create_resources()` on the
`grafana_plugin` resource type.

##### `package_name`

The name of the package managed with the 'package' install method. Defaults to
Expand Down Expand Up @@ -333,7 +338,7 @@ Example:

#### Custom Types and Providers

The module includes two custom types: `grafana_dashboard` and `grafana_datasource`
The module includes several custom types:

##### `grafana_dashboard`

Expand Down Expand Up @@ -380,14 +385,15 @@ from the browser, or `proxy` to send requests via grafana.
Authentication is optional, as is `database`; additional `json_data` can be
provided to allow custom configuration options.

##### `grafana::plugin`
##### `grafana_plugin`

There exists a custom defined resource which wraps grafana-cli to install
plugins. Deinstallation isn't supported right now, also docker support
completely missing. Example usage:
An example is provided for convenience; for more details, please view the
puppet strings docs.

```puppet
grafana::plugin{'grafana-simple-json-datasource':}
grafana_plugin { 'grafana-simple-json-datasource':
ensure => present,
}
```

##### `grafana::user`
Expand Down
56 changes: 56 additions & 0 deletions lib/puppet/provider/grafana_plugin/grafana_cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Puppet::Type.type(:grafana_plugin).provide(:grafana_cli) do
has_command(:grafana_cli, 'grafana-cli') do
is_optional
end

defaultfor feature: :posix

mk_resource_methods

def self.all_plugins
plugins = []
grafana_cli('plugins', 'ls').split(%r{\n}).each do |line|
next unless line =~ %r{^(\S+)\s+@\s+((?:\d\.).+)\s*$}
name = Regexp.last_match(1)
version = Regexp.last_match(2)
Puppet.debug("Found grafana plugin #{name} #{version}")
plugins.push(name)
end
plugins.sort
end

def self.instances
resources = []
all_plugins.each do |name|
plugin = {
ensure: :present,
name: name
}
resources << new(plugin) if plugin[:name]
end
resources
end

def self.prefetch(resources)
plugins = instances
resources.keys.each do |name|
if (provider = plugins.find { |plugin| plugin.name == name })
resources[name].provider = provider
end
end
end

def exists?
@property_hash[:ensure] == :present
end

def create
grafana_cli('plugins', 'install', resource[:name])
@property_hash[:ensure] = :present
end

def destroy
grafana_cli('plugins', 'uninstall', resource[:name])
@property_hash[:ensure] = :absent
end
end
31 changes: 31 additions & 0 deletions lib/puppet/type/grafana_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Puppet::Type.newtype(:grafana_plugin) do
desc <<-DESC
manages grafana plugins
@example Install a grafana plugin
grafana_plugin { 'grafana-simple-json-datasource': }
@example Uninstall a grafana plugin
grafana_plugin { 'grafana-simple-json-datasource':
ensure => absent,
}
@example Show resources
$ puppet resource grafana_plugin
DESC

ensurable do
defaultto(:present)
newvalue(:present) do
provider.create
end
newvalue(:absent) do
provider.destroy
end
end

newparam(:name, namevar: true) do
desc 'The name of the plugin to enable'
newvalues(%r{^\S+$})
end
end
12 changes: 11 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
# Set to 'testing' to install beta versions
# Defaults to stable.
#
# [*plugins*]
# A hash of plugins to be passed to `create_resources`, wraps around the
# `grafana_plugin` resource.
#
# === Examples
#
# class { '::grafana':
Expand All @@ -84,7 +88,8 @@
$repo_name = $::grafana::params::repo_name,
$rpm_iteration = $::grafana::params::rpm_iteration,
$service_name = $::grafana::params::service_name,
$version = $::grafana::params::version
$version = $::grafana::params::version,
$plugins = {}
) inherits grafana::params {

# validate parameters here
Expand All @@ -99,4 +104,9 @@
Class['grafana::install']
-> Class['grafana::config']
~> Class['grafana::service']

create_resources(grafana_plugin, $plugins)

Grafana_Plugin <| |> ~> Class['grafana::service']

}
10 changes: 0 additions & 10 deletions manifests/plugin.pp

This file was deleted.

38 changes: 38 additions & 0 deletions spec/acceptance/grafana_plugin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper_acceptance'

describe 'grafana_plugin' do
context 'create plugin resource' do
it 'runs successfully' do
pp = <<-EOS
class { 'grafana':}
grafana_plugin { 'grafana-simple-json-datasource': }
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

it 'has the plugin' do
shell('grafana-cli plugins ls') do |r|
expect(r.stdout).to match(%r{grafana-simple-json-datasource})
end
end
end
context 'destroy plugin resource' do
it 'runs successfully' do
pp = <<-EOS
class { 'grafana':}
grafana_plugin { 'grafana-simple-json-datasource':
ensure => absent,
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

it 'does not have the plugin' do
shell('grafana-cli plugins ls') do |r|
expect(r.stdout).not_to match(%r{grafana-simple-json-datasource})
end
end
end
end
15 changes: 15 additions & 0 deletions spec/classes/grafana_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@
end
end

context 'with some plugins passed in' do
let(:params) do
{
plugins:
{
'grafana-wizzle' => { 'ensure' => 'present' },
'grafana-woozle' => { 'ensure' => 'absent' }
}
}
end

it { is_expected.to contain_grafana_plugin('grafana-wizzle').with(ensure: 'present') }
it { is_expected.to contain_grafana_plugin('grafana-woozle').with(ensure: 'absent').that_notifies('Class[grafana::service]') }
end

context 'with parameter install_method is set to repo' do
let(:params) do
{
Expand Down
20 changes: 0 additions & 20 deletions spec/defines/plugin_spec.rb

This file was deleted.

53 changes: 53 additions & 0 deletions spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'spec_helper'

provider_class = Puppet::Type.type(:grafana_plugin).provider(:grafana_cli)
describe provider_class do
let(:resource) do
Puppet::Type::Grafana_plugin.new(
name: 'grafana-wizzle'
)
end
let(:provider) { provider_class.new(resource) }

describe '#instances' do
# rubocop:disable Layout/TrailingWhitespace
provider_class.expects(:grafana_cli).with('plugins', 'ls').returns <<-EOT
installed plugins:
grafana-simple-json-datasource @ 1.3.4
jdbranham-diagram-panel @ 1.4.0
Restart grafana after installing plugins . <service grafana-server restart>
EOT
# rubocop:enable Layout/TrailingWhitespace
instances = provider_class.instances
it 'has the right number of instances' do
expect(instances.size).to eq(2)
end

it 'has the correct names' do
names = instances.map(&:name)
expect(names).to include('grafana-simple-json-datasource', 'jdbranham-diagram-panel')
end

it 'does not match if there are no plugins' do
provider_class.expects(:grafana_cli).with('plugins', 'ls').returns <<-EOT
Restart grafana after installing plugins . <service grafana-server restart>
EOT
instances = provider_class.instances
expect(provider.exists?).to eq(false)
end
end

it '#create' do
provider.expects(:grafana_cli).with('plugins', 'install', 'grafana-wizzle')
provider.create
end

it '#destroy' do
provider.expects(:grafana_cli).with('plugins', 'uninstall', 'grafana-wizzle')
provider.destroy
end
end
16 changes: 16 additions & 0 deletions spec/unit/puppet/type/grafana_plugin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'
describe Puppet::Type.type(:grafana_plugin) do
let(:plugin) do
Puppet::Type.type(:grafana_plugin).new(name: 'grafana-whatsit')
end

it 'accepts a plugin name' do
plugin[:name] = 'plugin-name'
expect(plugin[:name]).to eq('plugin-name')
end
it 'requires a name' do
expect do
Puppet::Type.type(:grafana_plugin).new({})
end.to raise_error(Puppet::Error, 'Title or name must be provided')
end
end

0 comments on commit 15caac0

Please sign in to comment.