
    VhN:                        d Z dZd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 dd	lm	Z	 dd
l
mZ ddlmZ  ej                  d      d        Zd Zd Zd Zd Zd Zd Z ej                  d      d        Zd Z ej0                  d      d        Z ej4                  d       ej6                         d               Zd Zedk(  r e        yy)a  
---
module: iam_group
version_added: 1.0.0
version_added_collection: community.aws
short_description: Manage AWS IAM groups
description:
  - Manage AWS IAM groups.
author:
  - Nick Aslanidis (@naslanidis)
  - Maksym Postument (@infectsoldier)
options:
  name:
    description:
      - The name of the group.
      - >-
        Note: Group names are unique within an account.  Paths (O(path)) do B(not) affect
        the uniqueness requirements of O(name).  For example it is not permitted to have both
        C(/Path1/MyGroup) and C(/Path2/MyGroup) in the same account.
      - The alias O(group_name) was added in release 7.2.0.
    required: true
    aliases: ['group_name']
    type: str
  path:
    description:
      - The group path.
      - For more information about IAM paths, see the AWS IAM identifiers documentation
        U(https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html).
    aliases: ['prefix', 'path_prefix']
    version_added: 7.1.0
    type: str
  managed_policies:
    description:
      - A list of managed policy ARNs or friendly names to attach to the role.
      - If known, it is recommended to use ARNs rather than friendly names to avoid additional
        lookups.
      - To embed an inline policy, use M(amazon.aws.iam_policy).
    required: false
    type: list
    elements: str
    default: []
    aliases: ['managed_policy']
  users:
    description:
      - A list of existing users to add as members of the group.
    required: false
    type: list
    elements: str
    default: []
  state:
    description:
      - Create or remove the IAM group.
    required: true
    choices: [ 'present', 'absent' ]
    type: str
  purge_policies:
    description:
      - When O(purge_policies=true) any managed policies not listed in O(managed_policies) will be detatched.
    required: false
    default: false
    type: bool
    aliases: ['purge_policy', 'purge_managed_policies']
  purge_users:
    description:
      - When O(purge_users=true) users which are not included in I(users) will be detached.
    required: false
    default: false
    type: bool
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
ak  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: Create a group
  amazon.aws.iam_group:
    name: testgroup1
    state: present

- name: Create a group and attach a managed policy using its ARN
  amazon.aws.iam_group:
    name: testgroup1
    managed_policies:
      - arn:aws:iam::aws:policy/AmazonSNSFullAccess
    state: present

- name: Create a group with users as members and attach a managed policy using its ARN
  amazon.aws.iam_group:
    name: testgroup1
    managed_policies:
      - arn:aws:iam::aws:policy/AmazonSNSFullAccess
    users:
      - test_user1
      - test_user2
    state: present

- name: Remove all managed policies from an existing group with an empty list
  amazon.aws.iam_group:
    name: testgroup1
    state: present
    purge_policies: true

- name: Remove all group members from an existing group
  amazon.aws.iam_group:
    name: testgroup1
    managed_policies:
      - arn:aws:iam::aws:policy/AmazonSNSFullAccess
    purge_users: true
    state: present

- name: Delete the group
  amazon.aws.iam_group:
    name: testgroup1
    state: absent
