
    Vh+3                         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
 ddlmZ dd	lmZ dd
lmZ  G d de      Zd ZddZd Zd Zd Zd Zedk(  r e        yy# e$ r Y Qw xY w)a  
---
module: efs_info
version_added: 1.0.0
short_description: Get information about Amazon EFS file systems
description:
- This module can be used to search Amazon EFS file systems.
  Note that the M(community.aws.efs_info) module no longer returns C(ansible_facts)!
author:
- "Ryan Sydnor (@ryansydnor)"
options:
    name:
      description:
      - Creation Token of Amazon EFS file system.
      aliases: [ creation_token ]
      type: str
    id:
      description:
      - ID of Amazon EFS.
      type: str
    tags:
      description:
      - List of tags of Amazon EFS. Should be defined as dictionary.
      type: dict
      default: {}
    targets:
      description:
      - List of targets on which to filter the returned results.
      - Result must match all of the specified targets, each of which can be a security group ID, a subnet ID or an IP address.
      type: list
      elements: str
      default: []
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
- amazon.aws.boto3
a  
- name: Find all existing efs
  community.aws.efs_info:
  register: result

- name: Find efs using id
  community.aws.efs_info:
    id: fs-1234abcd
  register: result

- name: Searching all EFS instances with tag Name = 'myTestNameTag', in subnet 'subnet-1a2b3c4d' and with security group 'sg-4d3c2b1a'
  community.aws.efs_info:
    tags:
      Name: myTestNameTag
    targets:
      - subnet-1a2b3c4d
      - sg-4d3c2b1a
  register: result

- ansible.builtin.debug:
    msg: "{{ result['efs'] }}"
a
  
creation_time:
    description: timestamp of creation date
    returned: always
    type: str
    sample: "2015-11-16 07:30:57-05:00"
creation_token:
    description: EFS creation token
    returned: always
    type: str
    sample: console-88609e04-9a0e-4a2e-912c-feaa99509961
file_system_id:
    description: ID of the file system
    returned: always
    type: str
    sample: fs-xxxxxxxx
life_cycle_state:
    description: state of the EFS file system
    returned: always
    type: str
    sample: creating, available, deleting, deleted
mount_point:
    description: url of file system with leading dot from the time AWS EFS required to add network suffix to EFS address
    returned: always
    type: str
    sample: .fs-xxxxxxxx.efs.us-west-2.amazonaws.com:/
filesystem_address:
    description: url of file system
    returned: always
    type: str
    sample: fs-xxxxxxxx.efs.us-west-2.amazonaws.com:/
mount_targets:
    description: list of mount targets
    returned: always
    type: list
    sample:
        [
            {
                "file_system_id": "fs-a7ad440e",
                "ip_address": "172.31.17.173",
                "life_cycle_state": "available",
                "mount_target_id": "fsmt-d8907871",
                "network_interface_id": "eni-6e387e26",
                "owner_id": "123456789012",
                "security_groups": [
                    "sg-a30b22c6"
                ],
                "subnet_id": "subnet-e265c895"
            },
            ...
        ]
name:
    description: name of the file system
    returned: always
    type: str
    sample: my-efs
number_of_mount_targets:
    description: the number of targets mounted
    returned: always
    type: int
    sample: 3
owner_id:
    description: AWS account ID of EFS owner
    returned: always
    type: str
    sample: XXXXXXXXXXXX
size_in_bytes:
    description: size of the file system in bytes as of a timestamp
    returned: always
    type: dict
    sample:
        {
            "timestamp": "2015-12-21 13:59:59-05:00",
            "value": 12288
        }
performance_mode:
    description: performance mode of the file system
    returned: always
    type: str
    sample: "generalPurpose"
throughput_mode:
    description: mode of throughput for the file system
    returned: always
    type: str
    sample: "bursting"
provisioned_throughput_in_mibps:
    description: throughput provisioned in Mibps
    returned: when throughput_mode is set to "provisioned"
    type: float
    sample: 15.0
tags:
    description: tags on the efs instance
    returned: always
    type: dict
    sample:
        {
            "name": "my-efs",
            "key": "Value"
        }
    )defaultdictN)	to_native)camel_dict_to_snake_dict)AWSRetry)boto3_tag_list_to_ansible_dict)AnsibleCommunityAWSModulec                      e Zd ZdZdZdZdZd Z ej                  dg      d        Z
 ej                  dg      d	        Z ej                  dg      d
        Z ej                  dg      d        Zd Zd ZddZy)EFSConnectioncreating	availabledeletingdeletedc                     	 |j                  d      | _        || _        |j                  | _        y # t        $ r(}|j	                  dt        |              Y d }~>d }~ww xY w)NefszFailed to connect to AWS: msg)client
