Skip to content

Commit

Permalink
Add support for multi-build assets (#88)
Browse files Browse the repository at this point in the history
* first pass at adding support for multi-build assets

* satisfy cookstyle
  • Loading branch information
Cameron Johnston authored Jul 27, 2020
1 parent caa7436 commit 0b2c3c6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
11 changes: 7 additions & 4 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ def check_from_resource

def asset_from_resource
spec = {}
spec['sha512'] = new_resource.sha512
spec['url'] = new_resource.url
spec['filters'] = new_resource.filters if new_resource.filters

if new_resource.builds.empty?
spec['sha512'] = new_resource.sha512
spec['url'] = new_resource.url
spec['filters'] = new_resource.filters if new_resource.filters
else
spec['builds'] = new_resource.builds
end
a = base_resource(new_resource, spec)
a['metadata']['namespace'] = new_resource.namespace
a
Expand Down
51 changes: 39 additions & 12 deletions resources/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,54 @@
provides :sensu_asset

property :filters, Array
property :sha512, String, required: true
property :url, String, required: true
property :sha512, String, required: false
property :url, String, required: false
property :builds, Array, default: [], required: false, callbacks: {
'should be an array of hashes' => lambda do |arry|
arry.all? do |e|
e.respond_to?(:keys)
end
end,

'each build should contain sha512 and url' => lambda do |arry|
arry.all? do |e|
e.key?('url') && e.key?('sha512')
end
end,

'build filters should be an array' => lambda do |arry|
arry.all? do |e|
if e.key?('filters')
e['filters'].is_a?(Array)
else
true
end
end
end,
}
property :namespace, String, default: 'default'

action_class do
include SensuCookbook::Helpers
end

action :create do
directory object_dir do
action :create
recursive true
end
if !property_is_set?(:url) && !property_is_set?(:sha512) && new_resource.builds.empty?
raise Chef::Exceptions::ValidationFailed
else
directory object_dir do
action :create
recursive true
end

file object_file do
content JSON.generate(asset_from_resource)
notifies :run, "execute[sensuctl create -f #{object_file}]"
end
file object_file do
content JSON.generate(asset_from_resource)
notifies :run, "execute[sensuctl create -f #{object_file}]"
end

execute "sensuctl create -f #{object_file}" do
action :nothing
execute "sensuctl create -f #{object_file}" do
action :nothing
end
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/cookbooks/sensu_test/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@
end
end

sensu_asset 'multi-build' do
builds [
{
'filters' => [
"entity.system.os == 'linux'",
"entity.system.arch == 'amd64'",
"entity.system.platform_family == 'debian'",
"entity.system.platform_version.split('.')[0] == '9'",
],
'sha512' => 'a909f6eef2785302f648d5289fddfdd97014984e25751abb94ea70226ef8c5e56f9e333c054d79734c5f165f93494f34943aa9aa1ed06297fac599ff57328c27',
'url' => 'https://assets.bonsai.sensu.io/058af8cde8fbdd97cfebf81a2565346e404210d5/sensu-plugins-postgres_4.0.0-pre-jef.1_debian9_linux_amd64.tar.gz',
},
{
'filters' => [
"entity.system.os == 'linux'",
"entity.system.arch == 'amd64'",
"entity.system.platform_family == 'debian'",
],
'sha512' => 'b832ba248472e6c2713f60e946af322a8373e2144e6331afa6663ca13818b3c192e63f507dca1126d9b83b6c636b176a0e8e10a2a42292f457bf099f806ca57f',
'url' => 'https://assets.bonsai.sensu.io/058af8cde8fbdd97cfebf81a2565346e404210d5/sensu-plugins-postgres_4.0.0-pre-jef.1_debian_linux_amd64.tar.gz',
},
{
'filters' => [],
'sha512' => '555889017fcbcc6319f08097b79c3efccc3bf8ed0f8e59913ff7a508d4fe3d3b347118de11961657cd8504bdfd32af51f3c4add4ac639e378e82a05178248853',
'url' => 'https://assets.bonsai.sensu.io/058af8cde8fbdd97cfebf81a2565346e404210d5/sensu-plugins-postgres_4.0.0-pre-jef.1_centos6_linux_amd64.tar.gz',
},
]
end

sensu_handler 'slack' do
type 'pipe'
env_vars ['SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX']
Expand Down
8 changes: 8 additions & 0 deletions test/integration/default/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
end
end

describe json('/etc/sensu/assets/multi-build.json') do
require 'uri'
its(%w(type)) { should eq 'Asset' }
its(%w(metadata name)) { should eq 'multi-build' }
its(%w(metadata namespace)) { should eq 'default' }
its(['spec', 'builds', 0, 'url']) { should match URI::DEFAULT_PARSER.make_regexp }
end

describe json('/etc/sensu/assets/sensu-plugins-disk-checks.json') do
require 'uri'
its(%w(type)) { should eq 'Asset' }
Expand Down

0 comments on commit 0b2c3c6

Please sign in to comment.