
    VhiK                         d Z dZdZddlmZ 	 ddlZddlmZ ddl	m
Z  G d d	      Zd
 Zedk(  r e        yy# e$ r Y .w xY w)a@  
---
module: elasticache
version_added: 1.0.0
short_description: Manage cache clusters in Amazon ElastiCache
description:
  - Manage cache clusters in Amazon ElastiCache.
  - Returns information about the specified cache cluster.
author:
  - "Jim Dalton (@jsdalton)"
options:
  state:
    description:
      - C(absent) or C(present) are idempotent actions that will create or destroy a cache cluster as needed.
      - C(rebooted) will reboot the cluster, resulting in a momentary outage.
    choices: ['present', 'absent', 'rebooted']
    required: true
    type: str
  name:
    description:
      - The cache cluster identifier.
    required: true
    type: str
  engine:
    description:
      - Name of the cache engine to be used.
      - Supported values are C(redis) and C(memcached).
    default: memcached
    type: str
  cache_engine_version:
    description:
      - The version number of the cache engine.
    type: str
    default: ''
  node_type:
    description:
      - The compute and memory capacity of the nodes in the cache cluster.
    default: cache.t2.small
    type: str
  num_nodes:
    description:
      - The initial number of cache nodes that the cache cluster will have.
      - Required when I(state=present).
    type: int
    default: 1
  cache_port:
    description:
      - The port number on which each of the cache nodes will accept
        connections.
    type: int
  cache_parameter_group:
    description:
      - The name of the cache parameter group to associate with this cache cluster. If this argument is omitted, the default cache parameter group
        for the specified engine will be used.
    aliases: [ 'parameter_group' ]
    type: str
    default: ''
  cache_subnet_group:
    description:
      - The subnet group name to associate with. Only use if inside a VPC.
      - Required if inside a VPC.
    type: str
    default: ''
  security_group_ids:
    description:
      - A list of VPC security group IDs to associate with this cache cluster. Only use if inside a VPC.
    type: list
    elements: str
    default: []
  cache_security_groups:
    description:
      - A list of cache security group names to associate with this cache cluster.
      - Don't use if your Cache is inside a VPC. In that case use I(security_group_ids) instead!
    type: list
    elements: str
    default: []
  zone:
    description:
      - The EC2 Availability Zone in which the cache cluster will be created.
    type: str
  wait:
    description:
      - Wait for cache cluster result before returning.
    type: bool
    default: true
  hard_modify:
    description:
      - Whether to destroy and recreate an existing cache cluster if necessary in order to modify its state.
      - Defaults to C(false).
    type: bool
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
z # a_  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: Basic example
  community.aws.elasticache:
    name: "test-please-delete"
    state: present
    engine: memcached
    cache_engine_version: 1.4.14
    node_type: cache.m3.small
    num_nodes: 1
    cache_port: 11211
    cache_security_groups:
      - default
    zone: us-east-1d


- name: Ensure cache cluster is gone
  community.aws.elasticache:
    name: "test-please-delete"
    state: absent

- name: Reboot cache cluster
  community.aws.elasticache:
    name: "test-please-delete"
    state: rebooted
    )sleepN)is_boto3_error_code)AnsibleCommunityAWSModulec                       e Zd ZdZg dZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zy)ElastiCacheManagerz,Handles elasticache creation and destruction)	availablecreating	rebooting	modifyingc                 X   || _         || _        |j                         | _        || _        || _        || _        || _        || _        |	| _	        |
| _
        || _        || _        || _        || _        d| _        d | _        d| _        | j%                         | _        | j)                          y )NFgone)modulenamelowerenginecache_engine_version	node_type	num_nodes
