HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/vsphere.cpython-310.pyc
o

�N�g���@s�dZddlZddlZddlZddlmZddlZddlm	m
Z
ddlZddlZddl
ZddlZddlZddlmZmZddlmZmZmZddlmZmZddlmZddlmZmZmZm Z m!Z!m"Z"m#Z#m$Z$dd	l%m&Z&m'Z'dd
l(m)Z)ddl*m+Z+e�,e-�Z.zddl/Z/dZ0Wne1y�d
Z0Ynwz'ddl2m3Z3m4Z4m5Z5m6Z6de3j7vr�ej8dkr�ej8dkr�e.�9d�e1��dZ:Wne1y�d
Z:Ynwz3ddl;m<Z<m=Z=m>Z>m?Z?m@Z@ddlAmBZBmCZCmDZDmEZEmFZFddlGmHZHddlImJZJeBeCeDeEeFfZKdZLWne1�yd
ZLYnwej	jM�Nd�ZOeO�rdZPnd
ZPdZQgd�ZRdd�ZSdd�ZTeTdd��ZUd d!�ZVd"d#�ZWd$d%�ZXe&e:�eWd&d'd(d)d*�eT�d�d+d,����ZYe&e:�eWd&d'd(d)d*�eTd-d.����ZZe&eP�eT							�d�d/d0���Z[e&eP�eT	�d�d1d2���Z\e&eP�eT				�d�d3d4���Z]e&eP�eT			5	6		�d�d7d8���Z^e&eP�eT	�d�d9d:���Z_e&eP�eT				�d�d;d<���Z`e&eP�eT	�d�d=d>���Zae&eP�eT						�d�d?d@���Zbe&eP�eT	�d�dAdB���Zce&eP�eT					�d�dCdD���Zde'dE�eT					�d�dFdG���Zee'dE�eT	�d�dHdI���Zfe&e:�e'dE�eT	�d�dJdK����Zge&e:�e'dE�eT	�d�dLdM����Zhe&e:�e'dE�eT				�d�dNdO����Zie&e:�e'dE�eT				�d�dPdQ����Zje&e:�e'dE�eT				�d�dRdS����Zke&e:�e'dE�eT				�d�dTdU����Zle&e:�e'dE�eT				�d�dVdW����Zme&e:�eWd&d'd(d)d*�eXeT�d�dXdY�����Zne&e:�e'dE�eT			�d�dZd[����Zoe&e:�e'dE�eT	�d�d\d]����Zpe&e:�e'dE�eT�d�d^d_����Zqe&e:�e'dE�eT	�d�d`da����Zre&e:�e'dE�eT	�d�dbdc����Zse&e:�e'dE�eT�d�ddde����Zte&e:�e'dE�eT	�d�dfdg����Zue&e:�e'dE�eT�d�dhdi����Zve&e:�e'dE�eT�d�djdk����Zwe&e:�e'dE�eT�d�dldm����Zxe&e:�e'dE�eT�d�dndo����Zye&e:�e'dE�eT�d�dpdq����Zze&e:�e'dE�eT	�d�drds����Z{e&e:�e'dE�eT	�d�dtdu����Z|e&e:�e'dE�eT				�d�dvdw����Z}e&e:�e'dE�eT				�d�dxdy����Z~e&e:�e'dE�eT				�d�dzd{����Ze&e:�e'dE�eT				�d�d|d}����Z�e&e:�e'dE�eT				�d�d~d����Z�e&e:�e'dE�eT	�d�d�d�����Z�e&e:�e'dE�eT	�d�d�d�����Z�e&e:�e'dE�eT	�d�d�d�����Z�e&e:�e'dE�eT				5	�d�d�d�����Z�e&e:�e'dE�eT	�d�d�d�����Z�e&e:�e'dE�eT	�d�d�d�����Z�e&e:�e'dE�eT	�d�d�d�����Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�e&e:�eWd(d'�eXeT�d�d�d������Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�e&e:�eWd(d'�eXeT�d�d�d������Z�e&e:�eWd(d'�eXeT�d�d�d������Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�e&e:�eWd(d'�eXeT�d�d�d������Z�e&e:�eWd(d'�eXeT�d�d�d������Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�e&e:�eWd(d'�eXeT�d�d�d������Z�e&e:�eWd(d'�eXeT�d�d�d������Z�e&e:�eWd(d'�eXeT�d�d�dÄ����Z�d�dńZ�e&e:�eWd(d)�eXeT�d�d�dDŽ����Z�e&e:�eWd(d)�eXeT�d�d�dɄ����Z�d�d˄Z�e&e:�eWd(d)�eXeT�d�d�d̈́����Z�d�dτZ�e&e:�eWd(d)�eXeT�d�d�dф����Z�e&e:�eWd(d)�eXeT�d�d�dӄ����Z�e&e:�eWd'd(d)�eXeT�d�d�dՄ����Z�e&e:�eWd'd(d)�eXeT	�d�d�dׄ����Z�e&e:�eWd(d'd)d*�eXeT�d�d�dل����Z�e&e:�eWd(d)�eXeT�d�d�dۄ����Z�d�d݄Z�e&e:�eWd'd(�eXeT�d�d�d߄����Z��d�d�d�Z�e&e:�e&e0�eWd'd(�eXeT�d�d�d������Z�e&e:�e&e0�eWd'd(�eXeT�d�d�d������Z�e&e:�eWd&d'd(�eXeT				�d�d�d�����Z�e&e:�e&e0�eWd&�eXeT		�d�d�d������Z�e&e:�eWd&d'd(�eXeT�d�d�d�����Z�e&e:�eWd&d'd(�eXeT�d�d�d�����Z�e&e:�eWd'd(�eXeT�d�d�d�����Z�e&e:�eWd'd(�eXeT�d�d�d�����Z�d�d�Z�d�d��Z�e&e:�e&e0�eWd'd(�eXeT	�d�d�d�������Z�e&e:�e&e0�eWd'd(�eXeT		�d�d�d�������Z�e&e:�eWd&d'd(d)�eXeT	�d�d�d������Z�e&e:�eWd&�eXeT�d�d�d������Z�e&e:�eWd&�eXeT�d�d�d������Z�e&e:�eWd&�eXeT�d��d�d�����Z�e&e:�eWd&�eXeT�d��d�d�����Z�e&e:�e&e0�eWd&�eXeT	�d��d�d������Z�e&e:�e&e0�eWd&�eXeT	�d��d�d������Z�e&e:�e&e0�eWd&�eXeT			�d��d�d	������Z�e&e:�e&e0�eWd&�eXeT�d��d
�d������Z�e&e:�eWd&�eXeT�d��d�d
�����Z�e&e:�e&e0�eWd&�eXeT	�d��d�d������Zɐd�d�Zʐd�d�Zːd�d�Z̐d�d�Z͐d�d�Zΐd��d�d�Zϐd�d�ZАd�d�Zѐd �d!�ZҐd"�d#�ZӐd$�d%�Z�				�d��d&�d'�Z�					�d��d(�d)�Z�e&e:�e'dE�eT				�d��d*�d+����Z�e&e:�eWd&d'd(d)��d,�d-���Zؐd.�d/�Zِd0�d1�Zڐd2�d3�Z�e&e:�eXeT					�d��d4�d5����Z�e&e:�eXeT�d��d6�d7����Zݐd��d9�d:�Zސd;�d<�Zߐd=�d>�Z�e&e:�eWd*d'd(�eXeT�d��d?�d@�����Z�d��dA�dB�Z�e&e:�eWd*d'd(�eXeT�d��dC�dD�����Z�dE�dF�Z�e&e:�eWd*d'd(�eXeT	�d��dG�dH�����Z�dI�dJ�Z�		�	K					�d��dL�dM�Z�d��dO�dP�Z�dQ�dR�Z�	�	8			�d��dS�dT�Z�dU�dV�Z�dW�dX�Z�d��dY�dZ�Z�d[�d\�Z�d��d]�d^�Z�			�	_	�d��d`�da�Z�db�dc�Z�d��dd�de�Z�d��df�dg�Z�dh�di�Z�d��dj�dk�Z��dl�dm�Z��d��dn�do�Z��dp�dq�Z��dr�ds�Z��dt�du�Z�eT�dv�dw��Z�eXeT�d��dx�dy���Z��dz�d{�Z��d|�d}�Z��d~�d�Z��d��d���Z�d��d��d���Z�d��d���Z�d��d��d���Ze&e:eL�eWd)�eXeT						�d��d��d�������Ze&e:eL�eWd)�eXeT						�d��d��d�������Ze&e:eL�eWd)�eXeT�	�						�d��d��d�������Ze&e:eL�eWd)�eXeT�	�						�d��d��d�������Ze&e:eL�eWd)�eXeT						�d��d��d�������Ze&e:eL�eWd)�eXeT						�d��d��d�������Z	e&e:eL�eWd)�eXeT						�d��d��d�������Z
e&e:eL�eWd)�eXeT						�d��d��d�������Ze&e:�eWd*d'd(�eXeT						�d��d��d�������Ze&e:�eWd*d'd(�eXeT														�d��d��d�������Z
e&e:�eWd*d'd(�eXeT�d��d��d�������Ze&e:�eWd*d'd(�eXeT�d��d��d�������Ze&e:�eWd*d'd(�eXeT�d��d��d�������Z�d��d��d���Ze&e:�eWd*d'd(�eXeT�dd��d�������Ze&e:�eWd*d'd(�eXeT	�dd��d�������ZdS(�aN
Manage VMware vCenter servers and ESXi hosts.

.. versionadded:: 2015.8.4

:codeauthor: Alexandru Bleotu <alexandru.bleotu@morganstaley.com>

Dependencies
============

- pyVmomi Python Module
- ESXCLI

pyVmomi
-------

PyVmomi can be installed via pip:

.. code-block:: bash

    pip install pyVmomi

.. note::

    Version 6.0 of pyVmomi has some problems with SSL error handling on certain
    versions of Python. If using version 6.0 of pyVmomi, Python 2.7.9,
    or newer must be present. This is due to an upstream dependency
    in pyVmomi 6.0 that is not supported in Python versions 2.7 to 2.7.8. If the
    version of Python is not in the supported range, you will need to install an
    earlier version of pyVmomi. See `Issue #29537`_ for more information.

.. _Issue #29537: https://github.com/saltstack/salt/issues/29537

Based on the note above, to install an earlier version of pyVmomi than the
version currently listed in PyPi, run the following:

.. code-block:: bash

    pip install pyVmomi==5.5.0.2014.1.1

The 5.5.0.2014.1.1 is a known stable version that this original vSphere Execution
Module was developed against.

vSphere Automation SDK
----------------------

vSphere Automation SDK can be installed via pip:

.. code-block:: bash

    pip install --upgrade pip setuptools
    pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git

.. note::

    The SDK also requires OpenSSL 1.0.1+ if you want to connect to vSphere 6.5+ in order to support
    TLS1.1 & 1.2.

    In order to use the tagging functions in this module, vSphere Automation SDK is necessary to
    install.

The module is currently in version 1.0.3
(as of 8/26/2019)

ESXCLI
------

Currently, about a third of the functions used in the vSphere Execution Module require
the ESXCLI package be installed on the machine running the Proxy Minion process.

The ESXCLI package is also referred to as the VMware vSphere CLI, or vCLI. VMware
provides vCLI package installation instructions for `vSphere 5.5`_ and
`vSphere 6.0`_.

.. _vSphere 5.5: http://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.vcli.getstart.doc/cli_install.4.2.html
.. _vSphere 6.0: http://pubs.vmware.com/vsphere-60/index.jsp#com.vmware.vcli.getstart.doc/cli_install.4.2.html

Once all of the required dependencies are in place and the vCLI package is
installed, you can check to see if you can connect to your ESXi host or vCenter
server by running the following command:

.. code-block:: bash

    esxcli -s <host-location> -u <username> -p <password> system syslog config get

If the connection was successful, ESXCLI was successfully installed on your system.
You should see output related to the ESXi host's syslog configuration.

.. note::

    Be aware that some functionality in this execution module may depend on the
    type of license attached to a vCenter Server or ESXi host(s).

    For example, certain services are only available to manipulate service state
    or policies with a VMware vSphere Enterprise or Enterprise Plus license, while
    others are available with a Standard license. The ``ntpd`` service is restricted
    to an Enterprise Plus license, while ``ssh`` is available via the Standard
    license.

    Please see the `vSphere Comparison`_ page for more information.

.. _vSphere Comparison: https://www.vmware.com/products/vsphere/compare


About
=====

This execution module was designed to be able to handle connections both to a
vCenter Server, as well as to an ESXi host. It utilizes the pyVmomi Python
library and the ESXCLI package to run remote execution functions against either
the defined vCenter server or the ESXi host.

Whether or not the function runs against a vCenter Server or an ESXi host depends
entirely upon the arguments passed into the function. Each function requires a
``host`` location, ``username``, and ``password``. If the credentials provided
apply to a vCenter Server, then the function will be run against the vCenter
Server. For example, when listing hosts using vCenter credentials, you'll get a
list of hosts associated with that vCenter Server:

.. code-block:: bash

    # salt my-minion vsphere.list_hosts <vcenter-ip> <vcenter-user> <vcenter-password>
    my-minion:
    - esxi-1.example.com
    - esxi-2.example.com

However, some functions should be used against ESXi hosts, not vCenter Servers.
Functionality such as getting a host's coredump network configuration should be
performed against a host and not a vCenter server. If the authentication
information you're using is against a vCenter server and not an ESXi host, you
can provide the host name that is associated with the vCenter server in the
command, as a list, using the ``host_names`` or ``esxi_host`` kwarg. For
example:

.. code-block:: bash

    # salt my-minion vsphere.get_coredump_network_config <vcenter-ip> <vcenter-user>         <vcenter-password> esxi_hosts='[esxi-1.example.com, esxi-2.example.com]'
    my-minion:
    ----------
        esxi-1.example.com:
            ----------
            Coredump Config:
                ----------
                enabled:
                    False
        esxi-2.example.com:
            ----------
            Coredump Config:
                ----------
                enabled:
                    True
                host_vnic:
                    vmk0
                ip:
                    coredump-location.example.com
                port:
                    6500

You can also use these functions against an ESXi host directly by establishing a
connection to an ESXi host using the host's location, username, and password. If ESXi
connection credentials are used instead of vCenter credentials, the ``host_names`` and
``esxi_hosts`` arguments are not needed.

.. code-block:: bash

    # salt my-minion vsphere.get_coredump_network_config esxi-1.example.com root <host-password>
    local:
    ----------
        10.4.28.150:
            ----------
            Coredump Config:
                ----------
                enabled:
                    True
                host_vnic:
                    vmk0
                ip:
                    coredump-location.example.com
                port:
                    6500
�N��wraps)�ESXClusterConfigSchema�ESXClusterEntitySchema)�DiskGroupsDiskIdSchema�SimpleHostCacheSchema�VmfsDatastoreSchema)�ESXVirtualMachineDeleteSchema�!ESXVirtualMachineUnregisterSchema)�VCenterEntitySchema)�ArgumentValueError�CommandExecutionError�InvalidConfigError�InvalidEntityError�VMwareApiError�VMwareObjectExistsError�VMwareObjectRetrievalError�VMwareSaltError)�depends�ignores_kwargs)�recursive_diff)�	list_diffTF)�VmomiSupport�pbm�vim�vmodlz	vim25/6.0)��)rr�	zFpyVmomi not loaded: Incompatible versions of Python. See Issue #29537.)�Category�
CategoryModel�Tag�TagAssociation�TagModel)�
AlreadyExists�InvalidArgument�NotFound�Unauthenticated�Unauthorized)�	DynamicID)�Cluster�esxcliZvsphere)�esxi�
esxcluster�
esxdatacenter�vcenter�esxvmcCstS�N)�__virtualname__�r3r3�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/vsphere.py�__virtual__*sr5cst���fdd��}|S)z>
    Decorator wrapper to warn about azurearm deprecation
    cs6tjjjddtd��|itjjjdi|����}|S)NZArgonz�The 'vsphere' functionality in Salt has been deprecated and its functionality will be removed in version 3008 in favor of the saltext.vmware Salt Extension. (https://github.com/saltstack/salt-ext-modules-vmware))�categoryr3)�salt�utilsZversionsZ
warn_until�
FutureWarning�args�clean_kwargs)r:�kwargs�ret��functionr3r4�wrapped3s�z%_deprecation_message.<locals>.wrappedr)r?r@r3r>r4�_deprecation_message.srAcCs@t�di��d�rtddSt�di��d�rtddSdS)z�
    Returns the proxy type retrieved either from the pillar of from the proxy
    minion's config.  Returns ``<undefined>`` otherwise.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.get_proxy_type
    �proxy�	proxytypez<undefined>)Z
__pillar__�getZ__opts__r3r3r3r4�get_proxy_typeCs
rEc
Cs�t�}|dkr
td�}n0|dkrtd�}n&|dkr!td�}n|dkr+td�}n|d	kr5td
�}ntd|�d���d|vrF|�d�n|�d
�|�d�|�d�|�d�|�d�|�d�|�d�|�d�g}d|vrt|�|�d��t|�S)zG
    Returns the connection details of the following proxies: esxi
    r,�esxi.get_detailsr-�esxcluster.get_detailsr.�esxdatacenter.get_detailsr/�vcenter.get_detailsr0zesxvm.get_details�'z' proxy is not supported�host�username�password�protocol�port�	mechanism�	principal�domain�
verify_ssl)rE�__salt__r
rD�append�tuple)rC�detailsZ
proxy_detailsr3r3r4�_get_proxy_connection_detailsVs0�
rXcs�fdd�}|S)z�
    Decorator to specify which proxy types are supported by a function

    proxy_types:
        Arbitrary list of strings with the supported types of proxies
    cst����fdd��}|S)Ncs>t�}|�vrtd�|�j����|itjjjdi|����S)Nz*'{}' proxy is not supported by function {}r3)rEr
�format�__name__r7r8r:r;)r:r<�
proxy_type)�fn�proxy_typesr3r4�__supports_proxies_s��zJ_supports_proxies.<locals>._supports_proxies_.<locals>.__supports_proxies_r)r\r^�r])r\r4�_supports_proxies_~s
z-_supports_proxies.<locals>._supports_proxies_r3)r]r`r3r_r4�_supports_proxiesvsracsL�j�tjj���\�}���dur�ng�t�������fdd��}|S)a�
    Decorator that connects to a target system (vCenter or ESXi host) using the
    proxy details and passes the connection (vim.ServiceInstance) to
    the decorated function.

    Supported proxies: esxi, esxcluster, esxdatacenter.

    Notes:
        1. The decorated function must have a ``service_instance`` parameter
        or a ``**kwarg`` type argument (name of argument is not important);
        2. If the ``service_instance`` parameter is already defined, the value
        is passed through to the decorated function;
        3. If the ``service_instance`` parameter in not defined, the
        connection is created using the proxy details and the service instance
        is returned.
    Nc
s$d�vr
�s
td�����t�}d}d�vrP��d�}|t��t��krOt|�|kr?||s>tjjj|�}t	|�}|||<n!|�
d�sOtjjj|�}||d<n|�
d�s`tjjj|�}||d<z�|itjjjdi|����}|rytjj�
|�|WSty�}z
|r�tjj�
|��d}~ww)N�service_instancezQFunction {} must have either a 'service_instance', or a '**kwargs' type parameterr3)r
rYrX�index�lenr7r8�vmware�get_service_instance�listrDr:r;�
disconnect�	Exception)r:r<�connection_detailsZlocal_service_instance�idxr=�e�Z	arg_namesZdefault_valuesr\Zfn_nameZkwargs_namer3r4�!_gets_service_instance_via_proxy_�sP�
��
��
���zK_gets_service_instance_via_proxy.<locals>._gets_service_instance_via_proxy_)rZr7r8r:Zget_function_argspecr)r\Z	args_namernr3rmr4� _gets_service_instance_via_proxy�s�?ror,r-r.r/r0cCst�}tjjj|�S)a4
    Returns a service instance to the proxied endpoint (vCenter/ESXi host).

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    Note:
        Should be used by state functions not invoked directly.

    CLI Example:

        See note above
    )rXr7r8rerf)rbrjr3r3r4�get_service_instance_via_proxy�s�rpcCstjj�|�dS)z�
    Disconnects from a vCenter or ESXi host

    Note:
        Should be used by state functions, not invoked directly.

    service_instance
        Service instance (vim.ServiceInstance)

    CLI Example:

        See note above.
    T)r7r8rerh�rbr3r3r4rhsrhcCs�i}|r=t|t�s
td��|D]+}	tjjj|||||||	|d�}
|
ddkr3|�|	d|
�d�ii�q|�|	|
i�q|Stjjj|||||||d�}
|
ddkr_|�|d|
�d�ii�|S|�||
i�|S)a�
    Run an ESXCLI command directly on the host or list of hosts.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    cmd_str
        The ESXCLI command to run. Note: This should not include the ``-s``, ``-u``,
        ``-p``, ``-h``, ``--protocol``, or ``--portnumber`` arguments that are
        frequently passed when using a bare ESXCLI command from the command line.
        Those arguments are handled by this function via the other args and kwargs.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.esxcli_cmd my.esxi.host root bad-password             'system coredump network get'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.esxcli_cmd my.vcenter.location root bad-password             'system coredump network get' esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    �'esxi_hosts' must be a list.�rNrO�	esxi_host�	credstore�retcoder�Error�stdout�rNrOru�	�
isinstancergr
r7r8rer+�updaterD)Zcmd_strrKrLrMrNrO�
esxi_hostsrur=rt�responser3r3r4�
esxcli_cmdsB9
�
��	�rcCs�d}i}|rCt|t�std��|D]/}	tjjj|||||||	|d�}
|
ddkr5|�|	d|
�d�ii�q|�|	dt	|
�ii�q|Stjjj|||||||d	�}
|
ddkre|�|d|
�d�ii�|St	|
�}|�|d|ii�|S)
ah
    Retrieve information on ESXi or vCenter network dump collection and
    format it into a dictionary.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    :return: A dictionary with the network configuration, or, if getting
             the network config failed, a an error message retrieved from the
             standard cmd.run_all dictionary, per host.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.get_coredump_network_config my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_coredump_network_config my.vcenter.location root bad-password             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'

    zsystem coredump network getrrrsrvrrwrxzCoredump Configry)
r{rgr
r7r8rer+r|rD�_format_coredump_stdout)rKrLrMrNrOr}ru�cmdr=rtr~rxr3r3r4�get_coredump_network_configysJ1
�
���	�r�c
Cs�|rd}nd}d|��}	i}
|rKt|t�std��|D]-}tjjj||||	||||d�}|ddkr?|
�|d|�d�ii�q|
�|d	|ii�q|
Stjjj||||	|||d
�}|ddkrm|
�|d|�d�ii�|
S|
�|d	|ii�|
S)a}
    Enable or disable ESXi core dump collection. Returns ``True`` if coredump is enabled
    and returns ``False`` if core dump is not enabled. If there was an error, the error
    will be the value printed in the ``Error`` key dictionary for the given host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    enabled
        Python True or False to enable or disable coredumps.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.coredump_network_enable my.esxi.host root bad-password True

        # Used for connecting to a vCenter Server
        salt '*' vsphere.coredump_network_enable my.vcenter.location root bad-password True             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    �rzsystem coredump network set -e rrrsrvrwrxzCoredump Enabledryrz)
rKrLrM�enabledrNrOr}ruZ	enable_itr�r=rtr~r3r3r4�coredump_network_enable�sJ7

�
��	�r��vmk0�dc
Cs�d�|||�}
i}|r@t|t�std��|D]'}tjjj||||
||||	d�}
|
ddkr2d|
d<nd|
d<|�||
i�q|Stjjj||||
|||	d	�}
|
ddkrYd|
d<nd|
d<|�||
i�|S)
a�

    Set the network parameters for a network coredump collection.
    Note that ESXi requires that the dumps first be enabled (see
    `coredump_network_enable`) before these parameters may be set.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    dump_ip
        IP address of host that will accept the dump.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    host_vnic
        Host VNic port through which to communicate. Defaults to ``vmk0``.

    dump_port
        TCP port to use for the dump, defaults to ``6500``.

    credstore
        Optionally set to path to the credential store file.

    :return: A standard cmd.run_all dictionary with a `success` key added, per host.
             `success` will be True if the set succeeded, False otherwise.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.set_coredump_network_config my.esxi.host root bad-password 'dump_ip.host.com'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.set_coredump_network_config my.vcenter.location root bad-password 'dump_ip.host.com'             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    z-system coredump network set -v {} -i {} -o {}rrrsrvrF�successTry�	rYr{rgr
r7r8rer+r|)rKrLrMZdump_iprNrO�	host_vnicZ	dump_portr}rur�r=rtr~r3r3r4�set_coredump_network_config<sJC�
�

