
    VhyI                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZ  G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionag  
---
module: postgresql_tablespace
short_description: Add or remove PostgreSQL tablespaces from remote hosts
description:
- Adds or removes PostgreSQL tablespaces from remote hosts.
options:
  tablespace:
    description:
    - Name of the tablespace to add or remove.
    required: true
    type: str
    aliases:
    - name
  location:
    description:
    - Path to the tablespace directory in the file system.
    - Ensure that the location exists and has right privileges.
    type: path
    aliases:
    - path
  state:
    description:
    - Tablespace state.
    - I(state=present) implies the tablespace must be created if it doesn't exist.
    - I(state=absent) implies the tablespace must be removed if present.
      I(state=absent) is mutually exclusive with I(location), I(owner), i(set).
    - See the Notes section for information about check mode restrictions.
    type: str
    default: present
    choices: [ absent, present ]
  owner:
    description:
    - Name of the role to set as an owner of the tablespace.
    - If this option is not specified, the tablespace owner is a role that creates the tablespace.
    type: str
  set:
    description:
    - Dict of tablespace options to set. Supported from PostgreSQL 9.0.
    - For more information see U(https://www.postgresql.org/docs/current/sql-createtablespace.html).
    - When reset is passed as an option's value, if the option was set previously, it will be removed.
    type: dict
  rename_to:
    description:
    - DEPRECATED (see the L(discussion,https://github.com/ansible-collections/community.postgresql/issues/820)).
      This option will be removed in version 5.0.0.
      To rename a tablespace, use the M(community.postgresql.postgresql_query) module.
    - New name of the tablespace.
    - The new name cannot begin with pg_, as such names are reserved for system tablespaces.
    type: str
  session_role:
    description:
    - Switch to session_role after connecting. The specified session_role must
      be a role that the current login_user is a member of.
    - Permissions checking for SQL commands is carried out as though
      the session_role were the one that had logged in originally.
    type: str
  login_db:
    description:
    - Name of database to connect to and run queries against.
    - The V(db) alias is deprecated and will be removed in version 5.0.0.
    type: str
    aliases:
    - db
  trust_input:
    description:
    - If C(false), check whether values of parameters I(tablespace), I(location), I(owner),
      I(rename_to), I(session_role), I(settings_list) are potentially dangerous.
    - It makes sense to use C(false) only when SQL injections via the parameters are possible.
    type: bool
    default: true
    version_added: '0.2.0'
  comment:
    description:
    - Sets a comment on the tablespace.
    - To reset the comment, pass an empty string.
    type: str
    version_added: '3.3.0'

attributes:
  check_mode:
    support: partial
    details:
      - I(state=absent) and I(state=present) (the second one if the tablespace doesn't exist) do not
        support check mode because the corresponding PostgreSQL DROP and CREATE TABLESPACE commands
        can not be run inside the transaction block.

seealso:
- name: PostgreSQL tablespaces
  description: General information about PostgreSQL tablespaces.
  link: https://www.postgresql.org/docs/current/manage-ag-tablespaces.html
- name: CREATE TABLESPACE reference
  description: Complete reference of the CREATE TABLESPACE command documentation.
  link: https://www.postgresql.org/docs/current/sql-createtablespace.html
- name: ALTER TABLESPACE reference
  description: Complete reference of the ALTER TABLESPACE command documentation.
  link: https://www.postgresql.org/docs/current/sql-altertablespace.html
- name: DROP TABLESPACE reference
  description: Complete reference of the DROP TABLESPACE command documentation.
  link: https://www.postgresql.org/docs/current/sql-droptablespace.html

author:
- Flavien Chantelot (@Dorn-)
- Antoine Levy-Lambert (@antoinell)
- Andrew Klychkov (@Andersson007)
- Daniele Giudice (@RealGreenDragon)

extends_documentation_fragment:
- community.postgresql.postgres
a  
- name: Create a new tablespace called acme and set bob as an its owner
  community.postgresql.postgresql_tablespace:
    name: acme
    owner: bob
    location: /data/foo
    comment: "Bob's tablespace"

- name: Create a new tablespace called bar with tablespace options
  community.postgresql.postgresql_tablespace:
    name: bar
    set:
      random_page_cost: 1
      seq_page_cost: 1

- name: Reset random_page_cost option
  community.postgresql.postgresql_tablespace:
    name: bar
    set:
      random_page_cost: reset

- name: Drop tablespace called bloat
  community.postgresql.postgresql_tablespace:
    name: bloat
    state: absent
a  
queries:
    description: List of queries that was tried to be executed.
    returned: success
    type: str
    sample: [ "CREATE TABLESPACE bar LOCATION '/incredible/ssd'" ]
tablespace:
    description: Tablespace name.
    returned: success
    type: str
    sample: 'ssd'
owner:
    description: Tablespace owner.
    returned: success
    type: str
    sample: 'Bob'
comment:
    description: Tablespace comment.
    returned: success
    type: str
    sample: 'Test tablespace'
options:
    description: Tablespace options.
    returned: success
    type: dict
    sample: { 'random_page_cost': 1, 'seq_page_cost': 1 }
location:
    description: Path to the tablespace in the file system.
    returned: success
    type: str
    sample: '/incredible/fast/ssd'
newname:
    description: New tablespace name.
    returned: if existent
    type: str
    sample: new_ssd
state:
    description: Tablespace state at the end of execution.
    returned: success
    type: str
    sample: 'present'
)AnsibleModule)	iteritems)check_input)connect_to_dbensure_required_libsexec_sqlget_conn_paramspg_cursor_argspostgres_common_argument_specset_autocommitset_commentc                   L    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)PgTablespacea$  Class for working with PostgreSQL tablespaces.

    Args:
        module (AnsibleModule) -- object of AnsibleModule class
        cursor (cursor) -- cursor object of psycopg library
        name (str) -- name of the tablespace

    Attrs:
        module (AnsibleModule) -- object of AnsibleModule class
        cursor (cursor) -- cursor object of psycopg library
        name (str) -- name of the tablespace
        exists (bool) -- flag the tablespace exists in the DB or not
        owner (str) -- tablespace owner
        location (str) -- path to the tablespace directory in the file system
        executed_queries (list) -- list of executed queries
        new_name (str) -- new name for the tablespace
        opt_not_supported (bool) -- flag indicates a tablespace option is supported or not
    c                     || _         || _        || _        d| _        d| _        i | _        d| _        g | _        d| _        d| _	        d | _
        | j                          y )NF )modulecursornameexistsownersettingslocationexecuted_queriesnew_nameopt_not_supportedcommentget_info)selfr   r   r   s       ~/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_tablespace.py__init__zPgTablespace.__init__   sZ    	
 "!&    c                    t        | dd      }t        | dd      }|rd}nd}|sd| _        d|z  }nd	|z  }t        | |d