connectionmodule	Exception	fail_jsonr   region)selfr   es      j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/efs_info.py__init__zEFSConnection.__init__   s`    	N$mmE2DO DK mm  	N#=il^!LMM	Ns   1 	A"AA"ThrottlingException)catch_extra_error_codesc                 ~    | j                   j                  d      } |j                  di |j                         d   S )zR
        Returns generator of file systems including all attributes of FS
        describe_file_systemsFileSystems r   get_paginatorpaginatebuild_full_result)r   kwargs	paginators      r   list_file_systemszEFSConnection.list_file_systems   s=    
 OO112IJ	!y!!+F+==?NN    c                     | j                   j                  d      }t        |j                  |      j	                         d         S )z?
        Returns tag list for selected instance of EFS
        describe_tagsFileSystemIdTags)r   r$   r   r%   r&   r   file_system_idr(   s      r   get_tagszEFSConnection.get_tags   sE    
 OO11/B	-N;MMOPVW
 	
r*   c                 ~    | j                   j                  d      }|j                  |      j                         d   S )zD
        Returns mount targets for selected instance of EFS
        describe_mount_targetsr-   MountTargetsr#   r0   s      r   get_mount_targetszEFSConnection.get_mount_targets   s<    
 OO112JK	!!~!>PPRSabbr*   c                 @    | j                   j                  |      d   S )zF
        Returns security groups for selected instance of EFS
        )MountTargetIdSecurityGroups)r   %describe_mount_target_security_groups)r   mount_target_ids     r   get_security_groupsz!EFSConnection.get_security_groups   s#    
 DDSbDcdtuur*   c                 n   |D ]N  }|d   | j                   k(  s	 | j                  |d         }D ]  }|d   j                  t        |             ! P |S # t        j                  j                  t        j                  j
                  f$ r'}| j                  j                  |d       Y d }~d }~ww xY w)Nlife_cycle_stater1   zCouldn't get EFS targetsr   mount_targets)
STATE_AVAILABLEr6   botocore
exceptionsClientErrorBotoCoreErrorr   fail_json_awsappendr   )r   file_systemsitemr?   r   mts         r   get_mount_targets_dataz$EFSConnection.get_mount_targets_data   s      	OD&'4+?+??Q$($:$:4@P;Q$RM ( OB)001I"1MNO	O 	 !++779L9L9Z9Z[ QKK--a5O-PPQs   A7B4B//B4c                    |D ]Y  }|d   | j                   k(  r;|d   D ]2  }|d   | j                   k(  r	 | j                  |d         |d<   .g |d<   4 Pi |d<   g |d<   [ |S # t        j                  j                  t        j                  j
                  f$ r'}| j                  j                  |d       Y d }~d }~ww xY w)Nr>   r?   r;   security_groupsz Couldn't get EFS security groupsr   tags)r@   r<   rA   rB   rC   rD   r   rE   )r   rG   rH   targetr   s        r   get_security_groups_dataz&EFSConnection.get_security_groups_data   s      	+D&'4+?+??"?3 7F01T5I5IIa8<8P8PQWXiQj8kF#45 57017  "V(*_%	+  !) 3 3 ? ?ATATAbAbc a KK55a=_5``as   A!!7B?B::B?Nc                 d   t               }|r||d<   |r||d<   	  | j                  di |}t               }D ]  }t        |d         |d<   d|d    d| j                   d|d	<   |d    d| j                   d|d
<   d|d   v rt        |d   d         |d   d<   t        |      }i |d<   g |d<   |d   | j                  k(  r	 | j                  |d         |d<   |j                  |        |S # t        j                  j                  t        j                  j
                  f$ r(}| j                  j                  |d       Y d }~d }~ww xY w# t        j                  j                  t        j                  j
                  f$ r'}| j                  j                  |d       Y d }~d }~ww xY w)Nr.   CreationTokenzCouldn't get EFS file systemsr   CreationTime.z.efs.z.amazonaws.com:/
MountPointFilesystemAddress	TimestampSizeInBytesrM   r?   r>   r1   zCouldn't get EFS tagsr"   )dictr)   rA   rB   rC   rD   r   rE   liststrr   r   r@   r2   rF   )	r   r1   creation_tokenr'   rG   r   resultsrH   results	            r   get_file_systemszEFSConnection.get_file_systems  s   %3F>"&4F?#	N1411;F;L &  	#D#&tN';#<D  $%T.%9$:%}L\!]D+/+?*@dkk]Rb(cD$%d=1136tM7J;7W3X]#K0-d3FF6N&(F?#()T-A-AAN%)]]6:J3K%LF6N NN6"/	#0 9 ##//1D1D1R1RS 	NKK%%a-L%MM	N2 !++779L9L9Z9Z[ NKK--a5L-MMNs/   C/ E/7E&E		E7F/F**F/)NN)__name__
__module____qualname__STATE_CREATINGr@   STATE_DELETINGSTATE_DELETEDr   r   exponential_backoffr)   r2   r6   jittered_backoffr<   rJ   rO   r^   r"   r*   r   r
   r
      s    N!ONM$ "X!!;P:QRO SO "X!!;P:QR
 S
 "X!!;P:QRc Sc X8M7NOv Pv	 $r*   r
   c           	          ddddd}t        |j                         D cg c]"  \  }}t        |       j                  |      s!|$ c}}d      S c c}}w )zF
    Helper method to convert ID prefix to mount target attribute
    r;   	subnet_idnetwork_interface_idrL   )zfsmt-zsubnet-zeni-zsg-
ip_address)first_or_defaultitemsrZ   
startswith)attr_idattr_by_prefixprefix	attr_names       r   prefix_to_attrrr   ,  sZ    
 #& 	N .<.B.B.Dh*	GH_H_`fHghjv hs   "A
