
    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 d dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZ d Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: vsphere_file
short_description: Manage files on a vCenter datastore
description:
- Manage files on a vCenter datastore.
author:
- Dag Wieers (@dagwieers)
options:
  datacenter:
    description:
    - The datacenter on the vCenter server that holds the datastore.
    type: str
    required: true
  datastore:
    description:
    - The datastore on the vCenter server to push files to.
    type: str
    required: true
  path:
    description:
    - The file or directory on the datastore on the vCenter server.
    type: str
    required: true
    aliases: [ dest ]
  timeout:
    description:
    - The timeout in seconds for the upload to the datastore.
    type: int
    default: 10
  state:
    description:
    - The state of or the action on the provided path.
    - If V(absent), the file will be removed.
    - If V(directory), the directory will be created.
    - If V(file), more information of the (existing) file will be returned.
    - If V(touch), an empty file will be created if the path does not exist.
    type: str
    choices: [ absent, directory, file, touch ]
    default: file
notes:
- The vSphere folder API does not allow to remove directory objects.
extends_documentation_fragment:
- community.vmware.vmware.documentation
a  
- name: Create an empty file on a datastore
  community.vmware.vsphere_file:
    hostname: '{{ vhost }}'
    username: '{{ vuser }}'
    password: '{{ vpass }}'
    datacenter: DC1 Someplace
    datastore: datastore1
    path: some/remote/file
    state: touch
  delegate_to: localhost

- name: Create a directory on a datastore
  community.vmware.vsphere_file:
    hostname: '{{ vhost }}'
    username: '{{ vuser }}'
    password: '{{ vpass }}'
    datacenter: DC2 Someplace
    datastore: datastore2
    path: other/remote/file
    state: directory
  delegate_to: localhost

- name: Query a file on a datastore
  community.vmware.vsphere_file:
    hostname: '{{ vhost }}'
    username: '{{ vuser }}'
    password: '{{ vpass }}'
    datacenter: DC1 Someplace
    datastore: datastore1
    path: some/remote/file
    state: file
  delegate_to: localhost
  ignore_errors: true

- name: Delete a file on a datastore
  community.vmware.vsphere_file:
    hostname: '{{ vhost }}'
    username: '{{ vuser }}'
    password: '{{ vpass }}'
    datacenter: DC2 Someplace
    datastore: datastore2
    path: other/remote/file
    state: absent
  delegate_to: localhost

N)AnsibleModule)PY2)	HTTPError)quote	urlencode)open_url)	to_native)base_argument_specc                    dj                  t        |j                  d                  }|j                  dd      }|j	                  d      sd|z   }t        |       }|r||d<   dj                  |t        |            S )	z5 Constructs a URL path that VSphere accepts reliably z/folder/{path}/)path&z%26)dsNamedcPathz{0}?{1})formatr
   stripreplace
startswithdictr   )	datastore
datacenterr   paramss       q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/vmware/plugins/modules/vsphere_file.pyvmware_pathr   x   sz    ""djjo(>"?D ##C/J??3Tz#F%xD)F"344    c                     t               } | j                  t        t        dd      t        dd      t        dddg      t        ddg d      t        d	d
                   t        | 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      }
