
    Vh?                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZmZ d dlmZ d d	lmZm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Z%d Z&d Z'd Z(d Z)d Z*d  Z+d! Z,d" Z-d# Z.d$ Z/d% Z0e1d&k(  r e0        yy)'    )absolute_importdivisionprint_functiona 
  
---
module: ome_groups
short_description: Manages static device groups on OpenManage Enterprise
description: This module allows to create, modify, and delete static device groups on OpenManage Enterprise.
version_added: "3.5.0"
author:
  - Jagadeesh N V(@jagadeeshnv)
extends_documentation_fragment:
  - dellemc.openmanage.oment_auth_options
options:
  state:
    type: str
    description:
      - C(present) allows to create or modify a device group.
      - C(absent) allows to delete a device group.
    choices: [present, absent]
    default: present
  name:
    type: list
    elements: str
    description:
      - Name of the device group to be created, modified, or deleted.
      - If I(state) is absent, multiple names can be provided.
      - This option is case insensitive.
      - This option is mutually exclusive with I(group_id).
  group_id:
    type: list
    elements: int
    description:
      - ID of the device group to be created, modified, or deleted.
      - If I(state) is absent, multiple IDs can be provided.
      - This option is mutually exclusive with I(name).
  new_name:
    type: str
    description:
      - New name for the existing device group.
      - This is applicable only when I(state) is C(present).
  description:
    type: str
    description:
      - Description for the device group.
      - This is applicable only when I(state) is C(present).
  parent_group_name:
    type: str
    default: "Static Groups"
    description:
      - Name of the parent device group under which the device group to be created or modified.
      - This is applicable only when I(state) is C(present).
      - C(NOTE) If device group with such a name does not exist, device group with I(parent_group_name) is created.
      - This option is case insensitive.
      - This option is mutually exclusive with I(parent_group_id).
  parent_group_id:
    type: int
    description:
      - ID of the parent device group under which the device group to be created or modified.
      - This is applicable only when I(state) is C(present).
      - This option is mutually exclusive with I(parent_group_name).
requirements:
  - "python >= 3.9.6"
notes:
  - This module manages only static device groups on Dell OpenManage Enterprise.
  - If a device group with the name I(parent_group_name) does not exist, a new device group with the same name is created.
  - Make sure the entered parent group is not the descendant of the provided group.
  - Run this module from a system that has direct access to Dell OpenManage Enterprise.
  - This module supports C(check_mode).
aF  
---
- name: Create a new device group
  dellemc.openmanage.ome_groups:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    name: "group 1"
    description: "Group 1 description"
    parent_group_name: "group parent 1"

- name: Modify a device group using the group ID
  dellemc.openmanage.ome_groups:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    group_id: 1234
    description: "Group description updated"
    parent_group_name: "group parent 2"

- name: Delete a device group using the device group name
  dellemc.openmanage.ome_groups:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    state: absent
    name: "group 1"

- name: Delete multiple device groups using the group IDs
  dellemc.openmanage.ome_groups:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    state: absent
    group_id:
      - 1234
      - 5678
a  
---
msg:
  type: str
  description: Overall status of the device group operation.
  returned: always
  sample: "Successfully deleted the device group(s)."
group_status:
  description: Details of the device group operation status.
  returned: success
  type: dict
  sample: {
    "Description": "my group description",
    "Id": 12123,
    "MembershipTypeId": 12,
    "Name": "group 1",
    "ParentId": 12345,
    "TypeId": 3000,
    "IdOwner": 30,
    "CreatedBy": "admin",
    "CreationTime": "2021-01-01 10:10:10.100",
    "DefinitionDescription": "UserDefined",
    "DefinitionId": 400,
    "GlobalStatus": 5000,
    "HasAttributes": false,
    "UpdatedBy": "",
    "UpdatedTime": "2021-01-01 11:11:10.100",
    "Visible": true
  }
