summaryrefslogtreecommitdiff
path: root/puppet/modules/couchdb/manifests/document.pp
blob: 6180474b5c74aa8c809a9d6c58c5d3386aaf3dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Usage:
# couchdb::document { id:
#   db => "database",
#   data => "content",
#   ensure => {absent,present,*content*}
# }
#
define couchdb::document(
  $db,
  $id,
  $host   = '127.0.0.1:5984',
  $data   = '{}',
  $netrc  = '/etc/couchdb/couchdb.netrc',
  $ensure = 'content') {

  $url = "${host}/${db}/${id}"

  case $ensure {
    default: { err ( "unknown ensure value '${ensure}'" ) }
    content: {
      exec { "couch-doc-update --netrc-file ${netrc} --host ${host} --db ${db} --id ${id} --data \'${data}\'":
        require => Exec['wait_for_couchdb'],
        unless  => "couch-doc-diff $url '$data'"
      }
    }

    present: {
      couchdb::query { "create_${db}_${id}":
        cmd    => 'PUT',
        host   => $host,
        path   => "${db}/${id}",
        require => Exec['wait_for_couchdb'],
        unless => "/usr/bin/curl -s -f --netrc-file ${netrc} ${url}"
      }
    }

    absent: {
      couchdb::query { "destroy_${db}_${id}":
        cmd    => 'DELETE',
        host   => $host,
        path   => "${db}/${id}",
        require => Exec['wait_for_couchdb'],
        unless => "/usr/bin/curl -s -f --netrc-file ${netrc} ${url}"
      }
    }
  }
}