
    Vh%                         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 d	 Zd
 Z e
j                   dd      d        Zd Zd Zd Zd Zd Zd Zedk(  r e        yy# e$ r Y _w xY w)a  
---
module: msk_config
short_description: Manage Amazon MSK cluster configurations
version_added: "2.0.0"
description:
    - Create, delete and modify Amazon MSK (Managed Streaming for Apache Kafka) cluster configurations.
    - Prior to release 5.0.0 this module was called C(community.aws.aws_msk_config).
      The usage did not change.
author:
    - Daniil Kupchenko (@oukooveu)
options:
    state:
        description: Create (C(present)) or delete (C(absent)) cluster configuration.
        choices: ['present', 'absent']
        default: 'present'
        type: str
    name:
        description: The name of the configuration.
        required: true
        type: str
    description:
        description: The description of the configuration.
        type: str
        default: ''
    config:
        description: Contents of the server.properties file.
        type: dict
        default: {}
        aliases: ['configuration']
    kafka_versions:
        description:
            - The versions of Apache Kafka with which you can use this MSK configuration.
            - Required when I(state=present).
        type: list
        elements: str
extends_documentation_fragment:
    - amazon.aws.common.modules
    - amazon.aws.region.modules
    - amazon.aws.boto3
a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- community.aws.msk_config:
    name: kafka-cluster-configuration
    state: present
    kafka_versions:
      - 2.6.0
      - 2.6.1
    config:
      auto.create.topics.enable: false
      num.partitions: 1
      default.replication.factor: 3
      zookeeper.session.timeout.ms: 18000

- community.aws.msk_config:
    name: kafka-cluster-configuration
    state: absent
a  
# These are examples of possible return values, and in general should use other names for return values.

arn:
    description: The Amazon Resource Name (ARN) of the configuration.
    type: str
    returned: I(state=present)
    sample: "arn:aws:kafka:<region>:<account>:configuration/<name>/<resource-id>"
revision:
    description: The revision number.
    type: int
    returned: I(state=present)
    sample: 1
server_properties:
    description: Contents of the server.properties file.
    type: str
    returned: I(state=present)
    sample: "default.replication.factor=3\nnum.io.threads=8\nzookeeper.session.timeout.ms=18000"
response:
    description: The response from actual API call.
    type: dict
    returned: always
    sample: {}
    N)camel_dict_to_snake_dict)get_aws_account_info)AWSRetry)AnsibleCommunityAWSModulec                 l    t        |       dk(  rydj                  d | j                         D              S )z+convert dictionary to multi-line propertiesr    
