forked from bfraser/puppet-grafana
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create grafana_plugin resource type, and create grafana::plugins pass…
…through
- Loading branch information
William Yardley
committed
Sep 20, 2017
1 parent
7878f13
commit 15caac0
Showing
10 changed files
with
232 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |