
    Vh)                         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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_functiona  
---
module: postgresql_slot
short_description: Add or remove replication slots from a PostgreSQL database
description:
- Add or remove physical or logical replication slots from a PostgreSQL database.

options:
  name:
    description:
    - Name of the replication slot to add or remove.
    type: str
    required: true
    aliases:
    - slot_name
  slot_type:
    description:
    - Slot type.
    type: str
    default: physical
    choices: [ logical, physical ]
  state:
    description:
    - The slot state.
    - I(state=present) implies the slot must be present in the system.
    - I(state=absent) implies the I(groups) must be revoked from I(target_roles).
    type: str
    default: present
    choices: [ absent, present ]
  immediately_reserve:
    description:
    - Optional parameter that when C(true) specifies that the LSN for this replication slot be reserved
      immediately, otherwise the default, C(false), specifies that the LSN is reserved on the first connection
      from a streaming replication client.
    - Is available from PostgreSQL version 9.6.
    - Uses only with I(slot_type=physical).
    - Mutually exclusive with I(slot_type=logical).
    type: bool
    default: false
  output_plugin:
    description:
    - All logical slots must indicate which output plugin decoder they're using.
    - This parameter does not apply to physical slots.
    - It will be ignored with I(slot_type=physical).
    type: str
    default: "test_decoding"
  login_db:
    description:
    - Name of database to connect to.
    - The V(db) alias is deprecated and will be removed in version 5.0.0.
    type: str
    aliases:
    - db
  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
  trust_input:
    description:
    - If C(false), check the value of I(session_role) is potentially dangerous.
    - It makes sense to use C(false) only when SQL injections via I(session_role) are possible.
    type: bool
    default: true
    version_added: '0.2.0'

notes:
- Physical replication slots were introduced to PostgreSQL with version 9.4,
  while logical replication slots were added beginning with version 10.0.

attributes:
  check_mode:
    support: full

seealso:
- name: PostgreSQL pg_replication_slots view reference
  description: Complete reference of the PostgreSQL pg_replication_slots view.
  link: https://www.postgresql.org/docs/current/view-pg-replication-slots.html
- name: PostgreSQL streaming replication protocol reference
  description: Complete reference of the PostgreSQL streaming replication protocol documentation.
  link: https://www.postgresql.org/docs/current/protocol-replication.html
- name: PostgreSQL logical replication protocol reference
  description: Complete reference of the PostgreSQL logical replication protocol documentation.
  link: https://www.postgresql.org/docs/current/protocol-logical-replication.html

author:
- John Scalia (@jscalia)
- Andrew Klychkov (@Andersson007)
- Thomas O'Donnell (@andytom)
extends_documentation_fragment:
- community.postgresql.postgres
az  
- name: Create physical_one physical slot if doesn't exist
  become_user: postgres
  community.postgresql.postgresql_slot:
    slot_name: physical_one
    login_db: ansible

- name: Remove physical_one slot if exists
  become_user: postgres
  community.postgresql.postgresql_slot:
    slot_name: physical_one
    db: ansible
    state: absent

- name: Create logical_one logical slot to the database acme if doesn't exist
  community.postgresql.postgresql_slot:
    name: logical_slot_one
    slot_type: logical
    state: present
    output_plugin: custom_decoder_one
    login_db: acme

- name: Remove logical_one slot if exists from the cluster running on another host and non-standard port
  community.postgresql.postgresql_slot:
    name: logical_one
    login_host: mydatabase.example.org
    port: 5433
    login_user: ourSuperuser
    login_password: thePassword
    state: absent
a  
name:
  description: Name of the slot.
  returned: success
  type: str
  sample: "physical_one"