��	
r�cCs�d}i}|rBt|t�std��|D].}	tjjj|||||||	|d�}
|
ddkr6|�|	|
dddd	�i�q|�|	t|
�i�q|Stjjj|||||||d
�}
|
ddkre|�||
dddd	�i�|S|�|t|
�i�|S)a[
    Show status of all firewall rule sets.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    :return: Nested dictionary with two toplevel keys ``rulesets`` and ``success``
             ``success`` will be True or False depending on query success
             ``rulesets`` will list the rulesets and their statuses if ``success``
             was true, per host.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.get_firewall_status my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_firewall_status my.vcenter.location root bad-password             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    znetwork firewall ruleset listrrrsrvrrxFN)rwr��rulesetsry)	r{rgr
r7r8rer+r|�_format_firewall_stdout�rKrLrMrNrOr}rur�r=rtr~r3r3r4�get_firewall_status�s`0
�
�����	���
�r�c	
Cs�d�||�}	i}
|r0t|t�std��|D]}tjjj||||	||||d�}|
�||i�q|
Stjjj||||	|||d�}|
�||i�|
S)a-
    Enable or disable an ESXi firewall rule set.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    ruleset_enable
        True to enable the ruleset, false to disable.

    ruleset_name
        Name of ruleset to target.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    :return: A standard cmd.run_all dictionary, per host.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.enable_firewall_ruleset my.esxi.host root bad-password True 'syslog'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.enable_firewall_ruleset my.vcenter.location root bad-password True 'syslog'             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    z9network firewall ruleset set --enabled {} --ruleset-id={}rrrsryr�)
rKrLrM�ruleset_enable�ruleset_namerNrOr}rur�r=rtr~r3r3r4�enable_firewall_rulesets>;�
�
��	r�cCs�d}i}|r,t|t�std��|D]}	tjjj|||||||	|d�}
|�|	|
i�q|Stjjj|||||||d�}
|�||
i�|S)a
    Reload the syslog service so it will pick up any changes.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    :return: A standard cmd.run_all dictionary.  This dictionary will at least
             have a `retcode` key.  If `retcode` is 0 the command was successful.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.syslog_service_reload my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.syslog_service_reload my.vcenter.location root bad-password             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    zsystem syslog reloadrrrsry)r{rgr
r7r8rer+r|r�r3r3r4�syslog_service_reloadxs:.
�
��	r�c
Cs�i}|ry|dkry|	rJt|	t�std��|	D]3}t|||dd|||g|
d�	�|�}
|
ddkr=|�|d|
d	d
d�ii�q|�|dddiii�qn/t|||dd|||
d
��|�}
|
ddkrn|�|d|
d	d
d�ii�n|�|dddiii�|	r�t|	t�s�td��|	D]%}t||||||||||
d�
}
|�|�dur�|�|ii�||�|
�q�|St|||||||||
d�	}
|�|�dur�|�|ii�||�|
�|S)a6	
    Set the specified syslog configuration parameter. By default, this function will
    reset the syslog service after the configuration is set.

    host
        ESXi or vCenter host to connect to.

    username
        User to connect as, usually root.

    password
        Password to connect with.

    syslog_config
        Name of parameter to set (corresponds to the command line switch for
        esxcli without the double dashes (--))

        Valid syslog_config values are ``logdir``, ``loghost``, ``default-rotate`,
        ``default-size``, ``default-timeout``, and ``logdir-unique``.

    config_value
        Value for the above parameter. For ``loghost``, URLs or IP addresses to
        use for logging. Multiple log servers can be specified by listing them,
        comma-separated, but without spaces before or after commas.

        (reference: https://blogs.vmware.com/vsphere/2012/04/configuring-multiple-syslog-servers-for-esxi-5.html)

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    firewall
        Enable the firewall rule set for syslog. Defaults to ``True``.

    reset_service
        After a successful parameter set, reset the service. Defaults to ``True``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    :return: Dictionary with a top-level key of 'success' which indicates
             if all the parameters were reset, and individual keys
             for each parameter indicating which succeeded or failed, per host.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.set_syslog_config my.esxi.host root bad-password             loghost ssl://localhost:5432,tcp://10.1.0.1:1514

        # Used for connecting to a vCenter Server
        salt '*' vsphere.set_syslog_config my.vcenter.location root bad-password             loghost ssl://localhost:5432,tcp://10.1.0.1:1514             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'

    �loghostrrTZsyslog)r�r�rNrOr}rurvrZenable_firewallrxF)�messager�r�)r�r�rNrOru)rNrO�
reset_servicertruN)rNrOr�ru)r{rgr
r�rDr|�_set_syslog_config_helper)rKrLrM�
syslog_config�config_valuerNrOZfirewallr�r}rur=rtr~r3r3r4�set_syslog_config�s�Q
�
�������	�
����
���
r�cCs�d}i}|r.t|t�std��|D]}	tjjj|||||||	|d�}
|�|	t|
�i�q|Stjjj|||||||d�}
|�|t|
�i�|S)a�
    Retrieve the syslog configuration.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    :return: Dictionary with keys and values corresponding to the
             syslog configuration, per host.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.get_syslog_config my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_syslog_config my.vcenter.location root bad-password             esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    zsystem syslog config getrrrsry)	r{rgr
r7r8rer+r|�_format_syslog_configr�r3r3r4�get_syslog_config�s:.
���
r�c
Cs�|std��gd�}d}	d|vrdd�|�d�D�}
n
|dkr"|}
n|g}
i}|rNt|t�s2td��|D]}t||||	|
|||||d	�
}
|�||
i�q4|St||||	|
||||d
�	}
|�||
i�|S)aM
    Reset the syslog service to its default settings.

    Valid syslog_config values are ``logdir``, ``loghost``, ``logdir-unique``,
    ``default-rotate``, ``default-size``, ``default-timeout``,
    or ``all`` for all of these.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    syslog_config
        List of parameters to reset, provided as a comma-delimited string, or 'all' to
        reset all syslog configuration parameters. Required.

    esxi_hosts
        If ``host`` is a vCenter host, then use esxi_hosts to execute this function
        on a list of one or more ESXi machines.

    credstore
        Optionally set to path to the credential store file.

    :return: Dictionary with a top-level key of 'success' which indicates
             if all the parameters were reset, and individual keys
             for each parameter indicating which succeeded or failed, per host.

    .. note::

        ``syslog_config`` can be passed as a quoted, comma-separated string. See CLI Example for details.

    CLI Example:

    .. code-block:: bash

        # Used for ESXi host connection information
        salt '*' vsphere.reset_syslog_config my.esxi.host root bad-password             syslog_config='logdir,loghost'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.reset_syslog_config my.vcenter.location root bad-password             syslog_config='logdir,loghost' esxi_hosts='[esxi-1.host.com, esxi-2.host.com]'
    zFThe 'reset_syslog_config' function requires a 'syslog_config' setting.�Zlogdirr�zdefault-rotatezdefault-sizezdefault-timeoutz
logdir-uniquez!system syslog config set --reset=�,cSsg|]}|���qSr3)�strip)�.0Z	ind_resetr3r3r4�
<listcomp>(�z'reset_syslog_config.<locals>.<listcomp>�allrrrsry)r
�splitr{rg�_reset_syslog_config_paramsr|)rKrLrMrNrOr�r}ru�valid_resetsr��resetsr=rtZ
response_dictr3r3r4�reset_syslog_config�sVC�
���r�rucCs�|durd}|durd}|durd}|�d|�d|�d�}i}	d}
z?|r4tjjj|ddd||||d	�}
n|rFtjjj|ddd|||d
|d�	}
|
�d�d
krTd|	d<W|	Sd
|	d<|
d|	d<W|	Sty{}zd
|	d<||	d<WYd}~|	Sd}~ww)a�
    Upload an ssh key for root to an ESXi host via http PUT.
    This function only works for ESXi, not vCenter.
    Only one ssh key can be uploaded for root.  Uploading a second key will
    replace any existing key.

    :param host: The location of the ESXi Host
    :param username: Username to connect as
    :param password: Password for the ESXi web endpoint
    :param ssh_key: Public SSH key, will be added to authorized_keys on ESXi
    :param ssh_key_file: File containing the SSH key.  Use 'ssh_key' or
                         ssh_key_file, but not both.
    :param protocol: defaults to https, can be http if ssl is disabled on ESXi
    :param port: defaults to 443 for https
    :param certificate_verify: If true require that the SSL connection present
                               a valid certificate. Default: True
    :return: Dictionary with a 'status' key, True if upload is successful.
             If upload is unsuccessful, 'status' key will be False and
             an 'Error' key will have an informative message.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.upload_ssh_key my.esxi.host root bad-password ssh_key_file='/etc/salt/my_keys/my_key.pub'

    N�https�T�://�:�/host/ssh_root_authorized_keys�PUT)�status�text�methodrLrM�datarSF)r�r�r�rLrMZ	data_fileZdata_renderrSr����errorrw�r7r8�http�queryrDri)rKrLrMZssh_keyZssh_key_filerNrO�certificate_verify�urlr=�result�msgr3r3r4�upload_ssh_keySsZ'�
�
����r�c

Cs�|durd}|durd}|durd}|�d|�d|�d�}i}z/tjjj|ddd|||d	�}|�d
�dkrBd|d
<|d|d
<W|Sd|d
<|d|d<W|Styi}	zd|d
<|	|d<WYd}	~	|Sd}	~	ww)a�
    Retrieve the authorized_keys entry for root.
    This function only works for ESXi, not vCenter.

    :param host: The location of the ESXi Host
    :param username: Username to connect as
    :param password: Password for the ESXi web endpoint
    :param protocol: defaults to https, can be http if ssl is disabled on ESXi
    :param port: defaults to 443 for https
    :param certificate_verify: If true require that the SSL connection present
                               a valid certificate. Default: True
    :return: True if upload is successful

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.get_ssh_key my.esxi.host root bad-password certificate_verify=True

    Nr�r�Tr�r�r�ZGET)r�r�r�rLrMrSr�r�r��keyFr�rwr�)
rKrLrMrNrOr�r�r=r�r�r3r3r4�get_ssh_key�s>�	����r�c
Csdtjjj||||||d�}t|||�}i}|D]}	t|||	d�}
t|
�}|��}|�|	|i�q|S)a�
    Get the date/time information for a given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to get date/time information.

        If host_names is not provided, the date/time information will be retrieved for the
        ``host`` location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.get_host_datetime my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_host_datetime my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    �rKrLrMrNrOrS��	host_name)	r7r8rerf�_check_hosts�
_get_host_ref�_get_date_time_mgrZ
QueryDateTimer|)
rKrLrMrNrO�
host_namesrSrbr=r��host_ref�date_time_managerZ	date_timer3r3r4�get_host_datetime�s 1�r�cCsbtjjj||||||d�}t|||�}i}|D]}	t|||	d�}
|
jjjj	j
}|�|	|i�q|S)a�
    Get the NTP configuration information for a given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to get ntp configuration information.

        If host_names is not provided, the NTP configuration will be retrieved for the
        ``host`` location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.get_ntp_config my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_ntp_config my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�)r7r8rerfr�r��
configManager�dateTimeSystemZdateTimeInfo�	ntpConfig�serverr|)rKrLrMrNrOr�rSrbr=r�r��
ntp_configr3r3r4�get_ntp_config%s1�r�cC�tjjj||||||d�}gd�}	t|||�}i}
|D]p}||	vr1|
�|d|�d�ii�|
St|||d�}|jjj	j
}
|dksF|dkrId}n|}|
D]"}|j|kr`|
�|||jii�nd	�
||�}|
�|d|ii�qM|
�|�d
ur�d|�d�}t�|�|
�|d|ii�q|
S)
a"
    Get the service name's policy for a given host or list of hosts.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    service_name
        The name of the service for which to retrieve the policy. Supported service names are:
          - DCUI
          - TSM
          - SSH
          - lbtd
          - lsassd
          - lwiod
          - netlogond
          - ntpd
          - sfcbd-watchdog
          - snmpd
          - vprobed
          - vpxa
          - xorg

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to get service policy information.

        If host_names is not provided, the service policy information will be retrieved
        for the ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.get_service_policy my.esxi.host root bad-password 'ssh'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_service_policy my.vcenter.location root bad-password 'ntpd'         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r��ZDCUIZTSM�SSH�sshZlbtdZlsassdZlwiodZ	netlogondZntpdzsfcbd-watchdogZsnmpdZvprobedZvpxaZxorgrw� is not a valid service name.r�r�r��TSM-SSH�*Could not find service '{}' for host '{}'.Nz-'vsphere.get_service_policy' failed for host �.)r7r8rerfr�r|r�r��
serviceSystem�serviceInfo�servicer��policyrYrD�log�debug�rKrLrM�service_namerNrOr�rSrb�valid_servicesr=r�r��services�temp_service_namer�r�r3r3r4�get_service_policyh�HH��
�
�r�cCr�)
a)
    Get the service name's running state for a given host or list of hosts.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    service_name
        The name of the service for which to retrieve the policy. Supported service names are:
          - DCUI
          - TSM
          - SSH
          - lbtd
          - lsassd
          - lwiod
          - netlogond
          - ntpd
          - sfcbd-watchdog
          - snmpd
          - vprobed
          - vpxa
          - xorg

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to get the service's running state.

        If host_names is not provided, the service's running state will be retrieved
        for the ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.get_service_running my.esxi.host root bad-password 'ssh'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_service_running my.vcenter.location root bad-password 'ntpd'         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�rwr�r�r�r�r�r�Nz.'vsphere.get_service_running' failed for host r�)r7r8rerfr�r|r�r�r�r�r�r�ZrunningrYrDr�r�r�r3r3r4�get_service_running�r�r�cCs|tjjj||||||d�}t|||�}i}|D]$}	t|||	d�}
|
jjjj	}|r2|�
|	ddii�q|�
|	ddii�q|S)a�
    Get the VMotion enabled status for a given host or a list of host_names. Returns ``True``
    if VMotion is enabled, ``False`` if it is not enabled.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts to check if VMotion is enabled.

        If host_names is not provided, the VMotion status will be retrieved for the
        ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.get_vmotion_enabled my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_vmotion_enabled my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r��VMotion EnabledTF)r7r8rerfr�r�r��
vmotionSystemZ	netConfigZselectedVnicr|)rKrLrMrNrOr�rSrbr=r�r�Zvmotion_vnicr3r3r4�get_vmotion_enabled~s"8�r�c
Cs�tjjj||||||d�}t|||�}i}|D]0}	t|||	d�}
|
jj}|dur=d|	�d�}t�	|�|�
|	d|ii�q|�
|	d|jii�q|S)a
    Get the VSAN enabled status for a given host or a list of host_names. Returns ``True``
    if VSAN is enabled, ``False`` if it is not enabled, and ``None`` if a VSAN Host Config
    is unset, per host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts to check if VSAN enabled.

        If host_names is not provided, the VSAN status will be retrieved for the
        ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.get_vsan_enabled my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_vsan_enabled my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�Nz.VSAN System Config Manager is unset for host 'z'.rw�VSAN Enabled)r7r8rerfr�r��configZvsanHostConfigr�r�r|r�)
rKrLrMrNrOr�rSrbr=r�r��vsan_configr�r3r3r4�get_vsan_enabled�s&9�
r�cCs�tjjj||||||d�}t|||�}t|||�}i}	|��D]A\}
}|�d�}|r4|	�|
d|ii�q|�d�}
|
rWt	|
t
�rWg}|
D]}|�|j�qD|	�|
d|ii�q|	�|
d|
ii�q|	S)a�
    Returns a list of VSAN-eligible disks for a given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts to check if any VSAN-eligible disks are available.

        If host_names is not provided, the VSAN-eligible disks will be retrieved
        for the ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.get_vsan_eligible_disks my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.get_vsan_eligible_disks my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�rw�Eligible)
r7r8rerfr��_get_vsan_eligible_disks�itemsrDr|r{rgrU�
canonicalName)rKrLrMrNrOr�rSrbr~r=r��valuer��disks�
disk_names�diskr3r3r4�get_vsan_eligible_disks	s07�

r�cCs0z
tjj�|�rWdSWdStyYdSw)z�
    Checks if a connection is to a vCenter

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.test_vcenter_connection
    TF)r7r8re�is_connection_to_a_vcenterrrqr3r3r4�test_vcenter_connectionw	s���r�cCsZtjjj||||||d�}tjj�|�jj}d|vr+|ddkr+t�|tjj�	|��}|S)a�
    Return system information about a VMware environment.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.system_info 1.2.3.4 root bad-password
    r�ZapiTypeZ	HostAgent)
r7r8rerf�
get_inventoryZabout�__dict__�
dictupdater|Zget_hardware_grains)rKrLrMrNrOrSrbr=r3r3r4�system_info�	s(��r�cC�(tjjj||||||d�}tjj�|�S)a�
    Returns a list of datacenters for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_datacenters 1.2.3.4 root bad-password

    r�)r7r8rerf�list_datacenters�rKrLrMrNrOrSrbr3r3r4r��	s$�r�cCr�)a�
    Returns a list of clusters for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_clusters 1.2.3.4 root bad-password

    r�)r7r8rerf�
list_clustersr�r3r3r4r��	�"�r�cCr�)a�
    Returns a list of datastore clusters for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_datastore_clusters 1.2.3.4 root bad-password
    r�)r7r8rerf�list_datastore_clustersr�r3r3r4r"
�#�rcCr�)a�
    Returns a list of datastores for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_datastores 1.2.3.4 root bad-password
    r�)r7r8rerf�list_datastoresr�r3r3r4rP
rrcCr�)a�
    Returns a list of hosts for the specified VMware environment.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_hosts 1.2.3.4 root bad-password
    r�)r7r8rerf�
list_hostsr�r3r3r4r~
�!�rcCr�)a�
    Returns a list of resource pools for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_resourcepools 1.2.3.4 root bad-password
    r�)r7r8rerf�list_resourcepoolsr�r3r3r4r�
rrcCr�)a�
    Returns a list of networks for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_networks 1.2.3.4 root bad-password
    r�)r7r8rerf�
list_networksr�r3r3r4r�
rrcCr�)a�
    Returns a list of VMs for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_vms 1.2.3.4 root bad-password
    r�)r7r8rerf�list_vmsr�r3r3r4rrrcCr�)a�
    Returns a list of folders for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_folders 1.2.3.4 root bad-password
    r�)r7r8rerf�list_foldersr�r3r3r4r0rrcCr�)a�
    Returns a list of distributed virtual switches for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_dvs 1.2.3.4 root bad-password
    r�)r7r8rerf�list_dvsr�r3r3r4r	\rr	cCr�)a�
    Returns a list of vApps for the specified host.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # List vapps from all minions
        salt '*' vsphere.list_vapps 1.2.3.4 root bad-password
    r�)r7r8rerf�
list_vappsr�r3r3r4r
�r�r
cC�vtjjj||||||d�}t|||�}i}g}	|D]}
t|||
d�}t|�}|D]}
|	�|
j�q(|�	|
|	i�q|S)av
    Returns a list of SSDs for the given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter the hosts for which to retrieve SSDs.

        If host_names is not provided, SSDs will be retrieved for the
        ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.list_ssds my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.list_ssds my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�)
r7r8rerfr�r��_get_host_ssdsrUr�r|�rKrLrMrNrOr�rSrbr=�namesr�r�r�r�r3r3r4�	list_ssds�s$1�rcCr)a�
    Returns a list of Non-SSD disks for the given host or list of host_names.

    .. note::

        In the pyVmomi StorageSystem, ScsiDisks may, or may not have an ``ssd`` attribute.
        This attribute indicates if the ScsiDisk is SSD backed. As this option is optional,
        if a relevant disk in the StorageSystem does not have ``ssd = true``, it will end
        up in the ``non_ssds`` list here.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter the hosts for which to retrieve Non-SSD disks.

        If host_names is not provided, Non-SSD disks will be retrieved for the
        ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.list_non_ssds my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.list_non_ssds my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�)
r7r8rerfr�r��_get_host_non_ssdsrUr�r|r
r3r3r4�
list_non_ssds�s$8�rcCs�tjjj||||||d�}t|t�std��tj|d�}	tj	|	d�}
t
|||�}i}|D]M}t|||d�}
t|
�}t
�d||�z|j|
d�Wn'tjjyo}zd|��}t
�|�|�|d	|ii�WYd
}~q,d
}~ww|�|d|	ii�q,|S)aO
    Set NTP configuration for a given host of list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    ntp_servers
        A list of servers that should be added to and configured for the specified
        host's NTP configuration.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter which hosts to configure ntp servers.

        If host_names is not provided, the NTP servers will be configured for the
        ``host`` location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.ntp_configure my.esxi.host root bad-password '[192.174.1.100, 192.174.1.200]'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.ntp_configure my.vcenter.location root bad-password '[192.174.1.100, 192.174.1.200]'         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�z'ntp_servers' must be a list.)r�)r�r�z+Configuring NTP Servers '%s' for host '%s'.�r�z&vsphere.ntp_configure_servers failed: rwNzNTP Servers)r7r8rerfr{rgr
rZ
HostNtpConfigZHostDateTimeConfigr�r�r�r�r�ZUpdateDateTimeConfig�fault�HostConfigFaultr|)rKrLrMZntp_serversrNrOr�rSrbr�Zdate_configr=r�r�r��errr�r3r3r4�set_ntp_configHs:<�


��rcC�\tjjj||||||d�}t|||�}gd�}	i}
|dks!|dkr$d}n|}|D]�}||	vr>|
�|d|�d�ii�|
St|||d�}
t|
�}t�	d	||�z|j
|d
�WnItjj
y�}zd�||�}t�	|�|
�|d|ii�WYd}~q(d}~wtjjy�}zt�	|�|
�|d|ii�WYd}~q(d}~ww|
�|d
dii�q(|
S)a�
    Start the named service for the given host or list of hosts.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    service_name
        The name of the service for which to set the policy. Supported service names are:
          - DCUI
          - TSM
          - SSH
          - lbtd
          - lsassd
          - lwiod
          - netlogond
          - ntpd
          - sfcbd-watchdog
          - snmpd
          - vprobed
          - vpxa
          - xorg

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to start the service.

        If host_names is not provided, the service will be started for the ``host``
        location instead. This is useful for when service instance connection information
        is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.service_start my.esxi.host root bad-password 'ntpd'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.service_start my.vcenter.location root bad-password 'ntpd'         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�r�r�r�rwr�r�z Starting the '%s' service on %s.��idz.'vsphere.service_start' failed for host {}: {}NzService StartedT)r7r8rerfr�r|r��_get_service_managerr�r�ZStartServicerrrrY�RestrictedVersion�rKrLrMr�rNrOr�rSrbr�r=r�r�r��service_managerrr�r3r3r4�
