Replaces the user template contents.

    .. versionadded:: 2016.3.0

    name
        The name of the VM to update.

    path
        The path to a file containing new user template contents. Syntax within the
        file can be the usual attribute=value or XML. Can be used instead of ``data``.

    data
        Contains the new user template contents. Syntax can be the usual attribute=value
        or XML. Can be used instead of ``path``.

    update_type
        There are two ways to update a VM: ``replace`` the whole template
        or ``merge`` the new template with the existing one.

    CLI Example:

    .. code-block:: bash

        salt-cloud -a vm_update my-vm path=/path/to/user_template_file.txt update_type='replace'

def vm_update(name, kwargs=None, call=None):
    '''
    Replaces the user template contents.

    .. versionadded:: 2016.3.0

    name
        The name of the VM to update.

    path
        The path to a file containing new user template contents. Syntax within the
        file can be the usual attribute=value or XML. Can be used instead of ``data``.

    data
        Contains the new user template contents. Syntax can be the usual attribute=value
        or XML. Can be used instead of ``path``.

    update_type
        There are two ways to update a VM: ``replace`` the whole template
        or ``merge`` the new template with the existing one.

    CLI Example:

    .. code-block:: bash

        salt-cloud -a vm_update my-vm path=/path/to/user_template_file.txt update_type='replace'
    '''
    if call != 'action':
        raise SaltCloudSystemExit(
            'The vm_update action must be called with -a or --action.'
        )

    if kwargs is None:
        kwargs = {}

    path = kwargs.get('path', None)
    data = kwargs.get('data', None)
    update_type = kwargs.get('update_type', None)
    update_args = ['replace', 'merge']

    if update_type is None:
        raise SaltCloudSystemExit(
            'The vm_update function requires an \'update_type\' to be provided.'
        )

    if update_type == update_args[0]:
        update_number = 0
    elif update_type == update_args[1]:
        update_number = 1
    else:
        raise SaltCloudSystemExit(
            'The update_type argument must be either {0} or {1}.'.format(
                update_args[0],
                update_args[1]
            )
        )

    if data:
        if path:
            log.warning(
                'Both the \'data\' and \'path\' arguments were provided. '
                '\'data\' will take precedence.'
            )
    elif path:
        with salt.utils.files.fopen(path, mode='r') as rfh:
            data = rfh.read()
    else:
        raise SaltCloudSystemExit(
            'The vm_update function requires either \'data\' or a file \'path\' '
            'to be provided.'
        )

    server, user, password = _get_xml_rpc()
    auth = ':'.join([user, password])
    vm_id = int(get_vm_id(kwargs={'name': name}))
    response = server.one.vm.update(auth, vm_id, data, int(update_number))

    ret = {
        'action': 'vm.update',
        'updated': response[0],
        'resource_id': response[1],
        'error_code': response[2],
    }

    return ret