a  
iam_group:
    description: Dictionary containing all the group information including group membership.
    returned: success
    type: complex
    contains:
        group:
            description: Dictionary containing all the group information.
            returned: success
            type: complex
            contains:
                arn:
                    description: The Amazon Resource Name (ARN) specifying the group.
                    type: str
                    sample: "arn:aws:iam::1234567890:group/testgroup1"
                create_date:
                    description: The date and time, in ISO 8601 date-time format, when the group was created.
                    type: str
                    sample: "2017-02-08T04:36:28+00:00"
                group_id:
                    description: The stable and unique string identifying the group.
                    type: str
                    sample: AGPA12345EXAMPLE54321
                group_name:
                    description: The friendly name that identifies the group.
                    type: str
                    sample: testgroup1
                path:
                    description: The path to the group.
                    type: str
                    sample: /
        users:
            description: List containing all the group members.
            returned: success
            type: complex
            contains:
                arn:
                    description: The Amazon Resource Name (ARN) specifying the user.
                    type: str
                    sample: "arn:aws:iam::1234567890:user/test_user1"
                create_date:
                    description: The date and time, in ISO 8601 date-time format, when the user was created.
                    type: str
                    sample: "2017-02-08T04:36:28+00:00"
                user_id:
                    description: The stable and unique string identifying the user.
                    type: str
                    sample: AIDA12345EXAMPLE54321
                user_name:
                    description: The friendly name that identifies the user.
                    type: str
                    sample: testgroup1
                path:
                    description: The path to the user.
                    type: str
                    sample: /
        attached_policies:
            version_added: 7.1.0
            description:
                - List containing basic information about managed policies attached to the group.
            returned: success
            type: complex
            contains:
                policy_arn:
                    description: The Amazon Resource Name (ARN) specifying the managed policy.
                    type: str
                    sample: "arn:aws:iam::123456789012:policy/test_policy"
                policy_name:
                    description: The friendly name that identifies the policy.
                    type: str
                    sample: test_policy
    )AnsibleIAMError)IAMErrorHandler)$convert_managed_policy_names_to_arns)get_iam_group)normalize_iam_group)validate_iam_identifiers)AnsibleAWSModule)AWSRetryzupdate group pathc                     |y|d   j                  d      |k(  ry|j                  ry| j                  d|d   d   |       y)NFGroupPathT	GroupName)	aws_retryr   NewPath)get
check_modeupdate_group)
connectionmodule
group_infopaths       h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/iam_group.pyensure_pathr      s\    |'v&$.W%k2  
     c                 v    |D ]4  }  t        j                  d| d      | j                        d||       6 y )Nzdetach policy  from groupTr   r   	PolicyArn)r   deletion_error_handlerdetach_group_policyr   r   
group_namepolicies
policy_arns        r   detach_policiesr%      sL     F
	
X..
|;/WX**	
J*	FFr   c                 v    |D ]4  }  t        j                  d| d      | j                        d||       6 y )Nzattach policy 	 to groupTr   )r   common_error_handlerattach_group_policyr!   s        r   attach_policiesr*      sE     

tT,,~j\-STU_UsUstjJ	

r   c                 b   |y|rt        | |      }|d   d   }t        | ||      }|D cg c]  }|d   	 }}t        t        |      t        |      z
        }	g }
|r t        t        |      t        |      z
        }
|	s|
sy|j                  ryt        | |||
       t        | |||	       yc c}w )NFr   r   r   T)r   get_attached_policy_listlistsetr   r%   r*   )r   r   r   managed_policiespurge_policiesr"   current_attached_policies_descpolicycurrent_attached_policiespolicies_to_addpolicies_to_removes              r   ensure_managed_policiesr6      s    ?
L\]G$[1J%=j&R\%]"Ca b!4 b b3/037P3QQRO!#&?"@3GWCX"XY#5J
4FGJ
OD! !cs   B,c                 v    |D ]4  }  t        j                  d| d      | j                        d||       6 y )Nz	add user r'   Tr   r   UserName)r   r(   add_user_to_groupr   r   r"   membersusers        r   add_group_membersr>     sD     
gI,,yi-HI*JfJfgj4	

r   c                 v    |D ]4  }  t        j                  d| d      | j                        d||       6 y )Nzremove user r   Tr8   )r   r   remove_user_from_groupr;   s        r   remove_group_membersrA     sE     
sP..dV;/OPQ[QrQrsj4	

r   c                 2   |y|d   d   }|d   D cg c]  }|d   	 }}t        t        |      t        |      z
        }g }	|r t        t        |      t        |      z
        }	|s|	sy|j                  ryt        | |||       t	        | |||	       yc c}w )NFr   r   Usersr9   T)r-   r.   r   r>   rA   )