service_start��RH���
�
��rcCs`tjjj||||||d�}t|||�}gd�}	i}
|dks!|dkr$d}n|}|D]�}||	vr>|
�|d|�d�ii�|
St|||d�}
t|
�}t�	d	||�z|j
|d
�WnKtjj
y�}zd|�d|��}t�	|�|
�|d|ii�WYd
}~q(d
}~wtjjy�}zt�	|�|
�|d|ii�WYd
}~q(d
}~ww|
�|ddii�q(|
S)a�
    Stop the named service for the given host or list of hosts.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    service_name
        The name of the service for which to set the policy. Supported service names are:
          - DCUI
          - TSM
          - SSH
          - lbtd
          - lsassd
          - lwiod
          - netlogond
          - ntpd
          - sfcbd-watchdog
          - snmpd
          - vprobed
          - vpxa
          - xorg

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to stop the service.

        If host_names is not provided, the service will be stopped for the ``host``
        location instead. This is useful for when service instance connection information
        is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.service_stop my.esxi.host root bad-password 'ssh'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.service_stop my.vcenter.location root bad-password 'ssh'         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�r�r�r�rwr�r�z Stopping the '%s' service on %s.rz''vsphere.service_stop' failed for host �: NzService StoppedT)r7r8rerfr�r|r�rr�r�ZStopServicerrrrrr3r3r4�service_stop3
sNH��
�
��r!cCr)a�
    Restart the named service for the given host or list of hosts.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    service_name
        The name of the service for which to set the policy. Supported service names are:
          - DCUI
          - TSM
          - SSH
          - lbtd
          - lsassd
          - lwiod
          - netlogond
          - ntpd
          - sfcbd-watchdog
          - snmpd
          - vprobed
          - vpxa
          - xorg

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to restart the service.

        If host_names is not provided, the service will be restarted for the ``host``
        location instead. This is useful for when service instance connection information
        is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.service_restart my.esxi.host root bad-password 'ntpd'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.service_restart my.vcenter.location root bad-password 'ntpd'         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�r�r�r�rwr�r�z"Restarting the '%s' service on %s.rz0'vsphere.service_restart' failed for host {}: {}NzService RestartedT)r7r8rerfr�r|r�rr�r�ZRestartServicerrrrYrrr3r3r4�service_restart�
rr"c	Cs�tjjj||||||d�}	t|	||�}gd�}
i}|D]�}||
vr1|�|d|�d�ii�|St|	||d�}
t|
�}|
jj	j
j}|D]�}d}|j|krQ|j}n|dksY|dkr`|jd	kr`d	}|r�z	|j
||d
�WnFtjjy�d|�d�}t�|�|�|d|ii�YqDtjjy�}zd
�||�}t�|�|�|d|ii�WYd}~qDd}~ww|�|di�|�|�dur�d�||�}t�|�|�|d|ii�qDq|S)a�
    Set the service name's policy for a given host or list of hosts.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    service_name
        The name of the service for which to set the policy. Supported service names are:
          - DCUI
          - TSM
          - SSH
          - lbtd
          - lsassd
          - lwiod
          - netlogond
          - ntpd
          - sfcbd-watchdog
          - snmpd
          - vprobed
          - vpxa
          - xorg

    service_policy
        The policy to set for the service. For example, 'automatic'.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to tell
        vCenter the hosts for which to set the service policy.

        If host_names is not provided, the service policy information will be retrieved
        for the ``host`` location instead. This is useful for when service instance
        connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.set_service_policy my.esxi.host root bad-password 'ntpd' 'automatic'

        # Used for connecting to a vCenter Server
        salt '*' vsphere.set_service_policy my.vcenter.location root bad-password 'ntpd' 'automatic'         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�rwr�r�Nr�r�r�)rr�zThe service name 'z' was not found.z3'vsphere.set_service_policy' failed for host {}: {}Tr�)r7r8rerfr�r|r�rr�r�r�r�r�ZUpdateServicePolicyrrr&r�r�rrYrD)rKrLrMr�Zservice_policyrNrOr�rSrbr�r=r�r�rr�r�Zservice_keyr�rr3r3r4�set_service_policyGspL��


�
�
���
��(r#cCs�tjjj||||||d�}t|||�}i}|D]I}	t|||	d�}
t|
�}z
|�tj�	��Wn(t
jjyV}zd�
|	|�}
t�|
�|�|	d|
ii�WYd}~qd}~ww|�|	ddii�q|S)a�
    Update the date/time on the given host or list of host_names. This function should be
    used with caution since network delays and execution delays can result in time skews.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts should update their date/time.

        If host_names is not provided, the date/time will be updated for the ``host``
        location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.update_date_time my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.update_date_time my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�z1'vsphere.update_date_time' failed for host {}: {}rwNzDatetime UpdatedT)r7r8rerfr�r�r�ZUpdateDateTime�datetime�utcnowrrrrYr�r�r|)rKrLrMrNrOr�rSrbr=r�r�r�rr�r3r3r4�update_host_datetime�s42��
��r&c
Cs�tjjj||||||d�}tjj�|�j}tjj�	�}	||	_
||	_z|�|	�WdSt
jjy<}
zt|
j��d}
~
wtjjyKtd�|���tjjyVYdSw)a�
    Update the password for a given host.

    .. note:: Currently only works with connections to ESXi hosts. Does not work with vCenter servers.

    host
        The location of the ESXi host.

    username
        The username used to login to the ESXi host, such as ``root``.

    password
        The password used to login to the ESXi host.

    new_password
        The new password that will be updated for the provided username on the ESXi host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.update_host_password my.esxi.host root original-bad-password new-bad-password

    r�NzF'vsphere.update_host_password' failed for host {}: User was not found.T)r7r8rerfr�ZaccountManagerrrKZLocalAccountManagerZAccountSpecificationrrMZ
UpdateUserrr�SystemErrorr
r�ZUserNotFoundrYr$)rKrLrMZnew_passwordrNrOrSrbZaccount_managerZuser_accountrr3r3r4�update_host_password6s6)�	�
���r(cCs�tjjj||||||d�}t|||�}i}|D]E}	t|||	d�}
|
jj}z|��Wn(t	j
jyR}zd|��}
t�
|
�|�|	|
dd�i�WYd}~qd}~ww|�|	ddii�q|S)	ay
    Disable vMotion for a given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts should disable VMotion.

        If host_names is not provided, VMotion will be disabled for the ``host``
        location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.vmotion_disable my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.vmotion_disable my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�� vsphere.vmotion_disable failed: F)rw�VMotion DisabledNr*T)r7r8rerfr�r�r�r�ZDeselectVnicrrrr�r�r|)rKrLrMrNrOr�rSrbr=r�r��vmotion_systemrr�r3r3r4�vmotion_disable�s01�

��r,cCs�tjjj||||||d�}t|||�}i}	|D]F}
t|||
d�}|jj}z|�|�Wn(t	j
jyS}
zd|
��}t�
|�|	�|
|dd�i�WYd}
~
qd}
~
ww|	�|
ddii�q|	S)	a
    Enable vMotion for a given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts should enable VMotion.

        If host_names is not provided, VMotion will be enabled for the ``host``
        location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    device
        The device that uniquely identifies the VirtualNic that will be used for
        VMotion for each host. Defaults to ``vmk0``.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.vmotion_enable my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.vmotion_enable my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�r)F)rwr�Nr�T)r7r8rerfr�r�r�r�Z
SelectVnicrrrr�r�r|)rKrLrMrNrOr��devicerSrbr=r�r�r+rr�r3r3r4�vmotion_enable�s0<�

��r.cCs�tjjj||||||d�}t|||�}t|||�}i}	|��D]�\}
}t|||
d�}|jj	}
|
durFd�
|
�}t�|�|	�
|
d|ii�q|�d�}|�d�}|r�t|t�r�z|
�|�}tjjj||
ddd	�WnItjjy�}zt�|j�|	�
|
d|jii�WYd}~qd}~wty�}zd
�
|
|�}t�|�|	�
|
d|ii�WYd}~qd}~wwt�d|
�g}|D]}|�|j�q�|	�
|
d|ii�q|r�t|t�r�|	�
|
d|ii�q|r�|	�
|
d|ii�q|	�
|
dd
ii�q|	S)a�
    Add any VSAN-eligible disks to the VSAN System for the given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts need to add any VSAN-eligible disks to the host's
        VSAN system.

        If host_names is not provided, VSAN-eligible disks will be added to the hosts's
        VSAN system for the ``host`` location instead. This is useful for when service
        instance connection information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.vsan_add_disks my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.vsan_add_disks my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�r�N�yVSAN System Config Manager is unset for host '{}'. VSAN configuration cannot be changed without a configured VSAN System.rwr�zAdding disks to VSAN��Z
sleep_secondsz/'vsphere.vsan_add_disks' failed for host {}: {}z:Successfully added disks to the VSAN system for host '%s'.zDisks Addedz-No new VSAN-eligible disks were found to add.)r7r8rerfr�r�r�r�r��
vsanSystemrYr�r�r|rDr{rgZAddDisks�
wait_for_taskrrZInsufficientDisksr�rirUr��str)rKrLrMrNrOr�rSrbr~r=r�r�r��vsan_systemr��eligibler��taskrr�r�r3r3r4�vsan_add_disks%s|2��




���
������
r8cCsZtjjj||||||d�}tjj��}d|_t	|||�}i}	|D]�}
t
|||
d�}|jj}|durEd�
|
�}
t�|
�|	�|
d|
ii�q z|�|�}tjjj||
ddd	�WnItjjy{}zt�|j�|	�|
d|jii�WYd}~q d}~wty�}zd
�
|
|�}
t�|
�|	�|
d|
ii�WYd}~q d}~ww|	�|
ddii�q |	S)
aj
    Disable VSAN for a given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts should disable VSAN.

        If host_names is not provided, VSAN will be disabled for the ``host``
        location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.vsan_disable my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.vsan_disable my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�Fr�Nr/rwzDisabling VSANr0r1z-'vsphere.vsan_disable' failed for host {}: {}z
VSAN DisabledT)r7r8rerfr�vsanrK�
ConfigInfor�r�r�r�r2rYr�r�r|�UpdateVsan_Taskr3rrr'r�ri�rKrLrMrNrOr�rSrbr�r=r�r�r5r�r7rr3r3r4�vsan_disable�sT1�	�


���
��r=cCs^tjjj||||||d�}tjj��}d|_t	|||�}i}	|D]�}
t
|||
d�}|jj}|durEd�
|
�}
t�|
�|	�|
d|
ii�q z|�|�}tjjj||
ddd	�WnKtjjy{}zt�|j�|	�|
d|jii�WYd}~q d}~wtjjy�}zd
�
|
|�}
t�|
�|	�|
d|
ii�WYd}~q d}~ww|	�|
ddii�q |	S)ae
    Enable VSAN for a given host or list of host_names.

    host
        The location of the host.

    username
        The username used to login to the host, such as ``root``.

    password
        The password used to login to the host.

    protocol
        Optionally set to alternate protocol if the host is not using the default
        protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the host is not using the default
        port. Default port is ``443``.

    host_names
        List of ESXi host names. When the host, username, and password credentials
        are provided for a vCenter Server, the host_names argument is required to
        tell vCenter which hosts should enable VSAN.

        If host_names is not provided, VSAN will be enabled for the ``host``
        location instead. This is useful for when service instance connection
        information is used for a single ESXi host.

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        # Used for single ESXi host connection information
        salt '*' vsphere.vsan_enable my.esxi.host root bad-password

        # Used for connecting to a vCenter Server
        salt '*' vsphere.vsan_enable my.vcenter.location root bad-password         host_names='[esxi-1.host.com, esxi-2.host.com]'
    r�Tr�Nr/rwz
Enabling VSANr0r1z,'vsphere.vsan_enable' failed for host {}: {}r�)r7r8rerfrr9rKr:r�r�r�r�r2rYr�r�r|r;r3rrr'r�Z	VsanFaultr<r3r3r4�vsan_enablesT1�	�


���
��r>c	CsXt�d|�||jj|jj|j|j|j|j|jd�}t	|j
tj�r*|�
d|j
ji�|S)z�
    Returns the dict representation of the DVS config

    dvs_name
        The name of the DVS

    dvs_config
        The DVS config
    z(Building the dict of the DVS '%s' config)�name�
contact_email�contact_name�description�lacp_api_version� network_resource_control_version�#network_resource_management_enabled�max_mtu�uplink_names)r��trace�contactr?rB�lacpApiVersion�networkResourceControlVersionZ networkResourceManagementEnabled�maxMtur{�uplinkPortPolicyr�DVSNameArrayUplinkPortPolicyr|�uplinkPortName)�dvs_name�
dvs_configZ	conf_dictr3r3r4�_get_dvs_config_dictos
�
rRcCst�d|�|j|jd�S)z�
    Returns the dict representation of the DVS link discovery protocol

    dvs_name
        The name of the DVS

    dvs_link_disc_protocl
        The DVS link discovery protocol
    z9Building the dict of the DVS '%s' link discovery protocol��	operationrN)r�rHrTrN)rPZdvs_link_disc_protocolr3r3r4� _get_dvs_link_discovery_protocol�s
�rUcC�t�d|�|j|j|jd�S)z�
    Returns the dict representation of the DVS product_info

    dvs_name
        The name of the DVS

    dvs_product_info
        The DVS product info
    z.Building the dict of the DVS '%s' product info)r?�vendor�version)r�rHr?rWrX)rPZdvs_product_infor3r3r4�_get_dvs_product_info��

�rYcCrV)z�
    Returns the dict representation of the DVS product_info

    dvs_name
        The name of the DVS

    dvs_capability
        The DVS capability
    z,Building the dict of the DVS '%s' capability)�operation_supported�portgroup_operation_supported�port_operation_supported)r�rH�dvsOperationSupported�dvPortGroupOperationSupported�dvPortOperationSupported)rPZdvs_capabilityr3r3r4�_get_dvs_capability�rZracCsbt�d|�g}|D]$}|j|jj|jjd�}|jjr)|�|jjj|jjjd��|�	|�q
|S)z�
    Returns a list of dict representations of the DVS infrastructure traffic
    resource

    dvs_name
        The name of the DVS

    dvs_infra_traffic_ress
        The DVS infrastructure traffic resources
    zCBuilding the dicts of the DVS '%s' infrastructure traffic resources)r��limit�reservation)�
num_shares�share_level)
r�rHr��allocationInforbrc�sharesr|�levelrU)rPZdvs_infra_traffic_ressZ	res_dicts�res�res_dictr3r3r4�)_get_dvs_infrastructure_traffic_resources�s"����rkc	Cs0g}t�}|dkrtd�d}t|�}n|dkr(td�d}tjj�||�}tjj�|||�D]c}i}tjj�|gd��}t	|d|d�}|�
d	t|d|dj�i�|dj
rl|�
d
t|d|dj
�i�|�
dt|d|d�i�t|dd�r�|�
d
t|d|dj�i�|�|�q2|S)a�
    Returns a list of distributed virtual switches (DVSs).
    The list can be filtered by the datacenter or DVS names.

    datacenter
        The datacenter to look for DVSs in.
        Default value is None.

    dvs_names
        List of DVS names to look for. If None, all DVSs are returned.
        Default value is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_dvss

        salt '*' vsphere.list_dvss dvs_names=[dvs1,dvs2]
    r.rH�
datacenterr-rG)r?r��
capabilityZnetworkResourcePoolr?r��product_info�link_discovery_protocolrm�#infrastructureTrafficResourceConfig�%infrastructure_traffic_resource_pools)rErT�_get_proxy_targetr7r8re�get_datacenter�get_dvss� get_properties_of_managed_objectrRr|rY�productInfo�linkDiscoveryProtocolConfigrUra�hasattrrkrprU)	rl�	dvs_namesrb�ret_listr[�dc_ref�dvs�dvs_dict�propsr3r3r4�	list_dvss�sP
����
�������rcCs�|�d�r
|d|_|�d�s|�d�r*|jst��|_|�d�|j_|�d�|j_|�d�r5|�d�|_|�d�r@|�d�|_|�d�rK|�d�|_|�d�rV|�d�|_|�d�rr|j	ret
|j	tj�sjt��|_	|d|j	_d	Sd	S)
zi
    Applies the values of the config dict dictionary to a config spec
    (vim.VMwareDVSConfigSpec)
    r?r@rArBrFrCrDrGN)
rDr?rIrZDVSContactInforBrLrJrKrMr{rNrO)�config_specZconfig_dictr3r3r4�_apply_dvs_config,s0






�
�
�r�cCs|d|_|d|_dS)z�
    Applies the values of the disc_prot_dict dictionary to a link discovery
    protocol config object (vim.LinkDiscoveryProtocolConfig)
    rTrNNrS)Zdisc_prot_configZdisc_prot_dictr3r3r4�"_apply_dvs_link_discovery_protocolKs
r�cCsD|�d�r
|d|_|�d�r|d|_|�d�r |d|_dSdS)z�
    Applies the values of the product_info_dict dictionary to a product info
    spec (vim.DistributedVirtualSwitchProductSpec)
    r?rWrXN)rDr?rWrX)Zproduct_info_specZproduct_info_dictr3r3r4�_apply_dvs_product_infoTs




�r�cCs>d|vr	|d|_d|vr|d|_d|vr|d|_dSdS)zu
    Applies the values of the capability_dict dictionary to a DVS capability
    object (vim.vim.DVSCapability)
    r[r]r\N)r^r`r_)Zcapability_specZcapability_dictr3r3r4�_apply_dvs_capabilityas
