
    VhO                     r   d Z dZdZ	 ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 i d	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,iZd-d.d/d0d1d2d3d.d.d.d4d5Zg d6Zd7 ZdHd8Zd9 Zd: Zd; Zd< Zd= Zd> Zd? Zd@ ZdA ZdB ZdC ZdD ZdE ZdF ZedGk(  r e        yy# e$ r Y w xY w)Iam  
---
module: mq_broker
version_added: 6.0.0
short_description: MQ broker management
description:
  - Create/update/delete a broker.
  - Reboot a broker.
author:
  - FCO (@fotto)
options:
  broker_name:
    description:
      - The Name of the MQ broker to work on.
    type: str
    required: true
  state:
    description:
      - "C(present): Create/update broker."
      - "C(absent): Delete broker."
      - "C(restarted): Reboot broker."
    choices: [ 'present', 'absent', 'restarted' ]
    default: present
    type: str
  deployment_mode:
    description:
      - Set broker deployment type.
      - Can be used only during creation.
      - Defaults to C(SINGLE_INSTANCE).
    choices: [ 'SINGLE_INSTANCE', 'ACTIVE_STANDBY_MULTI_AZ', 'CLUSTER_MULTI_AZ' ]
    type: str
  use_aws_owned_key:
    description:
      - Must be set to C(false) if I(kms_key_id) is provided as well.
      - Can be used only during creation.
      - Defaults to C(true).
    type: bool
  kms_key_id:
    description:
      - Use referenced key to encrypt broker data at rest.
      - Can be used only during creation.
    type: str
  engine_type:
    description:
      - Set broker engine type.
      - Can be used only during creation.
      - Defaults to C(ACTIVEMQ).
    choices: [ 'ACTIVEMQ', 'RABBITMQ' ]
    type: str
  maintenance_window_start_time:
    description:
      - Set maintenance window for automatic minor upgrades.
      - Can be used only during creation.
      - Not providing any value means "no maintenance window".
    type: dict
  publicly_accessible:
    description:
      - Allow/disallow public access.
      - Can be used only during creation.
      - Defaults to C(false).
    type: bool
  storage_type:
    description:
      - Set underlying storage type.
      - Can be used only during creation.
      - Defaults to C(EFS).
    choices: [ 'EBS', 'EFS' ]
    type: str
  subnet_ids:
    description:
      - Defines where deploy broker instances to.
      - Minimum required number depends on deployment type.
      - Can be used only during creation.
    type: list
    elements: str
  users:
    description:
      - This parameter allows to use a custom set of initial user(s).
      - M(community.aws.mq_user) is the preferred way to manage (local) users
        however a broker cannot be created without any user.
      - If nothing is specified a default C(admin) user will be created along with brokers.
      - Can be used only during creation.  Use M(community.aws.mq_user) module for updates.
    type: list
    elements: dict
  tags:
    description:
      - Tag newly created brokers.
      - Can be used only during creation.
    type: dict
  authentication_strategy:
    description: Choose between locally and remotely managed users.
    choices: [ 'SIMPLE', 'LDAP' ]
    type: str
  auto_minor_version_upgrade:
    description: Allow/disallow automatic minor version upgrades.
    type: bool
    default: true
  engine_version:
    description:
      - Set engine version of broker.
      - The special value C(latest) will pick the latest available version.
      - The special value C(latest) is ignored on update.
    type: str
  host_instance_type:
    description: Instance type of broker instances.
    type: str
  enable_audit_log:
    description: Enable/disable to push audit logs to AWS CloudWatch.
    type: bool
    default: false
  enable_general_log:
    description: Enable/disable to push general logs to AWS CloudWatch.
    type: bool
    default: false
  security_groups:
    description:
      - Associate security groups with broker.
      - At least one must be provided during creation.
    type: list
    elements: str
  wait:
    description:
      - Specifies whether the module waits for the desired C(state).
      - The time to wait can be controlled by setting I(wait_timeout).
    type: bool
    default: false
    version_added: 7.1.0
  wait_timeout:
    description:
      - How long to wait (in seconds) for the broker to reach the desired state if I(wait=true).
    default: 900
    type: int
    version_added: 7.1.0

extends_documentation_fragment:
  - amazon.aws.boto3
  - amazon.aws.common.modules
  - amazon.aws.region.modules
a  
- name: create broker (if missing) with minimal required parameters
  community.aws.mq_broker:
    broker_name: "{{ broker_name }}"
    security_groups:
      - sg_xxxxxxx
    subnet_ids:
      - subnet_xxx
      - subnet_yyy
    register: result

- set_fact:
    broker_id: "{{ result.broker['BrokerId'] }}"

- name: use mq_broker_info to wait until broker is ready
  community.aws.mq_broker_info:
    broker_id: "{{ broker_id }}"
  register: result
  until: "result.broker['BrokerState'] == 'RUNNING'"
  retries: 15
  delay: 60

- name: create or update broker with almost all parameter set including credentials
  community.aws.mq_broker:
    broker_name: "my_broker_2"
    state: present
    deployment_mode: 'ACTIVE_STANDBY_MULTI_AZ'
    use_aws_owned_key: false
    kms_key_id: 'my-precreted-key-id'
    engine_type: 'ACTIVEMQ'
    maintenance_window_start_time:
      DayOfWeek: 'MONDAY'
      TimeOfDay: '03:15'
      TimeZone: 'Europe/Berlin'
    publicly_accessible: true
    storage_type: 'EFS'
    security_groups:
      - sg_xxxxxxx
    subnet_ids:
      - subnet_xxx
      - subnet_yyy
    users:
      - Username: 'initial-user'
        Password': 'plain-text-password'
        ConsoleAccess: true
    tags:
      env: Test
      creator: ansible
    authentication_strategy: 'SIMPLE'
    auto_minor_version_upgrade: true
    engine_version: "5.15.13"
    host_instance_type: 'mq.t3.micro'
    enable_audit_log: true
    enable_general_log: true

- name: reboot a broker
  community.aws.mq_broker:
    broker_name: "my_broker_2"
    state: restarted

- name: delete a broker
  community.aws.mq_broker:
    broker_name: "my_broker_2"
    state: absent
a  
broker:
  description:
    - "All API responses are converted to snake yaml except 'Tags'"
    - "'state=present': API response of create_broker() or update_broker() call"
    - "'state=absent': result of describe_broker() call before delete_broker() is triggerd"
    - "'state=restarted': result of describe_broker() after reboot has been triggered"
  type: dict
  returned: success
    N)sleep)time)camel_dict_to_snake_dict)AnsibleAWSModuleauthentication_strategyAuthenticationStrategyauto_minor_version_upgradeAutoMinorVersionUpgradebroker_name
