
    Vh                         d Z ddlmZmZmZ eZdZdZdZ	ddl
mZ ddlmZ ddlZ ej                  d	      Zd
Z G d de      Zd Zd Zd Zedk(  r e        yy)z)Ansible module for managing host on Unity    )absolute_importdivisionprint_functiona  
---
module: host

version_added: '1.1.0'

short_description: Manage Host operations on Unity

description:
- The Host module contains the operations
  Creation of a Host,
  Addition of initiators to Host,
  Removal of initiators from Host,
  Modification of host attributes,
  Get details of a Host,
  Deletion of a Host,
  Addition of network address to Host,
  Removal of network address from Host.

extends_documentation_fragment:
  - dellemc.unity.unity

author:
- Rajshree Khare (@kharer5) <ansible.team@dell.com>

options:
  host_name:
    description:
    - Name of the host.
    - Mandatory for host creation.
    type: str

  host_id:
    description:
    - Unique identifier of the host.
    - Host Id is auto generated during creation.
    - Except create, all other operations require either I(host_id) or Ihost_name).
    type: str

  description:
    description:
    - Host description.
    type: str

  host_os:
    description:
    - Operating system running on the host.
    choices: ['AIX', 'Citrix XenServer', 'HP-UX', 'IBM VIOS', 'Linux',
    'Mac OS', 'Solaris', 'VMware ESXi', 'Windows Client', 'Windows Server']
    type: str

  new_host_name:
    description:
    - New name for the host.
    - Only required in rename host operation.
    type: str

  initiators:
    description:
    - List of initiators to be added/removed to/from host.
    type: list
    elements: str

  initiator_state:
    description:
    - State of the initiator.
    choices: [present-in-host , absent-in-host]
    type: str

  network_address:
    description:
    - Network address to be added/removed to/from the host.
    - Enter valid IPV4 or host name.
    type: str

  network_address_state:
    description:
    - State of the Network address.
    choices: [present-in-host , absent-in-host]
    type: str

  state:
    description:
    - State of the host.
    choices: [present , absent]
    type: str
    required: true

notes:
  - The I(check_mode) is not supported.
aH  
- name: Create empty Host
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_name: "ansible-test-host"
    host_os: "Linux"
    description: "ansible-test-host"
    state: "present"

- name: Create Host with Initiators
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_name: "ansible-test-host-1"
    host_os: "Linux"
    description: "ansible-test-host-1"
    initiators:
      - "iqn.1994-05.com.redhat:c38e6e8cfd81"
      - "20:00:00:90:FA:13:81:8D:10:00:00:90:FA:13:81:8D"
    initiator_state: "present-in-host"
    state: "present"

- name: Modify Host using host_id
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_id: "Host_253"
    new_host_name: "ansible-test-host-2"
    host_os: "Mac OS"
    description: "Ansible tesing purpose"
    state: "present"

- name: Add Initiators to Host
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_name: "ansible-test-host-2"
    initiators:
      - "20:00:00:90:FA:13:81:8C:10:00:00:90:FA:13:81:8C"
    initiator_state: "present-in-host"
    state: "present"

- name: Get Host details using host_name
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_name: "ansible-test-host-2"
    state: "present"

- name: Get Host details using host_id
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_id: "Host_253"
    state: "present"

- name: Delete Host
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_name: "ansible-test-host-2"
    state: "absent"

- name: Add network address to Host
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_name: "{{host_name}}"
    network_address: "192.168.1.2"
    network_address_state: "present-in-host"
    state: "present"

- name: Delete network address from Host
  dellemc.unity.host:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    host_name: "{{host_name}}"
    network_address: "192.168.1.2"
    network_address_state: "absent-in-host"
    state: "present"
a  
changed:
    description: Whether or not the resource has changed.
    returned: always
    type: bool
    sample: true

