[_states/postergres_cluster] first attempt to have a working state module
authorDavid Douard <david.douard@logilab.fr>
Wed, 22 Jul 2015 14:47:16 +0200
changeset 56 ccacaf723c49
parent 55 13148a44982f
child 57 de48682b9f7d
[_states/postergres_cluster] first attempt to have a working state module
_states/postgres_cluster.py
--- a/_states/postgres_cluster.py	Tue Jul 21 18:00:44 2015 +0200
+++ b/_states/postgres_cluster.py	Wed Jul 22 14:47:16 2015 +0200
@@ -1,10 +1,10 @@
 # -*- coding: utf-8 -*-
 '''
-Management of PostgreSQL databases
-==================================
+Management of PostgreSQL clusters
+=================================
 
-The postgres_database module is used to create and manage Postgres databases.
-Databases can be set as either absent or present
+The postgres_cluster state module is used to manage PostgreSQL clusters.
+Clusters can be set as either absent or present
 
 .. code-block:: yaml
 
@@ -24,18 +24,9 @@
 
 
 def present(name,
-            version=None,
+            version,
+            port=None,
             encoding=None,
-            lc_collate=None,
-            lc_ctype=None,
-            owner=None,
-            template=None,
-            user=None,
-            maintenance_db=None,
-            db_password=None,
-            db_host=None,
-            db_port=None,
-            db_user=None,
             locale=None,
             datadir=None):
     '''
@@ -48,163 +39,88 @@
     version
         Version of the postgresql cluster
 
+    port
+        Cluster port
+
     encoding
         The character encoding scheme to be used in this database
 
-    lc_collate
-        The LC_COLLATE setting to be used in this database
-
-    lc_ctype
-        The LC_CTYPE setting to be used in this database
-
-    owner
-        The username of the database owner
-
-    template
-        The template database from which to build this database
-
-    user
-        System user all operations should be performed on behalf of
-
-    db_user
-        database username if different from config or default
-
-    db_password
-        user password if any password for a specified user
-
-    db_host
-        Database host if different from config or default
-
-    db_port
-        Database port if different from config or default
-
     locale
         Locale with which to create cluster
 
     datadir
         Where the cluster is stored
 
