
    Vh                         d dl mZmZmZ eZdZdZdZ	 d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZmZmZ d d	lmZ  G d
 de      Zd Zedk(  r e        yy# e$ r Y Cw xY w)    )absolute_importdivisionprint_functiona  
---
module: vmware_host
deprecated:
  removed_in: 7.0.0
  why: This module has been moved to the L(new vmware.vmware collection,https://forum.ansible.com/t/5880)
  alternative: Use M(vmware.vmware.esxi_host) and M(vmware.vmware.esxi_connection) instead.
short_description: Add, remove, or move an ESXi host to, from, or within vCenter
description:
- This module can be used to add, reconnect, or remove an ESXi host to or from vCenter.
- This module can also be used to move an ESXi host to a cluster or folder, or vice versa, within the same datacenter.
author:
- Joseph Callen (@jcpowermac)
- Russell Teague (@mtnbikenc)
- Maxime de Roucy (@tchernomax)
- Christian Kotte (@ckotte)
options:
  datacenter_name:
    description:
    - Name of the datacenter to add the host.
    required: true
    aliases: ['datacenter']
    type: str
  cluster_name:
    description:
    - Name of the cluster to add the host.
    - If O(folder) is not set, then this parameter is required.
    aliases: ['cluster']
    type: str
  folder:
    description:
    - Name of the folder under which host to add.
    - If O(cluster_name) is not set, then this parameter is required.
    - "For example, if there is a datacenter 'dc1' under folder called 'Site1' then, this value will be '/Site1/dc1/host'."
    - "Here 'host' is an invisible folder under VMware Web Client."
    - "Another example, if there is a nested folder structure like '/myhosts/india/pune' under
       datacenter 'dc2', then O(folder) value will be '/dc2/host/myhosts/india/pune'."
    - "Other Examples: '/Site2/dc2/Asia-Cluster/host' or '/dc3/Asia-Cluster/host'"
    aliases: ['folder_name']
    type: str
  add_connected:
    description:
    - If set to V(true), then the host should be connected as soon as it is added.
    - This parameter is ignored if not O(state=present).
    default: true
    type: bool
  esxi_hostname:
    description:
    - ESXi hostname to manage.
    required: true
    type: str
  esxi_username:
    description:
    - ESXi username.
    - Required for adding a host.
    - Optional for reconnect. If both O(esxi_username) and O(esxi_password) are used
    - Unused for removing.
    - No longer a required parameter from version 2.5.
    type: str
  esxi_password:
    description:
    - ESXi password.
    - Required for adding a host.
    - Optional for reconnect.
    - Unused for removing.
    - No longer a required parameter from version 2.5.
    type: str
  state:
    description:
    - If set to V(present), add the host if host is absent.
    - If set to V(present), update the location of the host if host already exists.
    - If set to V(absent), remove the host if host is present.
    - If set to V(absent), do nothing if host already does not exists.
    - If set to V(add_or_reconnect), add the host if it's absent else reconnect it and update the location.
    - If set to V(reconnect), then reconnect the host if it's present and update the location.
    - If set to V(disconnected), disconnect the host if the host already exists.
    default: present
    choices: ['present', 'absent', 'add_or_reconnect', 'reconnect', 'disconnected']
    type: str
  esxi_ssl_thumbprint:
    description:
    - "Specifying the hostsystem certificate's thumbprint."
    - "Use following command to get hostsystem certificate's thumbprint - "
    - "# openssl x509 -in /etc/vmware/ssl/rui.crt -fingerprint -sha1 -noout"
    default: ''
    type: str
    aliases: ['ssl_thumbprint']
  fetch_ssl_thumbprint:
    description:
    - Fetch the thumbprint of the host's SSL certificate.
    - This basically disables the host certificate verification (check if it was signed by a recognized CA).
    - Disable this option if you want to allow only hosts with valid certificates to be added to vCenter.
    - If this option is set to V(false) and the certificate can't be verified, an add or reconnect will fail.
    - Unused when O(esxi_ssl_thumbprint) is set.
    - Optional for reconnect, but only used if O(esxi_username) and O(esxi_password) are used.
    - Unused for removing.
    type: bool
    default: true
  force_connection:
    description:
    - Force the connection if the host is already being managed by another vCenter server.
    type: bool
    default: true
  reconnect_disconnected:
    description:
    - Reconnect disconnected hosts.
    - This is only used if O(state=present) and if the host already exists.
    type: bool
    default: true
extends_documentation_fragment:
- community.vmware.vmware.documentation

a  
- name: Add ESXi Host to vCenter
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    state: present
  delegate_to: localhost

- name: Add ESXi Host to vCenter under a specific folder
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    folder: '/Site2/Asia-Cluster/host'
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    state: present
    add_connected: true
  delegate_to: localhost

- name: Reconnect ESXi Host (with username/password set)
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    state: reconnect
  delegate_to: localhost

- name: Reconnect ESXi Host (with default username/password)
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    state: reconnect
  delegate_to: localhost

- name: Add ESXi Host with SSL Thumbprint to vCenter
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    esxi_ssl_thumbprint: "3C:A5:60:6F:7A:B7:C4:6C:48:28:3D:2F:A5:EC:A3:58:13:88:F6:DD"
    state: present
  delegate_to: localhost
z
result:
    description: metadata about the new host system added
    returned: on successful addition
    type: str
    sample: "Host already connected to vCenter 'vcenter01' in cluster 'cluster01'"
)vimvmodl)AnsibleModule)	to_native)PyVmomi	TaskErrorwait_for_taskfind_host_by_cluster_datacenterfind_hostsystem_by_name)base_argument_specc                   |     e Zd ZdZ f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 xZS )
VMwareHostz"Class to manage vCenter connectionc                    t         t        |   |       |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _	        |j                  d   | _
        |j                  d   | _        |j                  j                  d	d
      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        d x| _        x| _        x| _        x| _        | _        y )Nhostnamedatacenter_namecluster_namefolderesxi_hostnameesxi_usernameesxi_passwordstateesxi_ssl_thumbprint force_connectionfetch_ssl_thumbprintreconnect_disconnected)superr   __init__paramsvcenterr   r   folder_namer   r   r   r   getr   r   r   r   host_updatehostclusterr   host_parent_compute_resource)selfmodule	__class__s     p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/vmware/plugins/modules/vmware_host.pyr!   zVMwareHost.__init__   s    j$(0}}Z0%}}->?"MM.9!==2#]]?;#]]?;#]]?;]]7+