�
��r�cs�|D]j��fdd�|D�}|r|d}nt��}�d|_t��|_|�|���d�r2�d|j_��d�r=�d|j_��d�sG��d�rQ|jj	sQt�
�|j_	��d�r`t��d�|jj	_��d�rl�d|jj	_	qd	S)
z�
    Applies the values of the resource dictionaries to infra traffic resources,
    creating the infra traffic resource if required
    (vim.DistributedVirtualSwitchProductSpec)
    c�g|]}|j�dkr|�qS�r�r��r��r�rjr3r4r�{sz?_apply_dvs_infrastructure_traffic_resources.<locals>.<listcomp>rr�rbrcrdreN)
rZ$DvsHostInfrastructureTrafficResourcer�Z.DvsHostInfrastructureTrafficResourceAllocationrfrUrDrbrcrg�
SharesInfo�SharesLevelrh)Zinfra_traffic_resources�resource_dictsZfiltered_traffic_resourcesZtraffic_resr3r�r4�+_apply_dvs_infrastructure_traffic_resourcesrs4
�

�




�
��r�cs�|D]U��fdd�|D�}|r|d}nt��}�d|_t��|_|�|���d�r2�d|j_��d�rW��d�rW|jjsFt�	�|j_�d|jj_t�
�d�|jj_qdS)	z�
    Applies the values of the resource dictionaries to network resource pools,
    creating the resource pools if required
    (vim.DVSNetworkResourcePoolConfigSpec)
    cr�r�r�r�r�r3r4r��sz5_apply_dvs_network_resource_pools.<locals>.<listcomp>rr�rbrdreN)rZ DVSNetworkResourcePoolConfigSpecr�Z$DVSNetworkResourcePoolAllocationInforfrUrDrbrgr�r�rh)Znetwork_resource_poolsr�Zressrir3r�r4�!_apply_dvs_network_resource_pools�s"




��r�c	Cs�t�d||�t�}|dkrtd�d}t|�}n|dkr-td�d}tjj�||�}||d<t	�
�}t	��|_t
|j|�|�d�rRt	��|_t|j|d�|�d	�rdt	��|_t|j|d	�|�d
�rxt	��|j_t|jj|d
�|�d�r�g|j_t|jj|d�t�d|�tjj�|||�d
|vr�tjjj||gd�}|s�td|�d|�d���|d}tjj�||d
�dS)a�
    Creates a distributed virtual switch (DVS).

    Note: The ``dvs_name`` param will override any name set in ``dvs_dict``.

    dvs_dict
        Dict representation of the new DVS (example in salt.states.dvs)

    dvs_name
        Name of the DVS to be created.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.create_dvs dvs dict=$dvs_dict dvs_name=dvs_name
    z Creating dvs '%s' with dict = %sr.rHrlr-rGr?rnrmrorqzdvs_create_spec = %srE�ry�DVS '�' wasn't found in datacenter 'rJrT)r�rHrErTrrr7r8rersrZ
DVSCreateSpec�VMwareDVSConfigSpecZ
configSpecr�rDZ#DistributedVirtualSwitchProductSpecrvr�Z
DVSCapabilityrmr��LinkDiscoveryProtocolConfigrwr�rpr��
create_dvsrtr�+set_dvs_network_resource_management_enabled)	r}rPrbr[rlr{Zdvs_create_spec�dvs_refs�dvs_refr3r3r4r��sX






��
���r�cCs�t�d||�dD]	}||vr||=q	t�}|dkr&td�d}t|�}n|dkr9td�d}tjj�||�}tjjj	||gd�}|sPt
d	|�d
|�d���|d}tjj�|d
dg�}	t�
�}
dg}|
j��D]}||vrqqjt|	d
|�r�t|
|t|	d
|��qjt|
|�|�d�r�|
js�t��|
_t|
j|d�|�d�r�|
js�g|
_t|
j|d�t�d|
�tjjj||
d�d|vr�tjj�||d�dS)aZ
    Updates a distributed virtual switch (DVS).

    Note: Updating the product info, capability, uplinks of a DVS is not
          supported so the corresponding entries in ``dvs_dict`` will be
          ignored.

    dvs_dict
        Dictionary with the values the DVS should be update with
        (example in salt.states.dvs)

    dvs
        Name of the DVS to be updated.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.update_dvs dvs_dict=$dvs_dict dvs=dvs1
    z Updating dvs '%s' with dict = %s)rnrmrGr?r.rHrlr-rGr�r�r�rJrr�rmrKrorqzdvs_config = %s)Zdvs_config_specrET)r�rHrErTrrr7r8rersrtrrurr�r��keysrx�setattr�getattrr�rDrwr�r�rpr��
update_dvsr�)r}r|rb�propr[rlr{r�r�Z	dvs_propsrQZskipped_propertiesr3r3r4r��sd�
���



�
��r�cCs8t�d|�|j}|s
iS|jj|jj|jj|jjd�S)z�
    Returns the out shaping policy of a distributed virtual portgroup

    pg_name
        The name of the portgroup

    pg_default_port_config
        The dafault port config of the portgroup
    z.Retrieving portgroup's '%s' out shaping config)�average_bandwidth�
burst_sizer��peak_bandwidth)r�rH�outShapingPolicy�averageBandwidthr��	burstSizer��
peakBandwidth)�pg_name�pg_default_port_configZout_shaping_policyr3r3r4�_get_dvportgroup_out_shapingOs
�r�cCs2t�d|�|j}|s
iS|jj|jj|jjd�S)z�
    Returns the security policy of a distributed virtual portgroup

    pg_name
        The name of the portgroup

    pg_default_port_config
        The dafault port config of the portgroup
    z2Retrieving portgroup's '%s' security policy config)�allow_promiscuous�forged_transmits�mac_changes)r�rH�securityPolicy�allowPromiscuousr��forgedTransmits�
macChanges)r�r��
sec_policyr3r3r4� _get_dvportgroup_security_policyes
�r�cCs�t�d|�|j}|s
iS|jj|jj|jj|jjd�}|jr?|j}|�	d|j
j|jj|jj|j
j|jj|jj|jjd�i�|jrQ|j}|�	d|j|jd�i�|S)z�
    Returns the teaming of a distributed virtual portgroup

    pg_name
        The name of the portgroup

    pg_default_port_config
        The dafault port config of the portgroup
    z*Retrieving portgroup's '%s' teaming config)�notify_switchesr��reverse_policy�
rolling_order�failure_criteria)�check_beacon�check_duplex�check_error_percent�check_speed�full_duplex�
percentage�speed�
port_order)�active�standby)r�rH�uplinkTeamingPolicy�notifySwitchesr�r��
reversePolicy�rollingOrder�failureCriteriar|�checkBeacon�checkDuplex�checkErrorPercent�
checkSpeed�
fullDuplexr�r��uplinkPortOrder�activeUplinkPort�standbyUplinkPort)r�r�Zteaming_policy�ret_dictr�Zuplink_orderr3r3r4�_get_dvportgroup_teamingzsB
����
���r�cCs�tjj�|gd��}|d|�d�|d|dd�}|dr_|d}|jr5t|jtj�r5|�	d|jj
i�|�	d	t|d|d�i�|�	d
t|d|d�i�|�	dt
|d|d�i�|S)zq
    Returns a dictionary with a distributed virtual portgroup data


    pg_ref
        Portgroup reference
    )r?�config.description�config.numPorts�config.type�config.defaultPortConfigr?r�r�r�)r?rB�	num_ports�typer��vlan_id�out_shaping�security_policy�teaming)r7r8rerurD�vlanr{r�(VmwareDistributedVirtualSwitchVlanIdSpecr|�vlanIdr�r�r�)�pg_refr~Zpg_dictZdpgr3r3r4�_get_dvportgroup_dict�sD������������r�cCs�g}t�}|dkrtd�d}t|�}n|dkr(td�d}tjj�||�}|rBtjjj||gd�}|s>td|�d���|d	}|sFd
nd}	tjjj	|rP|n|||	d�D]	}
|�
t|
��qV|S)
a�
    Returns a list of distributed virtual switch portgroups.
    The list can be filtered by the portgroup names or by the DVS.

    dvs
        Name of the DVS containing the portgroups.
        Default value is None.

    portgroup_names
        List of portgroup names to look for. If None, all portgroups are
        returned.
        Default value is None

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_dvportgroups

        salt '*' vsphere.list_dvportgroups dvs=dvs1

        salt '*' vsphere.list_dvportgroups portgroup_names=[pg1]

        salt '*' vsphere.list_dvportgroups dvs=dvs1 portgroup_names=[pg1]
    r.rHrlr-rGr�r��' was not retrievedrTF)�
parent_ref�portgroup_names�get_all_portgroups)rErTrrr7r8rersrtr�get_dvportgroupsrUr�)r|r�rbr�r[rlr{r�r�r�r�r3r3r4�list_dvportgroups�s*"


�r�cCs�t�}|dkrtd�d}t|�}n|dkr&td�d}tjj�||�}tjjj||gd�}|s:td|�d���tjj�	|d	�}t
|�S)
a[
    Returns the uplink portgroup of a distributed virtual switch.

    dvs
        Name of the DVS containing the portgroup.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_uplink_dvportgroup dvs=dvs_name
    r.rHrlr-rGr�r�r�r)rErTrrr7r8rersrtrZget_uplink_dvportgroupr�)r|rbr[rlr{r�Z
uplink_pg_refr3r3r4�list_uplink_dvportgroups
r�cCs�t�d|�|�d�rt��|_|d|j_|�d�r&t��|_|d|j_d|vr5t��|_	|d|j	_|�d�rGt��|_
|d|j
_dSdS)z�
    Applies the values in out_shaping_conf to an out_shaping object

    pg_name
        The name of the portgroup

    out_shaping
        The vim.DVSTrafficShapingPolicy to apply the config to

    out_shaping_conf
        The out shaping config
    z,Building portgroup's '%s' out shaping policyr�r�r�r�N)r�rHrDrZ
LongPolicyr�r�r��
BoolPolicyr�r�)r�r�Zout_shaping_confr3r3r4�_apply_dvportgroup_out_shaping>s






�r�cCsnt�d|�d|vrt��|_|d|j_d|vr$t��|_|d|j_d|vr5t��|_|d|j_dSdS)z�
    Applies the values in sec_policy_conf to a security policy object

    pg_name
        The name of the portgroup

    sec_policy
        The vim.DVSTrafficShapingPolicy to apply the config to

    sec_policy_conf
        The out shaping config
    z)Building portgroup's '%s' security policyr�r�r�N)r�rHrr�r�r�r�r�)r�r�Zsec_policy_confr3r3r4�"_apply_dvportgroup_security_policyZs



�r�cCs�t�d|�d|vrt��|_|d|j_d|vr$t��|_|d|j_d|vr3t��|_|d|j_d|vrBt��|_	|d|j	_d|vr�|j
sNt��|_
|d}d|vrct��|j
_|d|j
j_d|vrtt��|j
_
|d|j
j
_d	|vr�t��|j
_|d	|j
j_d
|vr�t��|j
_|d
|j
j_d|vr�t��|j
_|d|j
j_d|vr�t��|j
_|d|j
j_d
|vr�t��|j
_|d
|j
j_d|vr�|js�t��|_d|dvr�|dd|j_d|dvr�|dd|j_dSdSdS)z�
    Applies the values in teaming_conf to a teaming policy object

    pg_name
        The name of the portgroup

    teaming
        The vim.VmwareUplinkPortTeamingPolicy to apply the config to

    teaming_conf
        The teaming config
    z!Building portgroup's '%s' teamingr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�N)r�rHrr�r�r�ZStringPolicyr�r�r�r�ZDVSFailureCriteriar�r�r�r�r�Z	IntPolicyr�r�r�ZVMwareUplinkPortOrderPolicyr�r�)r�r�Zteaming_confZfailure_criteria_confr3r3r4�_apply_dvportgroup_teamingss~






�
�
�
�
�
�
���r�cCs0t�d|�d|vr|d|_d|vr|d|_d|vr!|d|_d|vr*|d|_|js;dD]}||vr:t��|_q/d|vrLt�	�|j_
|d|jj
_d|vrd|jjsZt�
�|j_t||jj|d�d	|vr||jjsrt��|j_t||jj|d	�d
|vr�|jjs�t��|j_t||jj|d
�dSdS)z�
    Applies the values in conf to a distributed portgroup spec

    pg_name
        The name of the portgroup

    pg_spec
        The vim.DVPortgroupConfigSpec to apply the config to

    pg_conf
        The portgroup config
    zBuilding portgroup's '%s' specr?rBr�r�)r�r�r�r�r�r�r�r�N)r�rHr?rB�numPortsr��defaultPortConfigrZVMwareDVSPortSettingr�r�r�r�ZDVSTrafficShapingPolicyr�r�ZDVSSecurityPolicyr�r�ZVmwareUplinkPortTeamingPolicyr�)r�Zpg_specZpg_confr�r3r3r4�_apply_dvportgroup_config�sN





������r�c	Cs�t�d|||�t�}|dkrtd�d}t|�}n|dkr.td�d}tjj�||�}tjjj	||gd�}|sBt
d|�d	���||d
<t��}t
|||�tjj�|d|�dS)
a�
    Creates a distributed virtual portgroup.

    Note: The ``portgroup_name`` param will override any name already set
    in ``portgroup_dict``.

    portgroup_dict
        Dictionary with the config values the portgroup should be created with
        (example in salt.states.dvs).

    portgroup_name
        Name of the portgroup to be created.

    dvs
        Name of the DVS that will contain the portgroup.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.create_dvportgroup portgroup_dict=<dict>
            portgroup_name=pg1 dvs=dvs1
    z2Creating portgroup '%s' in dvs '%s' with dict = %sr.rHrlr-rGr�r�r�r?rT)r�rHrErTrrr7r8rersrtrr�DVPortgroupConfigSpecr��create_dvportgroup)	�portgroup_dictZportgroup_namer|rbr[rlr{r��specr3r3r4r��s* �
r�cCst�d|||�t�}|dkrtd�d}t|�}n|dkr.td�d}tjj�||�}tjjj	||gd�}|sBt
d|�d	���tjjj|d
|gd�}|sXt
d|�d	���tjj�|d
d
g�}	t
��}
dD]
}t|
|t|	d
|��qit||
|�tjj�|d
|
�dS)a�
    Updates a distributed virtual portgroup.

    portgroup_dict
        Dictionary with the values the portgroup should be update with
        (example in salt.states.dvs).

    portgroup
        Name of the portgroup to be updated.

    dvs
        Name of the DVS containing the portgroups.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.update_dvportgroup portgroup_dict=<dict>
            portgroup=pg1

        salt '*' vsphere.update_dvportgroup portgroup_dict=<dict>
            portgroup=pg1 dvs=dvs1
    z2Updating portgroup '%s' in dvs '%s' with dict = %sr.rHrlr-rGr�r�r�r�r��Portgroup 'r�)Z
autoExpand�
configVersionr�rBr?r�r�ZportNameFormatZscoper�ZvendorSpecificConfigT)r�rHrErTrrr7r8rersrtrr�rurr�r�r�r��update_dvportgroup)r��	portgroupr|rbr[rlr{r��pg_refsZpg_propsr�r�r3r3r4r�-s< �

�
�
r�cCs�t�d||�t�}|dkrtd�d}t|�}n|dkr-td�d}tjj�||�}tjjj	||gd�}|sAt
d|�d	���tjjj|d
|gd�}|sWt
d|�d	���tjj�|d
�d
S)a�
    Removes a distributed virtual portgroup.

    portgroup
        Name of the portgroup to be removed.

    dvs
        Name of the DVS containing the portgroups.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.remove_dvportgroup portgroup=pg1 dvs=dvs1
    z#Removing portgroup '%s' in dvs '%s'r.rHrlr-rGr�r�r�rr�r�T)
r�rHrErTrrr7r8rersrtrr��remove_dvportgroup)r�r|rbr[rlr{r�r�r3r3r4r�zs$

�r�c
Cs|j|j|jjd�}g}t|tjj�rzt|jtjj�rz|jj	D]Z}|j|j
d�}g}|jD]B}|jj
|jjd�}|jdjdj}t|tjjj�rRd|j|jd�}	nt|tjjj�rad|jd�}	nd	|d
�}	|	|d<|�|�q-||d<|�|�q||d
<|S)z/Returns a dictionary representation of a policy)r?rBZ
resource_type)r?�force_provision)�	namespacerr�range)r��min�max�set)r��values�scalar)r�r��setting�capabilities�subprofiles)r?rB�resourceTyper{r�profileZCapabilityBasedProfile�constraints�SubProfileCapabilityConstraints�subProfiles�forceProvisionrmrr��
constraint�propertyInstancer��types�Ranger�r��DiscreteSetr�rU)
r�Zprofile_dictZsubprofile_dictsZ
subprofile�subprofile_dictZ	cap_dictsZcap�cap_dict�valZval_dictr3r3r4�_get_policy_dict�s6�
��

rcCsDtjj�|�}|stjjj|dd�}ntjj�||�}dd�|D�S)a�
    Returns a list of storage policies.

    policy_names
        Names of policies to list. If None, all policies are listed.
        Default is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_storage_policies

        salt '*' vsphere.list_storage_policies policy_names=[policy_name]
    T�Zget_all_policiescS�g|]}t|��qSr3)r�r��pr3r3r4r��r�z)list_storage_policies.<locals>.<listcomp>)r7r8r�get_profile_manager�get_storage_policies)Zpolicy_namesrb�profile_manager�policiesr3r3r4�list_storage_policies�s�rcCsFtjj�|�}tjjj|dd�}dd�|D�}|std��t|d�S)z�
    Returns the default vsan storage policy.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_default_vsan_policy
    TrcSsg|]	}|jdkr|�qS)ZVsanDefaultProfile)ZsystemCreatedProfileTyperr3r3r4r�sz,list_default_vsan_policy.<locals>.<listcomp>z%Default VSAN policy was not retrievedr)r7r8rrrrr)rbrrZdef_policiesr3r3r4�list_default_vsan_policy�s��rcCs(|jj|jj|j|jj|jdjjd�S)Nr)r�r�	mandatoryrBr�)rr�r�summaryZpropertyMetadatar�ZtypeName)Zcap_metadatar3r3r4�_get_capability_definition_dict
s�rcCs*tjj�|�}dd�tjj�|�D�}|S)a
    Returns a list of the metadata of all capabilities in the vCenter.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_capabilities
    cSrr3)r)r��cr3r3r4r�*s��z/list_capability_definitions.<locals>.<listcomp>)r7r8rrZget_capability_definitions)rbrrzr3r3r4�list_capability_definitionss
�rc
Cs�t�d|�|�d�r|d|_|�d�r|d|_|�d�r�tj��|_g}|dD]�}tjjj	|dd�}g}|�d�rC|d|_
|dD]k}tjj|dd	�}|d
d}|dkrltjj
��|_|d
d
|j_n'|dkr�tjj
��|_|d
d|j_|d
d|j_n|dkr�|d
d|_tjjtjjj|d|dd�tjj|gd�gd�}	|�|	�qG||_|�|�q+||j_t�d|�|S)z,Applies a policy dictionary to a policy speczpolicy_dict = %sr?rBr��r?r�r�rrr�r�r�r�r�r�r�r�r�r�)rr�)r)rrzupdated policy_spec = %s)r�rHrDr?rBrrrrZ
SubProfilerrmZPropertyInstancerr
r�r�r	r�r�ZCapabilityInstanceZCapabilityMetadataZUniqueIdZConstraintInstancerUr)
Zpolicy_spec�policy_dictr�rZsubprofile_specZ	cap_specsrZprop_inst_specZsetting_typeZcap_specr3r3r4�_apply_policy_config1sT




�

����
r cCspt�d||�tjj�|�}tj��}tjjtjj	j
d�|_||d<t�d�t||�tjj�
||�ddiS)ap
    Creates a storage policy.

    Supported capability types: scalar, set, range.

    policy_name
        Name of the policy to create.
        The value of the argument will override any existing name in
        ``policy_dict``.

    policy_dict
        Dictionary containing the changes to apply to the policy.
        (example in salt.states.pbm)

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.create_storage_policy policy_name='policy name'
            policy_dict="$policy_dict"
    z%create storage policy '%s', dict = %s)rr?�+Setting policy values in policy_update_spec�create_storage_policyT)r�rHr7r8rrrZ CapabilityBasedProfileCreateSpecZResourceTypeZResourceTypeEnumZSTORAGErr r")Zpolicy_namerrbrZpolicy_create_specr3r3r4r"cs
�

r"cCs�t�d|�tjj�|�}tjj�||g�}|s td|�d���|d}tj�	�}t�d�dD]}t
||t||��q0t||�tjj�
|||�ddiS)	a
    Updates a storage policy.

    Supported capability types: scalar, set, range.

    policy
        Name of the policy to update.

    policy_dict
        Dictionary containing the changes to apply to the policy.
        (example in salt.states.pbm)

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.update_storage_policy policy='policy name'
            policy_dict="$policy_dict"
    z"updating storage policy, dict = %s�Policy '�' was not foundrr!)rBr�update_storage_policyT)r�rHr7r8rrrrrZ CapabilityBasedProfileUpdateSpecr�r�r r%)r�rrbrr�
policy_refZpolicy_update_specr�r3r3r4r%�s


�r%cCsht�d|�t|�}tjjj|||gd�}|std|�d���tjj�	|�}tjj�
||d�}t|�S)a�
    Returns a list of datastores assign the storage policies.

    datastore
        Name of the datastore to assign.
        The datastore needs to be visible to the VMware entity the proxy
        points to.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_default_storage_policy_of_datastore datastore=ds1
    z4Listing the default storage policy of datastore '%s'��datastore_names�Datastore 'r$r)r�rHrrr7r8re�get_datastoresrrrZ'get_default_storage_policy_of_datastorer)�	datastorerb�
target_ref�ds_refsrr�r3r3r4�(list_default_storage_policy_of_datastore�s��r.c	Cs�t�d||�tjj�|�}tjj�||g�}|s!td|�d���|d}t|�}tjj	j
|||gd�}|s>td|�d���|d}tjj�|||�dS)a
    Assigns a storage policy as the default policy to a datastore.

    policy
        Name of the policy to assign.

    datastore
        Name of the datastore to assign.
        The datastore needs to be visible to the VMware entity the proxy
        points to.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.assign_storage_policy_to_datastore
            policy='policy name' datastore=ds1
    z#Assigning policy %s to datastore %sr#r$rr'r)T)r�rHr7r8rrrrrrrer*�*assign_default_storage_policy_to_datastore)	r�r+rbrrr&r,r-�ds_refr3r3r4r/�s"��r/cCs6|stjjj|dd�}ntjj�||�}dd�|D�S)a�
    Returns a list of dict representations of VMware datacenters.
    Connection is done via the proxy details.

    Supported proxies: esxdatacenter

    datacenter_names
        List of datacenter names.
        Default is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_datacenters_via_proxy

        salt '*' vsphere.list_datacenters_via_proxy dc1

        salt '*' vsphere.list_datacenters_via_proxy dc1,dc2

        salt '*' vsphere.list_datacenters_via_proxy datacenter_names=[dc1, dc2]
    T)Zget_all_datacenterscSsg|]}dtjj�|�i�qSr�r7r8re�get_managed_object_name)r�r{r3r3r4r�;s��z.list_datacenters_via_proxy.<locals>.<listcomp>)r7r8reZget_datacenters)Zdatacenter_namesrbZdc_refsr3r3r4�list_datacenters_via_proxys��r3cCstjj�||�ddiS)a@
    Creates a datacenter.

    Supported proxies: esxdatacenter

    datacenter_name
        The datacenter name

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.create_datacenter dc1
    �create_datacenterT)r7r8rer4)�datacenter_namerbr3r3r4r4Asr4c
Cst�d|�tjjj|dgd�}d|djjid|djjid�}|dj}t�d|�|j	|dd<|j
rKt|j
tj
�rK|j
}|j|jd	�|dd
<|jr\|j}|j|jd�|dd<|j|dd
<|jrm|j|dd<|jr|dd�|jD�|dd<|j|dd<|dj}t�d|�d|j|dd<|j|dd<|dj|d<tjj�|�}tjj�|��rtjj�|�}	t|	j�dkr�tjj� |�}
t�d|
�|
j|
j!j"d�|d<|
j#r�|
j#}|d�$|j%p�d|j&d��|S|dj'�r|dj'j!}|dj'j|j"d�|d<|S)z�
    Returns a cluster dict representation from
    a vim.ClusterComputeResource object.

    cluster_name
        Name of the cluster

    cluster_ref
        Reference to the cluster
    z4Building a dictionary representation of cluster '%s'�configurationEx��
propertiesr�)�ha�drszha_conf = %sr9�admission_control_enabled)�cpu_failover_percent�memory_failover_percent�admission_control_policy)�isolation_response�restart_priority�default_vm_settings�hb_ds_candidate_policy�host_monitoringcSsg|]	}|j|jd��qS)�r�r�rD)r��or3r3r4r��s�z%_get_cluster_dict.<locals>.<listcomp>�options�
vm_monitoringz
drs_conf = %s�r:�vmotion_rate�default_vm_behavior�vm_swap_placement�jv7zvsan_conf = %s)r��auto_claim_storager9F)�compression_enabled�
dedup_enabled)(r�rHr7r8reru�	dasConfigr��	drsConfig�admissionControlEnabled�admissionControlPolicyr{r�.ClusterFailoverResourcesAdmissionControlPolicy�cpuFailoverResourcesPercent�memoryFailoverResourcesPercent�defaultVmSettings�isolationResponse�restartPriority�hBDatastoreCandidatePolicy�hostMonitoring�option�vmMonitoring�vmotionRate�defaultVmBehavior�vmSwapPlacement�(get_service_instance_from_managed_objectr9�vsan_supported�get_service_info�int�build�get_cluster_vsan_info�
defaultConfig�autoClaimStorage�dataEfficiencyConfigr|�compressionEnabled�dedupEnabled�vsanConfigInfo)
�cluster_name�cluster_refr~riZha_confZpolZ
def_vm_setZdrs_conf�si�vcenter_infoZ	vsan_confZdata_effZdefault_configr3r3r4�_get_cluster_dict\sv��
����

����

�rqcCsjt�}|dkrt|�}|std��tjj�||�}n|dkr)t|�}td�d}t�	d||�t
||�S)a�
    Returns a dict representation of an ESX cluster.

    datacenter
        Name of datacenter containing the cluster.
        Ignored if already contained by proxy details.
        Default value is None.

    cluster
        Name of cluster.
        Ignored if already contained by proxy details.
        Default value is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        # vcenter proxy
        salt '*' vsphere.list_cluster datacenter=dc1 cluster=cl1

        # esxdatacenter proxy
        salt '*' vsphere.list_cluster cluster=cl1

        # esxcluster proxy
        salt '*' vsphere.list_cluster
    r.�'cluster' needs to be specifiedr-rG�clusterz7Retrieving representation of cluster '%s' in a %s proxy)rErrrr7r8re�get_clusterrTr�rHrq)rlrsrbr[r{rnr3r3r4�list_cluster�s#�
ruc
CsTt�d|�|�d�r�|d}|jst��|_|j}d|vr*|d|_|dr*d|_d|vr3|d|_d|vrQ|d}|j	rEt
|j	tj�sQtj|d|dd	�|_	d
|vru|d
}|jsat�
�|_d|vrk|d|j_d|vru|d|j_d
|vr~|d
|_d|vr�|d|_d|vr�g|_|dD]}|j�tj|dd��d|vr�|d|jd_q�d|vr�|d|_||_|�d�r�|d}	t��}
d|	vr�|	d|
_d|	vr�d|	d|
_d|	vr�t�|	d�|
_|
|_|�d�r�|d|_|�d��r�|d}|�snd|v�r|j�stjj� �|_|d|j_d|v�rD|j�s&tjj� �|_|jj!�s2t�"�|j_!n|jj!j#�r=d|jj!_#|d|jj!_$d|v�rY|j%�sStj�&�|_%|d|j%_'d|v�rn|j%�shtj�&�|_%|d|j%_(|j)�swt�*�|_)|j)}d|v�r�|d|_d|v�r�|j!�s�t�"�|_!n	|j!j#�r�d|j!_#|d|j!_$t�d|�dS) a�
    Applies the values of cluster_dict dictionary to a cluster spec
    (vim.ClusterConfigSpecEx).

    All vsan values (cluster_dict['vsan']) will be applied to
    vsan_spec (vim.vsan.cluster.ConfigInfoEx). Can be not omitted
    if not required.

    VSAN 6.1 config needs to be applied differently than the post VSAN 6.1 way.
    The type of configuration desired is dictated by the flag vsan_61.
    zApplying cluster dict %sr9r�r�r;r>r<r=)rUrVrAr?r@rBrCrFr�r�r����rGr:rIrHrJrKr9rMNrNrOzcluster_spec = %s)+r�rHrDrPrZClusterDasConfigInfor�Z
failoverLevelrRrSr{rTrWZClusterDasVmSettingsrXrYrZr[r\rU�OptionValuer�r]ZClusterDrsConfigInfor^ZDrsBehaviorr_rQr`ZvsanClusterConfigr9rsr:rgZ$VsanClusterConfigInfoHostDefaultInfo�uuidrhriZDataEfficiencyConfigrjrk�
vsanConfigZVsanClusterConfigInfo)
�cluster_spec�cluster_dict�	vsan_spec�vsan_61Zha_dictZ
das_configZadm_pol_dictZvm_set_dictZopt_dictZdrs_dictZ
drs_configZ	vsan_dictr�r3r3r4�_apply_cluster_dict�s�



�����

��

�


�




�

�
�





r~c
Cs�t��}zt�||�Wntjjy}zt|��d}~wwt�}|dkr8td�d}t	|�}|s7t
d��n|dkrRtd�d}tjj
�||�}td�d}|�d	�rbtjj�|�sbtd
��|}t��}	d}
d}d}|�d	�r�tjj
�|�}
t|
j�dkr�t|
j�dkr�tjjd
d�}
d}|�di��d�r�d
}|d}|d=nd
}t|	||
|�tjj
�|||	�|s�|
r�tjj
�||�}tjj�||
�|r�t|	d|i�tjj
�||	�||d<dd
iS)a5
    Creates a cluster.

    Note: cluster_dict['name'] will be overridden by the cluster param value

    config_dict
        Dictionary with the config values of the new cluster.

    datacenter
        Name of datacenter containing the cluster.
        Ignored if already contained by proxy details.
        Default value is None.

    cluster
        Name of cluster.
        Ignored if already contained by proxy details.
        Default value is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        # esxdatacenter proxy
        salt '*' vsphere.create_cluster cluster_dict=$cluster_dict cluster=cl1

        # esxcluster proxy
        salt '*' vsphere.create_cluster cluster_dict=$cluster_dict
    Nr.rHrlrrr-rGrsr9�!VSAN operations are not supported�@rLT��modifyFr9r��create_cluster) r�	serialize�
jsonschema�validate�
exceptions�ValidationErrorrrErTrrrr7r8rersrDr9rbrr�ClusterConfigSpecExrc�float�
apiVersionrdre�ReconfigSpecr~r�rt�reconfigure_cluster_vsan�update_cluster)r{rlrsrb�schema�excr[r{rorzr|Z	ha_configr}rpZ	enable_harnr3r3r4r�gsb'����
�r�c
Cs<t��}zt�||�Wntjjy}zt|��d}~wwt�}|dkr8td�d}t	|�}|s7t
d��n|dkrRtd�d}tjj
�||�}td�d}|�d	�rbtjj�|�sbtd
��tjj
�||�}t��}	tjj
j|dgd�}
d
D]
}t|	|t|
d|��qz|
djr�|
dj|	_d}d}
|�d	�r�tjj
�|�}t|j�dkr�t|j�dkr�d}
tjj� |�}tjj!dd�}|j"|_"d|_"nd}
t#|	|||
�|�rt$�%d|�tjj�&||�t��}	tjj
j|dgd�}
d
D]
}t|	|t|
d|��q�|
dj�r
|
dj|	_t#|	|�tjj
�'||	�ddiS)a�
    Updates a cluster.

    config_dict
        Dictionary with the config values of the new cluster.

    datacenter
        Name of datacenter containing the cluster.
        Ignored if already contained by proxy details.
        Default value is None.

    cluster
        Name of cluster.
        Ignored if already contained by proxy details.
        Default value is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        # esxdatacenter proxy
        salt '*' vsphere.update_cluster cluster_dict=$cluster_dict cluster=cl1

        # esxcluster proxy
        salt '*' vsphere.update_cluster cluster_dict=$cluster_dict

    Nr.rHrlrrr-rGrsr9rr6r7)rPrQr�rLFTr�zvsan_spec = %sr�)(rr�r�r�r�r�rrErTrrrr7r8rersrDr9rbrrtrr�rur�r�rlryrcr�r�rdrerfr�rir~r�rHr�r�)r{rlrsrbr�r�r[r{rnrzr~rr|r}rpZ	vsan_infor3r3r4r��sr&�����

�
r�csLt|�}tjj�|�}t�d|�|s|s|sdnd}|r@t�d|�dd�tjjj||d�D�}t�d|�|r>|�	|�n|}tjj�
|||||�}g}	t|tj
�rktjj�|||�}
tjj�|
d	g�}|�d	g�}	g}|D]4��j�jj�jj�jjd
�}
g}�fdd�|	D�D]
}|�	dd�|jD��q�|r�||
d
<|�|
�qo|S)a�
    Returns a list of dict representations of the datastores visible to the
    proxy object. The list of datastores can be filtered by datastore names,
    backing disk ids (canonical names) or backing disk scsi addresses.

    Supported proxy types: esxi, esxcluster, esxdatacenter

    datastore_names
        List of the names of datastores to filter on

    backing_disk_ids
        List of canonical names of the backing disks of the datastores to filer.
        Default is None.

    backing_disk_scsi_addresses
        List of scsi addresses of the backing disks of the datastores to filter.
        Default is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_datastores_via_proxy

        salt '*' vsphere.list_datastores_via_proxy datastore_names=[ds1, ds2]
    ztarget name = %sTFz+Retrieving disk ids for scsi addresses '%s'cS�g|]}|j�qSr3�r��r��dr3r3r4r�xs��z-list_datastores_via_proxy.<locals>.<listcomp>)�scsi_addresseszFound disk ids '%s'zfileSystemVolumeInfo.mountInfo)r?r�Z