r   r   r   userspurge_usersr"   membercurrent_group_membersmembers_to_addmembers_to_removes
             r   ensure_group_membersrJ     s    }G$[1J>H>QRFVJ/RR#e*s+@'AABN %:!;c%j!HI"3j&*nEVZ9JK! Ss   Bzcreate groupc                     t        | |      }|rd|fS d|i}|||d<   |j                  r|j                  d|        | j                  dddi|}d|vrg |d<   d|fS )	NFr   r   T)changedcreate_paramsr   rC    )r   r   	exit_jsoncreate_group)r   r   r"   r   groupparamss         r   get_or_create_grouprS   5  s    *j1Ee|:&Fv V<#J##=d=f=Eeg;r   c           	      *   t        | ||j                  d   |j                  d         \  }}|t        | |||j                  d         z  }|t        | |||j                  d   |j                  d         z  }|t	        | |||j                  d   |j                  d         z  }|j
                  r|j                  |       t        | |j                  d         }t        | ||j                  d         }||d<   |j                  |t        |      	       y )
Nnamer   r/   r0   rD   rE   rL   AttachedPolicies)rL   	iam_group)
rS   rR   r   r6   rJ   r   rO   r   r,   r   )r   r   rL   r   r#   s        r   create_or_update_grouprY   K  s0   -j&&--PVBWY_YfYfgmYnoGZ {f	 G &()&' G #gm$ G ) z6==+@AJ'
FFMM&<QRH%-J!"
W0CJ0OPr   zdelete groupc                    |j                   j                  d      }t        | |      }|s|j                  d       |j                  r|j                  d       t        | ||      }|D cg c]  }|d   	 }}t        | |||       |d   D cg c]  }|d   	 }}t        | |||       | j                  d|       |j                  d       y c c}w c c}w )	NrU   FrV   Tr   rC   r9   )r   r   )	rR   r   r   rO   r   r,   r%   rA   delete_group)	r   r   r"   rQ   current_policies_descr2   current_policiesr=   rG   s	            r   destroy_groupr^   s  s    ""6*J*j1E' & 5ZT:OP{+PPJ
4DE ;@.I$T*-IIVZ9NOdjA
T" Q Js   +CCzlist policies attached to groupc                 j    | j                  d      }|j                  |      j                         d   S )Nlist_attached_group_policies)r   rW   )get_paginatorpaginatebuild_full_result)r   r   rU   	paginators       r   r,   r,     s8     (()GHI-??ABTUUr   c                     t        t        dgd      t        ddg      t        g ddgd	
      t        g dd	      t        ddgd      t        dd      t        ddddg            } t        | d      }t        d|j                  j	                  d      |j                  j	                  d            }|r|j                  |       |j                  dt        j                               }|j                  j	                  d      }	 |dk(  rt        ||       y t        ||       y # t        $ r}|j                  |       Y d }~y d }~ww xY w)Nr"   T)aliasesrequiredprefixpath_prefix)rf   r-   managed_policystr)defaulttyperf   elements)rl   rm   rn   presentabsent)choicesrg   Fbool)rl   rm   purge_policypurge_managed_policies)rl   rm   rf   )rU   r   r/   rD   staterE   r0   )argument_specsupports_check_moderQ   rU   r   )rU   r   )msgiam)retry_decoratorru   )dictr	   r   rR   r   	fail_jsonclientr
   jittered_backoffrY   r^   r   fail_json_aws_error)rv   r   identifier_problemr   ru   es         r   mainr     s;   <.488]34bv@P?Q\ab2FU;Ix04@V4EQi@jkM # F
 2fmm''/fmm6G6G6O /0uh6O6O6QRJMMg&E&I":v6*f- &""1%%&s   D, D, ,	E5EE__main__N)DOCUMENTATIONEXAMPLESRETURN7ansible_collections.amazon.aws.plugins.module_utils.iamr   r   r   r   r   r   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr	   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr
   r(   r   r%   r*   r6   r>   rA   rJ   rS   rY   r   r^   list_error_handlerr~   r,   r   __name__rN   r   r   <module>r      s  HT+ZG
R T S h Q W \ X P &%%&9: ;$F
:

0 &%%n5 6*%QP (''7# 8#4 $##$EFV  GV
 &F zF r   