#)==#4#45JB#O  & 1 12D E$*MM$5$56L$M!&,mm&7&78P&Q#hlll49lt|ldkDDe    c                 &   | j                   | j                   | j                  d| j                  | j                  | j                  d| j                  | j                  | j                  d| j                  | j                  d| j
                  | j                  dd}	  || j                     | j                                    y# t        j                  $ r9}| j                  j                  t        |j                               Y d}~yd}~wt        j                  $ r9}| j                  j                  t        |j                               Y d}~yd}~wt        $ r/}| j                  j                  t        |             Y d}~yd}~ww xY w)zCheck the current state)presentupdateabsent)r0   r1   )r0   r2   )r2   r0   add_or_reconnect	reconnectdisconnectedmsgN)state_remove_hoststate_exit_unchangedstate_update_hoststate_add_hoststate_reconnect_hoststate_disconnected_hostr   check_host_stater   RuntimeFaultr+   	fail_jsonr	   r7   MethodFault	Exception)r*   host_statesruntime_faultmethod_faultes        r-   process_statezVMwareHost.process_state   sW     110033  4400--  4400--!  4400
  7733)
4	4<K

#D$9$9$;<>!! 	DKK!!i0A0A&B!CC   	CKK!!i0@0@&A!BB 	4KK!!il!33	4s0   %C F/DF!/EF!%FFc                    t        | j                  | j                        | _        | j                  r4| j                  rR| j                  | j                  | j                  | j                        \  | _        | _        | j                  rd}|S d}|S | j                  r| j                  | j                        | _        | j                  j                  D ]}  }|rt        |t        j                        s 	 t        |j                  d   t        j                         r6|j"                  | j                  k(  r|| _        |j                  d   | _         n | j                  rd}|S d}S d}|S # t&        $ r Y w xY w)zCheck current stater0   r1   r   r2   )r   contentr   r&   r   search_clusterr   r'   r(   r$   search_folderr   childEntity
isinstancer   ComputeResource
HostSystemnamer)   
IndexError)r*   r   childs      r-   r>   zVMwareHost.check_host_state  s_    34<<ASAST   *.*=*=d>R>RTXTeTegkgygy*z'	4<99%E, ) %E( % !!"001A1AB![[44 	!E 
5#:M:M(N !%ejjmS^^DW[WiWiIi@ED=(-