free_space�capacitycs.g|]}|jj�jkrt|jtj�r|j�qSr3)Zvolumer?r{rZHostVmfsVolume)r��i��dsr3r4r��s
�cSr�r3)ZdiskName)r�rlr3r3r4r����backing_disk_ids)rrr7r8rer2r�rHr��	get_disks�extendr*r{rZ
HostSystemZget_storage_systemrurDr?rr�Z	freeSpacer�ZextentrU)r(r�Zbacking_disk_scsi_addressesrb�target�target_nameZget_all_datastores�disk_ids�
datastoresZmount_infos�storage_systemr~r�Zds_dictZvolr3r�r4�list_datastores_via_proxyAsf(��������
����

�r�c
Cs�t�d�t��}zt�d|||d�i|�Wntjjy)}zt|��d}~wwt	|�}t
d�d}|rNtjj
j||gd�}	|	sNtd|�d	|�d
���tjj
�|||	d|�}
dS)
a�
    Creates a ESXi host disk group with the specified cache and capacity disks.

    datastore_name
        The name of the datastore to be created.

    disk_id
        The disk id (canonical name) on which the datastore is created.

    vmfs_major_version
        The VMFS major version.

    safety_checks
        Specify whether to perform safety check or to skip the checks and try
        performing the required task. Default is True.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.create_vmfs_datastore datastore_name=ds1 disk_id=
            vmfs_major_version=5
    zValidating vmfs datastore inputr+)r?Zbacking_disk_idZvmfs_versionNrFrt�r�zDisk 'z' was not found in host 'rJrT)r�r�rr�r�r�r�r�rrr�	__proxy__r7r8rer�r�create_vmfs_datastore)�datastore_name�disk_idZvmfs_major_version�
safety_checksrbr�r�r��hostnamer�r0r3r3r4r��s8
'���
����r�cCs\t�d||�t|�}tjjj|||gd�}|s td|�d���|d}tjj�||�dS)a�
    Renames a datastore. The datastore needs to be visible to the proxy.

    datastore_name
        Current datastore name.

    new_datastore_name
        New datastore name.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.rename_datastore old_name new_name
    zRenaming datastore %s to %sr'r)r$rT)	r�rHrrr7r8rer*r�rename_datastore)r�Znew_datastore_namerbr�r�r�r3r3r4r��s�r�cCsrt�d|�t|�}tjjj|||gd�}|std|�d���t|�dkr-td|�d���tjj�	||d�d	S)
aF
    Removes a datastore. If multiple datastores an error is raised.

    datastore
        Datastore name

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.remove_datastore ds_name
    zRemoving datastore '%s')�	referencer(r)r$r�zMultiple datastores 'z' were foundrT)
r�rHrrr7r8rer*rrd�remove_datastore)r+rbr�r�r3r3r4r�s�
�r�cCs*t�d�tjj�|�}dd�|D�}|S)z�
    Lists all licenses on a vCenter.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_licenses
    zRetrieving all licensescSsPg|]$}|j|j|jr|jdjnd|jdkr|jntj|jr"|jndd��qS)rN)r�r?rBr��used)�
licenseKeyr?�labelsr��total�sys�maxsizer��r��lr3r3r4r�Hs	���z!list_licenses.<locals>.<listcomp>)r�rHr7r8re�get_licenses)rb�licensesr�r3r3r4�
list_licenses4s
	�r�cCs"t�d|�tjj�|||�dS)a

    Adds a license to the vCenter or ESXi host

    key
        License key.

    description
        License description added in as a label.

    safety_checks
        Specify whether to perform safety check or to skip the checks and try
        performing the required task

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.add_license key=<license_key> desc='License desc'
    zAdding license '%s'T)r�rHr7r8re�add_license)r�rBr�rbr3r3r4r�Vsr�cCsbt�d|�|ddkr tjj�||d�}tjj�||d�S|ddkr(dStd�|d���)a�
    Returns the entity associated with the entity dict representation

    Supported entities: cluster, vcenter

    Expected entity format:

    .. code-block:: python

        cluster:
            {'type': 'cluster',
             'datacenter': <datacenter_name>,
             'cluster': <cluster_name>}
        vcenter:
            {'type': 'vcenter'}

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.

    entity
        Entity dict in the format above
    zRetrieving entity: %sr�rsrlr/N�Unsupported entity type '{}')	r�rHr7r8rersrtrrY)rb�entityr{r3r3r4�_get_entityws�r�c
Csv|ddkrt��}n|ddkrt��}n	td�|d���z	t�||�WdStjjy:}zt	|��d}~ww)z�
    Validates the entity dict representation

    entity
        Dictionary representation of an entity.
        See ``_get_entity`` docstrings for format.
    r�rsr/r�N)
rr�rrrYr�r�r�r�r)r�r�r�r3r3r4�_validate_entity�s


��r�cs@t�d|�t|�tjjj|t||�|d�}�fdd�|D�S)ab
    Lists the licenses assigned to an entity

    entity
        Dictionary representation of an entity.
        See ``_get_entity`` docstrings for format.

    entity_display_name
        Entity name used in logging

    license_keys:
        List of license keys to be retrieved. Default is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_assigned_licenses
            entity={type:cluster,datacenter:dc,cluster:cl}
            entiy_display_name=cl
    z&Listing assigned licenses of entity %s�Z
entity_refZentity_namecsTg|]&}�dus
|j�vr|j|j|jr|jdjnd|jdkr#|jntjd��qS)Nr)r�r?rBr�)r�r?r�r�r�r�r�r���license_keysr3r4r��s��z*list_assigned_licenses.<locals>.<listcomp>)r�rHr�r7r8reZget_assigned_licensesr�)r��entity_display_namer�rbZassigned_licensesr3r�r4�list_assigned_licenses�s!�
�r�cslt�d�|�t|�|r%tjj�|�}�fdd�|D�s%td|�d���tjjj|�|t	||�|d�dS)a
    Assigns a license to an entity

    license_key
        Key of the license to assign
        See ``_get_entity`` docstrings for format.

    license_name
        Display name of license

    entity
        Dictionary representation of an entity

    entity_display_name
        Entity name used in logging

    safety_checks
        Specify whether to perform safety check or to skip the checks and try
        performing the required task. Default is False.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.assign_license license_key=AAAAA-11111-AAAAA-11111-AAAAA
            license_name=test entity={type:cluster,datacenter:dc,cluster:cl}
    z!Assigning license %s to entity %sc�g|]	}|j�kr|�qSr3)r�r���license_keyr3r4r��z"assign_license.<locals>.<listcomp>z	License 'z' wasn't foundr�N)
r�rHr�r7r8rer�r�assign_licenser�)r�Zlicense_namer�r�r�rbr�r3r�r4r��s,
�r�cCsF|r
|s
tj�d��d}|sd}tjjj|||||d�}dd�|D�S)a~
    Returns a list of hosts for the specified VMware environment. The list
    of hosts can be filtered by datacenter name and/or cluster name

    hostnames
        Hostnames to filter on.

    datacenter_name
        Name of datacenter. Only hosts in this datacenter will be retrieved.
        Default is None.

    cluster_name
        Name of cluster. Only hosts in this cluster will be retrieved. If a
        datacenter is not specified the first cluster with this name will be
        considerred. Default is None.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_hosts_via_proxy

        salt '*' vsphere.list_hosts_via_proxy hostnames=[esxi1.example.com]

        salt '*' vsphere.list_hosts_via_proxy datacenter=dc1 cluster=cluster1
    z0Datacenter is required when cluster is specifiedFT)r5r�rm�
get_all_hostscSsg|]	}tjj�|��qSr3r1)r��hr3r3r4r�Vr�z(list_hosts_via_proxy.<locals>.<listcomp>)r7r�rr8re�	get_hosts)Z	hostnamesrlrsrbr��hostsr3r3r4�list_hosts_via_proxy"s %��r�c
Cs�t|�}td�d}t�d|||�|s|sdnd}g}tjjj||d�}dd�|��D�}tjj�	||||�D]}	|�
|	j||	jd	��q9|S)
a�
    Returns a list of dict representations of the disks in an ESXi host.
    The list of disks can be filtered by disk canonical names or
    scsi addresses.

    disk_ids:
        List of disk canonical names to be retrieved. Default is None.

    scsi_addresses
        List of scsi addresses of disks to be retrieved. Default is None


    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_disks

        salt '*' vsphere.list_disks disk_ids='[naa.00, naa.001]'

        salt '*' vsphere.list_disks
            scsi_addresses='[vmhba0:C0:T0:L0, vmhba1:C0:T0:L0]'
    rFrtz?Retrieving disks of host '%s'; disc ids = %s; scsi_address = %sTF�r�cSsi|]\}}|j|�qSr3r�)r�Z	scsi_addrZlunr3r3r4�
<dictcomp>�s�zlist_disks.<locals>.<dictcomp>)r�scsi_address)rrr�r�rHr7r8re�get_scsi_address_to_lun_mapr�r�rUr�)
r�r�rbr�r�Z
get_all_disksrz�scsi_address_to_lunZcanonical_name_to_scsi_addressr�r3r3r4�
list_disksYs2 ������r�cCs�|s|std��t|�}td�d}|s5tjj�|�}||vr(td�||���||j	}t
�d|||�t
�d||�tjjj||||d�t
�
d||�d	S)
a
    Erases the partitions on a disk.
    The disk can be specified either by the canonical name, or by the
    scsi_address.

    disk_id
        Canonical name of the disk.
        Either ``disk_id`` or ``scsi_address`` needs to be specified
        (``disk_id`` supersedes ``scsi_address``.

    scsi_address
        Scsi address of the disk.
        ``disk_id`` or ``scsi_address`` needs to be specified
        (``disk_id`` supersedes ``scsi_address``.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.erase_disk_partitions scsi_address='vmhaba0:C0:T0:L0'

        salt '*' vsphere.erase_disk_partitions disk_id='naa.000000000000001'
    �8Either 'disk_id' or 'scsi_address' needs to be specifiedrFrt�5Scsi lun with address '{}' was not found on host '{}'�+[%s] Got disk id '%s' for scsi address '%s'z1Erasing disk partitions on disk '%s' in host '%s'r�z0Erased disk partitions on disk '%s' on host '%s'T)rrrr�r7r8rer�rrYr�r�rH�erase_disk_partitions�info)r�r�rbr�r�r�r3r3r4r��s6 ���
��r�c
	s|s|std��t|�}td�d}|s5tjj�|�}||vr(td�||���||j	}t
�d|||�t
�d||�tjj�||�}g}|j
jD]6��fdd�|jjD�d	}|||j
j�j�j�j�jd
|jj|jjd
|jjdd�}	|�|	�qJ|S)
a
    Lists the partitions on a disk.
    The disk can be specified either by the canonical name, or by the
    scsi_address.

    disk_id
        Canonical name of the disk.
        Either ``disk_id`` or ``scsi_address`` needs to be specified
        (``disk_id`` supersedes ``scsi_address``.

    scsi_address`
        Scsi address of the disk.
        ``disk_id`` or ``scsi_address`` needs to be specified
        (``disk_id`` supersedes ``scsi_address``.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_disk_partitions scsi_address='vmhaba0:C0:T0:L0'

        salt '*' vsphere.list_disk_partitions disk_id='naa.000000000000001'
    r�rFrtr�r�z1Listing disk partitions on disk '%s' in host '%s'csg|]
}|j�jkr|�qSr3)�	partitionr�Z	part_specr3r4r�s
�z(list_disk_partitions.<locals>.<listcomp>rr��)r�r-rYr�r�ZsectorsZsize_KB)rrrr�r7r8rer�rrYr�r�rHZget_disk_partition_infor�r�ZlayoutZpartitionFormatr�Z	endSectorZstartSector�end�block�startZ	blockSizerU)
r�r�rbr�r�r�Zpartition_inforzZpart_layoutZ	part_dictr3r�r4�list_disk_partitions�sX ���
�
�����r�cCsnt|�}td�d}t�d|�|sdnd}g}tjj�|||�D]}|�|j	j
dd�|jD�d��q"|S)	a�
    Returns a list of disk group dict representation on an ESXi host.
    The list of disk groups can be filtered by the cache disks
    canonical names. If no filtering is applied, all disk groups are returned.

    cache_disk_ids:
        List of cache disk canonical names of the disk groups to be retrieved.
        Default is None.

    use_proxy_details
        Specify whether to use the proxy minion's details instead of the
        arguments

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.list_diskgroups

        salt '*' vsphere.list_diskgroups cache_disk_ids='[naa.000000000000001]'
    rFrtzListing diskgroups in '%s'TFcSr�r3r�r�r3r3r4r�Kr�z#list_diskgroups.<locals>.<listcomp>)�
cache_disk�capacity_disks)rrr�r�rHr7r8re�get_diskgroupsrU�ssdr�ZnonSsd)�cache_disk_idsrbr�r�Zget_all_diskgroupsrz�dgr3r3r4�list_diskgroups"s���r�c
s@t�d�t��}zt�d��d�gi|�Wntjjy)}zt|��d}~wwt	|�}t
d�d}|rJtjj
�|�g�}|rJtd��|����dd�}	|	�d��tjj
j||	d	�}
|	D]��fd
d�|
D�swtd��d
|�d���qa�fdd�|
D�d}�fdd�|
D�}tjj�|�}
tjj�||
|||�}dS)a7
    Creates disk group on an ESXi host with the specified cache and
    capacity disks.

    cache_disk_id
        The canonical name of the disk to be used as a cache. The disk must be
        ssd.

    capacity_disk_ids
        A list containing canonical names of the capacity disks. Must contain at
        least one id. Default is True.

    safety_checks
        Specify whether to perform safety check or to skip the checks and try
        performing the required task. Default value is True.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.create_diskgroup cache_disk_id='naa.000000000000001'
            capacity_disk_ids='[naa.000000000000002, naa.000000000000003]'
    �Validating diskgroup input�
diskgroups�Zcache_idZcapacity_idsNrFrtz?Diskgroup with cache disk id '{}' already exists ESXi host '{}'rr�cr�r3r�r�rr3r4r��r�z$create_diskgroup.<locals>.<listcomp>zNo disk with id 'z' was found in ESXi host 'rJcr�r3r�r�)�
cache_disk_idr3r4r��r�c�g|]	}|j�vr|�qSr3r�r���capacity_disk_idsr3r4r��r�T)r�rHrr�r�r�r�r�rrrr�r7r8rer�rrY�insertr�rr9�get_vsan_disk_management_system�create_diskgroup)r�r�r�rbr�r�r�r�r�r�r�r�r��vsan_disk_mgmt_systemr�r3)r�r�rr4r�QsR
#���������
�r�c
st�d�t��}zt�d||d�gi|�Wntjjy)}zt|��d}~wwt	|�}t
d�d}tjj
j||d�}|rV|D]��fdd	�|D�sUtd
��|���qBtjj
j||gd�}	|	sjtd�||���tjj�|�}
tjj�||
||	d
|�dS)a�
    Adds capacity disks to the disk group with the specified cache disk.

    cache_disk_id
        The canonical name of the cache disk.

    capacity_disk_ids
        A list containing canonical names of the capacity disks to add.

    safety_checks
        Specify whether to perform safety check or to skip the checks and try
        performing the required task. Default value is True.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.add_capacity_to_diskgroup
            cache_disk_id='naa.000000000000001'
            capacity_disk_ids='[naa.000000000000002, naa.000000000000003]'
    r�r�r�NrFrtr�cr�r3r�r�rr3r4r��r�z-add_capacity_to_diskgroup.<locals>.<listcomp>�0No disk with id '{}' was found in ESXi host '{}'�r��@No diskgroup with cache disk id '{}' was found in ESXi host '{}'rT)r�rHrr�r�r�r�r�rrrr�r7r8rer�rrYr�r9r��add_capacity_to_diskgroup)r�r�r�rbr�r�r�r�r�r�r�r3rr4r��sT
!�������������r�c
st�d�t��}zt�d|�d�gi|�Wntjjy+}ztt	|���d}~wwt
|�}td�d}tj
jj|�d�}	|rX�D]��fdd	�|	D�sWtd
��|���qDtj
jj||gd�}
|
sltd�||���t�d
|�tj
jj|||
d�fdd	�|	D�|d�dS)a�
    Remove capacity disks from the disk group with the specified cache disk.

    cache_disk_id
        The canonical name of the cache disk.

    capacity_disk_ids
        A list containing canonical names of the capacity disks to add.

    data_evacuation
        Specifies whether to gracefully evacuate the data on the capacity disks
        before removing them from the disk group. Default value is True.

    safety_checks
        Specify whether to perform safety check or to skip the checks and try
        performing the required task. Default value is True.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.remove_capacity_from_diskgroup
            cache_disk_id='naa.000000000000001'
            capacity_disk_ids='[naa.000000000000002, naa.000000000000003]'
    r�r�r�NrFrtr�cr�r3r�r�rr3r4r�#r�z2remove_capacity_from_diskgroup.<locals>.<listcomp>r�r�r�zdata_evacuation = %srcr�r3r�r�r�r3r4r�7r�)r��data_evacuationT)r�rHrr�r�r�r�r�rr4rrr�r7r8rer�rrYr�r9�remove_capacity_from_diskgroup)r�r�r�r�rbr�r�r�r�r�r�r3)r�rr4r��sX
)������������r�cCsrt�d�t|�}td�d}tjjj||gd�}|s$td�	||���t�d|�tjj
j|||d|d�d	S)
a�
    Remove the diskgroup with the specified cache disk.

    cache_disk_id
        The canonical name of the cache disk.

    data_accessibility
        Specifies whether to ensure data accessibility. Default value is True.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.remove_diskgroup cache_disk_id='naa.000000000000001'
    r�rFrtr�r�zdata accessibility = %sr)�data_accessibilityT)r�rHrrr�r7r8rer�rrYr9�remove_diskgroup)r�r�rbr�r�r�r3r3r4r�=s"
����r�cCs`i}t|�}td�d}tjj�|�}|s"t�d|�d|d<|Sdd|jj	i|j
�d�d	�S)
a
    Returns the host cache configuration on the proxy host.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.get_host_cache
    rFrtz&Host cache not configured on host '%s'Fr�Tr?ZMiB)r�r+Z	swap_size)rrr�r7r8re�get_host_cacher�r�r�r?ZswapSize)rbr�r�r�Zhcir3r3r4r�is

�r�c
Cs�t�d�t��}zt�|||d�|�Wntjjy'}zt|��d}~ww|s.td��ddi}t	|�}t
d�d}|r[tjj
j|||gd	�}	|	sWtd
|�d|�d���|	d
}
tjj
�||
|�dS)a�
    Configures the host cache on the selected host.

    enabled
        Boolean flag specifying whether the host cache is enabled.

    datastore
        Name of the datastore that contains the host cache. Must be set if
        enabled is ``true``.

    swap_size_MiB
        Swap size in Mibibytes. Needs to be set if enabled is ``true``. Must be
        smaller than the datastore size.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.configure_host_cache enabled=False

        salt '*' vsphere.configure_host_cache enabled=True datastore=ds1
            swap_size_MiB=1024
    zValidating host cache input)r�r��
swap_size_MiBNz)Disabling the host cache is not supportedr�FrFrtr'r)z' was not found on host 'rJrT)r�r�rr�r�r�r�r�rrrr�r7r8rer*r�configure_host_cache)r�r+r�rbr�r�r�r�r�r-r0r3r3r4r��s<
#������r�cCs:|st||�}|r|g}|Std��t|t�std��|S)ag
    Helper function that checks to see if the host provided is a vCenter Server or
    an ESXi host. If it's an ESXi host, returns a list of a single host_name.

    If a host reference isn't found, we're trying to find a host object for a vCenter
    server. Raises a CommandExecutionError in this case, as we need host references to
    check against.
    zdNo host reference found. If connecting to a vCenter Server, a list of 'host_names' must be provided.z'host_names' must be a list.)r�r
r{rg)rbrKr�r�r3r3r4r��s	

��
r�cCs�i}|d��D]Z}|����}|�d�r,|�d�}d|dvr%d|d<nd|d<|S|�d	�r>|�d�}|d��|d
<|�d�rP|�d�}|d��|d<|�d
�rb|�d�}|d��|d<q|S)z�
    Helper function to format the stdout from the get_coredump_network_config function.

    cmd_ret
        The return dictionary that comes from a cmd.run_all call.
    rxzenabled:r��truer�Tr�Fz
host vnic:r�znetwork server ip:�ipznetwork server port:rO)�
splitlinesr��lower�
startswithr�)�cmd_retr��liner�r�r�Zip_portr3r3r4r��s*



�




�r�cCsXdid�}|d��D]}|�d�rq|�d�rq|��}t|d�|d|d<q|S)	z�
    Helper function to format the stdout from the get_firewall_status function.

    cmd_ret
        The return dictionary that comes from a cmd.run_all call.
    T)r�r�rx�Namez---r�r�r)r�rr��bool)rr�rZruleset_statusr3r3r4r�s


r�cCstd|ddki}|ddkr|d|d<|S|d��D]}|��}|�d�}|d��}|d��}|||<q|S)z�
    Helper function to format the stdout from the get_syslog_config function.

    cmd_ret
        The return dictionary that comes from a cmd.run_all call.
    r�rvrrxr�r r�)r�r�r�)rr�rZcfgvarsr�r�r3r3r4r�s	�

r�cC�|jjS)z?
    Helper function that returns a dateTimeManager object
    )r�r���host_referencer3r3r4r�0�r�cCsLtjj�|�j}|r|j|dd�}n|j|dd�}|dur$|j|dd�}|S)a,
    Helper function that returns a host object either from the host location or the host_name.
    If host_name is provided, that is the host_object that will be returned.

    The function will first search for hosts by DNS Name. If no hosts are found, it will
    try searching by IP Address.
    F)ZdnsName�vmSearchN)r�r	)r7r8rer�ZsearchIndexZ
FindByDnsNameZFindByIp)rbrKr�Zsearch_indexr�r3r3r4r�7sr�cC�t|��d�S)zN
    Helper function that returns a list of ssd objects for a given host.
    �SSDs��_get_host_disksrDrr3r3r4rN�rcCr
)zR
    Helper function that returns a list of Non-SSD objects for a given host.
    �Non-SSDsrrr3r3r4rUrrc	Csh|jj}|jj}g}g}|D] }z|j}Wnty d}Ynw|r)|�|�q|�|�q||d�S)z_
    Helper function that returns a dictionary containing a list of SSD and Non-SSD disks.
    F)rr)r�Z
storageSystemZstorageDeviceInfoZscsiLunr��AttributeErrorrU)rr�r�ZssdsZnon_ssdsr�Zhas_ssd_attrr3r3r4r
\s
�
r
cCr)zY
    Helper function that returns a service manager object from a given host object.
    )r�r�rr3r3r4rrrrcCsi}|D]y}t|||d�}|jj}|dur)d�|�}t�|�|�|d|ii�qg}|��}	|	D]}
