Update an element, including it's properties

def _update_element(name, element_type, data, server=None):
    '''
    Update an element, including it's properties
    '''
    # Urlencode the name (names may have slashes)
    name = quote(name, safe='')

    # Update properties first
    if 'properties' in data:
        properties = []
        for key, value in data['properties'].items():
            properties.append({'name': key, 'value': value})
        _api_post('{0}/{1}/property'.format(element_type, name), properties, server)
        del data['properties']

        # If the element only contained properties
        if not data:
            return unquote(name)

    # Get the current data then merge updated data into it
    update_data = _get_element(name, element_type, server, with_properties=False)
    if update_data:
        update_data.update(data)
    else:
        __context__['retcode'] = salt.defaults.exitcodes.SALT_BUILD_FAIL
        raise CommandExecutionError('Cannot update {0}'.format(name))

    # Finally, update the element
    _api_post('{0}/{1}'.format(element_type, name), _clean_data(update_data), server)
    return unquote(name)