BrokerNamedeployment_modeDeploymentModeuse_aws_owned_keyz EncryptionOptions/UseAwsOwnedKey
kms_key_idzEncryptionOptions/KmsKeyIdengine_type
EngineTypeengine_versionEngineVersionhost_instance_typeHostInstanceTypeenable_audit_logz
Logs/Auditenable_general_logzLogs/Generalmaintenance_window_start_timeMaintenanceWindowStartTimepublicly_accessiblePubliclyAccessiblesecurity_groupsSecurityGroupsstorage_typeStorageType
subnet_ids	SubnetIdsusersUserstagsTagsSIMPLEFSINGLE_INSTANCETACTIVEMQlatestzmq.t3.microEFS)r   r	   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r!   r#   r%   c                     t         |   }d|v r"|j                  d      }|j                          n|g}| }t        |      dkD  r-|j	                         }||vri ||<   ||   }t        |      dkD  r-|||d   <   y )N/   r   )
PARAMS_MAPsplitreverselenpop)kwargskeyvalue
mapped_keykey_listdatathis_keys          k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/mq_broker.py
_set_kwargr<      s    CJ
j##C(<D
h-!
<<>4DNH~ h-!
 D!    c                     i }|r)t         j                         D ]  \  }}t        |||        | j                  D ]?  }|r	|t        v r|t
        v r*| j                  |   t        ||| j                  |          @A |S )N)DEFAULTSitemsr<   paramsCREATE_ONLY_PARAMSr/   )moduleapply_defaultsignore_create_paramsr4   p_namep_values         r;   _fill_kwargsrH   1  s    F'~~/ 	0OFGvvw/	0-- 	F.@$@ ZFMM&$9$Evvv}}V'<= 	 Mr=   c                 n    t        |       t        |      k7  ryt        |       D ]  \  }}|||   k7  s y yNTF)r2   	enumerate)currentdesiredindexr6   s       r;   __list_needs_changerO   C  sA    
7|s7|#!'* uGEN" r=   c                 6    |D ]  }|| v s||   | |   k7  s y yrJ    )rL   rM   r5   s      r;   __dict_needs_changerR   M  s2      '>s|ws|+
 r=   c                    d}|D ]z  }| |   }||   }t        |t        t        t        f      r||k7  s.d} |S t        |t              rt        ||      sPd} |S t        |t              rt        ||      srd} |S d} |S  |S )NFT)
