
    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d Zd Zd Zedk(  r e        yy# e$ r Y <w xY w)a  
---
module: config_rule
version_added: 1.0.0
short_description: Manage AWS Config rule resources
description:
  - Module manages AWS Config rules.
  - Prior to release 5.0.0 this module was called C(community.aws.aws_config_rule).
    The usage did not change.
author:
  - "Aaron Smith (@slapula)"
options:
  name:
    description:
      - The name of the AWS Config resource.
    required: true
    type: str
  state:
    description:
      - Whether the Config rule should be present or absent.
    default: present
    choices: ['present', 'absent']
    type: str
  description:
    description:
      - The description that you provide for the AWS Config rule.
    type: str
  scope:
    description:
      - Defines which resources can trigger an evaluation for the rule.
    suboptions:
      compliance_types:
        description:
          - The resource types of only those AWS resources that you want to trigger an evaluation for the rule.
            You can only specify one type if you also specify a resource ID for I(compliance_id).
      compliance_id:
        description:
          - The ID of the only AWS resource that you want to trigger an evaluation for the rule. If you specify a resource ID,
            you must specify one resource type for I(compliance_types).
      tag_key:
        description:
          - The tag key that is applied to only those AWS resources that you want to trigger an evaluation for the rule.
      tag_value:
        description:
          - The tag value applied to only those AWS resources that you want to trigger an evaluation for the rule.
            If you specify a value for I(tag_value), you must also specify a value for I(tag_key).
    type: dict
  source:
    description:
      - Provides the rule owner (AWS or customer), the rule identifier, and the notifications that cause the function to
        evaluate your AWS resources.
    suboptions:
      owner:
        description:
          - The resource types of only those AWS resources that you want to trigger an evaluation for the rule.
            You can only specify one type if you also specify a resource ID for I(compliance_id).
      identifier:
        description:
          - The ID of the only AWS resource that you want to trigger an evaluation for the rule.
            If you specify a resource ID, you must specify one resource type for I(compliance_types).
      details:
        description:
          - Provides the source and type of the event that causes AWS Config to evaluate your AWS resources.
          - This parameter expects a list of dictionaries.  Each dictionary expects the following key/value pairs.
          - Key C(EventSource) The source of the event, such as an AWS service, that triggers AWS Config to evaluate your AWS resources.
          - Key C(MessageType) The type of notification that triggers AWS Config to run an evaluation for a rule.
          - Key C(MaximumExecutionFrequency) The frequency at which you want AWS Config to run evaluations for a custom rule with a periodic trigger.
    type: dict
    required: true
  input_parameters:
    description:
      - A string, in JSON format, that is passed to the AWS Config rule Lambda function.
    type: str
  execution_frequency:
    description:
      - The maximum frequency with which AWS Config runs evaluations for a rule.
    choices: ['One_Hour', 'Three_Hours', 'Six_Hours', 'Twelve_Hours', 'TwentyFour_Hours']
    type: str
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
a]  
- name: Create Config Rule for AWS Config
  community.aws.config_rule:
    name: test_config_rule
    state: present
    description: 'This AWS Config rule checks for public write access on S3 buckets'
    scope:
      compliance_types:
        - 'AWS::S3::Bucket'
    source:
      owner: AWS
      identifier: 'S3_BUCKET_PUBLIC_WRITE_PROHIBITED'
#    N)camel_dict_to_snake_dict)is_boto3_error_code)AWSRetry)AnsibleCommunityAWSModulec                 
   	 | j                  |d   gd      }|d   d   S # t        d      $ r Y y t        j                  j                  t        j                  j
                  f$ r}|j                  |       Y d }~y d }~ww xY w)NConfigRuleNameTConfigRuleNames	aws_retryConfigRulesr   NoSuchConfigRuleException)describe_config_rulesr   botocore
exceptionsClientErrorBotoCoreErrorfail_json_aws)clientmoduleparamsrulees        m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/config_rule.pyrule_existsr   y   s     ++#$456 , 
 M"1%%:; ''))   	Q	 s   ! B6B'A==Bc                     	 | j                  |       d|d<   |S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~y d }~ww xY w)N