cache_portcache_parameter_groupcache_subnet_groupcache_security_groupssecurity_group_idszonewaithard_modifychangeddatastatus_get_elasticache_connectionconn_refresh_data)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   s                  m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/elasticache.py__init__zElastiCacheManager.__init__   s    " 	lln$8!""$%:""4%:""4		&	446	    c                 f    | j                         r| j                          y| j                          y)z/Ensure cache cluster exists or create it if notN)existssynccreater#   s    r$   ensure_presentz!ElastiCacheManager.ensure_present   s    ;;=IIKKKMr&   c                 $    | j                          yz0Ensure cache cluster is gone or delete it if notN)deleter+   s    r$   ensure_absentz ElastiCacheManager.ensure_absent       r&   c                 $    | j                          yr.   )rebootr+   s    r$   ensure_rebootedz"ElastiCacheManager.ensure_rebooted   r1   r&   c                 2    | j                   | j                  v S )zCheck if cache cluster exists)r   EXIST_STATUSESr+   s    r$   r(   zElastiCacheManager.exists   s    {{d1111r&   c                    | j                   dk(  ry| j                   dv r| j                  r| j                  d       y| j                   dk(  rH| j                  r| j                  d       n*| j                  j	                  d| j
                   d       t        | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  		      }| j                  | j                  |d
<   | j                   | j                   |d<   	  | j"                  j$                  di | | j1                          d| _        | j                  r| j                  d       y# t&        j(                  j*                  t&        j(                  j,                  f$ r'}| j                  j/                  |d       Y d}~d}~ww xY w)zCreate an ElastiCache clusterr   Nr	   r
   r   deletingr   'z'' is currently deleting. Cannot create.msg)	CacheClusterIdNumCacheNodesCacheNodeTypeEngineEngineVersionCacheSecurityGroupNamesSecurityGroupIdsCacheParameterGroupNameCacheSubnetGroupNamePortPreferredAvailabilityZonezFailed to create cache clusterT )r   r   _wait_for_statusr   	fail_jsonr   dictr   r   r   r   r   r   r   r   r   r   r!   create_cache_clusterbotocore
exceptionsClientErrorBotoCoreErrorfail_json_awsr"   r   )r#   kwargses      r$   r*   zElastiCacheManager.create   s   ;;+%;;@@yy%%k2;;*$yy%%f-%%Adii[8_*`%a99....;;33$($>$>!44$($>$>!%!8!8

 ??&!__F6N99 26))F./	O*DII**4V4
 	99!!+. ##//1D1D1R1RS 	OKK%%a-M%NN	Os   9F
 
7G(G##G(c                    | j                   dk(  ry| j                   dk(  r| j                  r| j                  d       y| j                   dv rU| j                  r| j                  d       n7| j                  j	                  d| j
                   d| j                    d	       	 | j                  j                  | j
                  
      }d   }| j                  |       d| _        | j                  r| j                  d       yy# t        j                  j                  t        j                  j                  f$ r'}| j                  j                  |d	       Y d}~d}~ww xY w)zDestroy an ElastiCache clusterr   Nr9   r8   r   r:   ' is currently z. Cannot delete.r;   )r=   zFailed to delete cache clusterCacheClusterT)r   r   rI   r   rJ   r   r!   delete_cache_clusterrM   rN   rO   rP   rQ   r"   r   )r#   responserS   cache_cluster_datas       r$   r/   zElastiCacheManager.delete   s.   ;;& ;;*$yy%%f-;;@@yy%%k2%%Adii[}Td*e%f	Oyy55TYY5OH &n5-.99!!&)  ##//1D1D1R1RS 	OKK%%a-M%NN	Os   "&D 7E";EE"c                 h   | j                         s7| j                  j                  d| j                   d| j                   d       | j                  dv r| j
                  r| j                  d       ny| j                         r| j                  s*| j                  j                  d| j                   d       | j
                  s*| j                  j                  d| j                   d	       | j                          | j                          y| j                         r| j                          yy)
z$Sync settings to cluster if requiredr:   ' is z. Cannot sync.r;   r8   r   NzR' requires destructive modification. 'hard_modify' must be set to true to proceed.zK' requires destructive modification. 'wait' must be set to true to proceed.)r(   r   rJ   r   r   r   rI   _requires_destroy_and_creater   r/   r*   _requires_modificationmodifyr+   s    r$   r)   zElastiCacheManager.sync  s    {{}KK!!$))E$++n&U!V;;@@yy%%k2 ,,.##%%DII;&xy &  99%%DII;&qr &  KKMKKM&&(KKM )r&   c           
         | j                         }	 | j                  j                  | j                  | j                  || j
                  | j                  | j                  d| j                         | j                          d| _        | j                   r| j#                  d       yy# t        j                  j                  $ r'}| j                  j                  |d       Y d}~ud}~ww xY w)zQModify the cache cluster. Note it's only possible to modify a few select options.T)r=   r>   CacheNodeIdsToRemoverB   rD   rC   ApplyImmediatelyrA   zFailed to modify cache clusterr;   Nr   )_get_nodes_to_remover!   modify_cache_clusterr   r   r   r   r   r   rM   rN   rO   r   rQ   r"   r   r   rI   )r#   nodes_to_removerS   s      r$   r^   zElastiCacheManager.modify1  s    335	OII**#yy"nn%4(,(B(B(,(B(B!%!8!8!%"77 + 	 	99!!+.  "".. 	OKK%%a-M%NN	Os   AB' 'C+C&&C+c                    | j                         s7| j                  j                  d| j                   d| j                   d       | j                  dk(  ry| j                  dv rU| j
                  r| j                  d       n7| j                  j                  d| j                   d	| j                   d       | j                  d
   D cg c]  }|d   	 }}	 | j                  j                  | j                  |       | j                          d| _        | j
                  r| j                  d       yyc c}w # t        j                  j                  $ r'}| j                  j                  |d       Y d}~zd}~ww xY w)zReboot the cache clusterr:   r[   z. Cannot reboot.r;   r
   N)r	   r   r   rU   