A
c                     | D ]  }|c S  |S )zB
    Helper method to fetch first element of list (if exists)
    r"   )rl   defaultrH   s      r   rk   rk   ;  s      Nr*   c                 P    |j                         D ]  \  }}|| vs	|| |   k7  s y y)zD
    Helper method to determine if tag requested already exists
    FT)rl   )r   requiredkeyvalues       r   has_tagsry   D  s9     nn& 
Ui5IcN#: r*   c                 H    t        |       }|D ]  \  }}||vs|||   vs y y)zM
    Helper method to determine if mount target requested already exists
    FT)group_list_of_dict)r   rv   groupedrx   fields        r   has_targetsr~   N  s<     !+G  u5#> r*   c           	          t        t              }| D ]:  }|j                         D ]%  \  }}||xx   t        |t              r|n|gz  cc<   ' < |S )zN
    Helper method to group list of dict to dict with all possible values
    )r   rY   rl   
isinstance)arrayr]   rH   rw   rx   s        r   r{   r{   Y  s\     F I**, 	IJC3KJud$;5%HK	II Mr*   c            
         t        t               t        dg      t        di       t        dg d            } t        | d	
      }t        |      }|j                  j	                  d      }|j                  j	                  d      }|j                  j	                  d      }|j                  j	                  d      }|j                  ||      }|r|D cg c]  }t        |d   |      s| }}|j                  |      }|j                  |      }|r9|D cg c]  }|t        |      f }}|D cg c]  }t        |d   |      s| }}|j                  d|       yc c}w c c}w c c}w )z
    Module action handler
    r[   )aliasesrX   )typert   rY   rZ   )r   rt   elements)idnamerM   targetsT)argument_specsupports_check_moder   r   rM   r   r?   F)changedr   N)rX   AnsibleAWSModuler
   paramsgetr^   ry   rJ   rO   rr   r~   	exit_json)	r   r   r   r   fs_idrM   r   file_systems_inforH   s	            r   mainr   d  sa    6+,-vr*&"u=	M MtTFv&J==V$DMMd#E==V$Dmm	*G"33E4@.?`d8DQWLZ^C_T``"99:KL";;<MN<CDDD../DD.?od;tTcOdfmCnToo
U(9: a Eos   E"+E"E'3E,E,__main__)N)DOCUMENTATIONEXAMPLESRETURNcollectionsr   rA   ImportErroransible.module_utils._textr   0ansible.module_utils.common.dict_transformationsr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   >ansible_collections.community.aws.plugins.module_utils.modulesr   r   objectr
   rr   rk   ry   r~   r{   r   r_   r"   r*   r   <module>r      s   $L.c
L $	 1 U P f xoF od ;F zF s  		s   A A$#A$