1DI!	! 99%E
  %E  E & ! !s   4AE++	E76E7c                     | j                   j                  }|j                  |      }|rt        |t        j
                        s| j                  j                  d|z         |S )zQ
            Search folder in vCenter
            Returns: folder object
        zFolder '%s' not foundr6   )rI   searchIndexFindByInventoryPathrM   r   Folderr+   r@   )r*   r$   search_index
folder_objs       r-   rK   zVMwareHost.search_folder4  sT    
 ||//!55kB
z*cjjAKK!!&=&K!Lr.   c                 H    t        | j                  | j                  |||      S )z\
            Search cluster in vCenter
            Returns: host and cluster object
        )r   r+   rI   )r*   r   r   r   s       r-   rJ   zVMwareHost.search_cluster?  s$    
 /KKm
 	
r.   c                    | j                   sd}n| j                  r4| j                   j                  j                  dk(  r| j	                          nS| j
                  rd| j                  d| j
                  d}n)| j                  rd| j                  d| j                  d}| j                  j                  dt                     y	)
zExit with status messageHost already disconnectedr5   z#Host already connected to vCenter 'z' in folder ''z' in cluster 'FchangedresultN)r&   r   runtimeconnectionStater<   r$   r#   r   r+   	exit_jsonstr)r*   r_   s     r-   r9   zVMwareHost.state_exit_unchangedH  s    0F((T-=-=-E-E-U-UYg-g%%'TXT`T`bfbrbrs""UYUaUacgctctueCK@r.   c           	      L   d}d}| j                   j                  rd| j                  z  }n| j                         }| j                  j                  d      }d}d}d}| j                  rA| j                  | j                        | _        	 | j                  j                  ||||      }nj| j6                  r^| j9                  | j:                  | j6                  | j<                        \  | _        | _         	 | j@                  jC                  ||||      }	 tG        |      \  }}d| j                  z  }| j                   jK                  ||       y# t        j                  j                  $ r2}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j                  $ r2}	| j                   j                  dt        |	      z         Y d}	~	d}	~	wt        j                  j                   $ r3}
| j                   j                  d	t        |
      z         Y d}
~
!d}
~
wt"        j                  j$                  $ r3}| j                   j                  d
t        |      z         Y d}~od}~wt        j                  j&                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt"        j                  j(                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j*                  $ r3}| j                   j                  dt        |      z         Y d}~Yd}~wt"        j                  j,                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j.                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j0                  $ r3}| j                   j                  dt        |      z         Y d}~Cd}~wt        j                  j2                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j4                  $ r3}| j                   j                  dt        |      z         Y d}~d}~ww xY w# t        j                  j                  $ r3}| j                   j                  dt        |      z         Y d}~2d}~wt        j                  j                  $ r3}	| j                   j                  dt        |	      z         Y d}	~	d}	~	wt        j                  j                   $ r3}
| j                   j                  dt        |
      z         Y d}
