
    Vh~                     n    d Z dZdZ	 ddlZddlm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: dynamodb_ttl
version_added: 1.0.0
short_description: Set TTL for a given DynamoDB table
description:
- Sets the TTL for a given DynamoDB table.
options:
  state:
    description:
    - State to set DynamoDB table to.
    choices: ['enable', 'disable']
    required: false
    type: str
  table_name:
    description:
    - Name of the DynamoDB table to work on.
    required: true
    type: str
  attribute_name:
    description:
    - The name of the Time To Live attribute used to store the expiration time for items in the table.
    - This appears to be required by the API even when disabling TTL.
    required: true
    type: str

author:
- Ted Timmons (@tedder)
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
- amazon.aws.boto3
a0  
- name: enable TTL on my cowfacts table
  community.aws.dynamodb_ttl:
    state: enable
    table_name: cowfacts
    attribute_name: cow_deleted_date

- name: disable TTL on my cowfacts table
  community.aws.dynamodb_ttl:
    state: disable
    table_name: cowfacts
    attribute_name: cow_deleted_date
z
current_status:
  description: current or new TTL specification.
  type: dict
  returned: always
  sample:
  - { "AttributeName": "deploy_timestamp", "TimeToLiveStatus": "ENABLED" }
  - { "AttributeName": "deploy_timestamp", "Enabled": true }
    N)AnsibleCommunityAWSModulec                 H    | j                  |      }|j                  d      S )z!Fetch the state dict for a table.)	TableNameTimeToLiveDescription)describe_time_to_liveget)c
table_namecurrent_states      n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/dynamodb_ttl.pyget_current_ttl_stater   I   s'    ++j+AM455    c                     |sy|j                         dk(  r|j                  d      dvry|j                         dk(  r|j                  d      dvry| |j                  d      k7  ryy)	zMRun checks to see if the table needs to be modified. Basically a dirty check.TenableTimeToLiveStatus)ENABLINGENABLEDdisable)	DISABLINGDISABLEDAttributeNameF)lowerr   )attribute_namedesired_statecurrent_specs      r   does_state_need_changingr   O   sq    (\-=-=>P-QYp-p	)l.>.>?Q.RZs.s))/::r   c                 ~    d}|j                         dk(  rd}| j                  |||d      }|j                  d      S )z{Set our specification. Returns the update_time_to_live specification dict,
    which is different than the describe_* call.Fr   T)Enabledr   )r   TimeToLiveSpecificationr   )r   update_time_to_liver   )r	   r
   stater   
is_enabledrets         r   set_ttl_stater$   _   sQ     J{{} 



!+!
   C 77,--r   c                     t        t        ddg      t        d      t        d            } t        |       }	 |j                  d      }ddi}	 t        |j                  d         }t        |j                  d   |j                  d   |      r@t        ||j                  d   |j                  d   |j                  d         }||d<   d|d<   n||d<    |j                  di | y # t        j                  j
                  t        j                  j                  f$ r}|j                  |d	
       Y d }~d }~ww xY w# t        j                  j
                  $ r}|j                  |d
       Y d }~d }~wt        j                  j                  $ r}|j                  |d
       Y d }~d }~wt        $ r}|j                  |d
       Y d }~d }~ww xY w)Nr   r   )choicesT)required)r!   r
   r   )argument_specdynamodbzFailed to connect to AWS)msgchangedFr
   r   r!   current_statusz!Failed to get or update ttl statez Failed due to invalid parametersFailed )dictAnsibleAWSModuleclientbotocore
exceptionsClientErrorBotoCoreErrorfail_json_awsr   paramsr   r$   ParamValidationError
ValueError	exit_json)r(   moduledbclienteresultr   	new_states          r   mainr@   q   s   Hi01&T*M
 #F@==, F.-hl8ST#FMM2B$CV]]SZE[]jk%&--5v}}W7Mv}}]mOnI (1F#$ $F9 (5F#$ Fv9 ++X-@-@-N-NO @Q$>??@* ** IQ$GHH33 HQ$FGG .QH--.sH   C* BE *7D>!D99D>GE66 GF..G:GG__main__)DOCUMENTATIONEXAMPLESRETURNr2   ImportError>ansible_collections.community.aws.plugins.module_utils.modulesr   r0   r   r   r$   r@   __name__r.   r   r   <module>rH      sg    D
	 y6 .$(V zF s  		s   , 44