host_details:
    description: Details of the host.
    returned: When host exists.
    type: dict
    contains:
        id:
            description: The system ID given to the host.
            type: str
        name:
            description: The name of the host.
            type: str
        description:
            description: Description about the host.
            type: str
        fc_host_initiators:
            description: Details of the FC initiators associated with
                         the host.
            type: list
            contains:
                id:
                    description: Unique identifier of the FC initiator path.
                    type: str
                name:
                    description: FC Qualified Name (WWN) of the initiator.
                    type: str
                paths:
                    description: Details of the paths associated with the FC initiator.
                    type: list
                    contains:
                        id:
                            description: Unique identifier of the path.
                            type: str
                        is_logged_in:
                            description: Indicates whether the host initiator is logged into the storage system.
                            type: bool
        iscsi_host_initiators:
            description: Details of the ISCSI initiators associated
                         with the host.
            type: list
            contains:
                id:
                    description: Unique identifier of the ISCSI initiator path.
                    type: str
                name:
                    description: ISCSI Qualified Name (IQN) of the initiator.
                    type: str
                paths:
                    description: Details of the paths associated with the ISCSI initiator.
                    type: list
                    contains:
                        id:
                            description: Unique identifier of the path.
                            type: str
                        is_logged_in:
                            description: Indicates whether the host initiator is logged into the storage system.
                            type: bool
        network_addresses:
            description: List of network addresses mapped to the host.
            type: list
        os_type:
            description: Operating system running on the host.
            type: str
        type:
            description: HostTypeEnum of the host.
            type: str
        host_luns:
            description: Details of luns attached to host.
            type: list
    sample: {
        "auto_manage_type": "HostManageEnum.UNKNOWN",
        "datastores": null,
        "description": "ansible-test-host-1",
        "existed": true,
        "fc_host_initiators": [
            {
                "id": "HostInitiator_1",
                "name": "HostName_1",
                "paths": [
                    {
                        "id": "HostInitiator_1_Id1",
                        "is_logged_in": true
                    },
                    {
                        "id": "HostInitiator_1_Id2",
                        "is_logged_in": true
                    }
                ]
            }
        ],
        "hash": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
        "health": {
            "UnityHealth": {
                "hash": 8764429420954
            }
        },
        "host_container": null,
        "host_luns": [],
        "host_polled_uuid": null,
        "host_pushed_uuid": null,
        "host_uuid": null,
        "host_v_vol_datastore": null,
        "id": "Host_2198",
        "iscsi_host_initiators": [
            {
                "id": "HostInitiator_2",
                "name": "HostName_2",
                "paths": [
                    {
                        "id": "HostInitiator_2_Id1",
                        "is_logged_in": true
                    },
                    {
                        "id": "HostInitiator_2_Id2",
                        "is_logged_in": true
                    }
                ]
            }
        ],
        "last_poll_time": null,
        "name": "ansible-test-host-1",
        "network_addresses": [],
        "os_type": "Linux",
        "registration_type": null,
        "storage_resources": null,
        "tenant": null,
        "type": "HostTypeEnum.HOST_MANUAL",
        "vms": null
    }
)AnsibleModule)utilsNhostzAnsible/1.7.1c                       e Zd ZdZd Zd ZddZd Zd Zd Z	d	 Z
	 	 dd
ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)HostzClass with Host operationsc                    t        j                         | _        | j                  j                  t	                      ddgg}ddgg}ddgg}	 t        | j                  d|||      | _        t        j                  | j                         t        j                  | j                  j                  t              | _        t        j                  d       y)	z. Define all parameters required by this module	host_namehost_idnetwork_addressnetwork_address_stateF)argument_specsupports_check_modemutually_exclusiverequired_togetherrequired_one_ofz0Got the unity instance for provisioning on UnityN)r   $get_unity_management_host_parametersmodule_paramsupdateget_host_parametersr   moduleensure_required_libsget_unity_unisphere_connectionparamsapplication_typeunityLOGinfo)selfr   r   r   s       f/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/unity/plugins/modules/host.py__init__zHost.__init__d  s     #GGI!!"5"78*I67'34/1HIJ-#$2D2D8=7I6G4C	E
 	""4;;/99$++:L:LN^_
CD    c                     g }d}t         j                  j                  j                  | j                  j
                  |      }t        |      }|S )z/ To get the count of hosts with same host_name r   )cliname)r   r   UnityHostListgetr   _clilen)r!   r   hosts
host_counts       r"   get_host_countzHost.get_host_county  sI     