z   d| j                  id      }|sd| _        y|d   d   r|d| _        |d   d   | _        |d   d   r3|d   d   D ](  }|j                  d      }|d   | j                  |d   <   * |d   d   r|d   d   | _        |d   d   |d   d   nd| _        yy)zGet tablespace information.zjSELECT 1 FROM information_schema.columns WHERE table_name = 'pg_tablespace' AND column_name = 'spcoptions'F)add_to_executedzkSELECT 1 FROM information_schema.columns WHERE table_name = 'pg_tablespace' AND column_name = 'spclocation'spclocationzpg_tablespace_location(t.oid)TzSELECT shobj_description(t.oid, 'pg_tablespace') AS comment, r.rolname, (SELECT Null) spcoptions, %s loc_string FROM pg_catalog.pg_tablespace AS t JOIN pg_catalog.pg_roles AS r ON t.spcowner = r.oid zSELECT shobj_description(t.oid, 'pg_tablespace') AS comment, r.rolname, t.spcoptions, %s loc_string FROM pg_catalog.pg_tablespace AS t JOIN pg_catalog.pg_roles AS r ON t.spcowner = r.oid zWHERE t.spcname = %(name)sr   )query_paramsr&   r   rolname
spcoptions=   
loc_stringr   Nr   )	r   r   r   r   r   splitr   r   r   )r!   optr   queryresis         r"   r    zPgTablespace.get_info   sO    t >OTV
 D #DUZ\ $H6H%)D". 19	9E. 19	9E tU%AA%+TYY$7P DKq6)DKQ	*DJ1vl#Q- /AA*+A$DMM!A$'/ 1vl# #A| 403Ay0A0M3q6),SUDL r$   c                 D    d| j                   d|d}t        | |d      S )zCreate tablespace.

        Return True if success, otherwise, return False.

        args:
            location (str) -- tablespace directory path in the FS
        zCREATE TABLESPACE "z" LOCATION ''Treturn_boolr   r   )r!   r   r0   s      r"   createzPgTablespace.create+  s"     >BYYQe66r$   c                 8    t        | d| j                  z  d      S )zSDrop tablespace.

        Return True if success, otherwise, return False.
        zDROP TABLESPACE "%s"Tr5   )r   r   )r!   s    r"   dropzPgTablespace.drop6  s    
 4tyy@dSSr$   c                 d    || j                   k(  ryd| j                  d|d}t        | |d      S )zSet tablespace owner.

        Return True if success, otherwise, return False.

        args:
            new_owner (str) -- name of a new owner for the tablespace"
        FALTER TABLESPACE "z" OWNER TO ""Tr5   )r   r   r   )r!   	new_ownerr0   s      r"   	set_ownerzPgTablespace.set_owner=  s7     

