"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchConnectors = exports.fetchConnectorByIndexName = exports.fetchConnectorById = void 0; var _ = require("../.."); var _identify_exceptions = require("../../utils/identify_exceptions"); var _fetch_all = require("../fetch_all"); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ const fetchConnectorById = async (client, connectorId) => { try { const connectorResult = await client.asCurrentUser.get({ id: connectorId, index: _.CONNECTORS_INDEX }); return connectorResult._source ? { primaryTerm: connectorResult._primary_term, seqNo: connectorResult._seq_no, value: { ...connectorResult._source, id: connectorResult._id } } : undefined; } catch (error) { if ((0, _identify_exceptions.isIndexNotFoundException)(error)) { return undefined; } throw error; } }; exports.fetchConnectorById = fetchConnectorById; const fetchConnectorByIndexName = async (client, indexName) => { try { var _connectorResult$hits, _await$fetchConnector; const connectorResult = await client.asCurrentUser.search({ index: _.CONNECTORS_INDEX, query: { term: { index_name: indexName } } }); // Because we cannot guarantee that the index has been refreshed and is giving us the most recent source // we need to fetch the source with a direct get from the index, which will always be up to date const result = (_connectorResult$hits = connectorResult.hits.hits[0]) !== null && _connectorResult$hits !== void 0 && _connectorResult$hits._source ? (_await$fetchConnector = await fetchConnectorById(client, connectorResult.hits.hits[0]._id)) === null || _await$fetchConnector === void 0 ? void 0 : _await$fetchConnector.value : undefined; return result; } catch (error) { if ((0, _identify_exceptions.isIndexNotFoundException)(error)) { return undefined; } throw error; } }; exports.fetchConnectorByIndexName = fetchConnectorByIndexName; const fetchConnectors = async (client, indexNames) => { const query = indexNames ? { terms: { index_name: indexNames } } : { match_all: {} }; try { return await (0, _fetch_all.fetchAll)(client, _.CONNECTORS_INDEX, query); } catch (error) { if ((0, _identify_exceptions.isIndexNotFoundException)(error)) { return []; } throw error; } }; exports.fetchConnectors = fetchConnectors;