Skip to content

Commit

Permalink
Merge pull request #36464 from vutny/postgres-tablespace-options
Browse files Browse the repository at this point in the history
Fix `options` parameter processing in `postgres_tablespace.present`
  • Loading branch information
Mike Place committed Sep 22, 2016
2 parents 41d3c09 + 580aed8 commit b021ea5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
8 changes: 4 additions & 4 deletions salt/modules/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,13 @@ def tablespace_create(name, location, options=None, owner=None, user=None,
owner_query = 'OWNER {0}'.format(owner)
# should come out looking like: 'OWNER postgres'
if options:
optionstext = ['{0} = {1}'.format(k, v) for k, v in options.items()]
optionstext = ['{0} = {1}'.format(k, v) for k, v in six.iteritems(options)]
options_query = 'WITH ( {0} )'.format(', '.join(optionstext))
# should come out looking like: 'WITH ( opt1 = 1.0, opt2 = 4.0 )'
query = 'CREATE TABLESPACE {0} {1} LOCATION \'{2}\' {3}'.format(name,
owner_query,
location,
options_query)
owner_query,
location,
options_query)

# Execute the command
ret = _psql_prepare_and_run(['-c', query],
Expand Down
46 changes: 32 additions & 14 deletions salt/states/postgres_tablespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@
.. versionadded:: 2015.8.0
'''

# Import python libs
from __future__ import absolute_import

# Import salt libs
from salt.utils import dictupdate

# Import 3rd-party libs
from salt.ext.six import iteritems


def __virtual__():
'''
Expand Down Expand Up @@ -46,13 +54,21 @@ def present(name,
The directory where the tablespace will be located, must already exist
options
A dictionary of options to specify for the table.
Currently, the only tablespace options supported are
seq_page_cost - float; default=1.0
random_page_cost - float; default=4.0
A dictionary of options to specify for the tablespace.
Currently, the only tablespace options supported are ``seq_page_cost``
and ``random_page_cost``. Default values are shown in the example below:
.. code-block:: yaml
my_space:
postgres_tablespace.present:
- directory: /srv/my_tablespace
- options:
seq_page_cost: 1.0
random_page_cost: 4.0
owner
The database user that will be the owner of the tablespace
The database user that will be the owner of the tablespace.
Defaults to the user executing the command (i.e. the `user` option)
user
Expand All @@ -62,10 +78,10 @@ def present(name,
Database to act on
db_user
database username if different from config or default
Database username if different from config or default
db_password
user password if any password for a specified user
User password if any password for a specified user
db_host
Database host if different from config or default
Expand Down Expand Up @@ -113,6 +129,7 @@ def present(name,
if (__salt__['postgres.tablespace_alter'](name, new_owner=owner)
and not __opts__['test']):
ret['comment'] = 'Tablespace {0} owner changed'.format(name)
ret['changes'][name] = {'owner': owner}
ret['result'] = True

if options:
Expand All @@ -121,17 +138,18 @@ def present(name,
# that we should be able to string check:
# {seq_page_cost=1.1,random_page_cost=3.9}
# TODO remove options that exist if possible
for k, v in options:
for k, v in iteritems(options):
# if 'seq_page_cost=1.1' not in '{seq_page_cost=1.1,...}'
if '{0}={1}'.format(k, v) not in tblspaces[name]['Opts']:
# if 'seq_page_cost=1.1' not in '{seq_page_cost=1.1,...}'
if __opts__['test']:
ret['result'] = None
ret['comment'] = """Tablespace {0} options to be
altered""".format(name)
break # we know it's going to be altered, no reason to cont
if __salt__['postgres.tablespace_alter'](name,
set_options={k: v}):
set_option={k: v}):
ret['comment'] = 'Tablespace {0} opts changed'.format(name)
dictupdate.update(ret['changes'], {name: {'options': {k: v}}})
ret['result'] = True

return ret
Expand All @@ -145,10 +163,10 @@ def absent(name,
db_host=None,
db_port=None):
'''
Ensure that the named database is absent.
Ensure that the named tablespace is absent.
name
The name of the database to remove
The name of the tablespace to remove
user
System user all operations should be performed on behalf of
Expand All @@ -157,10 +175,10 @@ def absent(name,
Database to act on
db_user
database username if different from config or defaul
Database username if different from config or defaul
db_password
user password if any password for a specified user
User password if any password for a specified user
db_host
Database host if different from config or default
Expand Down

0 comments on commit b021ea5

Please sign in to comment.