|
jdkr=|�	|
�q1|sTd�|�}t�
|�|�|d|ii�qt|�t|�}g}|D]}
|D]}|
j
|jj
krr|�	|
�qdq`|�|d|ii�q|S)a
    Helper function that returns a dictionary of host_name keys with either a list of eligible
    disks that can be added to VSAN or either an 'Error' message or a message saying no
    eligible disks were found. Possible keys/values look like:

    return = {'host_1': {'Error': 'VSAN System Config Manager is unset ...'},
              'host_2': {'Eligible': 'The host xxx does not have any VSAN eligible disks.'},
              'host_3': {'Eligible': [disk1, disk2, disk3, disk4],
              'host_4': {'Eligible': []}}
    r�Nr/rwr6z4The host '{}' does not have any VSAN eligible disks.r�)r�r�r2rYr�r�r|ZQueryDisksForVsan�staterU�warningrrr�r�)rbrKr�r=r�r�r5r�Zsuitable_disksr��itemr�Zmatchingr�Z
suitable_diskr3r3r4r�ysD�


��

��r�c
Cs�i}
d}t|t�s|g}|D]H}||vrDtjjj|||||||||	d�}
i|
|<|
ddk|
|d<|
ddkrCd}|
d|
|d<qd}i|
|<d|
|d<d	|
|d<q||
d<|
S)
zm
    Helper function for reset_syslog_config that resets the config and populates the return dictionary.
    Trsrvrr�Frxr�z&Invalid syslog configuration parameter)r{rgr7r8rer+)rKrLrMr�r�r�rNrOrtrur�Zall_successZreset_paramr=r3r3r4r��s8
�
�r�c

Cs�d|�d|��}
i}gd�}||vr |�dd|�d�d��|Stjjj||||
||||	d�}
|
d	d
krB|�|d|
dd�i�n	|�|dd
ii�|rr|rU|}|g}n|}t|||||||	d��|�}
|�dd|
d	d
kii�|S)zi
    Helper function for set_syslog_config that sets the config and populates the return dictionary.
    zsystem syslog config set --� r�FrJz!' is not a valid config variable.)r�r�rsrvrrxr�T)rNrOr}ruZsyslog_restart)r|r7r8rer+r�rD)rKrLrMr�r�rNrOr�rtrur�r�r�r~r�r3r3r4r��sV
������	r�c)s�i}d|d<g|d<tjjj|||||	|d�}
tjj�|
|�}|s/|d�d|���d|d<tjj�||�}|sG|d�d|���d|d<tjj�||�}|s_|d�d|���d|d<|dre|S|jj	}zt
|
||
�}
Wnty�}zd	|j��|d<|WYd
}~Sd
}~ww|
D�]�}i||<||�
d||��|d��t|
||�}|s�||�
dd�|�i�d|d<q�tjjj|d
�}tjj|d�}tjj�|�}�fdd�|D�}|s�||�
dd��d�i�d|d<q�tjj�|�}�fdd�|D�}|�s||�
dd��d�i�d|d<q�tjj�|�}|�s.||�
ddi�d|d<q�tjjj�|jd�}tjjj|gd�}tjjj|dd�}tj|jj|gd�}|j|d�} z
tjjj| |ddd�WnEt�y�}z8t |d��r�t |j!d��r�||j!jv�r�d |j!jv�s�d|d<||�
d|j!ji�WYd
}~q�n�WYd
}~nd
}~ww|j"j#}!d
}"|jj$j%D]}#|#j&j'|d!j%k�r�|#}"n�q�|"�s�||�
dd"i�d|d<q�tj(d#|d!j)|"j&j'tj*tj+|j|jj,j	d$�d%�d&�}$|jj$j-d!j.}%tj/d#d'tj0|%d(�d)�}&tj1d#|tj2|d*�d+�}'tj3|&g|'gtj4d,|"j&d-�g|$gd.�}(z|!j5d/|(d0�||�
d1di�Wq�t�yc}zt |d��r^||�
dd2|j�d3�i�WYd
}~q��d
}~ww|S)4a�
    Adds an ESXi host to a vSphere Distributed Virtual Switch and migrates
    the desired adapters to the DVS from the standard switch.

    host
        The location of the vCenter server.

    username
        The username used to login to the vCenter server.

    password
        The password used to login to the vCenter server.

    vmknic_name
        The name of the virtual NIC to migrate.

    vmnic_name
        The name of the physical NIC to migrate.

    dvs_name
        The name of the Distributed Virtual Switch.

    target_portgroup_name
        The name of the distributed portgroup in which to migrate the
        virtual NIC.

    uplink_portgroup_name
        The name of the uplink portgroup in which to migrate the
        physical NIC.

    protocol
        Optionally set to alternate protocol if the vCenter server or ESX/ESXi host is not
        using the default protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the vCenter server or ESX/ESXi host is not
        using the default port. Default port is ``443``.

    host_names:
        An array of VMware host names to migrate

    verify_ssl
        Verify the SSL certificate. Default: True

    CLI Example:

    .. code-block:: bash

        salt some_host vsphere.add_host_to_dvs host='vsphere.corp.com'
            username='administrator@vsphere.corp.com' password='vsphere_password'
            vmknic_name='vmk0' vmnic_name='vnmic0' dvs_name='DSwitch'
            target_portgroup_name='DPortGroup' uplink_portgroup_name='DSwitch1-DVUplinks-181'
            protocol='https' port='443', host_names="['esxi1.corp.com','esxi2.corp.com','esxi3.corp.com']"

    Return Example:

    .. code-block:: yaml

        somehost:
            ----------
            esxi1.corp.com:
                ----------
                dvs:
                    DSwitch
                portgroup:
                    DPortGroup
                status:
                    True
                uplink:
                    DSwitch-DVUplinks-181
                vmknic:
                    vmk0
                vmnic:
                    vmnic0
            esxi2.corp.com:
                ----------
                dvs:
                    DSwitch
                portgroup:
                    DPortGroup
                status:
                    True
                uplink:
                    DSwitch-DVUplinks-181
                vmknic:
                    vmk0
                vmnic:
                    vmnic0
            esxi3.corp.com:
                ----------
                dvs:
                    DSwitch
                portgroup:
                    DPortGroup
                status:
                    True
                uplink:
                    DSwitch-DVUplinks-181
                vmknic:
                    vmk0
                vmnic:
                    vmnic0
            message:
            success:
                True

    This was very difficult to figure out.  VMware's PyVmomi documentation at

    https://github.com/vmware/pyvmomi/blob/master/docs/vim/DistributedVirtualSwitch.rst
    (which is a copy of the official documentation here:
    https://www.vmware.com/support/developer/converter-sdk/conv60_apireference/vim.DistributedVirtualSwitch.html)

    says to create the DVS, create distributed portgroups, and then add the
    host to the DVS specifying which physical NIC to use as the port backing.
    However, if the physical NIC is in use as the only link from the host
    to vSphere, this will fail with an unhelpful "busy" error.

    There is, however, a Powershell PowerCLI cmdlet called Add-VDSwitchPhysicalNetworkAdapter
    that does what we want.  I used Onyx (https://labs.vmware.com/flings/onyx)
    to sniff the SOAP stream from Powershell to our vSphere server and got
    this snippet out:

    .. code-block:: xml

        <UpdateNetworkConfig xmlns="urn:vim25">
          <_this type="HostNetworkSystem">networkSystem-187</_this>
          <config>
            <vswitch>
              <changeOperation>edit</changeOperation>
              <name>vSwitch0</name>
              <spec>
                <numPorts>7812</numPorts>
              </spec>
            </vswitch>
            <proxySwitch>
                <changeOperation>edit</changeOperation>
                <uuid>73 a4 05 50 b0 d2 7e b9-38 80 5d 24 65 8f da 70</uuid>
                <spec>
                <backing xsi:type="DistributedVirtualSwitchHostMemberPnicBacking">
                    <pnicSpec><pnicDevice>vmnic0</pnicDevice></pnicSpec>
                </backing>
                </spec>
            </proxySwitch>
            <portgroup>
              <changeOperation>remove</changeOperation>
              <spec>
                <name>Management Network</name><vlanId>-1</vlanId><vswitchName /><policy />
              </spec>
            </portgroup>
            <vnic>
              <changeOperation>edit</changeOperation>
              <device>vmk0</device>
              <portgroup />
              <spec>
                <distributedVirtualPort>
                  <switchUuid>73 a4 05 50 b0 d2 7e b9-38 80 5d 24 65 8f da 70</switchUuid>
                  <portgroupKey>dvportgroup-191</portgroupKey>
                </distributedVirtualPort>
              </spec>
            </vnic>
          </config>
          <changeMode>modify</changeMode>
        </UpdateNetworkConfig>

    The SOAP API maps closely to PyVmomi, so from there it was (relatively)
    easy to figure out what Python to write.
    Tr�r�r�z.No Distributed Virtual Switch found with name Fz$No target portgroup found with name z$No uplink portgroup found with name zError retrieving hosts: N)r�Zuplinkr�ZvmknicZvmnicr|zHost {1} not found)rKrcr�r3�r-�r��x)�
vmnic_namer3r4r�* r�z#add_host_to_dvs.<locals>.<listcomp>z
Physical nic z
 not foundcr�r3rr)�vmknic_namer3r4r�1 r�zVirtual nic z-Unable to get the host's virtual nic manager.)Z
pnicDeviceZuplinkPortgroupKey)ZpnicSpec�add)rKrT)r�rK)r�zAdding host to the DVSr0r1r�zalready existsrz$No matching portgroup on the vSwitch�edit��portgroupKeyZ
switchUuid)ZdistributedVirtualPort)�changeOperationr-r�r�ZvSwitch0)r�)rr?r�)�backing)rrxr��remove)rr�)�vswitchZproxySwitchr�Zvnicr�)Z
changeModer�r�zFailed to migrate adapters (�))6r7r8rerfZ_get_dvsrUZ_get_dvs_portgroupZ_get_dvs_uplink_portgroupr�rxr�r
r�r|r�rYrr|Z
HostMemberr:Z
_get_pnicsZ
_get_vnicsZ_get_vnic_managerZPnicSpecr�ZPnicBacking�
ConfigSpecZ
DVSConfigSpecr�ZReconfigureDvs_Taskr3rirxr�r�Z
networkSystem�networkr�r�r?ZHostVirtualNicConfigr-ZHostVirtualNicSpecZ&DistributedVirtualSwitchPortConnection�distributedVirtualSwitchr!r�ZHostVirtualSwitchConfigZHostVirtualSwitchSpecZHostProxySwitchConfigZHostProxySwitchSpecZHostNetworkConfigZHostPortGroupConfigZUpdateNetworkConfig))rKrLrMrrrPZtarget_portgroup_nameZuplink_portgroup_namerNrOr�rSr=rbr|Ztarget_portgroupZuplink_portgroupZdvs_uuidrlr�r�Zdvs_hostmember_configZdvs_hostmemberZp_nicsZp_nicZv_nicsZv_nicZ	v_nic_mgrZ
dvs_pnic_specZpnic_backingZdvs_hostmember_config_specrQr7Znetwork_systemZsource_portgroupZpgZvirtual_nic_configZcurrent_vswitch_portsZvswitch_configZproxyswitch_configZhost_network_configr3)rrr4�add_host_to_dvs0sF9�������
��
���
�
���������
�
��������	r&c
Cst�}tjj�|�std��d}|dkr2t�\
}}}}}}}	}
}}tjj�||�}
tjj�|
|�}nP|dkrKt	�\	}}}}}}}	}
}tjj�||�}n7|dkrWtjj�
|�}n+|dkr�td�}d|vrhtd��tjjj
||d	d
�}|s~td�|d	���|d}t�d
|�|S)z�
    Returns the target object of a proxy.

    If the object doesn't exist a VMwareObjectRetrievalError is raised

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter/ESXi host.
    zB'_get_proxy_target' not supported when connected via the ESXi hostNr-r.r/r,rFz:Proxies connected directly to ESXi hosts are not supportedrt)r�zESXi host '{}' was not foundrzreference = %s)rEr7r8rer�r
�_get_esxcluster_proxy_detailsrsrt� _get_esxdatacenter_proxy_detailsZget_root_folderr�rr�rrYr�rH)rbr[r�rKrLrMrNrOrPrQrRrlrsr{rWZ
referencesr3r3r4rr� sh���
���rrcCsVtd�}|�d�|�d�|�d�|�d�|�d�|�d�|�d�|�d	�|�d
�f	S)z;
    Returns the running esxdatacenter's proxy details
    rHr/rLrMrNrOrPrQrRrl�rTrD��detr3r3r4r(� s
�r(cCs^td�}|�d�|�d�|�d�|�d�|�d�|�d�|�d�|�d	�|�d
�|�d�f
S)z8
    Returns the running esxcluster's proxy details
    rGr/rLrMrNrOrPrQrRrlrsr)r*r3r3r4r'� s
�r'c
Cs~td�}|�d�}|�d�r|d}d}|�d�r|dg}||�d�|�d�|�d�|�d	�|�d
�|�d�|�d�|f	S)
z2
    Returns the running esxi's proxy details
    rFrKr/NrtrLrMrNrOrPrQrR)r�rD)r+rKr}r3r3r4�_get_esxi_proxy_details!s"




�r,cCstjjj||||||d�}|S)a�
    Returns vm object properties.

    name
        Name of the virtual machine.

    datacenter
        Datacenter name

    vm_properties
        List of vm properties.

    traversal_spec
        Traversal Spec object(s) for searching.

    parent_ref
        Container Reference object for searching under a given object.

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.
    )rl�
vm_properties�traversal_specr�)r7r8re�get_vm_by_property)r?rlr-r.r�rb�virtual_machiner3r3r4�get_vm!s!�r1cCs�tjj��}|}tjj��g|_tjj�	||�}d|vr'tjj�
||d�}ntjjj|||dgd�}	|	sAtj�
d�|d���|	d}tjj�|||g||�}
|
rbt|
dj�dkrbtj�d��|
rm|
djrm|
dSdS)	a�
    Queries the virtual machine config file and returns
    vim.host.DatastoreBrowser.SearchResults object on success None on failure

    name
        Name of the virtual machine

    datacenter
        Datacenter name

    datastore
        Datastore where the virtual machine files are stored

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.
    rsrK�r5r��"ESXi host named '{}' wasn't found.rr�zEMultiple configuration files found in the same virtual machine folderN)rrKZDatastoreBrowserZ
SearchSpecZ
VmConfigQueryr�r7r8rersrtr�r�rrYZget_datastore_filesrd�fileZVMwareMultipleObjectsError)r?rl�	placementr+rbZbrowser_spec�	directory�datacenter_object�container_objectZcontainer_objects�filesr3r3r4�get_vm_config_fileI!s4�����r:rcCsZt�d|�|dkr t�d|�tj��}d|_||_||_dS|dkr+t|�|_	dSdS)a�
    Specifies vm container version or schedules upgrade,
    returns True on change and False if nothing have been changed.

    hardware_version
        Hardware version string eg. vmx-08

    config_spec
        Configuration spec object

    operation
        Defines the operation which should be used,
        the possibles values: 'add' and 'edit', the default value is 'add'
    z7Configuring virtual machine hardware version version=%srz)Scheduling hardware version upgrade to %s�alwaysrN)
r�rHr�vmZScheduledHardwareUpgradeInfoZ
upgradePolicyZ
versionKeyZscheduledHardwareUpgradeInfor4rX)Zhardware_versionr�rTZscheduled_hardware_upgrader3r3r4�_apply_hardware_version!s�

�r=cCs�t�d|�d|vrt|d�|_d|vrt|d�|_d|vr)|dr)|d|_d|vr6|dr6|d|_d|vrE|drG|d|_dSdSdS)z�
    Sets CPU core count to the given value

    config_spec
        vm.ConfigSpec object

    cpu_props
        CPU properties dict
    z5Configuring virtual machine CPU settings cpu_props=%s�count�cores_per_socket�nested�hotadd�	hotremoveN)r�rHrdZnumCPUsZnumCoresPerSocketZnestedHVEnabledZcpuHotAddEnabledZcpuHotRemoveEnabled)r�Z	cpu_propsr3r3r4�_apply_cpu_config�!s


�rCc	Cs�t�d|�d|vrSd|vrSz0|d��dkr|dd}n|d��dkr+|d}n|d��dkr=tt|d�d�}WnttfyOt|d�}Ynw||_d|vr\|d|_d	|vrg|d	|_	d
Sd
S)z�
    Sets memory size to the given value

    config_spec
        vm.ConfigSpec object

    memory
        Memory size and unit
    z5Configuring virtual machine memory settings memory=%s�size�unitZkbr�ZmbZgb�reservation_maxrAN)
r�rHr�rdr��	TypeError�
ValueErrorZmemoryMBZmemoryReservationLockedToMaxZmemoryHotAddEnabled)r��memoryZ	memory_mbr3r3r4�_apply_memory_config�!s&

��
�rJcCst||d|d�}|dS)a&
    Returns extra config parameters from a virtual machine advanced config list

    vm_name
        Virtual machine name

    datacenter
        Datacenter name where the virtual machine is available

    service_instance
        vCenter service instance for connection and configuration
    T�rl�objectsrb�advanced_configs)�
get_vm_config)�vm_namerlrb�current_configr3r3r4�get_advanced_configs�!s�rQcCs~t�d|�t|t�rtj�d��|��D]'\}}|r.|D]}|j|kr,|j	t|�kr,qqqt
jj||d�}|j
�|�qdS)z�
    Sets configuration parameters for the vm

    config_spec
        vm.ConfigSpec object

    advanced_config
        config key value pairs

    vm_extra_config
        Virtual machine vm_ref.config.extraConfig object
    z0Configuring advanced configuration parameters %s�cThe specified 'advanced_configs' configuration option cannot be parsed, please check the parametersrDN)r�rHr{r4r7r�rr�r�r�rr\rw�extraConfigrU)r��advanced_config�vm_extra_configr�r�r\r3r3r4�_apply_advanced_config�!s
����rVc
Cs�t||d|d�}t||d�|�}tjj�||�}tjjj|tj|d|d�}tj	�
�}|dj}	t||dj
|jj�|	rDtjj�||�d|	iS)au
    Appends extra config parameters to a virtual machine advanced config list

    vm_name
        Virtual machine name

    datacenter
        Datacenter name where the virtual machine is available

    advanced_configs
        Dictionary with advanced parameter key value pairs

    service_instance
        vCenter service instance for connection and configuration
    TrK)r?rMr?�Z
property_nameZ
container_refrMZadvanced_config_changes)rN�compare_vm_configsr7r8rers�get_mor_by_propertyr�VirtualMachiner<r#�diffsrV�
new_valuesr�rS�	update_vm)
rOrlrMrbrPr[�datacenter_ref�vm_refr��changesr3r3r4�set_advanced_configs"s,�
��

�racCsnt�d|�t|t�rtj�d��g}|D]}|D]}|j|kr3tj	j
|dd�}|j�|�|�|�qq|S)z�
    Removes configuration parameters for the vm

    config_spec
        vm.ConfigSpec object

    advanced_config
        List of advanced config keys to be deleted

    vm_extra_config
        Virtual machine vm_ref.config.extraConfig object
    z-Removing advanced configuration parameters %srR�rD)
r�rHr{r4r7r�rr�rr\rwrSrU)r�rTrU�removed_configsr�r\r3r3r4�_delete_advanced_config."s
�

��rdcCs`tjj�||�}tjjj|tj|d|d�}tj��}t	|||j
j�}|r,tjj�||�d|iS)a\
    Removes extra config parameters from a virtual machine

    vm_name
        Virtual machine name

    datacenter
        Datacenter name where the virtual machine is available

    advanced_configs
        List of advanced config values to be removed

    service_instance
        vCenter service instance for connection and configuration
    r?rWrc)
r7r8rersrYrrZr<r#rdr�rSr])rOrlrMrbr^r_r�rcr3r3r4�delete_advanced_configsK"s�

�recs4��fdd��D�}|stj�d��d���|dS)z�
    Returns key number of the SCSI controller keys

    bus_number
        Controller bus number from the adapter

    scsi_ctrls
        List of SCSI Controller objects (old+newly created)
    cs g|]}�r|j�kr|j�qSr3)�	busNumberr�)r�Zctrl��
bus_number�
scsi_ctrlsr3r4r�"s�z,_get_scsi_controller_key.<locals>.<listcomp>zSCSI controller number z doesn't existr)r7r�ZVMwareVmCreationError)rhrir�r3rgr4�_get_scsi_controller_keyr"s
�
�rj�GBc
CsFt�d|||||||	|
�	tjj��}tjj��|_||j_||j_t�	�|j_
|r8tjj
�||�}|d|j_|r?||jj
_|dusG|durUtjjj��|j_d|jj_|dur^||jj_|durk|dkrk||jj_|rq||j_|dkr�tjjjjj|_d�tjj
�|	�|
�|jj_tjjjjj|_|S|dkr�tjjjjj |_|S)	a�
    Returns a vim.vm.device.VirtualDeviceSpec object specifying to add/edit
    a virtual disk device

    unit_number
        Add network adapter to this address

    key
        Device key number

    operation
        Action which should be done on the device add or edit

    disk_label
        Label of the new disk, can be overridden

    size
        Size of the disk

    unit
        Unit of the size, can be GB, MB, KB

    controller_key
        Unique umber of the controller key

    thin_provision
        Boolean for thin provision

    eagerly_scrub
        Boolean for eagerly scrubbing

    datastore
        Datastore name where the disk will be located

    filename
        Full file name of the vm disk
    z|Configuring hard disk %s size=%s, unit=%s, controller_key=%s, thin_provision=%s, eagerly_scrub=%s, datastore=%s, filename=%srDNZ
persistent�Nonerz[{}] {}r)!r�rHrr<r-�VirtualDeviceSpec�VirtualDiskr��
unitNumber�Description�
deviceInfor7r8re�
convert_to_kb�capacityInKB�labelZFlatVer2BackingInforZdiskMode�thinProvisioned�eagerlyScrub�
controllerKey�	OperationrrTrYr2�fileNameZ
FileOperation�createZ
fileOperationr)
�unit_numberr�rT�
disk_labelrDrE�controller_key�thin_provision�
eagerly_scrubr+�filename�	disk_specZconvert_sizer3r3r4�_apply_hard_disk�"sP2�




��r�rbcCs�t�d|�|dvr9tjj�|�}t|t|��r|}|S|r7t�d|jj	t|�j
�dd�ddd���|�|S|rJ|rFt�
d|jj	|�|}|S|sRt�d	|�tjj��}|S)
a
    Returns a vim.vm.device.VirtualEthernetCard object specifying a virtual
    ethernet card information

    network_adapter
        None or VirtualEthernet object

    adapter_type
        String, type of adapter

    network_adapter_label
        string, network adapter name
    z;Configuring virtual machine network adapter adapter_type=%s)ZvmxnetZvmxnet2Zvmxnet3Ze1000Ze1000ez'Changing type of '%s' from '%s' to '%s'r�r�rNz5Cannot change type of '%s' to '%s'. Not changing typezKThe type of '%s' has not been specified. Creating of default type 'vmxnet3')r�rHr7r8reZget_network_adapter_typer{r�rqrtrZ�rsplitr�r�rr<r-ZVirtualVmxnet3)Znetwork_adapter�adapter_type�network_adapter_labelZedited_network_adapterr3r3r4�_create_adapter_type�"sD��� ���	��r�cCs�t�d||tjj�|��i}|rr|dkr>tjjj||gd�}|s+tj�d|�d���|d}t	j
jj�
�}||_||_|S|dkrrtjjj||gd�}|sXtj�d	|�d���|d}t	jj|j|jjjd
�}t	j
jj��}||_|S)a
    Returns a vim.vm.device.VirtualDevice.BackingInfo object specifying a
    virtual ethernet card backing information

    network_name
        string, network name

    switch_type
        string, type of switch

    parent_ref
        Parent reference to search for network
    zTConfiguring virtual machine network backing network_name=%s switch_type=%s parent=%s�standard)Z
network_namesz
The network 'z' could not be retrieved.r�distributedr�zThe port group 'r)r�rHr7r8rer2Zget_networksr�rrr<r-�VirtualEthernetCard�NetworkBackingInfoZ
deviceNamer$r�r|ZPortConnectionr�r�r%rx�!DistributedVirtualPortBackingInforO)�network_name�switch_typer�rZnetworks�network_refZdvs_port_connectionr3r3r4�_create_network_backing#sL��
���
���r�c	
Cs|����|����t�d|||||�tjj��}	t|	j||d�|	_t�	�|	j_
|dkr8tjjjjj|	_
n|dkrDtjjjjj|	_
|rU|rUt|||�|	j_||	jj
_|r[||	j_|rb||	jj
_|rl||	j_d|	j_d|	j_|r�tjjj��|	j_|d|	jj_|d|	jj_|	S)	ac
    Returns a vim.vm.device.VirtualDeviceSpec object specifying to add/edit a
    network device

    network_adapter_label
        Network adapter label

    key
        Unique key for device creation

    network_name
        Network or port group name

    adapter_type
        Type of the adapter eg. vmxnet3

    switch_type
        Type of the switch: standard or distributed

    operation
        Type of operation: add or edit

    connectable
        Dictionary with the device connection properties

    mac
        MAC address of the network adapter

    parent
        Parent object reference
    zzConfiguring virtual machine network adapter network_adapter_label=%s network_name=%s adapter_type=%s switch_type=%s mac=%s)r�rrZManualT�start_connected�allow_guest_control)r�r�r�rHrr<r-rmr�rprqrxrrTrr�rrr�rt�
