Skip to content

add clickhouse_table provider #5

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/clickhouse_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Puppet::Parser::Functions
@summary
Convert hash to Clickhouse XML config.
@param [Hash] Settings for Clickhouse Server.
@return [Xml] Сlickhouse XML configuration.
@return [Xml] Clickhouse XML configuration.
EOS
) do |args|

Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/clickhouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class Puppet::Provider::Clickhouse < Puppet::Provider
commands :clickhouse_raw => 'clickhouse-client'

def self.clickhouse_caller(text_of_sql)
clickhouse_raw(['-c', '/root/.clickhouse-client/config.xml', '-q', text_of_sql].flatten.compact)
clickhouse_raw(['-c', '/etc/clickhouse-client/config.xml', '-q', text_of_sql].flatten.compact)
end
end
4 changes: 2 additions & 2 deletions lib/puppet/provider/clickhouse_database/clickhouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def self.prefetch(resources)
end

def create
self.class.clickhouse_caller("create database if not exists `#{@resource[:name]}`")
self.class.clickhouse_caller("create database if not exists #{@resource[:name]}")

@property_hash[:ensure] = :present

exists? ? (return true) : (return false)
end

def destroy
self.class.clickhouse_caller("drop database if exists `#{@resource[:name]}`")
self.class.clickhouse_caller("drop database if exists #{@resource[:name]}")

@property_hash.clear
exists? ? (return false) : (return true)
Expand Down
47 changes: 47 additions & 0 deletions lib/puppet/provider/clickhouse_table/clickhouse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'clickhouse'))
Puppet::Type.type(:clickhouse_table).provide(:clickhouse, parent: Puppet::Provider::Clickhouse) do
desc 'Manages Clickhouse table.'

commands clickhouse_raw: 'clickhouse-client'

def self.instances
clickhouse_caller('show tables').split("\n").map do |name|
new(name: name,
ensure: :present)
end
end

# We iterate over each mysql_database entry in the catalog and compare it against
# the contents of the property_hash generated by self.instances
def self.prefetch(resources)
databases = instances
resources.keys.each do |database|
provider = databases.find { |db| db.name == database }
resources[database].provider = provider if provider
end
end

def create
partition = if defined? (@resource[:partition]) then "partition by #{@resource[:partition]}" else "" end
order = if defined? (@resource[:order]) then "order by (#{@resource[:order].join(', ')})" else "" end
self.class.clickhouse_caller("create table if not exists #{@resource[:name]} (#{@resource[:types].join(', ')}) engine = #{@resource[:engine]} #{partition} #{order}")


@property_hash[:ensure] = :present

exists? ? (return true) : (return false)
end

def destroy
self.class.clickhouse_caller("drop table if exists #{@resource[:name]}")

@property_hash.clear
exists? ? (return false) : (return true)
end

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

mk_resource_methods
end
37 changes: 37 additions & 0 deletions lib/puppet/type/clickhouse_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Puppet::Type.newtype(:clickhouse_table) do
@doc = <<-PUPPET
@summary
Manages a Clickhouse Table
@param [namevar] Table name.
@param [ensure] Specifies whether to create table in Clickhouse. Valid values are 'present', 'absent'. Defaults to 'present'.
@param [types] Specify the data structure of the table
@param [engine] The engine to use for the table
@param [partition] What to partition the table on
@param [order] how to order the table

PUPPET

ensurable

autorequire(:file) { '/root/.clickhouse-client/config.xml' }
autorequire(:class) { 'clickhouse::server' }

newparam(:name, namevar: true) do
desc 'The name of the Clickhouse database to manage.'
end

newparam(:types) do
desc 'The Structure of the data on the table'
end

newparam(:engine) do
desc 'The engine to use for the table'
end

newparam(:partition) do
desc 'What to partition the table on'
end
newparam(:order) do
desc 'how to order the table'
end
end