diff --git a/src/legacy/core_plugins/telemetry/server/routes/index.ts b/src/legacy/core_plugins/telemetry/server/routes/index.ts index 12ba541d699f9..c9e435f916c70 100644 --- a/src/legacy/core_plugins/telemetry/server/routes/index.ts +++ b/src/legacy/core_plugins/telemetry/server/routes/index.ts @@ -18,10 +18,10 @@ */ import { CoreSetup } from 'src/core/server'; -import { registerOptInRoutes } from './opt_in'; +import { registerTelemetryConfigRoutes } from './telemetry_config'; import { registerTelemetryDataRoutes } from './telemetry_stats'; export function registerRoutes(core: CoreSetup) { - registerOptInRoutes(core); + registerTelemetryConfigRoutes(core); registerTelemetryDataRoutes(core); } diff --git a/src/legacy/core_plugins/telemetry/server/routes/opt_in.ts b/src/legacy/core_plugins/telemetry/server/routes/telemetry_config.ts similarity index 61% rename from src/legacy/core_plugins/telemetry/server/routes/opt_in.ts rename to src/legacy/core_plugins/telemetry/server/routes/telemetry_config.ts index aabc0259f08fc..551e3198c439e 100644 --- a/src/legacy/core_plugins/telemetry/server/routes/opt_in.ts +++ b/src/legacy/core_plugins/telemetry/server/routes/telemetry_config.ts @@ -21,9 +21,36 @@ import Joi from 'joi'; import { boomify } from 'boom'; import { CoreSetup } from 'src/core/server'; -export function registerOptInRoutes(core: CoreSetup) { +async function updateTelemetrySavedObjectFromReq(req: any) { + const savedObjectsClient = req.getSavedObjectsClient(); + return await savedObjectsClient.update('telemetry', 'telemetry', req.payload); +} + +export function registerTelemetryConfigRoutes(core: CoreSetup) { const { server } = core.http as any; + server.route({ + method: 'POST', + path: '/api/telemetry/v2/usageFetcher', + options: { + validate: { + payload: Joi.object({ + usageFetcher: Joi.string() + .allow(['browser', 'server']) + .required(), + }), + }, + }, + handler: async (req: any, h: any) => { + try { + await updateTelemetrySavedObjectFromReq(req); + } catch (err) { + return boomify(err); + } + return h.response({}).code(200); + }, + }); + server.route({ method: 'POST', path: '/api/telemetry/v2/optIn', @@ -35,18 +62,8 @@ export function registerOptInRoutes(core: CoreSetup) { }, }, handler: async (req: any, h: any) => { - const savedObjectsClient = req.getSavedObjectsClient(); try { - await savedObjectsClient.create( - 'telemetry', - { - enabled: req.payload.enabled, - }, - { - id: 'telemetry', - overwrite: true, - } - ); + await updateTelemetrySavedObjectFromReq(req); } catch (err) { return boomify(err); }