From 8e42336df31724b49aa277565a2244de7bd0b3d1 Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Fri, 17 Jun 2011 09:31:39 +0200
Subject: [PATCH] new type: __postgres_database

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 conf/type/__postgres_database/explorer/state  | 27 ++++++++++++
 conf/type/__postgres_database/gencode-remote  | 38 ++++++++++++++++
 conf/type/__postgres_database/man.text        | 44 +++++++++++++++++++
 .../__postgres_database/parameter/optional    |  1 +
 .../__postgres_database/parameter/required    |  1 +
 5 files changed, 111 insertions(+)
 create mode 100755 conf/type/__postgres_database/explorer/state
 create mode 100755 conf/type/__postgres_database/gencode-remote
 create mode 100644 conf/type/__postgres_database/man.text
 create mode 100644 conf/type/__postgres_database/parameter/optional
 create mode 100644 conf/type/__postgres_database/parameter/required

diff --git a/conf/type/__postgres_database/explorer/state b/conf/type/__postgres_database/explorer/state
new file mode 100755
index 00000000..565c8a63
--- /dev/null
+++ b/conf/type/__postgres_database/explorer/state
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
+#
+# This file is part of cdist.
+#
+# cdist is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# cdist is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with cdist. If not, see <http://www.gnu.org/licenses/>.
+#
+
+name="$__object_id"
+
+if su - postgres -c "psql -l | grep -q '^ *$name'"; then
+   echo "present"
+else
+   echo "absent"
+fi
diff --git a/conf/type/__postgres_database/gencode-remote b/conf/type/__postgres_database/gencode-remote
new file mode 100755
index 00000000..4c3f5b0e
--- /dev/null
+++ b/conf/type/__postgres_database/gencode-remote
@@ -0,0 +1,38 @@
+#!/bin/sh
+#
+# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
+#
+# This file is part of cdist.
+#
+# cdist is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# cdist is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with cdist. If not, see <http://www.gnu.org/licenses/>.
+#
+
+name="$__object_id"
+state_should="$(cat "$__object/parameter/state")"
+state_is="$(cat "$__object/explorer/state")"
+
+if [ "$state_should" != "$state_is" ]; then
+   case "$state_should" in
+      present)
+         owner=""
+         if [ -f "$__object/parameter/owner" ]; then
+            owner="-O '$(cat "$__object/parameter/owner")'"
+         fi
+         echo "su - postgres -c \"/usr/bin/createdb $owner '$name'\""
+      ;;
+      absent)
+         echo "su - postgres -c \"/usr/bin/dropdb '$name'\""
+      ;;
+   esac
+fi
diff --git a/conf/type/__postgres_database/man.text b/conf/type/__postgres_database/man.text
new file mode 100644
index 00000000..06cb736f
--- /dev/null
+++ b/conf/type/__postgres_database/man.text
@@ -0,0 +1,44 @@
+cdist-type__postgres_database(7)
+================================
+Steven Armstrong <steven-cdist--@--armstrong.cc>
+
+
+NAME
+----
+cdist-type__postgres_database - create/drop postgres databases
+
+
+DESCRIPTION
+-----------
+This cdist type allows you to create or drop postgres databases.
+
+
+REQUIRED PARAMETERS
+-------------------
+state::
+   either 'present' or 'absent'
+
+
+OPTIONAL PARAMETERS
+-------------------
+owner::
+   the role owning this database
+
+
+EXAMPLES
+--------
+
+--------------------------------------------------------------------------------
+__postgres_database mydbname --state present --owner mydbusername
+--------------------------------------------------------------------------------
+
+
+SEE ALSO
+--------
+- cdist-type(7)
+
+
+COPYING
+-------
+Copyright \(C) 2011 Steven Armstrong. Free use of this software is
+granted under the terms of the GNU General Public License version 3 (GPLv3).
diff --git a/conf/type/__postgres_database/parameter/optional b/conf/type/__postgres_database/parameter/optional
new file mode 100644
index 00000000..7ee3bde8
--- /dev/null
+++ b/conf/type/__postgres_database/parameter/optional
@@ -0,0 +1 @@
+owner
diff --git a/conf/type/__postgres_database/parameter/required b/conf/type/__postgres_database/parameter/required
new file mode 100644
index 00000000..ff72b5c7
--- /dev/null
+++ b/conf/type/__postgres_database/parameter/required
@@ -0,0 +1 @@
+state