((,,2; - =Z
r$   Nc                    |r|n|}	 t         j                  d|       |r)| j                  j                  |      }|j                  y|rw	 | j                  |      }|dk  ry|dkD  r>d|z   dz   |z   }t         j                  |       | j                  j                  |       S | j                  j                  |      }S # t        j                  $ r}|j                  d	k(  r4d
}t         j                  |       | j                  j                  |       nHd|dt        |      d}t         j                  |       | j                  j                  |       Y d}~yY d}~yd}~wt        j                  $ r:}dj                  |t        |            }t         j                  |       Y d}~yd}~wt        $ rL}dt        |      d|}t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)z Get details of a given host zGetting host %s details_idN   z!Duplicate hosts found: There are z# hosts(s) with the same host_name: msgr'   i  z(Incorrect username or password provided.z5Got HTTP Connection Error while getting host details z	 : Error  z0Failed to get details of host {0} with error {1}
Got error z while getting details of host )r   r    r   get_hostr'   r.   errorr   	fail_jsonr   	HttpErrorhttp_statusstrUnityResourceNotFoundErrorformat	Exception)	r!   r   r   host_id_or_namehost_detailsr-   error_messageer4   s	            r"   get_host_detailszHost.get_host_details  s    &-')+	5HH.@#zz22w2?$$,B!00;
>!^$G&0%143%35>%?M IIm,KK))m)<   $(::#6#6I#6#FL 		/}}#@		#%%#%. $ 3B3q6K		#%%#%.. / // 	1178;A2@  IIm$ 	5"1v8MIIm$KK!!m!44		5sE   A C 	C #AC &C H
BE''H
=0F22H
>AHH
c                    	 | j                   j                  d   }| j                   j                  d   }t        j                  j                  }| j                   j                  d   }| j                   j                  d   }d}|r8|dk(  r3d}t
        j                  |       | j                   j                  |       |t        |      d
k(  s|r|dk(  rd}	 |rb|dk(  r]| j                  |      }	|	t        |	      t        |      k  r3d}t
        j                  |       | j                   j                  |       |s| j                  |       t
        j                  d|       t        j                  j                  j                  | j                  j                   ||||      }
|sG| j                  j#                  |      }t
        j                  d|       | j%                  ||      \  }}
d|
fS # t&        $ rL}dt)        |      d|}t
        j                  |       | j                   j                  |       Y d	}~y	d	}~ww xY w)z Create a new host descriptionhost_os
initiatorsinitiator_stateFabsent-in-host"Incorrect 'initiator_state' given.r3   Nr   Tpresent-in-hostProvide valid initiators.zCreating empty host %s )r'   descos	host_typer5   zAdding initiators to %s hostr7   z while creation of host )r   r   r   HostTypeEnumHOST_MANUALr   r9   r:   r+   get_list_unmapped_initiatorsvalidate_initiatorsr    r   	UnityHostcreater   r*   r8   add_initiator_to_hostr@   r=   )r!   r   rG   rH   rQ   rI   rJ   empty_initiators_flagrC   unmapped_initiatorsnew_hostrB   resultrD   s                 r"   create_hostzHost.create_host  s   *	5++,,];Kkk((3G**66I++L9J"kk001BCO$)!2BB D		-(%%-%8"c*o&:*&*::(,%I2CC77
C $&.23c*oE$?MIIm,KK))m)<(((4HH.	:zz++224::??Yd6= 3 TH(#zz22	2B7C00zJ !>! 	5"1vy2MIIm$KK!!m!44		5s   G/G2 2	I;AIIc                     g }|D ]&  }|j                  t        j                  |             ( d|v r4d}t        j	                  |       | j
                  j                  |       y y )NFzLOne or more initiator provided is not valid, please provide valid initiatorsr3   )appendr   is_initiator_validr   r9   r   r:   )r!   rI   resultsitemrC   s        r"   rU   zHost.validate_initiators  s`     	;DNN533D9:	;GjMIIm$KK!!m!4 r$   c                    g }|j                   Vt        |j                         }|dkD  r<t        |      D ].  }	 |j                   j                  |   }	 |j	                  |       0 |j
                  Vt        |j
                        }|dkD  r<t        |      D ].  }	 |j
                  j                  |   }	 |j	                  |       0 |S )z, Get the list of existing initiators in hostr   )fc_host_initiatorsr+   rangeinitiator_idr_   iscsi_host_initiators)r!   rB   existing_initiatorsfc_lencountini_id	iscsi_lens          r"   get_host_initiators_listzHost.get_host_initiators_list  s     !**6889Fz"6] 7E1&99FFuM  G'..v67 --9L>>?I1}"9- 7E1&<<$U,  G'..v67 #"r$   c                 d   t         j                  d       d}| j                  j                  d   &| j                  j                  d   |j                  k7  s| j                  j                  d   &| j                  j                  d   |j
                  k7  s| j                  j                  d   &| j                  j                  d   |j                  k7  sD| j                  j                  d   B| j                  j                  d   | j                  |      k7  rt         j                  d       d}|S )	z> Determines whether the Host details are to be updated or not zChecking host attribute values.FrG   rH   new_host_namerI   zModification required.T)r   r    r   r   rG   os_typer'   rm   )r!   rB   modified_flags      r"   is_host_modifiedzHost.is_host_modified  s   23KK}-9""=1''(KK&&y1=**959M9MMKK&&7C**'),8,=,=>KK&&|4@**<844\BCHH-. Mr$   c                    	 t         j                  j                  j                  | j                  j
                        }|j                  }|D ]A  }||k(  s	d|z   dz   }t        j                  |       | j                  j                  |       C |j                  |||       y# t        $ rV}	dt        |	      d|j                  }t        j                  |       | j                  j                  |       Y d	}	~	y	d	}	~	ww xY w)