macAddressZaddressTypeZwakeOnLanEnabled�
VirtualDevice�ConnectInfo�connectable�startConnected�allowGuestControl)
r�r�r�r�r�rTr��mac�parent�network_specr3r3r4�_apply_network_adapter_configQ#sN*�	��


�r�cCsJt�d|||||�tjj��}|dkrd}tjj��|_n)|dkr,d}tjj��|_n|dkr:d}tjj��|_n
|dkrGd	}tjj�	�|_|d
krTtjjjj
j|_n|dkr`tjjjj
j
|_||j_||j_t��|j_||jj_||jj_|dkr�tjjjjj|j_|S|d
kr�tjjjjj|j_|S|dkr�tjjjjj|j_|S)a�
    Returns a vim.vm.device.VirtualDeviceSpec object specifying to
    add/edit a SCSI controller

    adapter
        SCSI controller adapter name

    adapter_type
        SCSI controller adapter type eg. paravirtual

    bus_sharing
         SCSI controller bus sharing eg. virtual_sharing

    key
        SCSI controller unique key

    bus_number
        Device bus number property

    operation
        Describes the operation which should be done on the object,
        the possibles values: 'add' and 'edit', the default value is 'add'

    .. code-block:: bash

        scsi:
          adapter: 'SCSI controller 0'
          type: paravirtual or lsilogic or lsilogic_sas
          bus_sharing: 'no_sharing' or 'virtual_sharing' or 'physical_sharing'
    zZConfiguring scsi controller adapter=%s adapter_type=%s bus_sharing=%s key=%s bus_number=%s�lsilogicz	LSI Logic�lsilogic_sasz
LSI Logic Sas�paravirtualzVMware paravirtual SCSI�buslogicz	Bus Logicrr�virtual_sharing�physical_sharing�
no_sharing)r�rHrr<r-rm�VirtualLsiLogicController�VirtualLsiLogicSASController�ParaVirtualSCSIController�VirtualBusLogicControllerrxrrTrr�rfrprqrtr�VirtualSCSIControllerZSharing�virtualSharing�	sharedBus�physicalSharing�	noSharing)�adapterr��bus_sharingr�rhrT�	scsi_specrr3r3r4�_apply_scsi_controller�#sT!�	

�
����r�c
Csfg}tddd�}|r1dd�|D�}t�d|�t||�D]\}}|�t|dd|t|d	���q|S)
z�
    Returns a list of vim.vm.device.VirtualDeviceSpec objects representing
    IDE controllers

    ide_controllers
        IDE properties
    i8���i���rvcS�g|]}|d�qS�r�r3)r��ider3r3r4r�$r�z+_create_ide_controllers.<locals>.<listcomp>zCreating IDE controllers %sr�rr�)r�r�rH�ziprU�_apply_ide_controller_config�abs)�ide_controllersZ	ide_ctrlsr��devsr�r�r3r3r4�_create_ide_controllers�#s�r�cCs�t�d|�tjj��}tjj��|_|dkrtjjjjj|_	|dkr+tjjjjj
|_	||j_||j_|rEt�
�|j_||jj_||jj_|S)a@
    Returns a vim.vm.device.VirtualDeviceSpec object specifying to add/edit an
    IDE controller

    ide_controller_label
        Controller label of the IDE adapter

    operation
        Type of operation: add or edit

    key
        Unique key of the device

    bus_number
        Device bus number property
    z2Configuring IDE controller ide_controller_label=%srr)r�rHrr<r-rmZVirtualIDEControllerrxrrTrr�rfrprqrtr)Zide_controller_labelrTr�rhZide_specr3r3r4r�$s �

r�c	Csbg}tddd�}|r/dd�|D�}t�d|�t||�D]\}}|�t|dd||d	��q|S)
z�
    Returns a list of vim.vm.device.VirtualDeviceSpec objects representing
    SATA controllers

    sata_controllers
        SATA properties
    ih��i6��rvcSr�r�r3)r��satar3r3r4r�<$r�z,_create_sata_controllers.<locals>.<listcomp>zCreating SATA controllers %sr�rrh)r�r�rHr�rU�_apply_sata_controller_config)�sata_controllersZ
sata_ctrlsr�r�r�r�r3r3r4�_create_sata_controllers1$s��r�cCs�t�d|�tjj��}tjj��|_|dkr tjjjjj|_	n|dkr,tjjjjj
|_	||j_d|j_||j_
|rJt��|j_||jj_||jj_|S)aB
    Returns a vim.vm.device.VirtualDeviceSpec object specifying to add/edit a
    SATA controller

    sata_controller_label
        Controller label of the SATA adapter

    operation
        Type of operation: add or edit

    key
        Unique key of the device

    bus_number
        Device bus number property
    z4Configuring SATA controller sata_controller_label=%srr�d)r�rHrr<r-rmZVirtualAHCIControllerrxrrTrr�rwrfrprqrtr)Zsata_controller_labelrTr�rhZ	sata_specr3r3r4r�G$s"�

r�r�c	Cs�t�d||||�tjj��}	tjj��|	_t��|	j_|dkr)tjjjj	j
|	_n|dkr5tjjjj	j|	_|dkr{tjjj�
�|	j_|d|	jj_|d�d�d�d�d	}
tjjjtjj�|�||
gd
�d	}|rp||	jj_d�|d�|	jj_n#|dkr�|d
dkr�tjjj��|	j_n|d
dkr�tjjj��|	j_||	j_||	jj_||	j_tjjj� �|	j_!|r�|d|	jj!_"|d|	jj!_#|	S)a�
    Returns a vim.vm.device.VirtualDeviceSpec object specifying to add/edit a
    CD/DVD drive

    drive_label
        Leble of the CD/DVD drive

    key
        Unique key of the device

    device_type
        Type of the device: client or iso

    operation
        Type of operation: add or edit

    client_device
        Client device properties

    datastore_iso_file
        ISO properties

    connectable
        Connection info for the device

    controller_key
        Controller unique identifier to which we will attach this device

    parent_ref
        Parent object

    .. code-block:: bash

        cd:
            adapter: "CD/DVD drive 1"
            device_type: datastore_iso_file or client_device
            client_device:
              mode: atapi or passthrough
            datastore_iso_file:
              path: "[share] iso/disk.iso"
            connectable:
              start_connected: True
              allow_guest_control:
    z]Configuring CD/DVD drive drive_label=%s device_type=%s client_device=%s datastore_iso_file=%srr�datastore_iso_file�path�[rv�]rr'z{}�
client_device�mode�passthrough�atapir�r�)$r�rHrr<r-rm�VirtualCdromrprqrxrrTr�IsoBackingInforryr��
rpartitionr7r8rer*rar+rYr�RemotePassthroughBackingInfo�RemoteAtapiBackingInfor�rtrwr�r�r�r�r�)Zdrive_labelr��device_typerTr�r�r�r}r�Z
drive_specr+�datastore_objectr3r3r4�_apply_cd_drivek$sZ7���
��

�r�cCsptjj��}||_tjj��|_|r||j_|r||j_|r.tjjj	|d�|j_
||j_|Stjj��|j_
|S)a4
    Returns a vim.vm.customization.AdapterMapping object containing the IP
    properties of a network adapter card

    domain
        Domain of the host

    gateway
        Gateway address

    ip_addr
        IP address

    subnet_mask
        Subnet mask

    mac
        MAC address of the guest
    )Z	ipAddress)
rr<Z
customizationZAdapterMappingr�Z
IPSettingsr�Z	dnsDomain�gatewayZFixedIpr�Z
subnetMaskZDhcpIpGenerator)rRr��ip_addr�subnet_maskr��adapter_mappingr3r3r4�_set_network_adapter_mapping�$s�r�cCs�t�d|d|d|d|d�tjj��}tjj��|_|dkr+tjjjjj|_	n|dkr7tjjjjj
|_	tjjj��}d}|dd	kr�tjjj�
�}d
|d��vrYtd��d
|d��vretd
��d|d��vrqtd��|dd
|_|dd|_|dd|_|ddkr�tjjj��}|ddkr�tjjj��}|ddkr�tjjj��}|dd|_|dd|_||j_||j_d|j_||j_|d|j_|S)ap
    Returns a vim.vm.device.VirtualSerialPort representing a serial port
    component

    serial_device_spec
        Serial device properties

    key
        Unique key of the device

    operation
        Add or edit the given device

    .. code-block:: bash

        serial_ports:
            adapter: 'Serial port 1'
            backing:
              type: uri
              uri: 'telnet://something:port'
              direction: <client|server>
              filename: 'service_uri'
            connectable:
              allow_guest_control: True
              start_connected: True
            yield: False
    z?Creating serial port adapter=%s type=%s connectable=%s yield=%sr�r�r��yieldrrNr$�urirz&vSPC proxy URI not specified in configz&vSPC Direction not specified in configr�z%vSPC Filename not specified in config�	direction�piper4r-r�r�r�)r�rHrr<r-rm�VirtualSerialPortrxrrTrr�r��URIBackingInfor�rH�proxyURIr��
serviceURI�PipeBackingInfo�FileBackingInfo�DeviceBackingInfor�r�rr�ror��yieldOnPoll)Zserial_device_specr�rT�device_specZconnect_infoZtype_backingr3r3r4�_apply_serial_port�$sT��r�cCs�g}tddd�}|rdd�|D�}t�d|�t||�D]�\}}d\}	}
}t|d�}d	}
d
|vrI|d
�d�\}}t|�}t|�}t||d�}
n"d
|vrk|D]}|d|d
kr_|d}
nqOtj	�
d�|d
���d|vr�tjj
j|||dgd�d}|d}
d|vr�|d}	|	s�|
s�|	r�|
s�tj	�d�|
|	���t|||d||d|
d|dd|vr�|dnd||	d�}|�|�|d7}q|S)a�
    Returns a list of disk specs representing the disks to be created for a
    virtual machine

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    disks
        List of disks with properties

    scsi_controllers
        List of SCSI controllers

    parent
        Parent object reference

    .. code-block:: bash

        disk:
          adapter: 'Hard disk 1'
          size: 16
          unit: GB
          address: '0:0'
          controller: 'SCSI controller 0'
          thin_provision: False
          eagerly_scrub: False
          datastore: 'myshare'
          filename: 'vm/mydisk.vmdk'
    i0���i����rvcSr�r�r3�r�r�r3r3r4r�a%r�z!_create_disks.<locals>.<listcomp>zCreating disks %s�NNNrDi��addressr�)ri�
controllerrtr�z'The given controller does not exist: {}r+r'rr�ziYou must specify both filename and datastore attributes to place your disk to a specific datastore {}, {}r�rErr~rN)	r|rDrEr}rTr~rr+r�r�)r�r�rHr�r�r�rdrjr7r��VMwareObjectNotFoundErrorrYr8rer*rr�rU)rbr��scsi_controllersr��
disk_specsr�r�r�r�r�r+Z
datastore_refrDr}Zcontroller_bus_numberr{Zcontrr�r3r3r4�
_create_disks?%sn
�������

r�cCsrtddd�}g}|r7dd�|D�}t�d|�t||�D]\}}t|d|d|d	||d
d�}|�|�q|S)z�
    Returns a list of vim.vm.device.VirtualDeviceSpec objects representing
    SCSI controllers

    scsi_devices:
        List of SCSI device properties
    i���i��rvcSr�r�r3�r�Zscsir3r3r4r��%r�z(_create_scsi_devices.<locals>.<listcomp>zCreating SCSI devices %sr�r�r�rhr)r�r�rHr�r�rU)�scsi_devicesr��
scsi_specsr�r�Zscsi_controllerr�r3r3r4�_create_scsi_devices�%s �r�c
Cs�g}g}tddd�}|rjdd�|D�}t�d|�t||�D]K\}}t||d|d|d	|d
dd|vr9|dnd
|d|d�	}|�|�d|vrit|dd|dd|dd|dd|d�}	|�|	�q||fS)a
    Returns a list of vim.vm.device.VirtualDeviceSpec objects representing
    the interfaces to be created for a virtual machine

    network_interfaces
        List of network interfaces and properties

    parent
        Parent object reference

    .. code-block:: bash

        interfaces:
          adapter: 'Network adapter 1'
          name: vlan100
          switch_type: distributed or standard
          adapter_type: vmxnet3 or vmxnet, vmxnet2, vmxnet3, e1000, e1000e
          mac: '00:11:22:33:44:55'
    i`�i.�rvcSr�r�r3�r�Zinterr3r3r4r��%r�z,_create_network_adapters.<locals>.<listcomp>zCreating network interfaces %sr?r�r�r�rr�Nr�)r�rTr�r�r��mappingrRr�r�r�)r�r�rHr�r�rUr�)
Znetwork_interfacesr�Z
network_specsZ
nics_settingsr�r��	interfacer�r�r�r3r3r4�_create_network_adapters�%s<�





�
�r�cCs\g}tddd�}|r,dd�|D�}t�d|�t||�D]\}}t||d�}|�|�q|S)z�
    Returns a list of vim.vm.device.VirtualDeviceSpec objects representing the
    serial ports to be created for a virtual machine

    serial_ports
        Serial port properties
    i���i���rvcSr�r�r3�r��serialr3r3r4r��%r�z(_create_serial_ports.<locals>.<listcomp>zCreating serial ports %sr)r�r�rHr�r�rU)�serial_portsZportsr�r�rOr�Zserial_port_devicer3r3r4�_create_serial_ports�%sr�c
Cs�g}tddd�}|r[dd�|D�}t�d|�t||�D]>\}}d}|r.t||d�}	|	j}|�t|d	||d
dd|vrA|dnd
d|vrJ|dnd
d|vrS|dnd
||d�	�q|S)a'
    Returns a list of vim.vm.device.VirtualDeviceSpec objects representing the
    CD/DVD drives to be created for a virtual machine

    cd_drives
        CD/DVD drive properties

    controllers
        CD/DVD drive controllers (IDE, SATA)

    parent_ref
        Parent object reference
    iH�i�rvcSr�r�r3)r�Zdvdr3r3r4r�&r�z%_create_cd_drives.<locals>.<listcomp>zCreating cd/dvd drives %sr�r�r�r�rr�Nr�r��r�r�r�r}r�)r�r�rHr��_get_device_by_labelr�rUr�)
�	cd_drives�controllersr��cd_drive_specsr�r��driver�r}r�r3r3r4�_create_cd_drives&s4���r�c�2�fdd�|D�}|r|dStj�d��d���)z�
    Returns the device with the given key, raises error if the device is
    not found.

    devices
        list of vim.vm.device.VirtualDevice objects

    key
        Unique key of device
    cr�r3r�r�r�r3r4r�>&r�z&_get_device_by_key.<locals>.<listcomp>rz'Virtual machine device with unique key � does not exist�r7r�r�)�devicesr�Zdevice_keysr3r�r4�_get_device_by_key3&�
�rcr�)z�
    Returns the device with the given label, raises error if the device is
    not found.

    devices
        list of vim.vm.device.VirtualDevice objects

    key
        Unique key of device
    csg|]
}|jj�kr|�qSr3)rqrtr��rtr3r4r�R&sz(_get_device_by_label.<locals>.<listcomp>rz"Virtual machine device with label r�r�)r�rtZ
device_labelsr3rr4r�G&rr�cCsD|r |D]}d|vrd|vr|�tjj�|d|d��qdSdS)z
    Updates the size and unit dictionary values with the new unit values

    devices
        List of device data objects
    rErDFT)r|r7r8rerr)r�r-r3r3r4�_convert_units[&s���rc
Cs(i}t|���}|�d�|�d�|�d�dD]}||vr8t|||i|||i�}|jr3|||<|�|�qd|vrR|�d�t|d|d�}|jrR||d<d|vry|�d�t|dg�t|dg�t|d|d�}|jry||d<d|vr�|�d�d}t||||�}	|	jr�|	||<d|vr�|�d�t|d�t|d�t|d|dd	�}
|
jd
d�|
jdd�|
jd
d�|
jr�|
|d<d|vr�|�d�t|d|dd�}|jd
d�|jr�||d<|D]#}||vs�||vr�t	d�
|���t||||d
�}|j�r|||<q�|S)a�
    Compares virtual machine current and new configuration, the current is the
    one which is deployed now, and the new is the target config. Returns the
    differences between the objects in a dictionary, the keys are the
    configuration parameter keys and the values are differences objects: either
    list or recursive difference

    new_config:
        New config dictionary with every available parameter

    current_config
        Currently deployed configuration
    r?rlr+)rX�image�cpurIrMr�r�r)�diff_keyr�r��
interfacesr�zdA general device {} configuration was not supplied or it was not retrieved from remote configuration)r�r��discardrr[r rr�remove_diffrHrY)
Z
new_configrPr[r�Zproperty_keyZsingle_value_diffZcpu_diffZmemory_diffr�Z
advanced_diffZ
disk_diffsZinterface_diffsZdevice_diffsr3r3r4rXm&s|




�
�




���rXc	Csjgd�}tjjj||||d�}tjjj||d�}d|i}|d|d|d|d|d	d
�|d<|dd
|d|dd�|d<|d|d<|d|d<i|d<|dD]}zt|j�|d|j<WqStyr|j|d|j<YqSwg|d<g|d<g|d<g|d<g|d<g|d<|dD�]�}	t	|	t
jjj
��ri}
|	jj|
d<|	j|
d <|	j}|d!kr�d"|
d#<n|d$kr�d%|
d#<n|d&kr�d'|
d#<t	|	t
jjj�r�d(|
d)<n&t	|	t
jjj�r�d*|
d)<nt	|	t
jjj�r�d+|
d)<nt	|	t
jjj�r�d,|
d)<|�r	|	j|
d-<|	j|
d.<|	|
d/<|d�|
�t	|	t
jjj��ri}|	jj|d<|	j|d0<d1|d2<t|d|	j�}
|
jj|d3<t|
j�d4t|	j�|d5<tjj�|	jj�|d6<|	jj |d7<|	jj!|d8<|�rx|	j|d.<|	j|d9<|
j|d <|	j|d:<|	|d/<|d�|�t	|	t
jjj"��r�i}
|	jj|
d<tjj�#|	�|
d;<|	j$j%|	j$j&|	j$j'd<�|
d=<|	j(|
d><t	|	jt
jjj"j)��r�d?|
d@<|	jj*j+}tjjj,|t
j-|d.|dA�}nt	|	jt
jjj"j.��r�dB|
d@<|	jj/}tjj�|�|
d<|�r�|	j|
d.<|	|
d/<|d�|
�t	|	t
jjj0��r�i}|	jj|d<t|d|	j�}
|
jj|d3<t	|	jt
jjj0j1��r1dC|dD<dEdFi|dC<t	|	jt
jjj0j2��rFdC|dD<dEdGi|dC<t	|	jt
jjj0j3��r]dH|dD<dI|	jj4i|dH<|	j$j%|	j$j&|	j$j'd<�|d=<|�r||	j|d.<|	j|d:<|	|d/<|d�|�t	|	t
jjj5��ri}|	jj|d<t	|	jt
jjj5j6��r�dJ|d)<|	jj7|	jj8|	jj9dK�|dL<t	|	jt
jjj5j:��r�dM|d)<t	|	jt
jjj5j;��r�dN|d)<t	|	jt
jjj5j<��r�d-|d)<|	j=|dO<|	j$j%|	j$j&|	j$j'd<�|d=<|�r�|	j|d.<|	|d/<|d�|�t	|	t
jjj>��r2i}|	jj|d<|	j|d <|�r+|	j|d-<|	j|d.<|	|d/<|d�|�q�|S)Pa�
    Queries and converts the virtual machine properties to the available format
    from the schema. If the objects attribute is True the config objects will
    have extra properties, like 'object' which will include the
    vim.vm.device.VirtualDevice, this is necessary for deletion and update
    actions.

    name
        Name of the virtual machine

    datacenter
        Datacenter's name where the virtual machine is available

    objects
        Indicates whether to return the vmware object properties
        (eg. object, key) or just the properties which can be set

    service_instance
        vCenter service instance for connection and configuration
    )
�config.hardware.device�config.hardware.numCPU�!config.hardware.numCoresPerSocket�config.nestedHVEnabled�config.cpuHotAddEnabled�config.cpuHotRemoveEnabled�config.hardware.memoryMB�#config.memoryReservationLockedToMax�config.memoryHotAddEnabled�config.version�config.guestId�config.extraConfigr?)r-rl)rbr5r?rrr
rr)r>r?r@rArBrrZMBrr)rDrErFrArIrrrrXrMrr�r�rr�r�r�r
r�rhr�r�r�r�r�r�r�r�r�r�r�r�r-r��objectrDZKBrEr�r�r�r+r~rr{r}r�)r��	connectedr�r�r�r�r�rWr�r�r�r�r�r�r�r�r$)r�r�r�rr�r4r�)?r7r8rer/rsrdr�r�rHr{rr<r-r�rqrtrfr�r�r�r�r�rUrnrsrrwr4ror2rr+rurvr�Zget_network_adapter_object_typer�r�rr�r�r�rOrrYZDistributedVirtualPortgroupr�r$r�r�r�r�ryr�r�r�r�r�r�r�r�r�ZVirtualSATAController)r?rlrLrbr8r0r�rPZ
extra_confr-r�r�r�r�Zpg_keyr�r�rOr�r3r3r4rN�&st��
�	
��









��



�
�

�
��

���
�

�
����

�



�rNc
Cs�g}|rwdd�|D�}t�d|�|D]c}|d}|d}t||�}d|_|��rv|d|dkrEtj�d�|d|d	|d
|d���t�d|d|d	|d
|d�t	|d|d
d|d|d	|d
d�}|dj
|j_
|�|�q|S)aC
    Changes the disk size and returns the config spec objects in a list.
    The controller property cannot be updated, because controller address
    identifies the disk by the unit and bus number properties.

    disks_diffs
        List of old and new disk properties, the properties are dictionary
        objects
    cS�g|]}|dd�qS)�oldr�r3r�r3r3r4r��'�z!_update_disks.<locals>.<listcomp>zUpdating disks %sr�newFrDzIDisk cannot be downsized size={} unit={} controller_key={} unit_number={}rEr}r{zUVirtual machine disk will be updated size=%s unit=%s controller_key=%s unit_number=%sr�r)rDrEr}r)
r�rHr�ignore_unset_values�changedr7r�rrYr�rr-rU)Z
disks_old_new�disk_changesr�r�current_diskZ	next_disk�
difference�device_config_specr3r3r4�
_update_disks�'sN

��
��

�r"c

CsRg}|r�dd�|D�}t�d|�|D]�}|d}|d}t||�}d|_|��r�t�d|d|d	|d
|d�|d
|d
kr�|�t|d��|�t|d
|d
|d|d|d	d��g}|dD]}	dd�|D�}
|�t|
|	��qg|D]}t	j
j��}||_d|_
|�|�q{q|�t|d
|d
|d|d|d	d��q|S)z�
    Returns a list of vim.vm.device.VirtualDeviceSpec specifying  the scsi
    properties as input the old and new configs are defined in a dictionary.

    scsi_diffs
        List of old and new scsi properties
    cSr�rr�r3r�r3r3r4r��'rz(_update_scsi_devices.<locals>.<listcomp>zUpdating SCSI controllers %srrFzWVirtual machine scsi device will be updated key=%s bus_number=%s type=%s bus_sharing=%sr�rhr�r�rr�rr-cSr��rr3r�r3r3r4r�(r�r)r�rHrrrrU�_delete_devicer�rrr<r-rmrT)