~
d}
~
wt        j                  j&                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt"        j                  j(                  $ r3}| j                   j                  dt        |      z         Y d}~jd}~wt        j                  j*                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j.                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  jD                  $ r3}| j                   j                  dt        |      z         Y d}~Td}~wt        j                  j0                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j2                  $ r3}| j                   j                  dt        |      z         Y d}~d}~wt        j                  j4                  $ r3}| j                   j                  dt        |      z         Y d}~>d}~ww xY w# tH        $ r@}| j                   j                  d| j                  dt        |             Y d}~ld}~ww xY w)z/Add ESXi host to a cluster of folder in vCenterTNz'Host would be connected to vCenter '%s'add_connected)speccompResSpecaddConnectedlicense&Cannot authenticate with the host : %sr6   z%An error occurred during connect : %sz:The folder already contains a host with the same name : %sz*An argument was specified incorrectly : %s@The host is already being managed by another vCenter server : %s3There are not enough licenses to add this host : %sUnable to contact the host : %sz$The folder is not a host folder : %sAThe host is running a software version that is not supported : %sz,Error during vCenter agent installation : %sz8The host is already connected to the vCenter server : %s4The host certificate could not be authenticated : %s)rf   asConnectedresourcePoolri   z;The cluster already contains a host with the same name : %szThe host is running a software version that is not supported; It may still be possible to add the host as a stand-alone host : %s4No additional hosts can be added to the cluster : %szHost connected to vCenter '%s'zFailed to add host to vCenter '' : r]   )&r+   
check_moder#   get_host_connect_specr"   r%   r$   rK   r   AddStandaloneHostr   faultInvalidLoginr@   r	   HostConnectFaultDuplicateNamer   InvalidArgumentAlreadyBeingManagedNotEnoughLicensesNoHostNotSupportedNotSupportedHostAgentInstallFailedAlreadyConnectedSSLVerifyFaultr   rJ   r   r   r'   r(   AddHost_TaskTooManyHostsr   r   rb   )r*   r^   r_   host_connect_specas_connectedesxi_licenseresource_pooltaskinvalid_loginconnect_faultduplicate_nameinvalid_argumentalready_managednot_enough_licensesno_hostnot_supportedhost_not_supportedagent_installalready_connected	ssl_faulttoo_many_hosts
task_errors                         r-   r;   zVMwareHost.state_add_hostU  sO   ;;!!>MF $ : : <;;???;LL MD"001A1AB7;;88.M%1< 9 Dn ""*.*=*=((%%&&+'	4<
4<<44.L%2L 5 Dh"/"59DLLH 	gf=m yy-- KK))DyQ^G__ *   yy11 KK))CiP]F^^ *   yy.. KK))X!.12 *   {{22 KK))H9UeKff *   yy44 KK))^!/23 *   {{44 KK))QT]^qTrr *   yy'' KK))=	'@RR *   {{// KK))BY}E]] *   yy11 KK))_!"456 *   yy33 KK))JYWdMee *   yy11 KK))VYbctYuu *   yy// KK))RU^_hUii *   yy-- KK))DyQ^G__ *   yy11 KK))CiP]F^^ *   yy.. KK))Y!.12 *   yy44 KK))^!/23 *   {{44 KK))QT]^qTrr *   yy'' KK))=	'@RR *   yy11 KK))^!"456 *  
 yy-- KK))RU^_mUnn *   yy33 KK))JYWdMee *   yy11 KK))VYbctYuu *   yy// KK))RU^_hUii *    %%EI\\S\]gShi &  sD  
E 5S; a S8-(F S8:(G'' S8(H55 S8(J S8#(K S81(L S8?(M-- S8(N;; S8(P		 S8)(Q S87(R%% S8(S33S8;a(U a&(V a4(W"" a(X00 a(Y>> a([ a,(\ a:(](( a(^66 a(` a$(aa	b##5bb#c                    | j                   rs| j                  dk(  rd| j                  | j                  | j                  j
                  d   | j                  j
                  d   | j                  j
                  d         }n| j                  }t        j                  j                         }||_	        | j                  |_
        | j                  |_        | j                  |_        | j                  |_        |S )zq
        Function to return Host connection specification
        Returns: host connection specification
        r   port
proxy_host
proxy_port)r   r   get_cert_fingerprintr   r+   r"   r   r'   ConnectSpecsslThumbprinthostNamer   userNamer   passwordr   force)r*   r   r   s      r-   ru   z VMwareHost.get_host_connect_spec  s     $$)A)AR)G 55d6H6H$++J\J\]cJd6:kk6H6H6VX\XcXcXjXjkwXxzM !44MHH002*7'%)%7%7"%)%7%7"%)%7%7""&"7"7  r.   c                     d}d}| j                   j                  rd| j                  z  }n*| j                  | j                         d| j                  z  }| j                   j                  |t        |             y)Reconnect host to vCenterTNz)Host would be reconnected to vCenter '%s'z Host reconnected to vCenter '%s'r]   )r+   rt   r#   reconnect_hostr'   rb   rc   r*   r^   r_   s      r-   r<   zVMwareHost.state_reconnect_host  sb    ;;!!@4<<OF		*7$,,FFgc&kBr.   c           	         i }t         j                  j                         |d<   d|d   _        | j                  r| j
                  r| j                         |d<   	  |j                  di |}	 t/              \  }}y# t         j                  j                  $ r2}| j                  j                  dt        |      z         Y d}~Zd}~wt         j                  j                  $ r2}| j                  j                  dt        |      z         Y d}~d}~wt         j                  j                  $ r2}| j                  j                  dt        |      z         Y d}~d}~wt         j                  j                  $ r3}| j                  j                  d	t        |      z         Y d}~Bd}~wt         j                  j"                  $ r3}| j                  j                  d
