diff --git a/manifests/schema/index.pp b/manifests/schema/index.pp index 13fcdf4..ec63cdc 100644 --- a/manifests/schema/index.pp +++ b/manifests/schema/index.pp @@ -1,69 +1,90 @@ # Create or drop indexes within the schema. # @param ensure [present|absent] Create or dro[ the index. # @param class_name [string] The name of the class to be associated with an # index when creating a custom index. # @param index [string] The name of the index. Defaults to the name of the # resource. # @param keys [string] The columns that the index is being created on. # @param keyspace [string] The name the keyspace that the index is to be associated # with. # @param options [string] Any options to be added to the index. # @param table [string] The name of the table that the index is to be associated with. define cassandra::schema::index( $keyspace, $table, $ensure = present, $class_name = undef, $index = $title, $keys = undef, $options = undef, $use_scl = $::cassandra::params::use_scl, $scl_name = $::cassandra::params::scl_name, ) { include 'cassandra::schema' + if $use_scl { + $quote = '\"' + } else { + $quote = '"' + } + # Fully qualified index name. $fqin = "${keyspace}.${index}" # Fully qualified table name. $fqtn = "${keyspace}.${table}" $read_script = "DESC INDEX ${fqin}" - $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 { if $class_name != undef { $create_part1 = "CREATE CUSTOM INDEX IF NOT EXISTS ${index} ON ${keyspace}.${table}" } else { $create_part1 = "CREATE INDEX IF NOT EXISTS ${index} ON ${keyspace}.${table}" } if $class_name != undef { $create_part2 = "${create_part1} (${keys}) USING '${class_name}'" } else { $create_part2 = "${create_part1} (${keys})" } if $options != undef { $create_script = "${create_part2} WITH OPTIONS = ${options}" } else { $create_script = $create_part2 } - $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 INDEX ${fqin}" - $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/index_spec.rb b/spec/defines/schema/index_spec.rb index de26cd9..930e580 100644 --- a/spec/defines/schema/index_spec.rb +++ b/spec/defines/schema/index_spec.rb @@ -1,129 +1,275 @@ require 'spec_helper' describe 'cassandra::schema::index' do context 'Create a basic index' do let :facts do { operatingsystemmajrelease: 7, osfamily: 'RedHat' } end let(:title) { 'user_index' } let(:params) do { keys: 'lname', keyspace: 'mykeyspace', table: 'users', use_scl: false, scl_name: 'nodefault' } end it do is_expected.to compile is_expected.to contain_cassandra__schema__index('user_index') - is_expected.to contain_exec('/usr/bin/cqlsh -e "CREATE INDEX IF NOT EXISTS user_index ON mykeyspace.users (lname)" localhost 9042') + read_command = '/usr/bin/cqlsh -e "DESC INDEX mykeyspace.user_index" localhost 9042' + exec_command = '/usr/bin/cqlsh -e "CREATE INDEX IF NOT EXISTS user_index ON mykeyspace.users (lname)" localhost 9042' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') + end + end + + context 'Create a basic index with SCL' do + let :facts do + { + operatingsystemmajrelease: 7, + osfamily: 'RedHat' + } + end + + let(:title) { 'user_index' } + + let(:params) do + { + keys: 'lname', + keyspace: 'mykeyspace', + table: 'users', + use_scl: true, + scl_name: 'testscl' + } + end + + it do + is_expected.to compile + is_expected.to contain_cassandra__schema__index('user_index') + read_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DESC INDEX mykeyspace.user_index\" localhost 9042"' + exec_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"CREATE INDEX IF NOT EXISTS user_index ON mykeyspace.users (lname)\" localhost 9042"' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') end end context 'Create a custom index.' do let :facts do { operatingsystemmajrelease: 7, osfamily: 'RedHat' } end let(:title) { 'user_index' } let(:params) do { class_name: 'path.to.the.IndexClass', keys: 'email', keyspace: 'Excelsior', table: 'users', use_scl: false, scl_name: 'nodefault' } end it do is_expected.to compile - is_expected.to contain_exec('/usr/bin/cqlsh -e "CREATE CUSTOM INDEX IF NOT EXISTS user_index ON Excelsior.users (email) USING \'path.to.the.IndexClass\'" localhost 9042') + read_command = '/usr/bin/cqlsh -e "DESC INDEX Excelsior.user_index" localhost 9042' + exec_command = '/usr/bin/cqlsh -e "CREATE CUSTOM INDEX IF NOT EXISTS user_index ON Excelsior.users (email) USING \'path.to.the.IndexClass\'" localhost 9042' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') end end + + context 'Create a custom index with SCL.' do + let :facts do + { + operatingsystemmajrelease: 7, + osfamily: 'RedHat' + } + end + + let(:title) { 'user_index' } + + let(:params) do + { + class_name: 'path.to.the.IndexClass', + keys: 'email', + keyspace: 'Excelsior', + table: 'users', + use_scl: true, + scl_name: 'testscl' + } + end + + it do + is_expected.to compile + read_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DESC INDEX Excelsior.user_index\" localhost 9042"' + exec_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"CREATE CUSTOM INDEX IF NOT EXISTS user_index ON Excelsior.users (email) USING \'path.to.the.IndexClass\'\" localhost 9042"' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') + end + end + context 'Create a custom index with options.' do let :facts do { operatingsystemmajrelease: 7, osfamily: 'RedHat' } end let(:title) { 'user_index' } let(:params) do { class_name: 'path.to.the.IndexClass', keys: 'email', keyspace: 'Excelsior', options: "{'storage': '/mnt/ssd/indexes/'}", table: 'users', use_scl: false, scl_name: 'nodefault' } end it do is_expected.to compile - is_expected.to contain_exec('/usr/bin/cqlsh -e "CREATE CUSTOM INDEX IF NOT EXISTS user_index ON Excelsior.users (email) USING \'path.to.the.IndexClass\' WITH OPTIONS = {\'storage\': \'/mnt/ssd/indexes/\'}" localhost 9042') + read_command = '/usr/bin/cqlsh -e "DESC INDEX Excelsior.user_index" localhost 9042' + exec_command = '/usr/bin/cqlsh -e "CREATE CUSTOM INDEX IF NOT EXISTS user_index ON ' + exec_command += 'Excelsior.users (email) USING \'path.to.the.IndexClass\' WITH OPTIONS = {' + exec_command += '\'storage\': \'/mnt/ssd/indexes/\'}" localhost 9042' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') + end + end + + context 'Create a custom index with options with SCL.' do + let :facts do + { + operatingsystemmajrelease: 7, + osfamily: 'RedHat' + } + end + + let(:title) { 'user_index' } + + let(:params) do + { + class_name: 'path.to.the.IndexClass', + keys: 'email', + keyspace: 'Excelsior', + options: "{'storage': '/mnt/ssd/indexes/'}", + table: 'users', + use_scl: true, + scl_name: 'testscl' + } + end + + it do + is_expected.to compile + read_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DESC INDEX Excelsior.user_index\" localhost 9042"' + exec_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"CREATE CUSTOM INDEX IF NOT EXISTS user_index ON ' + exec_command += 'Excelsior.users (email) USING \'path.to.the.IndexClass\' WITH OPTIONS = {' + exec_command += '\'storage\': \'/mnt/ssd/indexes/\'}\" localhost 9042"' + is_expected.to contain_exec(exec_command). + only_with(unless: read_command, + require: 'Exec[::cassandra::schema connection test]') end end context 'Drop Index' do let :facts do { operatingsystemmajrelease: 7, osfamily: 'RedHat' } end let(:title) { 'user_index' } let(:params) do { ensure: 'absent', keys: 'lname', keyspace: 'Excelsior', table: 'users', use_scl: false, scl_name: 'nodefault' } end it do is_expected.to compile - is_expected.to contain_exec('/usr/bin/cqlsh -e "DROP INDEX Excelsior.user_index" localhost 9042') + read_command = '/usr/bin/cqlsh -e "DESC INDEX Excelsior.user_index" localhost 9042' + exec_command = '/usr/bin/cqlsh -e "DROP INDEX Excelsior.user_index" localhost 9042' + is_expected.to contain_exec(exec_command). + only_with(onlyif: read_command, + require: 'Exec[::cassandra::schema connection test]') + end + end + + context 'Drop Index with SCL' do + let :facts do + { + operatingsystemmajrelease: 7, + osfamily: 'RedHat' + } + end + + let(:title) { 'user_index' } + + let(:params) do + { + ensure: 'absent', + keys: 'lname', + keyspace: 'Excelsior', + table: 'users', + use_scl: true, + scl_name: 'testscl' + } + end + + it do + is_expected.to compile + read_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DESC INDEX Excelsior.user_index\" localhost 9042"' + exec_command = '/usr/bin/scl enable testscl "/usr/bin/cqlsh -e \"DROP INDEX Excelsior.user_index\" 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