
    Vh                          d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dl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 d d	lmZ d
 Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona$  
---
module: vsphere_copy
short_description: Copy a file to a VMware datastore
description:
    - Upload files to a VMware datastore through a vCenter REST API.
author:
- Dag Wieers (@dagwieers)
options:
  src:
    description:
      - The file to push to vCenter.
    required: true
    type: str
    aliases: [ name ]
  datacenter:
    description:
      - The datacenter on the vCenter server that holds the datastore.
    required: false
    type: str
  datastore:
    description:
      - The datastore to push files to.
    required: true
    type: str
  path:
    description:
      - The file to push to the datastore.
    required: true
    type: str
    aliases: [ dest ]
  timeout:
    description:
      - The timeout in seconds for the upload to the datastore.
    default: 10
    type: int
  diskformat:
    version_added: 4.2.0
    description:
      - Optional argument - Set a diskformat for certain uploads like stream optimized VMDKs
      - There is no official documentation, but it looks like V(StreamVmdk) needs to be set for stream optimized VMDKs that are uploaded to vSAN storage
      - Setting this for non-VMDK files might lead to undefined behavior and is not supported.
    choices: ["StreamVmdk"]
    type: str

notes:
  - "This module ought to be run from a system that can access the vCenter or the ESXi directly and has the file to transfer.
    It can be the normal remote target or you can change it either by using C(transport: local) or using C(delegate_to)."
extends_documentation_fragment:
- community.vmware.vmware.documentation

a  
- name: Copy file to datastore using delegate_to
  community.vmware.vsphere_copy:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    src: /some/local/file
    datacenter: DC1 Someplace
    datastore: datastore1
    path: some/remote/file
  delegate_to: localhost

- name: Copy file to datastore when datacenter is inside folder called devel
  community.vmware.vsphere_copy:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    src: /some/local/file
    datacenter: devel/DC1
    datastore: datastore1
    path: some/remote/file
  delegate_to: localhost

- name: Copy file to datastore using other_system
  community.vmware.vsphere_copy:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    src: /other/local/file
    datacenter: DC2 Someplace
    datastore: datastore2
    path: other/remote/file
  delegate_to: other_system

- name: Copy file to datastore using other_system
  community.vmware.vsphere_copy:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    src: /other/local/streamOptimized.vmdk
    datacenter: DC2 Someplace
    datastore: datastore2
    path: disk_imports/streamOptimized.vmdk
    timeout: 360
    diskformat: StreamVmdk
  delegate_to: other_system
N)AnsibleModule)	urlencodequote)	to_native)open_url)base_argument_specc                     dt        |j                  d            z  }|j                  d      sd|z   }t        |       }|r|j	                  dd      }||d<   |r||d<   t        |      }|d|S )	z5 Constructs a URL path that vSphere accepts reliably z
/folder/%s/)dsName&z%26dcPath
diskFormat?)r   lstrip
startswithdictreplacer   )	datastore
datacenterpath
diskformatparamss        q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/vmware/plugins/modules/vsphere_copy.pyvmware_pathr      s|    %C 011D ??3Tz#F''U3
%x)|vFF##    c                  	   t               } | j                  t        t        ddg      t        d      t        d      t        ddgd      t        d	d
      t        dddg                   t        | d      }|j                  d   }|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        j                  |j                         t        j                  |      j                  dk(  rd}nSt!        j                   j#                         dt         j$                        }t        j                  |j                         t'        ||||	      }t)        |||g      s|j                  d        d!||}d"t+        t-        |            d#}d }	 t/        |||d$||||
d%	      }|s|j                  d*|d d d +       |jI                         }d,|cxk  rd-k  r#n n |jK                  d||jL                  |.       y |jN                  j                  d/d       }|jN                  j                  d0d      jQ                         d1k(  rd2}nd}|j                  d*d ||jL                  |t        |jN                        ||3       y # t        $ r)}|j                  dt        |      z         Y d }~d }~ww xY w# t0        j2                  $ r}t5        |j6                  t8              rmt-        |j6                        dkD  r9|d   t:        j<                  k(  r}|j                  d&d |d   t        |      |'       nZ|j                  t        |             n>|j                  t+        |      d |d   t+        |      |t?        j@                         (       Y d }~d }~wt        $ rw}d)}	 t5        |d   tB              r|d   }n# tD        tF        f$ r Y nw xY w|j                  t        |      d |t        |      |t?        j@                         (       Y d }~ld }~ww xY w)4NTname)requiredaliasesF)r!   deststr)r!   r"   type
   int)defaultr%   
StreamVmdk)r!   r%   choices)srcr   r   r   timeoutr   )argument_specsupports_check_modehostnameusernamepasswordr+   r   r   r   r   validate_certsr,   rbzFailed to open src file %s)msgr    )accesszDOne of following parameter is missing - hostname, username, passwordzhttps://zapplication/octet-stream)zContent-TypezContent-LengthPUT)dataheadersmethodr,   url_usernameurl_passwordr2   force_basic_authz'Failed to upload, image probably in use)r4   statuserrnoreasonurl)r4   r>   r?   r@   rA   	exceptionzFailed to upload)r4   rA   r?   r>   r@      i,  )changedr>   r@   rA   zcontent-lengthztransfer-encodingchunked   )r4   r?   r>   r@   lengthr9   rF   rA   ))r   updater   r   r   getopenatexitregisterclose	Exception	fail_jsonr	   osstatst_sizemmapfilenoACCESS_READr   allr$   lenr
   socketerror
isinstanceargstupler?   
ECONNRESET	traceback
format_excr'   KeyError	TypeErrorgetcode	exit_jsonr4   r9   lower)r-   moduler/   r0   r1   r+   r   r   r   r   r2   r,   fder8   remote_pathrA   r9   r
error_coder>   rH   rF   s                          r   mainrl      so   &(M$1'%4&>Re,U\NKM  #!F }}Z(H}}Z(H}}  ,H
--

E
"C""<0J!!+.I==V$D""<0J]]&&'78Nmm	*GJ#t_! 
wws|q yya0@0@A

#iT:FK(H-.cd$k
2C 3c$i.G
 	AYStWUG"*R`&*,0 /S#D 	 	?YY[F
fsfQUUL/699==,b1779YFGG/tFSTSXSXagquvwvv  rA  KR  X[  	\w  J9IaLHIIJ2 << 
Haffe$166{QQ45+++$$)R[_ghijgkt}~  uA  GJ$  K  Yq\ 2QAaDQ!$	0D0D0F  H Y
	!A$$qT
)$ 		Yq\$j )!#AUAUAW 	 	Y 	YYsa   4+M &N
 	NNN
S+CQ((S+4S&7RS&R"S&!R"">S&&S+__main__)
__future__r   r   r   r%   __metaclass__DOCUMENTATIONEXAMPLESrL   r?   rT   rQ   rY   r_   ansible.module_utils.basicr   +ansible.module_utils.six.moves.urllib.parser   r   ansible.module_utils._textr	   ansible.module_utils.urlsr
   Hansible_collections.community.vmware.plugins.module_utils._argument_specr   r   rl   __name__ r   r   <module>ry      sg    A @3j.`    	   4 H 0 . g$"Z\z zF r   