t        |      z         Y d}~d}~wt         j                  j$                  $ r3}	| j                  j                  dt        |	      z         Y d}	~	d}	~	wt         j                  j&                  $ r3}
| j                  j                  dt        |
      z         Y d}
~
,d}
~
wt         j                  j(                  $ r3}| j                  j                  dt        |      z         Y d}~zd}~wt         j                  j*                  $ r3}| j                  j                  dt        |      z         Y d}~d}~wt         j                  j,                  $ r3}| j                  j                  dt        |      z         Y d}~d}~ww xY w# t0        $ r?}| j                  j                  d| j2                  dt        |             Y d}~yd}~ww xY w)r   reconnectSpecTcnxSpecrj   r6   Nz!The host is not disconnected : %szThe host name is invalid : %sz'An error occurred during reconnect : %sz'No host can be added to this group : %srk   rl   rm   rn   ro   z%Failed to reconnect host to vCenter '	' due to  )r   rO   ReconnectSpec	syncStater   r   ru   ReconnectHost_Taskrw   rx   r+   r@   r	   InvalidStateInvalidNamery   r   r   r|   r}   r~   r   r   r   r   r#   )r*   host_objectreconnecthost_argsr   r   invalid_stateinvalid_namer   r   r   r   r   r   r   r^   r_   r   s                    r-   r   zVMwareHost.reconnect_host  s@   .1nn.J.J.L?+8<?+5$"4"4 -1,F,F,Hy)*	1;11G4FGDT	+D1OGVU yy%% 	KK!!<y?WW "   yy%% 	KK!!7)M:RR "   yy$$ 	KK!!3i6MM "   yy)) 	KK!!=	-@XX "   {{'' 	KK!!=	-@XX "   yy,, 	KK!!VYbcrYss "   {{,, 	KK!!IIViLjj "   yy 	KK!!5	'8JJ "   yy)) 	KK!!W,-. "   yy'' 	KK!!JYW`Maa "  	  	KK!!y46 "  	s   A< -N
 <N(C N&(D N3(E   N (F.. N(G<< N(I

 N*(J N8(K&& N(L44 N(NN
	O5OOc           	         d}d}| j                   j                  rd| j                  z  }n| j                  | j                        }|dk(  r| j                  | j                         	 | j                  r| j                  j                         }n&| j                  r| j                  j                         }	 t              \  }}d| j                  z  }| j                   j%                  |t'        |      	       y# t        j                  j                  $ r&}| j                   j                  |       Y d}~d}~ww xY w# t         $ r?}| j                   j                  d| j                  dt#        |             Y d}~d}~ww xY w)
zRemove host from vCenterTNz'Host would be removed from vCenter '%s'r(   r6   zHost removed from vCenter '%s'z(Failed to remove the host from vCenter 'rs   r]   )r+   rt   r#   get_parent_typer&   put_host_in_maintenance_moder$   r)   Destroy_Taskr   r'   r   rw   VimFaultr@   r   r   r	   rb   rc   )r*   r^   r_   parent_typer   	vim_faultr   s          r-   r8   zVMwareHost.state_remove_host>  sJ   ;;!!>MF ..t/?/?@Ki'11$2B2BC5##<<IIKD&&99113D"/"59DLLH
 	gc&kB 99%% 5%%)%445
  %%NRll\efp\qr &  s1   'AC9 5D? 9D<D77D<?	F5FFc                    |j                   j                  s!	 	 |j                  ddd      }t               yy# t        j                  j
                  $ r2}| j                  j                  dt        |      z         Y d}~Wd}~wt        j                  j                  $ r2}| j                  j                  dt        |      z         Y d}~d}~wt        j                  j                  $ r2}| j                  j                  dt        |      z         Y d}~d}~ww xY w# t        $ r2}| j                  j                  dt        |      z         Y d}~yd}~ww xY w)	z,Put host in maintenance mode, if not alreadyi,  TNz,The host is already in maintenance mode : %sr6   z-The maintenance mode operation timed out : %sz0The maintenance mode operation was canceled : %sz/Failed to put the host in maintenance mode : %s)r`   inMaintenanceModeEnterMaintenanceMode_Taskr   rw   r   r+   r@   r	   Timedoutr   r   )r*   r   maintenance_mode_taskr   	timed_outtask_errs         r-   r   z'VMwareHost.put_host_in_maintenance_modeY  sC   ""44,7,Q,QRUW[]a,b) 34! 5 yy-- KK))JYWdMee *   yy)) KK))KiXaNbb *   yy)) KK))NQZ[dQee *  
  %%IIV^L__ &  s\   9 D% D"(B>D%  D"#(CD%  D"0(DD% D""D% %	E .(EE c                     d}t        |j                  t        j                        rd}|S t        |j                  t        j                        rd}|S )zn
            Get the type of the parent object
            Returns: string with 'folder' or 'cluster'
        Nr(   r   )rM   parentr   ClusterComputeResourcerN   )r*   r   object_types      r-   r   zVMwareHost.get_parent_typeq  sQ    
  k((#*D*DE#K  **C,?,?@"Kr.   c                 z   d}d}d}| j                   r%| j                  j                  j                  dk(  rd}| j	                  | j                        }| j
                  ra| j                  j                  rB|s| j                  dk(  s| j                  dk(  rd| j
                  z  }nd| j
                  z  }n|s| j                  dk(  s| j                  dk(  r| j                  | j                         	 d}	 |d	k(  r1| j                  j                  | j                  j                  g      }nF|d
k(  rA| j                  | j                         | j                  j                  | j                  g      }t3        |      \  }}|s| j                  dk(  s| j                  dk(  rd| j
                  z  }nd| j
                  z  }n| j:                  r| j                  j                  rd| j:                  z  }n|d
