.. versionadded:: 2017.7

    Imports Zabbix configuration specified in file to Zabbix server.

    :param config_file: File with Zabbix config (local or remote)
    :param rules: Optional - Rules that have to be different from default (defaults are the same as in Zabbix web UI.)
    :param file_format: Config file format (default: xml)
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.configuration_import salt://zabbix/config/zabbix_templates.xml \
        "{'screens': {'createMissing': True, 'updateExisting': True}}"

def configuration_import(config_file, rules=None, file_format='xml', **kwargs):
    '''
    .. versionadded:: 2017.7

    Imports Zabbix configuration specified in file to Zabbix server.

    :param config_file: File with Zabbix config (local or remote)
    :param rules: Optional - Rules that have to be different from default (defaults are the same as in Zabbix web UI.)
    :param file_format: Config file format (default: xml)
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.configuration_import salt://zabbix/config/zabbix_templates.xml \
        "{'screens': {'createMissing': True, 'updateExisting': True}}"
    '''
    if rules is None:
        rules = {}
    default_rules = {'applications': {'createMissing': True, 'updateExisting': False, 'deleteMissing': False},
                     'discoveryRules': {'createMissing': True, 'updateExisting': True, 'deleteMissing': False},
                     'graphs': {'createMissing': True, 'updateExisting': True, 'deleteMissing': False},
                     'groups': {'createMissing': True},
                     'hosts': {'createMissing': False, 'updateExisting': False},
                     'images': {'createMissing': False, 'updateExisting': False},
                     'items': {'createMissing': True, 'updateExisting': True, 'deleteMissing': False},
                     'maps': {'createMissing': False, 'updateExisting': False},
                     'screens': {'createMissing': False, 'updateExisting': False},
                     'templateLinkage': {'createMissing': True},
                     'templates': {'createMissing': True, 'updateExisting': True},
                     'templateScreens': {'createMissing': True, 'updateExisting': True, 'deleteMissing': False},
                     'triggers': {'createMissing': True, 'updateExisting': True, 'deleteMissing': False},
                     'valueMaps': {'createMissing': True, 'updateExisting': False}}
    new_rules = dict(default_rules)

    if rules:
        for rule in rules:
            if rule in new_rules:
                new_rules[rule].update(rules[rule])
            else:
                new_rules[rule] = rules[rule]
    if 'salt://' in config_file:
        tmpfile = salt.utils.files.mkstemp()
        cfile = __salt__['cp.get_file'](config_file, tmpfile)
        if not cfile or os.path.getsize(cfile) == 0:
            return {'name': config_file, 'result': False, 'message': 'Failed to fetch config file.'}
    else:
        cfile = config_file
        if not os.path.isfile(cfile):
            return {'name': config_file, 'result': False, 'message': 'Invalid file path.'}

    with salt.utils.files.fopen(cfile, mode='r') as fp_:
        xml = fp_.read()

    if 'salt://' in config_file:
        salt.utils.files.safe_rm(cfile)

    params = {'format': file_format,
              'rules': new_rules,
              'source': xml}
    log.info('CONFIGURATION IMPORT: rules: %s', six.text_type(params['rules']))
    try:
        run_query('configuration.import', params, **kwargs)
        return {'name': config_file, 'result': True, 'message': 'Zabbix API "configuration.import" method '
                                                                'called successfully.'}
    except SaltException as exc:
        return {'name': config_file, 'result': False, 'message': six.text_type(exc)}