"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KibanaSavedObjectsSLORepository = void 0; var _coreSavedObjectsServer = require("@kbn/core-saved-objects-server"); var _sloSchema = require("@kbn/slo-schema"); var _Either = require("fp-ts/lib/Either"); var _pipeable = require("fp-ts/lib/pipeable"); var t = _interopRequireWildcard(require("io-ts")); var _errors = require("../../errors"); var _saved_objects = require("../../saved_objects"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* * 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. */ class KibanaSavedObjectsSLORepository { constructor(soClient) { this.soClient = soClient; } async save(slo, options = { throwOnConflict: false }) { let existingSavedObjectId; const findResponse = await this.soClient.find({ type: _saved_objects.SO_SLO_TYPE, page: 1, perPage: 1, filter: `slo.attributes.id:(${slo.id})` }); if (findResponse.total === 1) { if (options.throwOnConflict) { throw new _errors.SLOIdConflict(`SLO [${slo.id}] already exists`); } existingSavedObjectId = findResponse.saved_objects[0].id; } const savedSLO = await this.soClient.create(_saved_objects.SO_SLO_TYPE, toStoredSLO(slo), { id: existingSavedObjectId, overwrite: true }); return toSLO(savedSLO.attributes); } async findById(id) { const response = await this.soClient.find({ type: _saved_objects.SO_SLO_TYPE, page: 1, perPage: 1, filter: `slo.attributes.id:(${id})` }); if (response.total === 0) { throw new _errors.SLONotFound(`SLO [${id}] not found`); } return toSLO(response.saved_objects[0].attributes); } async deleteById(id) { const response = await this.soClient.find({ type: _saved_objects.SO_SLO_TYPE, page: 1, perPage: 1, filter: `slo.attributes.id:(${id})` }); if (response.total === 0) { throw new _errors.SLONotFound(`SLO [${id}] not found`); } await this.soClient.delete(_saved_objects.SO_SLO_TYPE, response.saved_objects[0].id); } async findAllByIds(ids) { if (ids.length === 0) return []; try { const response = await this.soClient.find({ type: _saved_objects.SO_SLO_TYPE, page: 1, perPage: ids.length, filter: `slo.attributes.id:(${ids.join(' or ')})` }); return response.saved_objects.map(slo => toSLO(slo.attributes)); } catch (err) { if (_coreSavedObjectsServer.SavedObjectsErrorHelpers.isNotFoundError(err)) { throw new _errors.SLONotFound(`SLOs [${ids.join(',')}] not found`); } throw err; } } async search(search) { try { const response = await this.soClient.find({ type: _saved_objects.SO_SLO_TYPE, page: 1, perPage: 25, search, searchFields: ['name'] }); return response.saved_objects.map(slo => toSLO(slo.attributes)); } catch (err) { throw err; } } } exports.KibanaSavedObjectsSLORepository = KibanaSavedObjectsSLORepository; function toStoredSLO(slo) { return _sloSchema.sloSchema.encode(slo); } function toSLO(storedSLO) { return (0, _pipeable.pipe)(_sloSchema.sloSchema.decode(storedSLO), (0, _Either.fold)(() => { throw new Error('Invalid Stored SLO'); }, t.identity)); }