CacheNodesCacheNodeId)r=   CacheNodeIdsToRebootzFailed to reboot cache clusterT)r(   r   rJ   r   r   r   rI   r   r!   reboot_cache_clusterrM   rN   rO   rQ   r"   r   )r#   cncache_node_idsrS   s       r$   r3   zElastiCacheManager.rebootH  sK   {{}KK!!$))E$++FV&W!X;;+%;;33yy%%k2%%Adii[}Td*e%f 7;ii6MN"]+NN	OII**$))Zh*i 	99!!+.  O "".. 	OKK%%a-M%NN	Os   D8'D= =FE<<Fc                 n    | j                   | j                  d}| j                  r| j                  |d<   |S )z)Return basic info about the cache cluster)r   r   r   )r   r   r   )r#   infos     r$   get_infozElastiCacheManager.get_infoa  s-    		T[[99999DLr&   c                 ~   ddddd}| j                   |k(  ry|| j                      |k7  r-| j                  j                  d| j                    d| d       |t        |j	                               vr | j                  j                  d| d	       	 t        d
       | j                          | j                   |k(  ry,)z?Wait for status to change from present status to awaited_statusr   r   )r	   r
   r   r9   NzInvalid awaited status. 'z' cannot transition to 'r:   r;   z ' is not a valid awaited status.   )r   r   rJ   setvaluesr   r"   )r#   awaited_status