z  Modify a host )r&   z#Cannot modify name, new_host_name: z already in use.r3   )r'   rO   rP   Tr7   z while modifying host N)r   r   r(   r)   r   r*   r'   r   r9   r   r:   modifyr@   r=   )
r!   rB   ro   rG   rH   r,   host_names_listr'   rC   rD   s
             r"   modify_hostzHost.modify_host  s    	5JJ,,00TZZ__0EE#jjO' = D($I&3%46H%IMIIm,KK))m)<= ]#*   , 	5"1v|'8'8:MIIm$KK!!m!44		5s    AB' AB' '	D0ADDc                    g }|D ]  }t         j                  j                  j                  | j                  j
                  |      j                         }	 d|d   v r|j                  |d   d          p|d   s|j                  |       d|z   dz   }t        j                  |       | j                  j                  |        |S )	zN Get the list of those initiators which are
            not mapped to any hostr&   rf   Nparent_hostrf   r   z
Initiator z mapped to another Host.r3   )r   r   UnityHostInitiatorListr)   r   r*   _get_propertiesr_   r   r9   r   r:   )r!   rI   r   rZ   idinitiator_detailsrC   s          r"   rT   z!Host.get_list_unmapped_initiators2  s     ! 	9B %

 A Ar: " &(77#**+<,:,<<=,? @&}5#**2. ,r 14N N		-(%%-%8	9  #"r$   c                    	 | j                  |      }	 |rGt        |      j                  t        |            r$t        j	                  d|j
                         d|fS 	 |j                  }| j                  ||      }	 |t        |      t        |      k  r3d}t        j                  |       | j                  j                  |       t        j	                  d|j
                         |D ]:  }|j                  |       | j                  j                  |j
                        }< d	fS # t        $ rV}	d
t!        |	      d|j
                  }t        j                  |       | j                  j                  |       Y d}	~	yd}	~	ww xY w)z Add initiator to host z*Initiators are already present in host: %sFNrN   r3   zAdding initiators to host %suidr5   Tr7   z  while adding initiator to host )rm   setissubsetr   r    r'   r|   rT   r+   r9   r   r:   add_initiatorr   r8   r@   r=   )
r!   rB   rI   rh   r   rZ   rC   r|   updated_hostrD   s
             r"   rX   zHost.add_initiator_to_hostI  sn   "	5"&"?"?"M>Z11#6I2JKE%**,l**$"ooG33JH   J"*./#j/A ;		-(%%-%8HH3\5F5FG) B**r*2jj))|/@/@)A B %% 	5"1v|'8'8:MIIm$KK!!m!44		5s    AD, CD, ,	F5AFFc                    	 | j                  |      }|$t        j                  d|j                         d|fS t	        |      j                  t	        |            s$t        j                  d|j                         d|fS t        j                  d|j                         t        |      dkD  r| j                  |       |D ]  }t        j                  j                  j                  | j                  j                  |      j                         }	 |d   d	   Nt        j                  d
       |j                  |       | j                  j!                  |j                        }	 |d   d	   d   D ]  }|d   d   }t        j                  j"                  j                  | j                  j                  |      }	|	j                         }
	 |