-        .. versionadded:: Berylyum
+        .. versionadded:: 2015.XX
     '''
     ret = {'name': name,
            'changes': {},
            'result': True,
-           'comment': 'Database {0} is already present'.format(name)}
-
-    db_args = {
-        'maintenance_db': maintenance_db,
-        'runas': user,
-        'host': db_host,
-        'user': db_user,
-        'port': db_port,
-        'password': db_password,
-    }
-    dbs = __salt__['postgres.db_list'](**db_args)
-    db_params = dbs.get(name, {})
+           'comment': 'Cluster {0}/{1} is already present'.format(version, name)}
 
-    if name in dbs and all((
-        db_params.get('Tablespace') == tablespace if tablespace else True,
-        (
-            db_params.get('Encoding').lower() == encoding.lower()
-            if encoding else True
-        ),
-        db_params.get('Collate') == lc_collate if lc_collate else True,
-        db_params.get('Ctype') == lc_ctype if lc_ctype else True,
-        db_params.get('Owner') == owner if owner else True
-    )):
+    if __salt__['postgres.cluster_exists'](version, name):
+        # check cluster config is correct 
+        infos = __salt__['postgres.cluster_list'](verbose=True)
+        infos = infos['{0}/{1}'.format(version, name)]
+        # TODO: check locale en encoding configs also
+        if any((port != infos['port'] if port else False,
+                datadir != infos['datadir'] if datadir else False,)):
+            ret['comment'] = 'Cluster {0}/{1} has wrong parameters ' \
+                             'which couldn\'t be changed on fly.' \
+                                 .format(version, name)
+            ret['result'] = False
         return ret
-    elif name in dbs and any((
-        db_params.get('Encoding').lower() != encoding.lower() if encoding else False,
-        db_params.get('Collate') != lc_collate if lc_collate else False,
-        db_params.get('Ctype') != lc_ctype if lc_ctype else False
-    )):
-        ret['comment'] = 'Database {0} has wrong parameters ' \
-                         'which couldn\'t be changed on fly.'.format(name)
-        ret['result'] = False
-        return ret
-
-    # The database is not present, make it!
+        
+    # The cluster is not present, add it!
     if __opts__['test']:
         ret['result'] = None
-        if name not in dbs:
-            ret['comment'] = 'Database {0} is set to be created'.format(name)
-        else:
-            ret['comment'] = 'Database {0} exists, but parameters ' \
-                             'need to be changed'.format(name)
+        ret['comment'] = 'Cluster {0}/{1} is set to be created'.format(version, name)
         return ret
-    if (
-        name not in dbs and __salt__['postgres.cluster_create'](
-            version=version,
-            name=name,
-            port=db_port,
-            locale=locale,
-            encoding=encoding,
-            datadir=datadir)
-    ):
-        ret['comment'] = 'The cluster {0}/{1} has been created'.format(version,name)
+    cluster = __salt__['postgres.cluster_create'](
+        version=version,
+        name=name,
+        port=port,
+        locale=locale,
+        encoding=encoding,
+        datadir=datadir)
+    if cluster:
+        ret['comment'] = 'The cluster {0}/{1} has been created'.format(version, name)
         ret['changes']['{0}/{1}'.format(version, name)] = 'Present'
     else:
-        ret['comment'] = 'Failed to create database {0}'.format(name)
+        ret['comment'] = 'Failed to create cluster {0}/{1}'.format(version, name)
         ret['result'] = False
-
     return ret
 
 
-def absent(name,
-           user=None,
-           maintenance_db=None,
-           db_password=None,
-           db_host=None,
-           db_port=None,
-           db_user=None):
+def absent(name, version):
     '''
-    Ensure that the named database is absent
+    Ensure that the named cluster is absent
 
     name
-        The name of the database to remove
-
-    db_user
-        database username if different from config or defaul
-
-    db_password
-        user password if any password for a specified user
+        The name of the cluster to remove
 
-    db_host
-        Database host if different from config or default
+    version
+        Version of the postgresql server of the cluster to remove
 
-    db_port
-        Database port if different from config or default
-
-    user
-        System user all operations should be performed on behalf of
-
-        .. versionadded:: 0.17.0
+        .. versionadded:: 2015.XX
     '''
     ret = {'name': name,
            'changes': {},
            'result': True,
            'comment': ''}
 
-    db_args = {
-        'maintenance_db': maintenance_db,
-        'runas': user,
-        'host': db_host,
-        'user': db_user,
-        'port': db_port,
-        'password': db_password,
-    }
-    #check if db exists and remove it
-    if __salt__['postgres.db_exists'](name, **db_args):
+    #check if cluster exists and remove it
+    if __salt__['postgres.cluster_exists'](version, name):
         if __opts__['test']:
             ret['result'] = None
-            ret['comment'] = 'Database {0} is set to be removed'.format(name)
+            ret['comment'] = 'Cluster {0}/{1} is set to be removed'.format(version, name)
             return ret
-        if __salt__['postgres.db_remove'](name, **db_args):
-            ret['comment'] = 'Database {0} has been removed'.format(name)
+        if __salt__['postgres.cluster_remove'](version, name):
+            ret['comment'] = 'Cluster {0}/{1} has been removed'.format(version, name)
             ret['changes'][name] = 'Absent'
             return ret
 
     # fallback
-    ret['comment'] = 'Database {0} is not present, so it cannot ' \
-                     'be removed'.format(name)
+    ret['comment'] = 'Cluster {0}/{1} is not present, so it cannot ' \
+                     'be removed'.format(version, name)
     return ret