k(  r| j                  | j                         d}	 	 | j<                  j?                  | j                  |      }t3              \  }}|s| j                  dk(  s| j                  dk(  rd| j:                  z  }nd | j:                  z  }| j                  jE                  |tG        |      !       y# t        j                  j                   $ r3}| j                  j#                  dt%        |      z         Y d}~d}~wt        j                  j&                  $ r3}| j                  j#                  dt%        |      z         Y d}~d}~wt        j                  j(                  $ r3}| j                  j#                  dt%        |      z         Y d}~&d}~wt*        j                  j,                  $ r3}	| j                  j#                  dt%        |	      z         Y d}	~	td}	~	wt        j                  j.                  $ r3}
| j                  j#                  dt%        |
      z         Y d}
~
d}
~
wt        j                  j0                  $ r3}| j                  j#                  dt%        |      z         Y d}~d}~ww xY w# t4        $ r\}|j6                  d   }| j                  j#                  d| j8                  d| j
                  dt%        |             Y d}~jd}~ww xY w# t        j                  j@                  $ r3}| j                  j#                  dt%        |      z         Y d}~ d}~wt        j                  j(                  $ r3}| j                  j#                  dt%        |      z         Y d}~Nd}~wt*        j                  jB                  $ r3}| j                  j#                  dt%        |      z         Y d}~d}~ww xY w# t4        $ rO}|j6                  d   }| j                  j#                  d| j:                  dt%        |             Y d}~d}~ww xY w)"z1Move host to a cluster or a folder, or vice versaTNFr5   r3   r4   z2Host would be reconnected and moved to folder '%s'z"Host would be moved to folder '%s'r   r(   zBThe folder already contains an object with the specified name : %sr6   z8The parent of this folder is in the list of objects : %szFailed to move host, this can be due to either of following : 1. The host is not part of the same datacenter, 2. The host is not in maintenance mode : %sz+The target folder is not a host folder : %sz.The host is configured as a failover host : %sz_The host's virtual machines are already registered to a host in the destination datacenter : %sr   zFailed to move host z to folder z due to z)Host reconnected and moved to folder '%s'zHost moved to folder '%s'z#Host would be moved to cluster '%s')r'   rq   rr   zIThe host is already part of a cluster and is not in maintenance mode : %szFailed to move host, this can be due to either of following : 1. The host is is not a part of the same datacenter as the cluster, 2. The source and destination clusters are the same : %sz Failed to move host to cluster 'z' due to : z*Host reconnected and moved to cluster '%s'zHost moved to cluster '%s')r^   r7   )$r   r&   r`   ra   r   r$   r+   rt   r   r   r   MoveIntoFolder_Taskr   r   r   rw   rz   r@   r	   InvalidFolderr   r   r   !DisallowedOperationOnFailoverHostVmAlreadyExistsInDatacenterr   r   argsr   r   r(   MoveHostInto_Taskr   r{   rb   rc   )r*   r^   r_   r4   r   r   r   invalid_folderr   r   failover_hostalready_existstask_error_exceptionr   r   r   r   s                    r-   r:   zVMwareHost.state_update_host  s   	 &&4+;+;+C+C+S+SWe+eI **4+;+;<{{%%