d   r:d|z   dz   }t        j%                  |       | j&                  j)                  |       |
d   s	 |	j+                          	 d|z   dz   }t        j%                  |       | j&                  j)                  |        |j                  |       | j                  j!                  |j                        } dfS # t,        $ rV}dt/        |      d|j                  }t        j%                  |       | j&                  j)                  |       Y d}~yd}~ww xY w)z Remove initiator from host Nz#No exisiting initiators in host: %sFz%Initiators already absent in host: %sz Removing initiators from host %sr2   rx   pathsr   zInitiator Path does not exist.r   r5   UnityHostInitiatorPathListUnityHostInitiatorPathr|   )r&   r1   is_logged_inzCannot remove initiator z#, as it is logged in the with host.r3   z. logged_in state does not exist for initiator .Tr7   z$ while removing initiator from host )rm   r   r    r'   r   r   r+   check_if_initiators_logged_inr   r   rz   r)   r   r*   r{   delete_initiatorr8   r   r9   r   r:   deleter@   r=   )r!   rB   rI   rh   r|   r}   r   pathpath_idpath_id_objpath_id_detailsrC   rD   s                r"   remove_initiator_from_hostzHost.remove_initiator_from_hostp  s   F	5"&"?"?"M"*>%**,l**
O,,S1D-EF@%**,l**HH79J9JK:"22:>  +F$)JJ$E$ESTZZ__2S>$_& " I$W-a08HH=> 11b19**--<3D3D-E ! C 1' :1 =>Z [ E"&'?"@"F&+jj&K&K STZZ__'SB $ +6*E*E*GN+N;,F.0-14D-DM  IIm4 KK11m1D"1."AN'..0 F-CEG-HJM-NMIIm4 KK11m1D3E6 !11b19**--<3D3D-E !U+FZ %% 	5  #1v|'8'8:M IIm$KK!!m!44	5s%   6J! AJ!  H J! !	L *AK;;L c                 :   |D ]  }t         j                  j                  j                  | j                  j
                  |      j                         }|d   d   Yd|d   d   v sdd}t        j                  |       | j                  j                  |        y)z5 Checks if any of the initiators is of type logged-inrx   r   r   Nr   zFRemoval operation cannot be done since host has logged in initiator(s)r3   )r   r   rz   r)   r   r*   r{   r   r9   r   r:   )r!   rI   rb   r}   rC   s        r"   r   z"Host.check_if_initiators_logged_in  s      	9D!&!B!B"%#$**//#"M"1/"3  !)!,8=Y]nov]wxy]z=z h		-(%%-%8	9r$   c                     	 |j                          y# t        $ rV}dt        |      d|j                  }t        j                  |       | j                  j                  |       Y d}~yd}~ww xY w)z Delete an existing host Tr7   z while deletion of host r3   N)r   r@   r=   r'   r   r9   r   r:   )r!   rB   rD   rC   s       r"   delete_hostzHost.delete_host  s`    	5! 	5"1v|'8'8:MIIm$KK!!m!44		5s    	A2AA--A2c                 D   g }|D ]  }| j                   j                  |j                        }g }|j                  9|j                  D ]*  }|j	                  |j                  |j
                  d       , |j	                  |j                  |j                  |d        |S )z5 Get the details of existing ISCSI initiators in hostr0   r|   r   r|   r'   r   r   get_initiatorr|   r   r_   r   rf   )r!   rg   iscsi_initiator_listiscsiiscsi_initiator_detailsiscsi_path_listr   s          r"   !get_iscsi_host_initiators_detailsz&Host.get_iscsi_host_initiators_details  s      "* 	E&*jj&>&>588&>&L# O&,,8399 D#**"gg(,(9(9, 
 !''-00/<<() 	 $#r$   c                 ~    g }|r8|j                   ,|j                   D ]  }|j                  |j                          |S N)host_ip_portsr_   address)r!   rB   network_address_listports       r"   get_host_network_address_listz"Host.get_host_network_address_list  sC    !L66B$22 :$++DLL9:##r$   c                    	 d}d}|D ]'  }|j                         |j                         k(  s%d} n |s:|dk(  r5t        j                  d||j                         |j	                  |       d}n;|r9|dk(  r4t        j                  d||j                         |j                  |       d}|r7| j                  j                  |j                        }| j                  |      }||fS # t        $ rY}	dt        |	      d	|d