t        |||      }d||}t        |d |
d |      }	 t        |d|	|||d      }t        rt!        j"                          j%                         }|dk(  r+d}t'        |j(                  j                  dd             |d<   nH|d k(  rd!}n@|j*                  |d"<   ||d#<    |j                  d:d$|z  d t        |j(                        d%| |
d&k(  rs |j,                  d:d'd!i| |j.                  rd(|d"<   d)|d#<   n	 t        |d*|	|||d      }t        rt!        j"                          |j*                  |d"<   |j%                         |d#<   |d#   d+k(  r/d,|d<    |j                  d:d-d t        |j(                        d%| n4|d#   d)k7  r, |j                  d:d.|z  d t        |j(                        d%| d |d<    |j,                  d:d'di| y |
d,k(  rVr |j,                  d:d'd!i| |j.                  rd/|d"<   d0|d#<   nt        |||d1z         }d||}	 t        |d2|	|||d      }t        rt!        j"                          |j*                  |d"<   |j%                         |d#<   |d#   d0k7  r.||d3<    |j                  d:d4d t        |j(                        d%| 	 t        |d*|	|||d      }t        rt!        j"                          |j%                         }|d)k7  r5|j*                  |d"<   ||d#<   |j1                   d5j2                  d:i |        |j,                  d:d'di| y |
dk(  r=s!d&|d<   ||d#<    |j                  d:d6d7|z  i| ||d#<    |j,                  d:d'd!i| y |
d8k(  rrd|d<    |j,                  d:d'd!i| |j.                  rd/|d"<   d0|d#<   n	 t        |d2|	|||d      }t        rt!        j"                          |j*                  |d"<   |j%                         |d#<   |d#   d0k7  r, |j                  d:d9|z  d t        |j(                        d%| d|d<   d|d<    |j,                  d:d'di| y y # t        $ r}|}Y d }~2d }~wt        j                  $ r6} |j                  d:t        |      |d   t        |      d| Y d }~yd }~wt        $ r<} |j                  d:t        |      t        |      t        |      d| Y d }~d }~ww xY w# t        $ r}|}Y d }~d }~wt        j                  $ r6} |j                  d:t        |      |d   t        |      d| Y d }~4d }~wt        $ r6} |j                  d:t        |      |d   t        |      d| Y d }~qd }~ww xY w# t        $ r}|}Y d }~kd }~wt        j                  $ r6} |j                  d:t        |      |d   t        |      d| Y d }~d }~wt        $ r6} |j                  d:t        |      |d   t        |      d| Y d }~d }~ww xY w# t        $ r}|}Y d }~d }~wt        j                  $ r6} |j                  d:t        |      |d   t        |      d| Y d }~d }~wt        $ r6} |j                  d:t        |      |d   t        |      d| Y d }~d }~ww xY w# t        $ r}|}Y d }~d }~wt        j                  $ r6} |j                  d:t        |      |d   t        |      d| Y d }~bd }~wt        $ r6} |j                  d:t        |      |d   t        |      d| Y d }~d }~ww xY w);NstrT)typerequireddest)r"   r#   aliasesfile)absent	directoryr&   touch)r"   defaultchoicesint
   )r"   r*   )r   r   r   statetimeout)argument_specsupports_check_modehostnameusernamepasswordr   r   r   validate_certsr/   r.   zhttps://)r   sizer.   statusurlHEAD)methodr/   url_usernameurl_passwordr5   force_basic_authr   )msgerrnoreason   zcontent-lengthr6   i  Fr@   r7   zFailed to query for file '%s')r>   r?   headersr'   changedz
No Content   DELETEi  r(   z.Directories cannot be removed with this modulezFailed to remove '%s'Created   z/foobar.tmpPUTr8   zFailed to create temporary filez*Failed to remove temporary file ({reason})r>   z$File '%s' is absent, cannot continuer)   zFailed to touch '%s' )r   updater   r   r   getr   r   r	   socketerror	fail_jsonr   	Exceptiondirr   sys	exc_cleargetcoder,   rB   r>   	exit_json
check_modewarnr   )r0   modulehostr3   r4   r   r   r   r5   r/   r.   remote_pathr8   resultrer7   existstemp_urls                      r   mainr_      s   &(MUT2ED1utfX>v7_`%,  # F
 ==Z(D}}  ,H}}  ,H""<0J!!+.I==V$D]]&&'78Nmm	*GMMg&EiT:K +
.CF	XS"*$2TK YY[F}QYY]]+;TBCv	355x!xs<tC4Y]^_^g^gYhslrsF5U5f5+F8"F8	^S7*2,:TS  uuF8 yy{F8h3&"-w     F%U]akopqpypykz  F  E  F!S(   s%<t%C4Y]^_^g^gYhslrsv000 
+	F5U5f5(F8"F8 &iTM=QRK)-{;H	^XeW*2,:TS  uuF8 yy{F8h3& (u   v%Fd\`abajaj\kvouv	^Xh*2,:TS YY[F}#$55x #)x OHOOYRXYZ000	&&F7O%F8FY!G$!NYRXY!x11&1	'	$F7OF5U5f5(F8"F8	^S*2,:TS  uuF8 yy{F8h3&   r%;d%B$X\]^]f]fXgrkqrv w000= 
q  << VUYq\1ilUfUU XWYq\Q	!WPVWWX:  << ^   ]Yq\1iPQl]V\]] ^   ]Yq\1iPQl]V\]]^F  << ^   ]Yq\1iPQl]V\]] ^   ]Yq\1iPQl]V\]]^   << ^   ]Yq\1iPQl]V\]] ^   ]Yq\1iPQl]V\]]^J  << ^   ]Yq\1iPQl]V\]] ^   ]Yq\1iPQl]V\]]^s   V> 3Y! [> ^ `8 >	YWY%+XY"1YY!	[;*Y22[;+Z99[;+[66[;>	^\^%+]^"+^^	`5$^,,`5+_33`5?+`00`58	ca		c+bc+cc__main__)
__future__r   r   r   r"   __metaclass__DOCUMENTATIONEXAMPLESRETURNrL   rQ   ansible.module_utils.basicr   ansible.module_utils.sixr   +ansible.module_utils.six.moves.urllib.errorr	   +ansible.module_utils.six.moves.urllib.parser
   r   ansible.module_utils.urlsr   ansible.module_utils._textr   Hansible_collections.community.vmware.plugins.module_utils._argument_specr   r   r_   __name__rI   r   r   <module>rn      sg    A @,\-^
  
 4 ( A H . 0 g5A1H zF r   