.@ @DJJR]D]QTXTdTddFADDTDTTF 

.@ @DJJR]D]''(8(89/D&&(2#';;#B#BDDTDTD[D[C\#]D(I5 ==d>N>NO#';;#B#BDDTDTCU#VD@ '4D&9OGV 

.@ @DJJR]D]H4K[K[[F84;K;KKF{{%%>ARARR)+55d6F6FG $#||==!%!1!1  >  & '4D&9OGV 

.@ @DJJR]D]IDL]L]]F9D<M<MMFg3v;?g 9922 -- d%n5!6 .   9922 -- Z%n5!6 .   9911 --!{%m4!5 .  
 !;;33 -- M%m4!5 .   99FF -- P%m4!5 .   99@@ --!>@I.@Y!Z .   ! !5!:!:1!=JKK))++T-=-=y?TV *  . 9911 -- VYbcqYr r .   9911 -- k%m4!5 .   !;;66 --!X &&67!8 .   ! !5!:!:1!=JKK))**Ij,AC *  s   4R 7A<J2 3R 'S3 >W" 2R(K=7R = R(MR  R+(NR  R9(O'!R ' R(P5/R 5 R(R=R RR 	S0AS++S03W(T>8W" > W(VW"  W,(WW" WW" "	X:+AX55X:c                    d}d}| j                   j                  r8| j                  j                  j                  dk(  rd}d}nbd| j
                  z  }nR| j                  j                  j                  dk(  rd}d}n*| j                  | j                         d| j
                  z  }| j                   j                  |t        |             y)	Disconnect host to vCenterTNr5   r[   Fz1Host would be disconnected host from vCenter '%s'z#Host disconnected from vCenter '%s'r]   )	r+   rt   r'   r`   ra   r#   disconnect_hostrb   r	   r   s      r-   r=   z"VMwareHost.state_disconnected_host  s    ;;!!yy  00NB4Lt||[yy  00NB4$$TYY/>Mgi6GHr.   c           	      V   	 |j                         }	 t              \  }}y# t        $ r2}| j                  j                  dt	        |      z         Y d}~Fd}~ww xY w# t        $ r?}| j                  j                  d| j                  dt	        |             Y d}~yd}~ww xY w)r   z*Failed to disconnect host from vCenter: %sr6   Nz(Failed to disconnect host from vCenter 'r   )DisconnectHost_TaskrB   r+   r@   r	   r   r   r#   )r*   r   r   rF   r^   r_   r   s          r-   r   zVMwareHost.disconnect_host	  s    	c224D	+D1OGV	  	cKK!!&RU^_`Ua&a!bb	c
  	KK!!\\9Z#8: "  	s+   " A  	A(AA 	B()5B##B()__name__
__module____qualname____doc__r!   rG   r>   rK   rJ   r9   r;   ru   r<   r   r8   r   r   r:   r=   r   __classcell__)r,   s   @r-   r   r      sc    ,m #4J!F	
AJ>X!(
C;zC60t@lI(r.   r   c                     t               } | j                  t        dddg      t        ddg      t        dd      t        d      t        dd	      t        dd
dg      t        dd      t        dg dd      t        ddg      t        dd      t        dd      t        dd             t        | dddddggddddgggddggddgg      }t	        |      }|j                          y)Mainrc   T
datacenter)typerequiredaliasesr(   )r   r   )r   r   )r   )r   no_logr   ssl_thumbprint)r   defaultr   bool)r   r   r0   )r0   r2   r3   r4   r5   )r   choicesr   r$   )r   r   r   r   r   r   r   r   r   re   r   r   r   r   r   r3   r   r   )argument_specsupports_check_moderequired_ifrequired_one_ofmutually_exclusiveN)r   r1   dictr   r   rG   )r   r+   vmware_hosts      r-   mainr     s   &(M%$Ouyk:5&d3 eRBRAST!vt<9a 85648#>  " # i/?!CD(?O*LM

 X&
 X&
F V$Kr.   __main__N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNpyVmomir   r   ImportErroransible.module_utils.basicr   ansible.module_utils._textr	   @ansible_collections.community.vmware.plugins.module_utils.vmwarer
   r   r   r   r   Hansible_collections.community.vmware.plugins.module_utils._argument_specr   r   r   r   r   r.   r-   <module>r      s    A @pdAF
	" 5 0  h} }@$ N zF g  		s   A A A 