Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input resource #104

Open
wants to merge 12 commits into
base: api-support
Choose a base branch
from
1 change: 1 addition & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ suites:
- recipe[graylog2::collector_sidecar]
- recipe[graylog2::authbind]
- recipe[graylog2::api]
- recipe[api_resources_test]
- name: openjdk
attributes:
machine_fqdn: graylog.local
Expand Down
1 change: 1 addition & 0 deletions Berksfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ group :integration do

cookbook "apt_backports", :path => "./test/fixtures/cookbooks/apt_backports"
cookbook "elasticsearch_test", :path => "./test/fixtures/cookbooks/elasticsearch_test"
cookbook "api_resources_test", :path => "./test/fixtures/cookbooks/api_resources_test"
end
4 changes: 4 additions & 0 deletions Berksfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
DEPENDENCIES
api_resources_test
path: test/fixtures/cookbooks/api_resources_test
apt
apt_backports
path: test/fixtures/cookbooks/apt_backports
Expand All @@ -14,6 +16,8 @@ DEPENDENCIES
mongodb

GRAPH
api_resources_test (0.0.1)
graylog2 (>= 0.0.0)
apt (4.0.1)
compat_resource (>= 12.10)
apt_backports (0.0.1)
Expand Down
17 changes: 17 additions & 0 deletions libraries/api_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Extensions
module ApiHelper
def auth_params(new_resource)
{
base_url: new_resource.api_uri || node['graylog2']['rest']['listen_uri'],
token: new_resource.api_token || node['graylog2']['rest']['admin_access_token']
}
end

def keys_to_symbols(hash)
hash.each_with_object({}) do |(k, v), new_hash|
new_hash[k.to_sym] = v
new_hash
end
end
end
end
9 changes: 4 additions & 5 deletions resources/check_api.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
property :uri, String, name_property: true
property :token, String
property :api_uri, [String, nil], name_property: true
property :api_token, [String, nil], default: nil

default_action :check

action :check do
require 'graylogapi'
extend Extensions::ApiHelper

log 'Wait until the Graylog2 API is ready' do
level :info
end

url = new_resource.uri || node['graylog2']['rest']['listen_uri']
token = new_resource.token || node['graylog2']['rest']['admin_access_token']
retries = node['graylog2']['api_client_timeout'] || 300

graylogapi = GraylogAPI.new(base_url: url, token: token)
graylogapi = GraylogAPI.new(auth_params(new_resource))

begin
graylogapi.client.request(:get, '/')
Expand Down
83 changes: 83 additions & 0 deletions resources/input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
property :title, String, name_property: true, required: true
property :hostname, [String, nil], default: nil
property :type, String, required: true
property :settings, Hash, required: true

# api auth settings
property :api_uri, [String, nil], default: nil
property :api_token, [String, nil], default: nil

default_action :create

# this field use for update request
attr_reader :input_id

load_current_value do
require 'graylogapi'
extend Extensions::ApiHelper
graylogapi = GraylogAPI.new(auth_params(self))

graylog_inputs = graylogapi.system.inputs
inputs = graylog_inputs.all['inputs']
current_input = inputs.find { |i| i['title'] == title }

current_value_does_not_exist! if current_input.nil?

node = graylogapi.system.cluster.node_by_id(current_input['node'])

title current_input['title']
type graylog_inputs.types.type_to_name(current_input['type'])
hostname current_input['global'] ? nil : node['hostname']
settings(configuration: keys_to_symbols(current_input['attributes']))

# this use for update request
@input_id = current_input['id']
end

action :create do
converge_if_changed do
require 'graylogapi'
extend Extensions::ApiHelper
graylogapi = GraylogAPI.new(auth_params(new_resource))
graylogapi_inputs = graylogapi.system.inputs

options = {
title: new_resource.title,
global: new_resource.hostname.nil?,
type: graylogapi_inputs.types.name_to_type(new_resource.type),
}.merge(new_resource.settings)

unless new_resource.hostname.nil?
options[:node] = graylogapi.system.cluster.node_by_hostname(new_resource.hostname)['node_id']
end

response =
if current_resource.nil?
graylogapi_inputs.create(options)
else
graylogapi_inputs.update(current_resource.input_id, options)
end

log 'Can`t create input' do
message response.body.to_s
level :fatal
only_if { response.fail? }
end
end
end

action :delete do
require 'graylogapi'
extend Extensions::ApiHelper
graylogapi = GraylogAPI.new(auth_params(new_resource))

input = graylogapi.system.inputs.all['inputs'].find { |i| i['title'] == 'global beats input' }

if input.nil?
log 'Input already deleted' do
level :info
end
else
graylogapi.system.inputs.delete(input['id'])
end
end
Empty file.
9 changes: 9 additions & 0 deletions test/fixtures/cookbooks/api_resources_test/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name 'api_resources_test'
maintainer 'Marius Sturm'
maintainer_email '[email protected]'
license 'Apache 2.0'
description 'A wrapper cookbook used for Graylog testing'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.0.1'

depends 'graylog2'
10 changes: 10 additions & 0 deletions test/fixtures/cookbooks/api_resources_test/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
graylog2_input 'global beats input' do
type 'Beats'
settings(configuration: { bind_address: '0.0.0.0', port: 5044 })
end

graylog2_input 'local syslog tcp input' do
hostname 'graylog.local'
type 'Syslog TCP'
settings(configuration: { bind_address: '0.0.0.0', port: 5015 })
end