group_ids:
  type: list
  elements: int
  description: List of the deleted device group IDs.
  returned: when I(state) is C(absent)
  sample: [1234, 5678]
invalid_groups:
  type: list
  elements: str
  description: List of the invalid device group IDs or names.
  returned: when I(state) is C(absent)
  sample: [1234, 5678]
error_info:
  description: Details of the HTTP Error.
  returned: on HTTP error
  type: dict
  sample: {
    "error": {
        "code": "Base.1.0.GeneralError",
        "message": "A general error has occurred. See ExtendedInfo for more information.",
        "@Message.ExtendedInfo": [
            {
                "MessageId": "CGRP9013",
                "RelatedProperties": [],
                "Message": "Unable to update group  12345  with the provided parent  54321  because a group/parent
                relationship already exists.",
                "MessageArgs": [
                    "12345",
                    "54321"
                ],
                "Severity": "Warning",
                "Resolution": "Make sure the entered parent ID does not create a bidirectional relationship and retry
                the operation."
            }
        ]
    }
}
N)SSLError)URLError	HTTPError)ConnectionError)RestOMEOmeAnsibleModulezGroupService/Groupsz+GroupService/Actions/GroupService.{op}Groupz;Provide only one unique device group when state is present.z3A device group with the provided ID does not exist.z:A parent device group with the provided ID does not exist.zQThe provided parent device group is not a valid user-defined static device group.z2Provide valid static device group(s) for deletion.z3Provide valid static device group for modification.z=Unable to create a parent device group with the name {pname}.z$Successfully {op}d the device group.z8Provided parent and the device group cannot be the same.z[Unable to rename the group because a group with the provided name '{gname}' already exists.z)Successfully deleted the device group(s).zNo changes found to be applied.zChanges found to be applied.zStatic Groups   c           	         i }i }i }|j                  t              }| j                  j                  d      dk(  rct	        |j                  d      D cg c]>  }t        ||         j                         |v rt        ||         j                         |f@ c}      }n| j                  j                  d      }	|	rd}
nd}
| j                  j                  d      }	d}|j                  d      D ]~  }t        ||         j                         |v r|}|d	z   }t        ||
         j                         t        |	      j                         k(  r|}|d	z   }|d   t        k(  r|}|d	z   }|d
k(  s~ n |||fS c c}w )Nstateabsentvalueparent_group_idIdNameparent_group_namer         )get_all_items_with_pagination	GROUP_URIparamsgetdictstrlowerSTATIC_ROOT)modulerest_obj	group_arg	group_setparentstatic_root
group_dict
group_respgpargpkeycounts               q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/openmanage/plugins/modules/ome_groups.pyget_valid_groupsr,      s}   FKJ77	BJ}}!X-:>>RYCZ Fa!!I,/5579D  )-335q9 F G
 }}  !23DD==$$%89D( 	A1Y< &&(I5
	1T7|!!#s4y'88	yK'	z	 v{**-Fs   AE3c                 &    | d   dk(  r	| d   dk(  ryy)NTypeIdi  MembershipTypeId   TF )grps    r+   is_valid_static_groupr3      s!    
8}%7!8B!>    c                    	 |}i }d|d<   |j                   j                  d      |d<   |d   |d<   | j                  dt        j	                  d	      d
|i      }t        |j                        S # t        $ r |d   cY S w xY w)Nr0   r/   r   r   r   ParentIdPOSTCreateop
GroupModeldata)r   r   invoke_requestOP_URIformatint	json_data	Exception)r    r   r$   prtpayloadprt_resps         r+   create_parentrG     s    	!&("# --++,?@!$i
**66==H=3MUacjTk*l8%%&& !4  !s   A-A0 0B Bc                    |j                   j                  d      }|rJ|s|j                  t               |d   t        k7  r!t        |      s|j                  t               |d   S |r2|d   t        k7  r!t        |      s|j                  t               |d   S |j                  ryt        | ||      }t        j                  t               |S )Nr   msgr   r   r   )r   r   	fail_jsonNONEXIST_PARENT_IDr   r3   INVALID_PARENT
check_moderG   timesleepSETTLING_TIME)r    r   r#   r$   	parent_idprtids         r+   get_parent_idrT     s    !!"34I!34&>[((0  ^ 4d|f~,,V4$$$8$<  %hD

