Defined Type: cassandra::schema::keyspace

Defined in:
manifests/schema/keyspace.pp

Overview

Create or drop keyspaces within the schema.

Examples:

$network_topology_strategy = {
  keyspace_class => 'NetworkTopologyStrategy',
    dc1            => 3,
    dc2            => 2
}
cassandra::schema::keyspace { 'mykeyspace':
  replication_map => {
    keyspace_class     => 'SimpleStrategy',
    replication_factor => 1,
  },
  durable_writes  => false,
}

Parameters:

  • ensure (present|absent) (defaults to: present)

    Create or drop the keyspace.

  • durable_writes (boolean) (defaults to: true)

    When set to false, data written to the keyspace bypasses the commit log. Be careful using this option because you risk losing data. Set this attribute to false on a keyspace using the SimpleStrategy.

  • keyspace_name (string) (defaults to: $title)

    The name of the keyspace to be created.

  • replication_map (hash) (defaults to: {})

    Needed if the keyspace is to be present. Optional if it is to be absent.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'manifests/schema/keyspace.pp', line 24

define cassandra::schema::keyspace(
  $ensure          = present,
  $durable_writes  = true,
  $keyspace_name   = $title,
  $replication_map = {},
  ) {
  include 'cassandra::schema'

  $read_script = "DESC KEYSPACE ${keyspace_name}"
  $read_command = "${::cassandra::schema::cqlsh_opts} -e \"${read_script}\" ${::cassandra::schema::cqlsh_conn}"

  if $ensure == present {
    $keyspace_class = $replication_map[keyspace_class]

    case $keyspace_class {
      'SimpleStrategy': {
        $replication_factor = $replication_map[replication_factor]
        $map_str = "{ 'class' : 'SimpleStrategy', 'replication_factor' : ${replication_factor} }"
      }
      'NetworkTopologyStrategy': {
        $map_str1 = "{ 'class' : 'NetworkTopologyStrategy'"
        $new_map = prefix(delete($replication_map, 'keyspace_class'), "'")
        $map_str2 = join(join_keys_to_values($new_map, "': "), ', ')
        $map_str = "${map_str1}, ${map_str2} }"
      }
      default: {
        $msg_part1 = "Invalid or no class (${keyspace_class}) specified for"
        $msg_part2 = "keyspace ${keyspace_name}."
        fail("${msg_part1} ${msg_part2}")
      }
    }

    $create_script1 = "CREATE KEYSPACE IF NOT EXISTS ${keyspace_name}"
    $create_script2 = "WITH REPLICATION = ${map_str}"
    $create_script3 = "AND DURABLE_WRITES = ${durable_writes}"
    $create_script = "${create_script1} ${create_script2} ${create_script3}"
    $create_command = "${::cassandra::schema::cqlsh_opts} -e \"${create_script}\" ${::cassandra::schema::cqlsh_conn}"

    exec { $create_command:
      unless  => $read_command,
      require => Exec['::cassandra::schema connection test'],
    }
  } elsif $ensure == absent {
    $delete_script = "DROP KEYSPACE ${keyspace_name}"
    $delete_command = "${::cassandra::schema::cqlsh_opts} -e \"${delete_script}\" ${::cassandra::schema::cqlsh_conn}"
    exec { $delete_command:
      onlyif  => $read_command,
      require => Exec['::cassandra::schema connection test'],
    }
  } else {
    fail("Unknown action (${ensure}) for ensure attribute.")
  }
}