" # :>INe66r$   c                 ~    || j                   k(  ryt        | j                  |d| j                  || j                        S )zSet tablespace comment.

        Return True if success, otherwise, return False.

        args:
            comment (str) -- comment to set for the tablespace"
        F
tablespace)r   r   r   r   r   )r!   r   
check_modes      r"   r   zPgTablespace.set_commentK  s;     dll"4;;tyy%t'<'<> 	>r$   c                 R    d| j                   d|d}|| _        t        | |d      S )zRename tablespace.

        Return True if success, otherwise, return False.

        args:
            newname (str) -- new name for the tablespace"
        r<   z" RENAME TO "r=   Tr5   )r   r   r   )r!   newnamer0   s      r"   renamezPgTablespace.renameY  s(     ;?))WMe66r$   c                 0   | j                   ryd}|D ]  }||   dk(  r0|| j                  v s| j                  |      }d| j                  |<   ;|| j                  vst        ||         | j                  |   k7  sh| j	                  |d||   d      } |S )zSet tablespace settings (options).

        If some setting has been changed, set changed = True.
        After all settings list is handling, return changed.

        args:
            new_settings (list) -- list of new settings
        FresetNz = 'r4   )r   r   _PgTablespace__reset_settingstr_PgTablespace__set_setting)r!   new_settingschangedr2   s       r"   set_settingszPgTablespace.set_settingse  s     !!  	QAA')%"2215G'+DMM!$4==(c,q/.BdmmTUFV.V,,A|A-OP	Q r$   c                 D    d| j                   d|d}t        | |d      S )zReset tablespace setting.

        Return True if success, otherwise, return False.

        args:
            setting (str) -- string in format "setting_name = 'setting_value'"
        r<   z	" RESET ()Tr5   r7   r!   settingr0   s      r"   __reset_settingzPgTablespace.__reset_setting  s!     7;iiIe66r$   c                 D    d| j                   d|d}t        | |d      S )zSet tablespace setting.

        Return True if success, otherwise, return False.

        args:
            setting (str) -- string in format "setting_name = 'setting_value'"
        r<   z" SET (rO   Tr5   r7   rP   s      r"   __set_settingzPgTablespace.__set_setting  s!     59IIwGe66r$   N)__name__