Z
scsis_old_newZ
current_disksZdevice_config_specsr�rZ	next_scsiZcurrent_scsir Zdisks_to_updateZdisk_keyZdisk_objectsrr�r3r3r4�_update_scsi_devices�'sj
���
�����
r&c	
Cs�g}|rUdd�|D�}t�d|�|D]A}|d}|d}t||�}d|_|��rTt�d|d|d	|d
|d�t|d|d	|d
|dd
|d|d�}|�|�q|S)a�
    Returns a list of vim.vm.device.VirtualDeviceSpec specifying
    configuration(s) for changed network adapters, the adapter type cannot
    be changed, as input the old and new configs are defined in a dictionary.

    interface_old_new
        Dictionary with old and new keys which contains the current and the
        next config for a network device

    parent
        Parent managed object reference
    cSr)rr�r3r�r3r3r4r�D(rz,_update_network_adapters.<locals>.<listcomp>zUpdating network interfaces %srrFz]Virtual machine network adapter will be updated switch_type=%s name=%s adapter_type=%s mac=%sr�r?r�r�r�r)rTr�r�)r�rHrrrr�rU)	Zinterface_old_newr��network_changesr�rZcurrent_interfaceZnext_interfacer r!r3r3r4�_update_network_adapters5(s:

��
	�r(cCsrg}|r7dd�|D�}t�d|�|D]!}|d}|d}t||�}d|_|��r4|�t||dd��q|Sd	S)
a
    Returns a list of vim.vm.device.VirtualDeviceSpec specifying to edit a
    deployed serial port configuration to the new given config

    serial_old_new
         Dictionary with old and new keys which contains the current and the
          next config for a serial port device
    cSrr#r3r�r3r3r4r�l(rz(_update_serial_ports.<locals>.<listcomp>zUpdating serial ports %srrFr�rN)r�rHrrrrUr�)Zserial_old_new�serial_changesr�rZcurrent_serialZnext_serialr r3r3r4�_update_serial_portsa(s 	
���r*cCs�g}|rcdd�|D�}t�d|�|D]O}|d}|d}t||�}d|_|��rb|r6t||d�}	|	j}
n|d}
|�t|d	|d
|ddd
|vrO|d
ndd|vrX|dnd|d|
|d�	�q|S)a�
    Returns a list of vim.vm.device.VirtualDeviceSpec specifying to edit a
    deployed cd drive configuration to the new given config

    drives_old_new
        Dictionary with old and new keys which contains the current and the
        next config for a cd drive

    controllers
        Controller device list

    parent
        Managed object reference of the parent object
    cSrr#r3)r�r�r3r3r4r��(rz%_update_cd_drives.<locals>.<listcomp>zUpdating cd/dvd drives %srrFr�r}r�r�r�rr�Nr�r�r�)	r�rHrrrr�r�rUr�)Zdrives_old_newr�r��
cd_changesr�rZ
current_driveZ	new_driver r�r}r3r3r4�_update_cd_drivesz(sF
������r,cCs6t�dt|��tjj��}tjjjjj|_	||_|S)z�
    Returns a vim.vm.device.VirtualDeviceSpec specifying to remove a virtual
    machine device

    device
        Device data type object
    zDeleting device with type %s)
r�rHr�rr<r-rmrxr rT)r-r�r3r3r4r%�(s
r%cCs�d}|r|r|std�}|d}|d}|d}|dur2|dur&td�}|�dd�}|dur2d}|durE|dur?td�}|�dd�}|d	urT|durTt�d
�d	S|r_tjj�d|i�}tjjj	|||||d�}|S)aJ
    Establish client through proxy or with user provided credentials.

    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :returns:
        vSphere Client instance.
    :rtype:
        vSphere.Client
    NrIr/rLrMrST�	ca_bundleFz5Cannot set verify_ssl to False and ca_bundle together)r�rLrMrSr-)
rTrDr�r�r7r8r�Z
get_ca_bundlereZget_vsphere_client)r�rLrMrSr-rW�clientr3r3r4�_get_client�(s:



�r/cC�.d}t|||||d�}|r|jj��}d|iS)a�
    List existing categories a user has access to.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.list_tag_categories

    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :returns:
        Value(s) of category_id.
    :rtype:
        list of str
    N�rSr-Z
Categories)r/�taggingrrg)r�rLrMrbrSr-�
categoriesr.r3r3r4�list_tag_categories�(�$
�r4cCr0)a�
    List existing tags a user has access to.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.list_tags

    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :return:
        Value(s) of tag_id.
    :rtype:
        list of str
    Nr1ZTags)r/r2r!rg)r�rLrMrbrSr-�tagsr.r3r3r4�	list_tags*)r5r7�ClusterComputeResourcec	Csnd}	t|||||d�}
|
r3t||d�}z|
jjj||d�}	Wd|	iSty2t�d�Yd|	iSwd|	iS)a�
    Attach an existing tag to an input object.

    The tag needs to meet the cardinality (`CategoryModel.cardinality`) and
    associability (`CategoryModel.associable_types`) criteria in order to be
    eligible for attachment. If the tag is already attached to the object,
    then this method is a no-op and an error will not be thrown. To invoke
    this method, you need the attach tag privilege on the tag and the read
    privilege on the object.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.attach_tag domain-c2283                 urn:vmomi:InventoryServiceTag:b55ecc77-f4a5-49f8-ab52-38865467cfbe:GLOBAL

    :param str object_id:
        The identifier of the input object.
    :param str tag_id:
        The identifier of the tag object.
    :param str managed_obj:
        Classes that contain methods for creating and deleting resources
        typically contain a class attribute specifying the resource type
        for the resources being created and deleted.
    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :return:
        The list of all tag identifiers that correspond to the
        tags attached to the given object.
    :rtype:
        list of tags
    :raise: Unauthorized
        if you do not have the privilege to read the object.
    :raise: Unauthenticated
        if the user can not be authenticated.
    Nr1�r�r)�tag_id�	object_idzMUnable to attach tag. Check user privileges and object_id (must be a string).zTag attached)r/r)r2r"�attach�vsphere_errorsr�r)r;r:�managed_objr�rLrMrbrSr-Ztag_attachedr.�
dynamic_idr3r3r4�
attach_tagX)s$<
�����r@cCsjd}t|||||d�}	|	r1t||d�}
z|	jj�|
�}Wd|iSty0t�d�Yd|iSwd|iS)a�
    List existing tags a user has access to.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.list_attached_tags domain-c2283

    :param str object_id:
        The identifier of the input object.
    :param str managed_obj:
        Classes that contain methods for creating and deleting resources
        typically contain a class attribute specifying the resource type
        for the resources being created and deleted.
    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :return:
        The list of all tag identifiers that correspond to the
        tags attached to the given object.
    :rtype:
        list of tags
    :raise: Unauthorized
        if you do not have the privilege to read the object.
    :raise: Unauthenticated
        if the user can not be authenticated.
    Nr1r9zUUnable to list attached tags. Check user privileges and object_id (must be a string).z
Attached tags)r/r)r2r"�list_attached_tagsr=r�r)r;r>r�rLrMrbrSr-Z
attached_tagsr.r?r3r3r4rA�)s 1
����rAc	
Cs�d}	t|||||d�}
|
rT|dkrtjj}n|dkrtjj}nd}|
jj��}||_||_	||_
t�}||_z|
jj�
|�}	Wd|	iStySt�d�Yd|	iSwd|	iS)a�
    Create a category with given cardinality.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.create_tag_category

    :param str name:
        Name of tag category to create (ex. Machine, OS, Availability, etc.)
    :param str description:
        Given description of tag category.
    :param str cardinality:
        The associated cardinality (SINGLE, MULTIPLE) of the category.
    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :return:
        Identifier of the created category.
    :rtype:
        str
    :raise: AlreadyExists
        if the name` provided in the create_spec is the name of an already
        existing category.
    :raise: InvalidArgument
        if any of the information in the create_spec is invalid.
    :raise: Unauthorized
        if you do not have the privilege to create a category.
    Nr1�SINGLE�MULTIPLEzOUnable to create tag category. Check user privilege and see if category exists.zCategory created)r/r ZCardinalityrBrCr2r�
CreateSpecr?rB�cardinalityr��associable_typesrzr=r�r)
r?rBrEr�rLrMrbrSr-Zcategory_createdr.�create_specrFr3r3r4�create_tag_category�)s44
�

���rHc	C�^d}t|||||d�}|r+z|jj�|�}Wd|iSty*t�d�Yd|iSwd|iS)a�
    Delete a category.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.delete_tag_category

    :param str category_id:
        The identifier of category to be deleted.
        The parameter must be an identifier for the resource type:
        ``com.vmware.cis.tagging.Category``.
    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :raise: NotFound
        if the tag for the given tag_id does not exist in the system.
    :raise: Unauthorized
        if you do not have the privilege to delete the tag.
    :raise: Unauthenticated
        if the user can not be authenticated.
    Nr1zOUnable to delete tag category. Check user privilege and see if category exists.zCategory deleted)r/r2r�deleter=r�r)	�category_idr�rLrMrbrSr-Zcategory_deletedr.r3r3r4�delete_tag_categoryI*s+
����rLc	Cs|d}	t|||||d�}
|
r:|
jj��}||_||_||_z|
jj�|�}	Wd|	iSty9t	�
d�Yd|	iSwd|	iS)aa
    Create a tag under a category with given description.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.create_tag

    :param basestring server:
        Target DNS or IP of vCenter client.
    :param basestring username:
         Username associated with the vCenter client.
    :param basestring password:
        Password associated with the vCenter client.
    :param str name:
        Name of tag category to create (ex. Machine, OS, Availability, etc.)
    :param str description:
        Given description of tag category.
    :param str category_id:
        Value of category_id representative of the category created previously.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :return:
        The identifier of the created tag.
    :rtype:
        str
    :raise: AlreadyExists
        if the name provided in the create_spec is the name of an already
        existing tag in the input category.
    :raise: InvalidArgument
        if any of the input information in the create_spec is invalid.
    :raise: NotFound
        if the category for in the given create_spec does not exist in
        the system.
    :raise: Unauthorized
        if you do not have the privilege to create tag.
    Nr1zFUnable to create tag. Check user privilege and see if category exists.zTag created)r/r2r!rDr?rBrKrzr=r�r)r?rBrKr�rLrMrbrSr-Ztag_createdr.rGr3r3r4�
create_tag�*s&7
����rMc	CrI)a
    Delete a tag.

    CLI Example:

    .. code-block:: bash

            salt vm_minion vsphere.delete_tag

    :param str tag_id:
        The identifier of tag to be deleted.
        The parameter must be an identifier for the resource type:
        ``com.vmware.cis.tagging.Tag``.
    :param basestring server:
        Target DNS or IP of vCenter center.
    :param basestring username:
        Username associated with the vCenter center.
    :param basestring password:
        Password associated with the vCenter center.
    :param boolean verify_ssl:
        Verify the SSL certificate. Default: True
    :param basestring ca_bundle:
        Path to the ca bundle to use when verifying SSL certificates.
    :raise: AlreadyExists
        if the name provided in the create_spec is the name of an already
        existing category.
    :raise: InvalidArgument
        if any of the information in the create_spec is invalid.
    :raise: Unauthorized
        if you do not have the privilege to create a category.
    Nr1zJUnable to delete category. Check user privileges and that category exists.zTag deleted)r/r2r!rJr=r�r)	r:r�rLrMrbrSr-Ztag_deletedr.r3r3r4�
delete_tag�*s,
����rNc"CsJtjj�||�}tjjj|||d�\}}tjj�|||�}tj��}||_	||_
tj��|_tjjj
|||gd�d}|sGtj�d|�d���z*tjj�|d�}d|vrh|ddkrht�d�d	|�d
�|j_nd�||�|j_Wntjjy�d�||�|j_Ynwg}|r�t||d�|r�t||�|r�t||�|
r�t|
�}|j�|�|	r�d
d�|D�}t||	||d�}|j�|�|r�t||d�\}}|j�|�|r�t|�}|j�|�|r�t|�}|j�|�|�|�|
r�t |
�} |j�| �|�| �|�rt!|||d�}!|j�|!�|�rt"||�tjj�#|||||�ddiS)a�

    Creates a virtual machine container.

    CLI Example:

    .. code-block:: bash

        salt vm_minion vsphere.create_vm vm_name=vmname cpu='{count: 2, nested: True}' ...

    vm_name
        Name of the virtual machine

    cpu
        Properties of CPUs for freshly created machines

    memory
        Memory size for freshly created machines

    image
        Virtual machine guest OS version identifier
        VirtualMachineGuestOsIdentifier

    version
        Virtual machine container hardware version

    datacenter
        Datacenter where the virtual machine will be deployed (mandatory)

    datastore
        Datastore where the virtual machine files will be placed

    placement
        Resource pool or cluster or host or folder where the virtual machine
        will be deployed

    devices
        interfaces

        .. code-block:: bash

            interfaces:
              adapter: 'Network adapter 1'
              name: vlan100
              switch_type: distributed or standard
              adapter_type: vmxnet3 or vmxnet, vmxnet2, vmxnet3, e1000, e1000e
              mac: '00:11:22:33:44:55'
              connectable:
                allow_guest_control: True
                connected: True
                start_connected: True

        disks

        .. code-block:: bash

            disks:
              adapter: 'Hard disk 1'
              size: 16
              unit: GB
              address: '0:0'
              controller: 'SCSI controller 0'
              thin_provision: False
              eagerly_scrub: False
              datastore: 'myshare'
              filename: 'vm/mydisk.vmdk'

        scsi_devices

        .. code-block:: bash

            scsi_devices:
              controller: 'SCSI controller 0'
              type: paravirtual
              bus_sharing: no_sharing

        serial_ports

        .. code-block:: bash

            serial_ports:
              adapter: 'Serial port 1'
              type: network
              backing:
                uri: 'telnet://something:port'
                direction: <client|server>
                filename: 'service_uri'
              connectable:
                allow_guest_control: True
                connected: True
                start_connected: True
              yield: False

        cd_drives

        .. code-block:: bash

            cd_drives:
              adapter: 'CD/DVD drive 0'
              controller: 'IDE 0'
              device_type: datastore_iso_file
              datastore_iso_file:
                path: path_to_iso
              connectable:
                allow_guest_control: True
                connected: True
                start_connected: True

    advanced_config
        Advanced config parameters to be set for the virtual machine
    )r5r'rzSpecified datastore: 'z' does not exist.zsummary.typer9zIThe vmPathName should be the datastore name if the datastore type is vsanr�r�z[{0}] {1}/{1}.vmxrcSr�r3r)r�r�r3r3r4r��+r�zcreate_vm.<locals>.<listcomp>�r�r�)r��r�r��	create_vmT)$r7r8rers�
get_placementZ
get_folderrr<r#r?�guestIdZFileInfor9r*r�rrur�rHZ
vmPathNamerYrr=rCrJr��deviceChanger�r�r�r�r�r�r�rVrQ)"rOrrIrrXrlr+r5rr�r�r�r�r�r�rMrbr8�resourcepool_object�placement_objectZ
folder_objectr�r�Z
ds_summaryZcd_controllersr�r�r�Zinterface_specsZnic_settingsZserial_port_specsZ	ide_specs�
sata_specsr�r3r3r4rQ
+s�
��
��
������

�
�

�

�rQc%Cs^t||	d|d�}t||||||||||	|
|||
d�|�}tj��}tjj�||	�}tjjj	|tj
|d|d�}|��}d|vrO|d��t
�krOt||dj�d|vrd|d��t
�krdt||dj�d|vrst||dj|jj�d	|vr}t||d
�d|vr�||_g}d|vr�d
|vr�g}|�t|dj|d
��|djD]}|�t|d��q�t|dj�}|�|�|j�|�d
|v�r	g}|�t |d
j��|d
jD]}|�t|d��q�dd�|dD�}|�dd�|D��|�t!||d
j||d��|j�|�d|v�rBg}|�t"|dj|��|djD]}|�t|d���q t#|dj|�\}}|�|�|j�|�d|v�rug}|�t$|dj��|djD]}|�t|d���qX|�t%|dj��|j�|�g}d|v�r�t&|dj�}|djD]}|�t|d���q�|�|�|j�|�d|v�r�g} dd�|dD�}!|!�dd�|D��| �t'|dj|!|d��|djD]}| �t|d���q�| �t(|dj|!|d��|j�| �|�r�tjj�)||�i}"|�*�D]/\}#}$t+|$tjj,j-��r&|$j.ddd�|$j.ddd�|$j.ddd�|$j.ddd�|$j/|"|#<�q�|"S)a
    Updates the configuration of the virtual machine if the config differs

    vm_name
        Virtual Machine name to be updated

    cpu
        CPU configuration options

    memory
        Memory configuration options

    version
        Virtual machine container hardware version

    image
        Virtual machine guest OS version identifier
        VirtualMachineGuestOsIdentifier

    interfaces
        Network interfaces configuration options

    disks
        Disks configuration options

    scsi_devices
        SCSI devices configuration options

    serial_ports
        Serial ports configuration options

    datacenter
        Datacenter where the virtual machine is available

    datastore
        Datastore where the virtual machine config files are available

    cd_dvd_drives
        CD/DVD drives configuration options

    advanced_config
        Advanced config parameters to be set for the virtual machine

    service_instance
        vCenter service instance for connection and configuration
    TrK)r?rrIrrXrr�r�r�rlr+r�r�rMr?rWrrIrMrXrrr�r�rcSr�r$r3�r��devr3r3r4r�n,r�zupdate_vm.<locals>.<listcomp>cSr�r3r�r�r�r3r3r4r�p,r�rOrr�r�r�cSr�r$r3rXr3r3r4r��,r�cSr�r3rrZr3r3r4r��,r�)r�r�rP�	intersect)rZ	diff_listr��removed)0rNrXrr<r#r7r8rersrYrZr�rr�rCZcurrent_dictrJrVr\r�rSr=rSr�r&r[r\rUr%r�ZaddedrTr"r�r(r�r*r�r�r,r�r]r�r{Z
listdifferZListDictDifferr	r[)%rOrrIrrXrr�r�r�rlr+Z
cd_dvd_drivesr�rMrbrPr[r�r^r_Zdifference_keysZnew_scsi_devicesZscsi_changesrrr�r'ZadaptersZnicsr)Znew_controllersrWr+r�r`r�r8r3r3r4r]�+sC���
����

���
�
�




����r]cCsTt�d|||�tjj�||�}d|vrBtjj�||d�}tjjj|dgd�}d|vr1|d}ntj�	d��tjj�
||||�n_d|vr�tjjj|||dgd�}	|	s`tj�	d�|d���|	d	}
tjjj|
d
gd�}d
|vr�|d
}tjjj|d
gd�}
d
|
vr�|
d
}ntj�	d��tj�	d��tjjj
|||||
d
�dddid�}|S)a�
    Registers a virtual machine to the inventory with the given vmx file.
    Returns comments and change list

    name
        Name of the virtual machine

    datacenter
        Datacenter of the virtual machine

    placement
        Placement dictionary of the virtual machine, host or cluster

    vmx_path:
        Full path to the vmx file, datastore name should be included

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.
    zTRegistering virtual machine with properties datacenter=%s, placement=%s, vmx_path=%srsZresourcePoolr7z:The cluster's resource pool object could not be retrieved.rKr2r3rr�z7The host parent's parent object could not be retrieved.z0The host's parent object could not be retrieved.)Zhost_objectz-Virtual machine registration action succeeded�register_vmT�Zcommentr`)
r�rHr7r8rersrtrur�rr]r�rY)r?rlr5Zvmx_pathrbr7Zcluster_objZ
cluster_propsZresourcepoolr�Zhost_objZ
host_propsZhost_parentr�r�r3r3r4r]�,sh���
�����
��
��r]cC�pt�d|�ddg}tjjj||||d�}|ddkr$dddid	�}|Stjjj|d
dd�d
ddid	�}|S)ap
    Powers on a virtual machine specified by its name.

    name
        Name of the virtual machine

    datacenter
        Datacenter of the virtual machine

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.power_on_vm name=my_vm

    zPowering on virtual machine %sr?�summary.runtime.powerState�rlr-Z	poweredOnz%Virtual machine is already powered onZpower_onTr^r�on��actionz)Virtual machine power on action succeeded�r�rHr7r8rer/Zpower_cycle_vm�r?rlrbr-r0r�r3r3r4�power_on_vm-����rgcCr_)ar
    Powers off a virtual machine specified by its name.

    name
        Name of the virtual machine

    datacenter
        Datacenter of the virtual machine

    service_instance
        Service instance (vim.ServiceInstance) of the vCenter.
        Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.power_off_vm name=my_vm

    zPowering off virtual machine %sr?r`raZ
poweredOffz&Virtual machine is already powered off�	power_offTr^rZoffrcz*Virtual machine power off action succeededrerfr3r3r4�power_off_vm>-rhrjc	Cs�i}|rtjj�|||�\}}ntjj�||�}|r$t|||�d|d<tjjj|tj|d|d�}|s=tj	�
d�||���||fS)a9
    Helper function to remove a virtual machine

    name
        Name of the virtual machine

    service_instance
        vCenter service instance for connection and configuration

    datacenter
        Datacenter of the virtual machine

    placement
        Placement information of the virtual machine
    TZpowered_offr?rWz<The virtual machine object {} in datacenter {} was not found)r7r8rerRrsrjrYrrZr�rrY)	r?rlrbr5ri�resultsrUrVr_r3r3r4�
_remove_vmj-s2
�����rlc	
C�zi}t��}zt�|||d�|�Wntjjy$}zt|��d}~wwt|||||d�\}}tj	j
�|�d|d<|S)a�
    Deletes a virtual machine defined by name and placement

    name
        Name of the virtual machine

    datacenter
        Datacenter of the virtual machine

    placement
        Placement information of the virtual machine

    service_instance
        vCenter service instance for connection and configuration

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.delete_vm name=my_vm datacenter=my_datacenter

    �r?rlr5N�rbr5riTZ
deleted_vm)r	r�r�r�r�r�rrlr7r8re�	delete_vm�	r?rlr5rirbrkr�r�r_r3r3r4rp�-s(���
�rpc	
Crm)a�
    Unregisters a virtual machine defined by name and placement

    name
        Name of the virtual machine

    datacenter
        Datacenter of the virtual machine

    placement
        Placement information of the virtual machine

    service_instance
        vCenter service instance for connection and configuration

    CLI Example:

    .. code-block:: bash

        salt '*' vsphere.unregister_vm name=my_vm datacenter=my_datacenter

    rnNroTZunregistered_vm)r
r�r�r�r�r�rrlr7r8re�
unregister_vmrqr3r3r4rr�-s(���
�rrr1)NNNNNNN)NNNN)NNr�r�NN)NNTTNN)NNNNNr�)NNNT)NNT)NNNr�T)T)NN)NT)TN)TTN)r)NNrkNNNNN)rb)NrNNN)r)NNNr�N)NTN)NNNNNN)r8NNNNNN)NNNNNNNNNNNNNN)NFN(�__doc__r$�loggingr��	functoolsrZsalt.utils.argsr7Zsalt.utils.dictupdater8r�Zsalt.utils.httpZsalt.utils.pathZsalt.utils.pbmZsalt.utils.vmwareZsalt.utils.vsanZsalt.config.schemas.esxclusterrrZsalt.config.schemas.esxirrrZsalt.config.schemas.esxvmr	r
Zsalt.config.schemas.vcenterrZsalt.exceptionsrr
rrrrrrZsalt.utils.decoratorsrrZsalt.utils.dictdifferrZsalt.utils.listdifferr�	getLoggerrZr�r�ZHAS_JSONSCHEMA�ImportErrorZpyVmomirrrrZ
versionMap�version_infor�ZHAS_PYVMOMIZcom.vmware.cis.tagging_clientrr r!r"r#Z!com.vmware.vapi.std.errors_clientr$r%r&r'r(Zcom.vmware.vapi.std_clientr)Zcom.vmware.vcenter_clientr*r=ZHAS_VSPHERE_SDKr��whichZesx_cliZHAS_ESX_CLIr2Z__proxyenabled__r5rArErXrarorprhrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrrr	r
rrrrr!r"r#r&r(r,r.r8r=r>rRrUrYrarkrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrr r"r%r.r/r3r4rqrur~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr
rr�r�r�r&rrr(r'r,r1r:r=rCrJrQrVrardrerjr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�rrXrNr"r&r(r*r,r%r/r4r7r@rArHrLrMrNrQr]r]rgrjrlrprrr3r3r3r4�<module>s�8(

�


��	��
 ]�^�\�c�o�i�^�O�9�Q�{�S�7�A�@�	�	�J�O�W�6�,*�+�+)�+))))*�C�J�]�	��	��K�G�J�U��a�a J	
&GP076L64I($
2))
"
�.(X._q
�b�?
!
 #�2�6�399K+�G�F�P' �>B�7�K
�iD�)3

(
�#�\5<�VS#)�g#J\53_k7E,8;�*�*�R�E�N�7�F�8

�\
�R
P
(
(,
+