status_maps      r$   rI   z#ElastiCacheManager._wait_for_statush  s    "-KVaouv
;;.(dkk"n4KK!!/}<TUcTddef "  Z%6%6%8!99KK!!.)99Y&Z![!H {{n,	 r&   c                     | j                   | j                  d}|j                         D ]  \  }}|	|s| j                  |   |k7  s y g }| j                  d   D ]  }|j	                  |d           t        |      t        | j                        k7  ry| j                  r[g }| j                  j                  dg       }|D ]  }|j	                  |d           t        |      t        | j                        k7  ryy)z7Check if cluster requires (nondestructive) modification)r>   rA   TCacheSecurityGroupsCacheSecurityGroupNameSecurityGroupsSecurityGroupIdF)	r   r   itemsr   appendrq   r   r   get)r#   modifiable_datakeyvaluer   sgvpc_security_groupssecurity_groupss           r$   r]   z)ElastiCacheManager._requires_modification|  s    -1NNTMfMfg)//1 	JC Utyy~/F	
 !#))12 	GB!((,D)EF	G$%T-G-G)HH """$"iimm,<bAO% B#**2.?+@AB&'3t/F/F+GGr&   c                    | j                   d   | j                   d   | j                         d}| j                  | j                   d   |d<   |j                         D ]#  \  }}t	        | |      t	        | |      |k7  s# y y)zX
        Check whether a destroy and create is required to synchronize cluster.
        r?   r@   )r   r   r   rG   r   TF)r   	_get_portr   rz   getattr)r#   unmodifiable_datar~   r   s       r$   r\   z/ElastiCacheManager._requires_destroy_and_create  s    
 ?3ii)..*
 99 (,		2M(Nf%+113 	JCtS!-'$2D2M	 r&   c                     	 | j                   j                  d      S # t        j                  j                  t        j                  j
                  f$ r'}| j                   j                  |d       Y d}~yd}~ww xY w)zGet an elasticache connectionelasticachezFailed to connect to AWSr;   N)r   clientrM   rN   rO   rP   rQ   )r#   rS   s     r$   r    z.ElastiCacheManager._get_elasticache_connection  sg    	I;;%%m44##//1D1D1R1RS 	IKK%%a-G%HH	Is    7A;A66A;c                     | j                   d   dk(  r| j                   d   d   S | j                   d   dk(  r| j                   d   d   d   d   S y	)
zKGet the port. Where this information is retrieved from is engine dependent.r@   	memcachedConfigurationEndpointrF   redisrf   r   EndpointN)r   r+   s    r$   r   zElastiCacheManager._get_port  s^    99X+-9945f==YYx G+ 99\*1-j9&AA ,r&   Nc                    |0	 | j                   j                  | j                  d      }d   d	   }|| _        | j                  d
   | _        | j
                  dk(  rd| _        yy# t        d      $ r d| _        d| _        Y yt        j                  j                  $ r'}| j                  j                  |d       Y d}~d}~ww xY w)z%Refresh data about this cache clusterNT)r=   ShowCacheNodeInfoCacheClusterNotFoundr   z!Failed to describe cache clustersr;   CacheClustersr   CacheClusterStatuszrebooting cache cluster nodesr
   )r!   describe_cache_clustersr   r   r   r   rM   rN   rO   r   rQ   )r#   rY   rX   rS   s       r$   r"   z ElastiCacheManager._refresh_data  s     %V99<<DIIim<n "*/!:1!=&	ii 45
 ;;99%DK : ''=>  	$&&22 V))!1T)UUVs   'A& &CC CCc                    | j                   d   | j                  z
  }|dk  rg S | j                  s*| j                  j	                  d| j
                   d       | j                   d   D cg c]  }|d   	 }}|| d S c c}w )	zEIf there are nodes to remove, it figures out which need to be removedr>   r   r:   zP' requires removal of cache nodes. 'hard_modify' must be set to true to proceed.r;   rf   rg   N)r   r   r   r   rJ   r   )r#   num_nodes_to_removerj   rk   s       r$   rb   z'ElastiCacheManager._get_nodes_to_remove  s    "ii84>>I!#IKK!!		{"rs "  7;ii6MN"]+NN22344 Os   +A?)N)__name__
__module____qualname____doc__r6   r%   r,   r0   r4   r(   r*   r/   r)   r^   r3   rn   rI   r]   r\   r    r   r"   rb   rH   r&   r$   r   r      sh    6HN$L2)V*4:/./2(4"IB&,5r&   r   c                     t        t        dg d      t        d      t        d      t        d      t        d      t        d	d
      t        ddg      t        d
      t        d      t        g dd      t        g dd      t               t        dd      t        d            } t        |       }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|j                  d   }|j                  d    }|j                  d!   }|j                  d"   }|	r|
r|j                  d#$       |d%k(  r|s|j                  d&$       t	        |||||||||	|
||||      }|d%k(  r|j                          n+|d'k(  r|j                          n|d(k(  r|j                          t        |j                  |j                         )      } |j                  d+i | y*),zelasticache ansible moduleT)presentabsentrebooted)requiredchoices)r   r   )default zcache.t2.smallrp   int)r   typeparameter_group)r   aliases)r   liststr)r   r   elementsbool)stater   r   r   r   r   r   r   r   r   r   r   r   r   )argument_specr   r   r   r   r   r   r   r   r   r   r   r   r   r   z?Can't specify both cache_subnet_group and cache_security_groupsr;   r   zA'num_nodes' is a required parameter. Please specify num_nodes > 0r   r   )r   r   NrH   )rK   AnsibleAWSModuleparamsrJ   r   r,   r0   r4   r   rn   	exit_json)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   elasticache_managerfacts_results                     r$   mainr     sE   D*KL4 K(!"-/0qu-"28I7JKU#+"2FUK%HV$V,f%M$ #F == DMM'"E]]8$F!==)?@k*Ik*I|,J';<"MM*AB';<== D== D--.K"MM*AB3^_	)`a," 	**,	(	))+	*	++- 3 ; ;I\IeIeIghLF$|$r&   __main__)DOCUMENTATIONRETURNEXAMPLEStimer   rM   ImportError<ansible_collections.amazon.aws.plugins.module_utils.botocorer   >ansible_collections.community.aws.plugins.module_utils.modulesr   r   r   r   r   rH   r&   r$   <module>r      so   ^@ 
8 	 ] xH5 H5V
G%T zF {  		s   9 A A