Takes particular component list, compares it with existing, call appropriate API methods - create, update, delete.

    :param component: component name
    :param parent_id: ID of parent entity under which component should be created
    :param defined: list of defined items of named component
    :param existing: list of existing items of named component
    :param template_id: In case that component need also template ID for creation (although parent_id is given?!?!?)

def _manage_component(component, parent_id, defined, existing, template_id=None, **kwargs):
    '''
    Takes particular component list, compares it with existing, call appropriate API methods - create, update, delete.

    :param component: component name
    :param parent_id: ID of parent entity under which component should be created
    :param defined: list of defined items of named component
    :param existing: list of existing items of named component
    :param template_id: In case that component need also template ID for creation (although parent_id is given?!?!?)
    '''
    zabbix_id_mapper = __salt__['zabbix.get_zabbix_id_mapper']()

    dry_run = __opts__['test']
    c_def = TEMPLATE_COMPONENT_DEF[component]
    compare_key = c_def['filter']

    defined_set = set([item[compare_key] for item in defined])
    existing_set = set([item[compare_key] for item in existing])

    create_set = defined_set - existing_set
    update_set = defined_set & existing_set
    delete_set = existing_set - defined_set

    create_list = [item for item in defined if item[compare_key] in create_set]
    for object_params in create_list:
        if parent_id:
            object_params.update({c_def['pid_ref_name']: parent_id})

        if 'pid_ref_name2' in c_def:
            object_params.update({c_def['pid_ref_name2']: template_id})

        _adjust_object_lists(object_params)

        if not dry_run:
            object_create = __salt__['zabbix.run_query'](c_def['qtype'] + '.create', object_params, **kwargs)
            if object_create:
                object_ids = object_create[c_def['res_id_name']]
                CHANGE_STACK.append({'component': component, 'action': 'create', 'params': object_params,
                                     c_def['filter']: object_params[c_def['filter']], 'object_id': object_ids})
        else:
            CHANGE_STACK.append({'component': component, 'action': 'create', 'params': object_params,
                                 'object_id': 'CREATED '+TEMPLATE_COMPONENT_DEF[component]['qtype']+' ID'})

    delete_list = [item for item in existing if item[compare_key] in delete_set]
    for object_del in delete_list:
        object_id_name = zabbix_id_mapper[c_def['qtype']]
        CHANGE_STACK.append({'component': component, 'action': 'delete', 'params': [object_del[object_id_name]]})
        if not dry_run:
            __salt__['zabbix.run_query'](c_def['qtype'] + '.delete', [object_del[object_id_name]], **kwargs)

    for object_name in update_set:
        ditem = next((item for item in defined if item[compare_key] == object_name), None)
        eitem = next((item for item in existing if item[compare_key] == object_name), None)
        diff_params = __salt__['zabbix.compare_params'](ditem, eitem, True)

        if diff_params['new']:
            diff_params['new'][zabbix_id_mapper[c_def['qtype']]] = eitem[zabbix_id_mapper[c_def['qtype']]]
            diff_params['old'][zabbix_id_mapper[c_def['qtype']]] = eitem[zabbix_id_mapper[c_def['qtype']]]
            _adjust_object_lists(diff_params['new'])
            _adjust_object_lists(diff_params['old'])
            CHANGE_STACK.append({'component': component, 'action': 'update', 'params': diff_params['new']})

            if not dry_run:
                __salt__['zabbix.run_query'](c_def['qtype'] + '.update', diff_params['new'], **kwargs)