queries:
  description: List of executed queries.
  returned: success
  type: str
  sample: [ "SELECT pg_create_physical_replication_slot('physical_one', False, False)" ]
)AnsibleModule)check_input)connect_to_dbensure_required_libsexec_sqlget_conn_paramsget_server_versionpg_cursor_argspostgres_common_argument_specc                   &    e Zd Zd ZddZd Zd Zy)PgSlotc                     || _         || _        || _        d| _        d| _        | j                          d| _        g | _        y )NF )modulecursornameexistskind_PgSlot__slot_existschangedexecuted_queries)selfr   r   r   s       x/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_slot.py__init__zPgSlot.__init__   s@    		 "    c                    | j                   rG| j                  |k(  ry| j                  j                  d| j                  d| j                  d       y|ry |dk(  rIt        | j                  j                        dk  rd}nd}t        | || j                  |d	d
      | _	        y |dk(  r$d}t        | || j                  |dd
      | _	        y y )NFzslot with name 'z'' already exists but has another type ''physicalia z4SELECT pg_create_physical_replication_slot(%(name)s)zCSELECT pg_create_physical_replication_slot(%(name)s, %(i_reserve)s))r   	i_reserveTquery_paramsreturn_boollogicalzASELECT pg_create_logical_replication_slot(%(name)s, %(o_plugin)s))r   o_plugin)
r   r   r   warnr   r   r   
connectionr
   r   )r   r   immediately_reserveoutput_plugin
just_checkquerys         r   createzPgSlot.create   s    ;;yyD   @D		499"V W:!$++"8"89EAN ^#D%:>))Re1f046DL YWE#D%:>))Q^1_mqsDL r   c                 b    | j                   syd}t        | |d| j                  id      | _        y )NFz)SELECT pg_drop_replication_slot(%(name)s)r   Tr#   )r   r
   r   r   )r   r-   s     r   dropzPgSlot.drop   s-    {{;e6499:M[_`r   c                 l    d}t        | |d| j                  id      }|rd| _        |d   d   | _        y y )NzESELECT slot_type FROM pg_replication_slots WHERE slot_name = %(name)sr   F)r$   add_to_executedTr   	slot_type)r
   r   r   r   )r   r-   ress      r   __slot_existszPgSlot.__slot_exists   s@    WtU&$))1DV[\DKA{+DI r   N)r!   FFF)__name__
__module____qualname__r   r.   r0   r    r   r   r   r      s    #s:a,r   r   c                  p   t               } | j                  t        ddgddddg      t        dddg	      t        dd
dd
g      t        dd      t        d      t        dd      t        ddddg      t        dd             t        | d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   st        ||j                  d          |r|dk(  r|j                  d       |dk(  rd}nd}t        |       t        ||j                  |      }t        ||d       \  }	}
 |	j                  d#i t        }t        |||      }d}|j                  r=|dk(  r$|j                  sd}|j                  |||d!       nN|dk(  rI|j                  r=d}n:|dk(  r|j!                          n|dk(  r|j                  |||       |j"                  }|	j%                          |j'                  |||j(                  "       y )$Nstrdbz5.0.0zcommunity.postgresql)r   versioncollection_name)typealiasesdeprecated_aliasesT	slot_name)r?   requiredr@   r!   r&   )r?   defaultchoicesboolF)r?   rD   )r?   test_decodingpresentabsent)login_dbr   r3   r*   session_roler+   statetrust_input)argument_specsupports_check_moder   r3   r*   rL   r+   rM   rK   zRModule parameters immediately_reserve and slot_type=logical are mutually exclusive)msg)warn_db_default)
autocommit)r,   )r   r   queriesr9   )r   updatedictr   paramsr   	fail_jsonr	   r   r   r   r   r   
check_moder   r.   r0   r   close	exit_jsonr   )rN   r   r   r3   r*   rL   r+   rQ   conn_paramsdb_connectiondummyr   pg_slotr   s                 r   mainr_      s<   13M54&"#9F 
 utk]CE:	:?VW fe<u%?y8Y:OPfd3  " # F
 == Dk*I --(=>MM'"EMM/2M=='FFMM.9:yI5qr I  !&&--YK(NM5!]!!3N3F VVT*GGI>>NN9&9=UYNZh~~HLLNiNN9&9=I//
W49Q9QRr   __main__N)
__future__r   r   r   r?   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Fansible_collections.community.postgresql.plugins.module_utils.databaser   Fansible_collections.community.postgresql.plugins.module_utils.postgresr   r	   r
   r   r   r   r   objectr   r_   r6   r9   r   r   <module>rj      sm    A @]~@
 5  4,V 4,xOSd zF r   