=)r4   c                     i }	 | j                  dt        ddj                  |      i      }|j                  j	                  d      }|r|d   }|S # t
        $ r i }Y |S w xY w)NGETz$filterzName eq '{0}')query_paramr   r   )r>   r   r@   rB   r   rC   )r    namer2   respr&   s        r+   get_ome_group_by_namerZ   (  sw    
C&&uiiQ`QgQghlQmEn&o^^''0
Q-C J  Js   AA A A c                     i }	 | j                  dt        dj                  |      z         }|j                  }|S # t        $ r i }Y |S w xY w)NrV   z({0}))r>   r   r@   rB   rC   )r    idr2   rY   s       r+   get_ome_group_by_idr]   4  sW    
C&&ui'..:L.LMnn J  Js   4: A	A	c                    |j                  dt        j                  |      d|i      }t        |j                        }t        j                  t               	 t        ||      }|j                  |      }| j                  dt        j                  |j                               |       y # t        $ r
 ||d<   |}Y Iw xY w)Nr7   r9   r;   r<   r   T)changedrJ   group_status)r>   r?   r@   rA   rB   rO   rP   rQ   r]   strip_substr_dictrC   	exit_jsonCREATE_SUCCESSr   )r   r    rE   	operationr&   cidr2   groups           r+   exit_group_operationrg   >  s    (()1LT`biSj(kJ
j""
#CJJ}!(C0**3/ T~'<'<	@Q'<'Rafg  s   B. .C Cc                    i }d|d<   |j                   }|j                  d      d   |d<   |j                  d      j                         |d   j                         k(  r|j                  t               t        | |||      }||d<   |j                  d	      r|j                  d	      |d
<   |j                  r|j                  dt               t        || |d       y )Nr0   r/   rX   r   r   r   rI   r6   descriptionDescriptionTr_   rJ   r8   )
r   r   r   rK   GROUP_PARENT_SAMErT   rN   rb   CHANGES_FOUNDrg   )r    r   r#   r$   rE   mparamsrR   s          r+   create_groupro   K  s    G"$GmmGkk&)!,GFO{{&'--/76?3H3H3JJ./hDI#GJ{{=!!(]!;=97H=r4   c           	         t        |      s|j                  t               |}d}t        dD cg c]  }||j	                  |      f c}      }|j
                  j	                  d      }	|	rF|	|d   k7  r>t        | |	      }
|
r&|j                  t        j                  |	             |	|d<   |dz  }|j
                  j	                  d      }|r||d	   k7  r
||d	<   |dz  }t        | |||      }||d
   k(  r|j                  t               ||d   k7  r
||d<   |dz  }|dk(  r(| j                  |      }|j                  t        |       |j                  r|j                  dt               t!        || |d       y c c}w )NrI   r   )r   rj   r/   r6   r   new_namer   )gnamer   ri   rj   r   r6   )rJ   r`   Trk   Update)r3   rK   INVALID_GROUPS_MODIFYr   r   r   rZ   GROUP_NAME_EXISTSr@   rT   rl   ra   rb   NO_CHANGES_MSGrN   rm   rg   )r    r   valid_group_dictr#   r$   r2   diffkrE   rq   dup_grpdescrR   gss                 r+   modify_groupr}   [  s    !1223
CD-jkQ
OklG}}  ,Hwv&+Hh?G  %6%=%=H%=%M N&GFOAID==]+D7=))%)GM"AIDhDIGDM!./GJ'''
	qy'',^"==97H=3 ls   E?c                    g }g }|D ]^  }|j                  t        |      j                               }|s.t        |      r|j	                  |d          N|j	                  |       ` |r|j                  t        |       |j                  r|j                  dt        |       | j                  dt        j                  d      d|i	       |j                  dt        |       y )