c              3   0   K   | ]  \  }}| d |   yw)=N ).0kvs      l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/msk_config.py	<genexpr>zdict_to_prop.<locals>.<genexpr>o   s     6DAqs!A3Z6s   )lenjoinitems)ds    r   dict_to_propr   k   s+    
1v{996AGGI666    c                     t        |       dk(  ri S i }| j                         j                  d      D ]:  }|j                  d      }|d   j                         ||d   j                         <   < |S )z convert properties to dictionaryr   r	   r      )r   decodesplitstrip)pr_dictskvs       r   prop_to_dictr!   r   si    
1v{	FXXZd# .WWS\ "1r!u{{}. Mr      )retriesdelayc                 `    | j                  d      }|j                         j                         S )Nlist_configurations)get_paginatorpaginatebuild_full_result)client	paginators     r   get_configurations_with_backoffr,      s+    $$%:;I1133r   c                 n   |j                   d   	 t        |       d   }t        fdD              }|r)t        |      dk(  r|d   S |j                  d	 d
       y# t        j                  j                  t        j                  j
                  f$ r}|j                  |d       Y d}~d}~ww xY w)z+
    looking for configuration by name
    nameConfigurationsz%failed to obtain kafka configurations)msgNc              3   F   K   | ]  }|d    k(  s|d   dk(  s|  yw)NameStateACTIVENr   )r   itemr.   s     r   r   z%find_active_config.<locals>.<genexpr>   s+     m4$v,$:NSWX_S`dlSl$ms   !!!r   r   z-found more than one active config with name '')	paramsr,   botocore
exceptionsClientErrorBotoCoreErrorfail_json_awslistr   )r*   moduleall_configseactive_configsr.   s        @r   find_active_configrB      s    
 == DM5f=>NO m;mmN~!#!!$$  'TUYTZZ[%\ ] ++X-@-@-N-NO MQ$KLLMs   A   7B4B//B4c                     	 | j                  ||d      S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~y d }~ww xY w)NT)ArnRevision	aws_retryz/failed to describe kafka configuration revision)describe_configuration_revisionr8   r9   r;   r:   r<   )r*   r>   arnrevisionr@   s        r   get_configuration_revisionrJ      si    S55#\`5aa))'' S 	Q QRR	Ss    7A)A$$A)c                 2   i }| j                   j                  d      j                         D ]  \  }}t        |      |t        |      <    t	        |j                  dd            |k(  r/|j                  dd      | j                   j                  d      k(  ryy)z
    compare configuration's description and properties
    python 2.7+ version:
    prop_module = {str(k): str(v) for k, v in module.params.get("config").items()}
    configServerPropertiesr   DescriptiondescriptionFT)r7   getr   strr!   )r>   currentprop_moduler   r   s        r   is_configuration_changedrT      s     K!!(+113 %1!!fCF%GKK 2B78KG;;}b)V]]->->}-MMr   c           
      0   t        | |      }|s|j                  rdi fS 	 | j                  |j                  j	                  d      |j                  j	                  d      |j                  j	                  d      t        |j                  j	                  d            j                         d      }nt        | ||d	   |d
   d         }t        ||      sd|fS |j                  rdi fS 	 | j                  |d	   |j                  j	                  d      t        |j                  j	                  d            j                         d      }d	   }|d
   d   }t        | |||      }d|fS # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d}~nd}~ww xY w# t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d}~d}~ww xY w)z+create new or update existing configurationTr.   rO   kafka_versionsrL   )r2   rN   KafkaVersionsrM   rF   z$failed to create kafka configurationNrD   LatestRevisionrE   )rH   rI   F)rD   rN   rM   rF   z$failed to update kafka configuration)rB   
check_modecreate_configurationr7   rP   r   encoder8   r9   r;   r:   r<   rJ   rT   update_configuration)r*   r>   rL   responser@   rH   rI   results           r   create_configr_      s	     /F 8O	L22]]&&v."MM--m<$mm//0@A!-fmm.?.?.I!J!Q!Q!S 3 H  .Fu?O8PQ[8\
 (9(?"8O	L225M"MM--m<!-fmm.?.?.I!J!Q!Q!S	 3 H 5/C()*5H'C(SF<I --++
 	L   $JKK		L6 --++
 	L   $JKK		Ls1   BE, +A!G ,7F?#F::F?7H9HHc                 :   t        | |      }|j                  r
|rd|fS di fS |r	 | j                  |d   d      }d|fS di fS # t        j                  j
                  t        j                  j                  f$ r}|j                  |d       Y d}~dfS d}~ww xY w)zdelete configurationTFrD   )rD   rF   z(failed to delete the kafka configurationN)rB   rY   delete_configurationr8   r9   r;   r:   r<   )r*   r>   rL   r]   r@   s        r   delete_configrb      s      /F<"9	P22ve}PT2UH X~"9 --++
 	P   $NOOX~	Ps   A 7B;BBc            
         t        t        dd      t        dd      t        ddgd      t        d	d
gi       t        dd            } t        | d      }|j                  dt        j                               }|j
                  d   dk(  rt        ||      \  }}n!|j
                  d   dk(  rt        ||      \  }}|j                  rJj                  d      s9t        |      \  }}d| d|j                   d| d|j
                  d    d	}d}d}	n4j                  d      }|j                  d      }|j                  dd      }	|j                  |||	t        |             y )NrQ   T)typerequiredr   )rd   defaultpresentabsent)choicesrf   dictconfiguration)rd   aliasesrf   r=   )rd   elements)r.   rO   staterL   rV   )argument_specsupports_check_modekafka)retry_decoratorrn   rD   zarn:z:kafka::z:configuration/r.   z/idr   rE   rM   )changedrH   rI   server_propertiesr]   )rj   AnsibleAWSModuler*   r   jittered_backoffr7   r_   rb   rY   rP   r   region	exit_jsonr   )
module_argsr>   r*   rt   r]   
account_id	partitionrH   rI   ru   s
             r   mainr}   	  sm   ut,eR0Ix0)D/):BG%8K KTRF]]7H4M4M4O]PF}}W*)&&9	w	8	+)&&9 e!4 4V <
IYKwv}}oQzl/RXR_R_`fRgQhhklll5!<<
+$LL);R@
+)(3  r   __main__)DOCUMENTATIONEXAMPLESRETURNr8   ImportError0ansible.module_utils.common.dict_transformationsr   7ansible_collections.amazon.aws.plugins.module_utils.iamr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   >ansible_collections.community.aws.plugins.module_utils.modulesr   rv   r   r!   rw   r,   rB   rJ   rT   r_   rb   r}   __name__r   r   r   <module>r      s   (T(
2	 V X P x7 1A.4 /4
.S7t0%P zF c  		s   A$ $A,+A,