|j                  }
t        j                  |
       | j                  j                  |
       Y d }	~	y d }	~	ww xY w)NFTrM   z$Adding network address %s to Host %srK   z(Deleting network address %s from Host %sr5   r7   z! while modifying network address z	 of host r3   )lowerr   r    r'   add_ip_portdelete_ip_portr   r8   r   r@   r=   r9   r   r:   )r!   rB   r   r   r   	is_mappedchangedaddrr   rD   rC   s              r"   manage_network_addresszHost.manage_network_address  s<   	5IG, ::<?#8#8#:: $I !6:K!K?%**,((948HHC_%**,++O<#zz228I8I2J'+'I'I,'W$'00 	5"1v8I8IKMIIm$KK!!m!44		5s   *C& B8C& &	E/AEEc                     g }|rQ|j                   E|j                   j                  D ],  }|j                  |j                  d}|j	                  |       . |S )z Get luns attached to host)r'   r|   )	host_lunslunr'   r|   r_   )r!   rB   host_luns_listr   host_luns        r"   get_host_lun_listzHost.get_host_lun_list  sX    L22>#--11 0$'HHCFF;%%h/0 r$   c                 D   g }|D ]  }| j                   j                  |j                        }g }|j                  9|j                  D ]*  }|j	                  |j                  |j
                  d       , |j	                  |j                  |j                  |d        |S )z2 Get the details of existing FC initiators in hostr0   r   r   r   )r!   rd   fc_initiator_listfcfc_initiator_detailsfc_path_listr   s          r"   get_fc_host_initiators_detailsz#Host.get_fc_host_initiators_details  s     $ 	B#'::#;#;#;#F L#))5066 D ''"gg(,(9(9) 
 $$*--,99%& 	 ! r$   c                 l   | j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d	   }	| j                   j                  d
   }