isinstanceintstrboollistrO   dictrR   )rL   rM   needs_changer5   current_valuedesired_values         r;   _needs_changer]   X  s    L mc3%56-#   t,"=-@#  t,"=-@#   L-, r=   c                     	 | j                  |      }|d   d   d   d   d   S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~y d }~ww xY w)N)r   BrokerEngineTypesr   EngineVersionsNamezCouldn't list engine versionsmsg)describe_broker_engine_typesbotocore
exceptionsClientErrorBotoCoreErrorfail_json_aws)connrC   r   responsees        r;   get_latest_engine_versionrm   s  s    E444L+,Q/0@A!DVLL++X-@-@-N-NO EQ$CDDEs   "% 7A9A44A9c                 0   	 |j                   d   }d }| j                  d      }|d   D ]  }|d   |k(  s|d   } |S  |S # t        j                  j                  t        j                  j
                  f$ r}|j                  |d       Y d }~y d }~ww xY w)	Nr   d   )
MaxResultsBrokerSummariesr   BrokerIdzCouldn't list broker brokers.rb   )rA   list_brokersre   rf   rg   rh   ri   )rj   rC   r   	broker_idrk   brokerrl   s          r;   get_broker_idrv   {  s    
EmmM2	$$$401 	Fl#{2":.			 ++X-@-@-N-NO EQ$CDDEs!   3A A A 7B8BBc                     	 | j                  |      S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~y d }~ww xY w)Nrr   zCouldn't get broker details.rb   )describe_brokerre   rf   rg   rh   ri   )rj   rC   rt   rl   s       r;   get_broker_inforz     sb    D##Y#77++X-@-@-N-NO DQ$BCCDs    7A(A##A(c                     d}|j                   j                  dd      }|j                   j                  d      }|j                   j                  d      }d}| j                  d      }|j                  ddd	d
      }t	               |z   }	|	t	               kD  r{	 |j                  d| d      }
t        |
      }|j                  ry t        |      dk  r|dk(  rd}n7|dv r|d   d   dk(  rd}n%|d   d   dk(  rnt        |       |	t	               kD  r{|s|j                  d       y y # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~sd }~ww xY w)N   wait_timeout  r   stateFrs   ro    )MaxItemsPageSizeStartingToken)PaginationConfigz BrokerSummaries[?BrokerName == `z`][]r.   absentT)presentrebootedr   BrokerStateRUNNINGCREATION_FAILEDzCouldn't paginate brokers.rb   zdesired state not reached)rA   getget_paginatorpaginater   searchrX   
check_moder2   r   re   rf   rg   rh   ri   	fail_json)rj   rC   interval_secstimeoutr   desired_statedone	paginatorpage_iteratorr}   filtered_iteratorbroker_listrl   s                r;   wait_for_statusr     s   Mmm4G--##M2KMM%%g.MD"">2I&&SVYln8o&pM6G#L

	F - 4 47WXcWddh5i j01K  ;!#(A 77KN=<Y]f<f1~m,0AA- ' 
0 89  ##//1D1D1R1RS 	F  (D EE	Fs0   ,D) D) D) .D) :D) )7E= E88E=c                 ,   |j                   j                  d      }	 | j                  |      }|rt        | |       S # t        j                  j
                  t        j                  j                  f$ r}|j                  |d       Y d }~_d }~ww xY w)Nwaitrx   zCouldn't reboot broker.rb   )	rA   r   reboot_brokerre   rf   rg   rh   ri   r   rj   rC   rt   r   rk   rl   s         r;   r   r         ==V$D?%%y%9 f%O ++X-@-@-N-NO ?Q$=>>?   ? 7B6BBc                 ,   |j                   j                  d      }	 | j                  |      }|rt        | |       S # t        j                  j
                  t        j                  j                  f$ r}|j                  |d       Y d }~_d }~ww xY w)Nr   rx   zCouldn't delete broker.rb   )	rA   r   delete_brokerre   rf   rg   rh   ri   r   r   s         r;   r   r     r   r   c                    t        |      }|j                  j                  d      }d|v r|d   dk(  rt        | ||d         |d<   |d   dk(  r|j	                  d       d	|vrd
