From: Sarah Hoffmann Date: Fri, 19 Aug 2016 22:02:47 +0000 (+0200) Subject: add function to create and delete tablespaces X-Git-Url: https://git.openstreetmap.org/chef.git/commitdiff_plain/600a5055b909a46e372c0bfe510bb412d2ccba34 add function to create and delete tablespaces --- diff --git a/cookbooks/postgresql/README.md b/cookbooks/postgresql/README.md index 7f895005c..b23cd2bc6 100644 --- a/cookbooks/postgresql/README.md +++ b/cookbooks/postgresql/README.md @@ -9,3 +9,4 @@ defines a number of providers that can be used in other cookbooks: * munin: For configuring munin plugins for a given database name. * table: For managing database tables. * user: For managing database users. +* tablespace: For managing tablespaces. diff --git a/cookbooks/postgresql/libraries/postgresql.rb b/cookbooks/postgresql/libraries/postgresql.rb index 266a7b833..5d2007424 100644 --- a/cookbooks/postgresql/libraries/postgresql.rb +++ b/cookbooks/postgresql/libraries/postgresql.rb @@ -93,6 +93,14 @@ class Chef end end + def tablespaces + @tablespaces ||= query("SELECT spcname, usename FROM pg_tablespace AS t INNER JOIN pg_user AS u ON t.spcowner = u.usesysid").each_with_object({}) do |tablespace, tablespaces| + tablespaces[tablespace[:spcname]] = { + :owner => tablespace[:usename] + } + end + end + def tables(database) @tables ||= {} @tables[database] ||= query("SELECT n.nspname, c.relname, u.usename, c.relacl FROM pg_class AS c INNER JOIN pg_user AS u ON c.relowner = u.usesysid INNER JOIN pg_namespace AS n ON c.relnamespace = n.oid", :database => database).each_with_object({}) do |table, tables| diff --git a/cookbooks/postgresql/providers/tablespace.rb b/cookbooks/postgresql/providers/tablespace.rb new file mode 100644 index 000000000..a2d4af925 --- /dev/null +++ b/cookbooks/postgresql/providers/tablespace.rb @@ -0,0 +1,43 @@ +# +# Cookbook Name:: postgresql +# Provider:: postgresql_tablespace +# +# Copyright 2016, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +use_inline_resources + +def load_current_resource + @pg = Chef::PostgreSQL.new(new_resource.cluster) + + @current_resource = Chef::Resource::PostgresqlTablespace.new(new_resource.name) + @current_resource.tablespace(new_resource.tablespace) + @current_resource.location(new_resource.location) + @current_resource +end + +action :create do + unless @pg.tablespaces.include?(new_resource.tablespace) + @pg.execute(:command => "CREATE TABLESPACE #{new_resource.tablespace} LOCATION '#{new_resource.location}'") + new_resource.updated_by_last_action(true) + end +end + +action :drop do + if @pg.tablespaces.include?(new_resource.tablespace) + @pg.execute(:command => "DROP TABLESPACE #{new_resource.tablespace}") + new_resource.updated_by_last_action(true) + end +end diff --git a/cookbooks/postgresql/resources/tablespace.rb b/cookbooks/postgresql/resources/tablespace.rb new file mode 100644 index 000000000..167a68bfd --- /dev/null +++ b/cookbooks/postgresql/resources/tablespace.rb @@ -0,0 +1,29 @@ +# +# Cookbook Name:: postgresql +# Resource:: postgresql_tablespace +# +# Copyright 2016, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +actions :create, :drop + +attribute :tablespace, :kind_of => String, :name_attribute => true +attribute :cluster, :kind_of => String, :required => true +attribute :location, :kind_of => String, :required => true + +def initialize(*args) + super + @action = :create +end