diff --git a/manifests/schema/table.pp b/manifests/schema/table.pp index eb1efa0..1051bb7 100644 --- a/manifests/schema/table.pp +++ b/manifests/schema/table.pp @@ -1,59 +1,81 @@ # Create or drop tables within the schema. # @param keyspace [string] The name of the keyspace. # @param columns [hash] A hash of the columns to be placed in the table. # Optional if the table is to be absent. # @param ensure [present|absent] Ensure a keyspace is created or dropped. # @param options [array] Options to be added to the table creation. # @param table [string] The name of the table. Defaults to the name of the # resource. # @example # cassandra::schema::table { 'users': # keyspace => 'mykeyspace', # columns => { # 'userid' => 'int', # 'fname' => 'text', # 'lname' => 'text', # 'PRIMARY KEY' => '(userid)', # }, # } define cassandra::schema::table ( $keyspace, $ensure = present, $columns = {}, $options = [], $table = $title, $use_scl = $::cassandra::params::use_scl, $scl_name = $::cassandra::params::scl_name, ){ include 'cassandra::schema' + + if $use_scl { + $quote = '\"' + } else { + $quote = '"' + } + $read_script = "DESC TABLE ${keyspace}.${table}" - $read_command = "${::cassandra::schema::cqlsh_opts} -e \"${read_script}\" ${::cassandra::schema::cqlsh_conn}" + $read_command_tmp = "${::cassandra::schema::cqlsh_opts} -e ${quote}${read_script}${quote} ${::cassandra::schema::cqlsh_conn}" + if $use_scl { + $read_command = "/usr/bin/scl enable ${scl_name} \"${read_command_tmp}\"" + } else { + $read_command = $read_command_tmp + } if $ensure == present { $create_script1 = "CREATE TABLE IF NOT EXISTS ${keyspace}.${table}" $cols_def = join(join_keys_to_values($columns, ' '), ', ') $cols_def_rm_collection_type = delete($cols_def, 'COLLECTION-TYPE ') if count($options) > 0 { $options_def = join($options, ' AND ') $create_script = "${create_script1} (${cols_def_rm_collection_type}) WITH ${options_def}" } else { $create_script = "${create_script1} (${cols_def_rm_collection_type})" } - $create_command = "${::cassandra::schema::cqlsh_opts} -e \"${create_script}\" ${::cassandra::schema::cqlsh_conn}" + $create_command_tmp = "${::cassandra::schema::cqlsh_opts} -e ${quote}${create_script}${quote} ${::cassandra::schema::cqlsh_conn}" + if $use_scl { + $create_command = "/usr/bin/scl enable ${scl_name} \"${create_command_tmp}\"" + } else { + $create_command = $create_command_tmp + } exec { $create_command: unless => $read_command, require => Exec['::cassandra::schema connection test'], } } elsif $ensure == absent { $delete_script = "DROP TABLE IF EXISTS ${keyspace}.${table}" - $delete_command = "${::cassandra::schema::cqlsh_opts} -e \"${delete_script}\" ${::cassandra::schema::cqlsh_conn}" + $delete_command_tmp = "${::cassandra::schema::cqlsh_opts} -e ${quote}${delete_script}${quote} ${::cassandra::schema::cqlsh_conn}" + if $use_scl { + $delete_command = "/usr/bin/scl enable ${scl_name} \"${delete_command_tmp}\"" + } else { + $delete_command = $delete_command_tmp + } exec { $delete_command: onlyif => $read_command, require => Exec['::cassandra::schema connection test'], } } else { fail("Unknown action (${ensure}) for ensure attribute.") } } diff --git a/spec/defines/schema/table_spec.rb b/spec/defines/schema/table_spec.rb index 0efcadb..a84e914 100644 --- a/spec/defines/schema/table_spec.rb +++ b/spec/defines/schema/table_spec.rb @@ -1,86 +1,173 @@ require 'spec_helper' describe 'cassandra::schema::table' do context 'Create Table' do let :facts do { operatingsystemmajrelease: 7, osfamily: 'RedHat' } end let(:title) { 'users' } let(:params) do { use_scl: false, scl_name: 'nodefault', keyspace: 'Excelsior', columns: { 'userid' => 'text', 'username' => 'FROZEN', 'emails' => 'set', 'top_scores' => 'list', 'todo' => 'map', 'COLLECTION-TYPE' => 'tuple', 'PRIMARY KEY' => '(userid)' }, options: [ 'COMPACT STORAGE', 'ID=\'5a1c395e-b41f-11e5-9f22-ba0be0483c18\'' ] } end it do is_expected.to compile is_expected.to contain_cassandra__schema__table('users') - is_expected.to contain_exec('/usr/bin/cqlsh -e "CREATE TABLE IF NOT EXISTS Excelsior.users (userid text, username FROZEN, emails set, top_scores list, todo map, tuple, PRIMARY KEY (userid)) WITH COMPACT STORAGE AND ID=\'5a1c395e-b41f-11e5-9f22-ba0be0483c18\'" localhost 9042') + read_command = '/usr/bin/cqlsh -e "DESC TABLE Excelsior.users" localhost 9042' + exec_command = '/usr/bin/cqlsh -e "CREATE TABLE IF NOT EXISTS Excelsior.users ' + exec_command += '(userid text, username FROZEN, emails set, top_scores list, ' + exec_command += 'todo map, tuple, PRIMARY KEY (userid)) ' + exec_command += 'WITH COMPACT STORAGE AND ID=\'5a1c395e-b41f-11e5-9f22-ba0be0483c18\'" localhost 9042' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') + end + end + + context 'Create Table with SCL' do + let :facts do + { + operatingsystemmajrelease: 7, + osfamily: 'RedHat' + } + end + + let(:title) { 'users' } + + let(:params) do + { + use_scl: true, + scl_name: 'testscl', + keyspace: 'Excelsior', + columns: + { + 'userid' => 'text', + 'username' => 'FROZEN', + 'emails' => 'set', + 'top_scores' => 'list', + 'todo' => 'map', + 'COLLECTION-TYPE' => 'tuple', + 'PRIMARY KEY' => '(userid)' + }, + options: + [ + 'COMPACT STORAGE', + 'ID=\'5a1c395e-b41f-11e5-9f22-ba0be0483c18\'' + ] + } + end + + it do + is_expected.to compile + is_expected.to contain_cassandra__schema__table('users') + read_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DESC TABLE Excelsior.users\" localhost 9042"' + exec_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"CREATE TABLE IF NOT EXISTS Excelsior.users ' + exec_command += '(userid text, username FROZEN, emails set, top_scores list, ' + exec_command += 'todo map, tuple, PRIMARY KEY (userid)) ' + exec_command += 'WITH COMPACT STORAGE AND ID=\'5a1c395e-b41f-11e5-9f22-ba0be0483c18\'\" localhost 9042"' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') end end context 'Drop Table' do let :facts do { operatingsystemmajrelease: 7, osfamily: 'RedHat' } end let(:title) { 'users' } let(:params) do { use_scl: false, scl_name: 'nodefault', keyspace: 'Excelsior', ensure: 'absent' } end it do is_expected.to compile - is_expected.to contain_exec('/usr/bin/cqlsh -e "DROP TABLE IF EXISTS Excelsior.users" localhost 9042') + read_command = '/usr/bin/cqlsh -e "DESC TABLE Excelsior.users" localhost 9042' + exec_command = '/usr/bin/cqlsh -e "DROP TABLE IF EXISTS Excelsior.users" localhost 9042' + is_expected.to contain_exec(exec_command). + only_with(onlyif: read_command, + require: 'Exec[::cassandra::schema connection test]') + end + end + + context 'Drop Table with SCL' do + let :facts do + { + operatingsystemmajrelease: 7, + osfamily: 'RedHat' + } + end + + let(:title) { 'users' } + + let(:params) do + { + use_scl: true, + scl_name: 'testscl', + keyspace: 'Excelsior', + ensure: 'absent' + } + end + + it do + is_expected.to compile + read_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DESC TABLE Excelsior.users\" localhost 9042"' + exec_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DROP TABLE IF EXISTS Excelsior.users\" localhost 9042"' + is_expected.to contain_exec(exec_command). + only_with(onlyif: read_command, + require: 'Exec[::cassandra::schema connection test]') end end context 'Set ensure to latest' do let :facts do { operatingsystemmajrelease: 7, osfamily: 'RedHat' } end let(:title) { 'foobar' } let(:params) do { ensure: 'latest' } end it { is_expected.to raise_error(Puppet::Error) } end end