|rAt        |      dkD  r3d}t        j	                  |       | j                   j                  |       |rAt        |      dkD  r3d}t        j	                  |       | j                   j                  |       |rAt        |      dkD  r3d}t        j	                  |       | j                   j                  |       |s5|r3d}t        j	                  |       | j                   j                  |       |s5|r3d}t        j	                  |       | j                   j                  |       t        di       }	 | j                  ||      }|s |
dk(  r|r3d}t        j	                  |       | j                   j                  |       |s3d}t        j	                  |       | j                   j                  |       |r3d}t        j	                  |       | j                   j                  |       |r8|dk(  r3d}t        j	                  |       | j                   j                  |       t        j                  d|       | j                  |      \  |d<   }|j                         |d<   n|
dk(  r|r| j                  |      }|r| j                  ||||      |d<   |r| j                  ||      }n| j                  ||      }|j                         |d<   |dk(  r|rt        |      dkD  ryt        j                  d|j                         | j                  ||      \  |d<   }|j                         |d<   n-t        j                  d        d|d<   |j                         |d<   |r`|dk(  r[|rYt        |      dkD  rKt        j                  d!|j                         | j                  ||      \  |d<   }|j                         |d<   	 |r?|j                   3| j#                  |j                         |_        |j                         |d<   |r?|j$                  3| j'                  |j$                        |_        |j                         |d<   	 |d   r;| j)                  |      |d   d#<   | j+                  |      |d   d$<   d%|d   v r|d   d%= |=|	;| j-                  |       | j/                  ||d   d$   ||	      \  }}||d   d$<   ||d<   |
d&k(  rA|r5t        j                  d'|j                         | j1                  |      |d<   nd|d<   g |d<    | j                   j2                  d(i | y"))zZ Perform different actions on host based on user parameter
            chosen in playbook r   r   rG   rH   ro   rJ   rI   r   r   state   z+'host_name' is greater than 255 characters.r3   z/'new_host_name' is greater than 255 characters.z-'description' is greater than 255 characters.z:'initiator_state' is given, 'initiators' are not specifiedz:'initiators' are given, 'initiator_state' is not specifiedF)r   rB   presentz0Invalid argument 'host_id' while creating a hostz&host_name is required to create a hostz6Invalid argument 'new_host_name' while creating a hostrK   rL   zCreating host: %sr   rB   rM   r   zAdding Initiators to Host %szKHost modification is not applicable, as none of the attributes has changed.z Removing Initiators from Host %sNr   network_addressesr   absentzDeleting host %s )r   r   r+   r   r9   r:   dictrE   r    r]   r{   rr   rv   r'   rX   r   rd   r   rg   r   r   r   validate_network_address_paramsr   r   	exit_json)r!   r   r   rG   rH   ro   rJ   rI   r   r   r   err_msgr\   rB   rC   rq   r   r   s                     r"   perform_module_operationzHost.perform_module_operation)  s    KK&&{3	++$$Y/kk((7++$$Y/**?;++,,->?[[''5
++,,->? $ 2 23J K""7+Y#-CGIIgKK!!g!.S/#5GGIIgKK!!g!.3{+c1EGIIgKK!!g!.o7GIIgKK!!g!.:;GIIgKK!!g!. 

 	;,,Wi@ 2,		'"%%'%2B		'"%%'%2,		'"%%'%22BB D		-(%%-%8 HH()4""9- ,F9|%1%A%A%CF>" y \ 11,?M %)$4$4\5B5@5<%>y! !#'#8#89F$HL $(#8#8)#LL)5)E)E)G~& $'88Z
Oa/HH;)..0 44\:N 4F9%|-9-I-I-KF>*  B C$)y!)5)E)E)G~& _0@@3z?Q#6HH7!&&( 11,2<> ,F9| &2%A%A%CF>"	L;;G.2.Q.QR^RqRq.rL+%1%A%A%CF>"L>>J151W1WXdXzXz1{L.%1%A%A%CF>":.!262H2H2VF>";/:>:\:\]i:jF>"#67&"88>*?; #(=(I00A,0,G,G~&':;%	-') '
 ;OF>"#67 'F9 H+\->->?$($4$4\$By!$)y!%'F>"''r$   c                    d|v r>t        |      s3d}t        j                  |       | j                  j	                  |       t        |      dk  st        |      dkD  r3d}t        j                  |       | j                  j	                  |       t        j                  |      sd|v r4d}t        j                  |       | j                  j	                  |       y y )	Nr   z3Please enter valid IPV4 address for network addressr3   r2   ?   z;'network_address' should be in range of 1 to 63 characters.r6   z@Please enter valid IPV4 address or host name for network address)is_valid_ipr   r9   r   r:   r+   r   has_special_char)r!   r   r   s      r"   r   z$Host.validate_network_address_params  s    /!+o*FKGIIgKK!!g!.!#s?';b'@SGIIgKK!!g!.!!/2c_6LXGIIgKK!!g!. 7Mr$   )NN)NNNr   )__name__
__module____qualname____doc__r#   r.   rE   r]   rU   rm   rr   rv   rT   rX   r   r   r   r   r   r   r   r   r   r   r   r$   r"   r
   r
   a  s{    $E*/5b,5\5#8* IM 5,#.%5NI5V
9
5$($5<!(\(|/r$   r
   c                 N    	 t        j                  |        y# t        $ r Y yw xY w)NTF)	ipaddress
ip_address
ValueError)r   s    r"   r   r     s*    W% s    	$$c                  "   t        t        dd      t        dd      t        dd      t        ddg d      t        dd      t        ddd      t        dddd	g      t        dd      t        dddd	g      t        d
dddg      
      S )zQThis method provides parameters required for the ansible host
    module on UnityFr=   )requiredtype)
AIXzCitrix XenServerzHP-UXzIBM VIOSLinuxzMac OSSolariszVMware ESXizWindows ClientzWindows Server)r   r   choiceslist)r   r   elementsrM   rK   Tr   r   )
r   r   rG   rH   ro   rI   rJ   r   r   r   )r   r   r$   r"   r   r     s     E2e%0%e4e%01
 E6VeDe%&7&6&89 e%8"E,=,<,>? Du%x02% r$   c                  8    t               } | j                          y)z\ Create Unity host object and perform action on it
        based on user input from playbookN)r
   r   )objs    r"   mainr     s     &C  "r$   __main__)r   
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.dellemc.unity.plugins.module_utils.storage.dellr   r   
get_loggerr   r   objectr
   r   r   r   r   r   r$   r"   <module>r      s    0 @ @ZxcJG
T 5 ev" t	/6 t	/n4# zF r$   