ddg dg|d	<   d|v rd|d   v rd|d   d<   d|vst        |d         dk(  r|j	                  d       d} | j                  di |}|rt        | |       t        |dg      |dS )Nr   r   r*   r   r   LDAPz0'AuthenticationStrategy=LDAP' not supported, yetrb   r$   adminadminPasswordT)UsernamePasswordConsoleAccessGroupsEncryptionOptionsUseAwsOwnedKeyFr   r   z@At least one security group must be specified on broker creationr&   ignore_listru   changedrQ   )	rH   rA   r   rm   r   r2   create_brokerr   r   )rj   rC   r4   r   r   results         r;   r   r     s   &!F==V$D& VO%<%H";D&&Q]J^"_&'61OPf(/__coqrswf$)9VDW=X)X8="#$45v%V4D-E)F!)K_`GT)&)Ff%.vF8LY`aar=   c                    t        |dd      }|j                  j                  d      }|d   }|d= ||d<   t        | ||      }|d   dk7  r|j	                  d	| d
|d           d|v r|d   dk(  r|d   |d<   ||d}d}t        ||      r d}|j                  s | j                  di |}|rt        | |       ||dS )NFT)rD   rE   r   r   rr   r   r   z$Cannot trigger update while broker (z) is in state rb   r   r*   )rt   r   r   rQ   )	rH   rA   r   rz   r   r]   r   update_brokerr   )	rj   rC   rt   r4   r   r   
api_resultr   r   s	            r;   r   r     s   &TRF==V$D&K|"F: vy9J- I-6ykPZ[hPiOjk 	 	

 & VO%<%H",_"=$[AFGZ(  +++5f5J f%11r=   c                 `   |j                   d   d d}|j                  rt        |dg      ddS t        | |      }||d<   |s|ddS 	 t	        | ||      }|d	   d
k(  r|ddS t        | ||       |ddS # t        j                  j                  $ r}|j                  |       Y d }~8d }~ww xY w)Nr   )r   rt   r&   r   Tr   rt   Fr   DELETION_IN_PROGRESS)
rA   r   r   rv   rz   r   re   rf   rg   ri   )rj   rC   r   rt   r   rl   s         r;   ensure_absentr     s    #]]=9MF26xP]abbdF+I#F; U33 $T69=
m$(>>$77dFI. .. **  Q s   	A5 #A5 5B-B((B-c                 x    |j                   rdddddS t        | |      }|rt        | ||      S t        | |      S )NfakeArnfakeId)
broker_arnrt   Tr   )r   rv   r   r   )rj   rC   rt   s      r;   ensure_presentr   #  sG    )2JW[\\dF+IT6955v&&r=   c            
         t        d7i dt        dd      dt        dg d      d	t        d
d      dt        dd      dt        g d      dt        d      dt        d      dt        ddgd      dt        d      dt        d      dt        ddg      d t        d!d"      d#t        d!d"      d$t        d      d%t        d&d'g      d(t        dd      d)t        d      d*t        d      d+t        d
d      d,t        d
d      d-t        d!d"      } t        | d.      }|j                  d/      }|j                  d   dk(  r	 t	        ||      } |j                  d7i  |j                  d   d0k(  r	 t        ||      } |j                  d7i  |j                  d   d1k(  rt        ||      }|j                  r|j                  d2|r|nd3id4       |s|j                  d56       	 d}|j                  st        |||       t        |||      }|j                  4       y y # t
        j                  j                  $ r}|j                  |       Y d }~d }~ww xY w# t
        j                  j                  $ r}|j                  |       Y d }~d }~ww xY w# t
        j                  j                  $ r}|j                  |       Y d }~d }~ww xY w)8Nr   TrV   )requiredtyper   r   )r   r   	restarted)defaultchoicesr   FrW   )r   r   r}   r~   rU   r   )r(   ACTIVE_STANDBY_MULTI_AZCLUSTER_MULTI_AZ)r   r   )r   r   r   r)   RABBITMQ)r   r   r   rY   r   r   EBSr+   r!   rX   )r   elementsr#   r%   r   r'   r   r	   r   r   r   r   r   )argument_specsupports_check_modemqr   r   rt   r   r   z<Cannot find broker with name {module.params['broker_name']}.rb   rQ   )rY   r   clientrA   r   re   rf   rg   ri   	exit_jsonr   rv   r   r   r   rz   )r   rC   
connectioncompound_resultrl   rt   r   r   s           r;   mainr   .  s!    $U39.PQ %f- #E2	 %gh F+ U# *j!9F '+&7 !f- 5%.1 Ve4 0 v" !%h-? @#$ $(6#B%& ''(  U+)* e&9+,  F;-. &59/M4 MtTFt$J}}W*	$,Z@O 	+?+}}W)	$+J?O 	+?+}}W,!*f5	[y)h$WaefR  	$G$$j&)<$ZCF 	8! - "".. 	$  ##	$ "".. 	$  ##	$& "".. 	$  ##	$sH   !I J (J= I>"I99I>J:J55J:=K5K00K5__main__)TF)DOCUMENTATIONEXAMPLESRETURNre   ImportErrorr   r   0ansible.module_utils.common.dict_transformationsr   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   r/   r?   rB   r<   rH   rO   rR   r]   rm   rv   rz   r   r   r   r   r   r   r   r   __name__rQ   r=   r;   <module>r      s  JZ@D	
	
   U X7 "; < '	
 ; . < o ,  . $%A / ' M  +!" W#$ F%
.  ("'('  "$6EED$:Nb42>/.'?9D zF [  		s   B. .B65B6