Nr   )rJ   invalid_groupsT)r_   rJ   	group_idsr7   Deleter9   GroupIdsr<   )r   r   r   r3   appendrK   INVALID_GROUPS_DELETErN   rb   rm   r>   r?   r@   DELETE_SUCCESS)r    r   r"   r%   
deletablesinvalidsr'   r2   s           r+   delete_groupsr   |  s    JH #nnSV\\^,$S)!!#d),"# 28L=JOFFMMXM$>jR\E]^
T~Lr4   c                     dddddddddgddddiddidt         d	ddid
} t        | g dgddgdgd      }	 |j                  j                  d      r.d}t	        d |j                  j                  d      D              }n-d}t	        d |j                  j                  d      D              }t        |      dk7  r(|j                  d   dk(  r|j                  t               t        |j                  d      5 }t        ||||      \  }}}|j                  d   dk(  r'|rt        ||||       |j                  t               n;|rt        |||||       n|dk(  r|j                  t               t        ||||       d d d        y # 1 sw Y   y xY w# t         $ r9}|j                  t#        |      t%        j&                  |             Y d }~y d }~wt(        $ r&}|j                  t#        |      d       Y d }~y d }~wt*        t,        t.        t0        t2        t4        t6        t8        t:        f	$ r%}|j                  t#        |             Y d }~y d }~ww xY w)Nlistr   )typeelementsrA   presentr   )r   choicesdefaultr   )r   r   )rX   group_idr   ri   rq   r   r   )r   r   )rq   ri   r   r   T)rX   r   )r   r   T)argument_specrequired_ifmutually_exclusiverequired_one_ofsupports_check_moderX   r   c              3   <   K   | ]  }|j                           y wN)r   .0vs     r+   	<genexpr>zmain.<locals>.<genexpr>  s     I!AGGIIs   r   c              3   N   K   | ]  }t        |      j                           y wr   )r   r   r   s     r+   r   zmain.<locals>.<genexpr>  s     RqCFLLNRs   #%r   r   r   rI   )req_session)rJ   
error_info)rJ   unreachable)r   r   r   r   setlenrK   MULTIPLE_GROUPS_MSGr
   r,   r   rb   rv   r}   NONEXIST_GROUP_IDro   r   r   jsonloadr   IOError
ValueErrorr   	TypeErrorr	   AttributeError
IndexErrorKeyErrorOSError)	specsr   r!   r"   r    rw   r#   r$   errs	            r+   mainr     sB   U3#7Y,AiXUO&+D"E?E k
 !"J
 .. 
F'==V$IIv}}/@/@/HIIIIRFMM4E4Ej4QRRIy>Q6==#9Y#F!45V]]5 	D4DVXW`bk4l1fk}}W%1#!(FI?OP  ^ 4# 63CV[Y$&$$):$;Xvv{C	D 	D 	D  BSX$))C.AA 9SX488Z9o~Wackmtu 'SX&&'sO   CF >BFF FF F 	I#/GI#H5I9II__main__)2
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rO   sslr   +ansible.module_utils.six.moves.urllib.errorr   r   ansible.module_utils.urlsr	   ?ansible_collections.dellemc.openmanage.plugins.module_utils.omer
   r   r   r?   r   r   rL   rM   r   rt   PARENT_CREATION_FAILEDrc   rl   ru   r   rv   rm   r   rQ   r,   r3   rG   rT   rZ   r]   rg   ro   r}   r   r   __name__r1   r4   r+   <module>r      s    C BBH)VB
H    K 5 e!		6S I Q dL M X 7N q <2.+>
!2	
h> >BM$1'h zF r4   