diff --git a/lib/puppet/type/grafana_dashboard_permission.rb b/lib/puppet/type/grafana_dashboard_permission.rb index b626414..2642103 100644 --- a/lib/puppet/type/grafana_dashboard_permission.rb +++ b/lib/puppet/type/grafana_dashboard_permission.rb @@ -1,87 +1,91 @@ # frozen_string_literal: true Puppet::Type.newtype(:grafana_dashboard_permission) do @doc = 'Manage dashboard permissions in Grafana' ensurable newparam(:name, namevar: true) do desc 'The name of the permission.' end newparam(:user) do desc 'User to add the permission for' validate do |value| raise ArgumentError, 'Only user or team can be set, not both' if value && @resource[:team] end end newparam(:team) do desc 'Team to add the permission for' validate do |value| raise ArgumentError, 'Only user or team can be set, not both' if value && @resource[:user] end end newparam(:dashboard) do desc 'Dashboard to modify permissions for' end newparam(:organization) do desc 'The name of the organization to add permission for' defaultto 'Main Org.' end newparam(:grafana_api_path) do desc 'The absolute path to the API endpoint' defaultto '/api' validate do |value| raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end newparam(:grafana_url) do desc 'The URL of the Grafana server' defaultto '' validate do |value| raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} 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(:permission) do desc 'The role to apply' newvalues(:Admin, :Edit, :View) end autorequire(:service) do 'grafana-server' end autorequire(:grafana_organization) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_organization)) } end autorequire(:grafana_user) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_user)) } end autorequire(:grafana_team) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_team)) } end autorequire(:grafana_dashboard) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_dashboard)) } end + + autorequire(:grafana_conn_validator) do + 'grafana' + end end diff --git a/lib/puppet/type/grafana_membership.rb b/lib/puppet/type/grafana_membership.rb index a49eecc..a391d2a 100644 --- a/lib/puppet/type/grafana_membership.rb +++ b/lib/puppet/type/grafana_membership.rb @@ -1,76 +1,80 @@ # frozen_string_literal: true Puppet::Type.newtype(:grafana_membership) do @doc = 'Manage resource memberships in Grafana' ensurable newparam(:name, namevar: true) do desc 'The name of the membership.' end newparam(:user_name) do desc 'The name of the user to add membership for' end newparam(:target_name) do desc 'The name of the target to add membership for' end newparam(:organization) do desc 'The name of the organization to add membership for (team only)' defaultto 'Main Org.' end newparam(:grafana_api_path) do desc 'The absolute path to the API endpoint' defaultto '/api' validate do |value| raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end newparam(:grafana_url) do desc 'The URL of the Grafana server' defaultto '' validate do |value| raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} 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 newparam(:membership_type) do desc 'The underlying type of the membership (organization, team)' newvalues(:organization, :team) end newproperty(:role) do desc 'The role to apply to the membership (Admin, Editor, Viewer)' newvalues(:Admin, :Editor, :Viewer) end autorequire(:service) do 'grafana-server' end autorequire(:grafana_organization) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_organization)) } end autorequire(:grafana_team) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_team)) } end autorequire(:grafana_membership) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_membership)) && r['membership_type'] == :organization } if self[:membership_type] == :team end + + autorequire(:grafana_conn_validator) do + 'grafana' + end end diff --git a/lib/puppet/type/grafana_team.rb b/lib/puppet/type/grafana_team.rb index f5a6c4a..b623173 100644 --- a/lib/puppet/type/grafana_team.rb +++ b/lib/puppet/type/grafana_team.rb @@ -1,76 +1,80 @@ # frozen_string_literal: true Puppet::Type.newtype(:grafana_team) do @doc = 'Manage teams in Grafana' ensurable newparam(:name, namevar: true) do desc 'The name of the team' 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 newparam(:organization) do desc 'The organization the team belongs to' defaultto 'Main Org.' end newparam(:email) do desc 'The email for the team' defaultto '' end newproperty(:home_dashboard) do desc 'The id or name of the home dashboard' defaultto 'Default' end newproperty(:theme) do desc 'The theme to use for the team' end newproperty(:timezone) do desc 'The timezone to use for the team' end autorequire(:service) do 'grafana-server' end autorequire(:grafana_dashboard) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_dashboard)) } end autorequire(:grafana_organization) do catalog.resources.select { |r| r.is_a?(Puppet::Type.type(:grafana_organization)) } end + + autorequire(:grafana_conn_validator) do + 'grafana' + end end diff --git a/spec/grafana_dashboard_permission_type_spec.rb b/spec/grafana_dashboard_permission_type_spec.rb index 5e45622..ebb4fc6 100644 --- a/spec/grafana_dashboard_permission_type_spec.rb +++ b/spec/grafana_dashboard_permission_type_spec.rb @@ -1,60 +1,72 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_dashboard_permission) do let(:gpermission) do described_class.new( title: 'foo_title', grafana_url: 'http://example.com/', grafana_api_path: '/api', user: 'foo_user', dashboard: 'foo_dashboard', permission: 'View', ensure: :present ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new title: 'foo_title', name: 'foo', grafana_url: 'example.com', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if grafana_api_path isn't properly formed" do expect do described_class.new title: 'foo_title', grafana_url: 'http://example.com', grafana_api_path: '/invalidpath', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid API path}) end it 'fails if both user and team set' do expect do described_class.new title: 'foo title', user: 'foo_user', team: 'foo_team' end.to raise_error(Puppet::Error, %r{Only user or team can be set, not both}) end it 'accepts valid parameters' do expect(gpermission[:user]).to eq('foo_user') expect(gpermission[:grafana_api_path]).to eq('/api') expect(gpermission[:grafana_url]).to eq('http://example.com/') expect(gpermission[:dashboard]).to eq('foo_dashboard') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gpermission relationship = gpermission.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gpermission.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gpermission expect(gpermission.autorequire).to be_empty end + + it 'autorequires grafana_conn_validator' do + catalog = Puppet::Resource::Catalog.new + validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') + catalog.add_resource validator + catalog.add_resource gpermission + + relationship = gpermission.autorequire.find do |rel| + (rel.source.to_s == 'Grafana_conn_validator[grafana]') && (rel.target.to_s == gpermission.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end end end diff --git a/spec/grafana_membership_type_spec.rb b/spec/grafana_membership_type_spec.rb index b0ef316..4886757 100644 --- a/spec/grafana_membership_type_spec.rb +++ b/spec/grafana_membership_type_spec.rb @@ -1,62 +1,74 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_membership) do let(:gmembership) do described_class.new( title: 'foo_title', user_name: 'foo_user', target_name: 'foo_target', grafana_url: 'http://example.com/', grafana_api_path: '/api', membership_type: 'organization', role: 'Viewer', ensure: :present ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new title: 'foo_title', name: 'foo', grafana_url: 'example.com', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if grafana_api_path isn't properly formed" do expect do described_class.new title: 'foo_title', grafana_url: 'http://example.com', grafana_api_path: '/invalidpath', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid API path}) end it 'fails if membership type not valid' do expect do described_class.new title: 'foo title', membership_type: 'foo' end.to raise_error(Puppet::Error, %r{Invalid value "foo"}) end it 'accepts valid parameters' do expect(gmembership[:user_name]).to eq('foo_user') expect(gmembership[:target_name]).to eq('foo_target') expect(gmembership[:grafana_api_path]).to eq('/api') expect(gmembership[:grafana_url]).to eq('http://example.com/') expect(gmembership[:membership_type]).to eq(:organization) end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gmembership relationship = gmembership.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gmembership.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gmembership expect(gmembership.autorequire).to be_empty end + + it 'autorequires grafana_conn_validator' do + catalog = Puppet::Resource::Catalog.new + validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') + catalog.add_resource validator + catalog.add_resource gmembership + + relationship = gmembership.autorequire.find do |rel| + (rel.source.to_s == 'Grafana_conn_validator[grafana]') && (rel.target.to_s == gmembership.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end end end diff --git a/spec/unit/puppet/type/grafana_dashboard_type_spec.rb b/spec/unit/puppet/type/grafana_dashboard_type_spec.rb index a59d53e..32b76e6 100644 --- a/spec/unit/puppet/type/grafana_dashboard_type_spec.rb +++ b/spec/unit/puppet/type/grafana_dashboard_type_spec.rb @@ -1,61 +1,72 @@ # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_dashboard) do let(:gdashboard) do described_class.new name: 'foo', grafana_url: 'http://example.com/', content: '{}', ensure: :present end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if content isn't provided" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com', ensure: :present end.to raise_error(Puppet::Error, %r{content is required}) end it "fails if content isn't JSON" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com/', content: '{invalid', ensure: :present end.to raise_error(Puppet::Error, %r{Invalid JSON}) end it 'accepts valid parameters' do expect(gdashboard[:name]).to eq('foo') expect(gdashboard[:grafana_url]).to eq('http://example.com/') expect(gdashboard[:content]).to eq({}) end it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gdashboard relationship = gdashboard.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gdashboard.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gdashboard expect(gdashboard.autorequire).to be_empty end + it 'autorequires grafana_conn_validator' do + catalog = Puppet::Resource::Catalog.new + validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') + catalog.add_resource validator + catalog.add_resource gdashboard + + relationship = gdashboard.autorequire.find do |rel| + (rel.source.to_s == 'Grafana_conn_validator[grafana]') && (rel.target.to_s == gdashboard.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end end end diff --git a/spec/unit/puppet/type/grafana_folder_type_spec.rb b/spec/unit/puppet/type/grafana_folder_type_spec.rb index 7d01ac1..f3ffa10 100644 --- a/spec/unit/puppet/type/grafana_folder_type_spec.rb +++ b/spec/unit/puppet/type/grafana_folder_type_spec.rb @@ -1,54 +1,65 @@ # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_folder) do let(:gfolder) do described_class.new name: 'foo', grafana_url: 'http://example.com/', ensure: :present end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if grafana_api_path isn't properly formed" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com', grafana_api_path: '/invalidpath', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid API path}) end it 'accepts valid parameters' do expect(gfolder[:name]).to eq('foo') expect(gfolder[:grafana_url]).to eq('http://example.com/') end it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gfolder relationship = gfolder.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gfolder.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gfolder expect(gfolder.autorequire).to be_empty end + it 'autorequires grafana_conn_validator' do + catalog = Puppet::Resource::Catalog.new + validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') + catalog.add_resource validator + catalog.add_resource gfolder + + relationship = gfolder.autorequire.find do |rel| + (rel.source.to_s == 'Grafana_conn_validator[grafana]') && (rel.target.to_s == gfolder.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end end end diff --git a/spec/unit/puppet/type/grafana_organization_type_spec.rb b/spec/unit/puppet/type/grafana_organization_type_spec.rb index 9fb7dd6..e195920 100644 --- a/spec/unit/puppet/type/grafana_organization_type_spec.rb +++ b/spec/unit/puppet/type/grafana_organization_type_spec.rb @@ -1,53 +1,65 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_organization) do let(:gorganization) do described_class.new( name: 'foo', grafana_url: 'http://example.com', grafana_user: 'admin', grafana_password: 'admin', address: { address1: 'test address1', address2: 'test address2', city: 'CityName', state: 'NewState', zipcode: '12345', country: 'USA' } ) end context 'when setting parameters' do it "fails if json_data isn't valid" do expect do described_class.new name: 'foo', address: 'invalid address' end.to raise_error(Puppet::Error, %r{address should be a Hash!}) end it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it 'accepts valid parameters' do expect(gorganization[:name]).to eq('foo') expect(gorganization[:grafana_user]).to eq('admin') expect(gorganization[:grafana_password]).to eq('admin') expect(gorganization[:grafana_url]).to eq('http://example.com') expect(gorganization[:address]).to eq(address1: 'test address1', address2: 'test address2', city: 'CityName', state: 'NewState', zipcode: '12345', country: 'USA') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gorganization relationship = gorganization.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gorganization.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gorganization expect(gorganization.autorequire).to be_empty end + + it 'autorequires grafana_conn_validator' do + catalog = Puppet::Resource::Catalog.new + validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') + catalog.add_resource validator + catalog.add_resource gorganization + + relationship = gorganization.autorequire.find do |rel| + (rel.source.to_s == 'Grafana_conn_validator[grafana]') && (rel.target.to_s == gorganization.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end end end diff --git a/spec/unit/puppet/type/grafana_team_type_spec.rb b/spec/unit/puppet/type/grafana_team_type_spec.rb index f2e2f77..e4cea6a 100644 --- a/spec/unit/puppet/type/grafana_team_type_spec.rb +++ b/spec/unit/puppet/type/grafana_team_type_spec.rb @@ -1,49 +1,61 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_team) do let(:gteam) do described_class.new( name: 'foo', grafana_url: 'http://example.com', grafana_user: 'admin', grafana_password: 'admin', home_dashboard: 'foo_dashboard', organization: 'foo_organization' ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it 'accepts valid parameters' do expect(gteam[:name]).to eq('foo') expect(gteam[:grafana_user]).to eq('admin') expect(gteam[:grafana_password]).to eq('admin') expect(gteam[:grafana_url]).to eq('http://example.com') expect(gteam[:home_dashboard]).to eq('foo_dashboard') expect(gteam[:organization]).to eq('foo_organization') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gteam relationship = gteam.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gteam.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gteam expect(gteam.autorequire).to be_empty end + + it 'autorequires grafana_conn_validator' do + catalog = Puppet::Resource::Catalog.new + validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') + catalog.add_resource validator + catalog.add_resource gteam + + relationship = gteam.autorequire.find do |rel| + (rel.source.to_s == 'Grafana_conn_validator[grafana]') && (rel.target.to_s == gteam.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end end end diff --git a/spec/unit/puppet/type/grafana_user_type_spec.rb b/spec/unit/puppet/type/grafana_user_type_spec.rb index 0e0f0a7..c45aa90 100644 --- a/spec/unit/puppet/type/grafana_user_type_spec.rb +++ b/spec/unit/puppet/type/grafana_user_type_spec.rb @@ -1,48 +1,59 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_user) do let(:guser) do described_class.new name: 'test', full_name: 'Mr tester', password: 't3st', grafana_url: 'http://example.com/' end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'test', grafana_url: 'example.com' end.to raise_error(Puppet::Error, %r{not a valid URL}) end it 'accepts valid parameters' do expect(guser[:name]).to eq('test') expect(guser[:full_name]).to eq('Mr tester') expect(guser[:password]).to eq('t3st') expect(guser[:grafana_url]).to eq('http://example.com/') end it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource guser relationship = guser.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == guser.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource guser expect(guser.autorequire).to be_empty end + it 'autorequires grafana_conn_validator' do + catalog = Puppet::Resource::Catalog.new + validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') + catalog.add_resource validator + catalog.add_resource guser + + relationship = guser.autorequire.find do |rel| + (rel.source.to_s == 'Grafana_conn_validator[grafana]') && (rel.target.to_s == guser.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end end end