__module____qualname____doc__r#   r    r8   r:   r?   r   rE   rM   rH   rJ    r$   r"   r   r      s<    &3Vj	7T7>
76	7	7r$   r   c                     t               } | j                  t        dddg      t        ddddg      t        ddg	      t        d
      t        d
      t        ddgddddg      t        ddd      t        d
      t        dd      t        dd       
       t        | d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|dk(  r|s|s|s|r|j                  d        |	s9|sd }n"t        |      D cg c]  \  }}|d!| }}}t        ||||||||
       t        |       t        ||j                  d"#      }t        |||j                  rd"nd$      \  }} |j                  d,i t        }d"}d"}t        |||      }|j                  r>|r<||j                   k7  r-|j                  d%|j"                  d&|j                   d'        |j                  sP|dk(  rK|r|j                  d(|z          |s|j                  d)        d}t%        |d       |j'                  |      }n^|j                  r$|dk(  rd}t%        |d       |j)                         }n.|j                  r"|r |j"                  |k7  r|j+                  |      }|dk(  r|j-                          |dk(  rl|j                  r`|r|j/                  |      xs |}|r|j1                  |      xs |}|
 |j3                  |
|j                        xs |}|j-                          |s-|j                  r|j5                          n|j7                          |j9                          |j9                          t        |d|j"                  |j:                  |j<                  |j>                  |j                   |j@                  *      }|dk(  r!d|d<   |jB                  r|jB                  |d+<   n
|dk(  rd|d<    |jD                  d,i | y c c}}w )-NrI   Tr   )typerequiredaliasespresentabsent)r[   defaultchoicespath)r[   r]   )r[   dictdbz5.0.0zcommunity.postgresql)r   versioncollection_name)r[   r]   deprecated_aliases)r[   removed_in_versionremoved_from_collectionbool)r[   r`   )
rA   stater   r   setlogin_db	rename_tosession_roletrust_inputr   )argument_specsupports_check_moderA   rk   r   r   rn   rl   ro   rp   r   zFstate=absent is mutually exclusive location, owner, rename_to, and set)msgz = F)warn_db_default)
autocommitzTablespace 'z"' exists with different location 'r4   z/Tablespace %s does not exist, nothing to renamezV'location' parameter must be passed with state=present if the tablespace doesn't exist)rL   rk   rA   r   queriesoptionsr   r   rD   rY   )#r   updaterc   r   params	fail_jsonr   r   r
   r   r	   rB   r   r   r   r   r   r   r   r8   r:   rE   r    r?   rM   r   rollbackcommitcloser   r   r   r   r   	exit_json)rq   r   rA   rk   r   r   rn   r   ro   rp   r   settings_listkvconn_paramsdb_connectiondummyr   ru   rL   tblspacekws                         r"   mainr     sa   13MUTF8Dy8Y:OP6F84f54&"#9F 
 Eg/EGu%fd3%.#  ( # F
 |,JMM'"E}}Z(HMM'"Ek*I}}U#H==0L--.KmmI&Gh%9 9 	:  M<Eh<OPDAq!Q/PMPFJ%|]G	E  !&&--OK(RXRcRcimnM5!]!!3N3F JG FFJ7H 8H4E4E(E:B--IZIZ\ 	] ??u	1!RU_!_` "Q R 
}d+//(+ 
Uh.
}d+--/ 
Y==I%ooi0G	 	hoo((/:7G++H5@G**7F4E4EFQ'G 	 ""$  "
LLN 
==nn))!!""  	
B 	7$--ByM	(	7FrK Qs   'Q!__main__N)
__future__r   r   r   r[   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ansible.module_utils.sixr   Fansible_collections.community.postgresql.plugins.module_utils.databaser   Fansible_collections.community.postgresql.plugins.module_utils.postgresr	   r
   r   r   r   r   r   r   objectr   r   rU   rY   r$   r"   <module>r      sq    A @m^6)
V 5 .	 	 	B76 B7TSl zF r$   