ConfigRuleTchangedCouldn't create AWS Config rulemsg)put_config_ruler   r   r   r   r   r   r   r   resultr   s        r   create_resourcer&      sn    G&1 y++X-@-@-N-NO GQ$EFFGs    7A/A**A/c                    | j                  |d   gd      }|d   d   d= |d   d   d= |d   d   d= ||d   d   k7  r3	 | j                  |	       d|d
<   t        t        | ||            |d<   |S y # t        j
                  j                  t        j
                  j                  f$ r}|j                  |d       Y d }~y d }~ww xY w)Nr	   Tr
   r   r   ConfigRuleArnConfigRuleIdEvaluationModesr   r   r   r    r!   )	r   r#   r   r   r   r   r   r   r   )r   r   r   r%   current_paramsr   s         r   update_resourcer,      s    11 012 2 N
 	}%a(9}%a(8}%a():;.q11	K""f"5 $F95k&&RX6YZF6NM 2 ##//1D1D1R1RS 	K  (I JJ	Ks   1A2 27C)CCc                     	 | j                  |d   d       d|d<   i |d<   |S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~y d }~ww xY w)Nr	   T)r	   r   r   r   zCouldn't delete AWS Config ruler!   )delete_config_ruler   r   r   r   r   r$   s        r   delete_resourcer/      s    	G!!!"23 	" 	
 !yv++X-@-@-N-NO GQ$EFFGs   !$ 7A8A33A8c                  	   t        t        dd      t        dddgd      t        d      t        d      t        dd      t        d      t        dg d	
      dd      } ddi}| j                  j                  d      }| j                  j                  d      }i }|r||d<   | j                  j                  d      r| j                  j                  d      |d<   | j                  j                  d      ri |d<   | j                  j                  d      j                  d      r>|d   j	                  d| j                  j                  d      j                  d      i       | j                  j                  d      j                  d      r>|d   j	                  d| j                  j                  d      j                  d      i       | j                  j                  d      j                  d      r>|d   j	                  d| j                  j                  d      j                  d      i       | j                  j                  d      j                  d      r>|d   j	                  d| j                  j                  d      j                  d      i       | j                  j                  d      r=i |d<   | j                  j                  d      j                  d       r>|d   j	                  d!| j                  j                  d      j                  d       i       | j                  j                  d      j                  d"      r>|d   j	                  d#| j                  j                  d      j                  d"      i       | j                  j                  d      j                  d$      r>|d   j	                  d%| j                  j                  d      j                  d$      i       | j                  j                  d&      r| j                  j                  d&      |d'<   | j                  j                  d(      r| j                  j                  d(      |d)<   d*|d+<   | j                  d,t        j                         -      }t        || |      }|dk(  r|st        || ||       nt        || ||       |dk(  r|rt        || ||        | j                  d.i | y )/NstrT)typerequiredpresentabsent)r2   choicesdefault)r2   dict)One_HourThree_Hours	Six_HoursTwelve_HoursTwentyFour_Hours)r2   r6   )namestatedescriptionscopesourceinput_parametersexecution_frequencyF)argument_specsupports_check_moder   r>   r?   r	   r@   DescriptionrA   Scopecompliance_typesComplianceResourceTypestag_keyTagKey	tag_valueTagValuecompliance_idComplianceResourceIdrB   SourceownerOwner
identifierSourceIdentifierdetailsSourceDetailsrC   InputParametersrD   MaximumExecutionFrequencyACTIVEConfigRuleStateconfig)retry_decorator )AnsibleAWSModuler8   r   getupdater   r   jittered_backoffr   r&   r,   r/   	exit_json)r   r%   r>   r?   r   r   existing_rules          r   mainre      sG   ed3uy(.CYWU+v&6 $% 0#'	$
$ "'F, F==V$DMMg&EF#' }}' & 1 1- @}}}!w==W%))*<=7O""-v}}/@/@/I/M/MN`/a
 ==W%)))47O""fmm//8<<YG
 ==W%))+67O"" 1 1' : > >{ K
 ==W%))/:7O""*FMM,=,=g,F,J,J?,[
 }}"x==X&**738##V]]..x8<<WE
 ==X&**<88##&(9(9((C(G(G(U
 ==X&**958###V]]%6%6x%@%D%DY%O
 }}+,$*MM$5$56H$I !}}./.4mm.?.?@U.V*+ (F]]8X5N5N5P]QF7M	FFFF;FFFF;FFFF;Fv    __main__)DOCUMENTATIONEXAMPLESRETURNr   ImportError0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   >ansible_collections.community.aws.plugins.module_utils.modulesr   r_   r   r&   r,   r/   re   __name__r^   rf   r   <module>rq      sz   Rh 
	 V \ P x  GK(
GcL zF W  		s   A A	A	