Skip to content

Commit

Permalink
use api keys on serverless
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiota committed Jul 9, 2024
1 parent d2c5096 commit 5ab6a6c
Showing 1 changed file with 110 additions and 33 deletions.
143 changes: 110 additions & 33 deletions x-pack/test/api_integration/services/slo_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@kbn/slo-schema';
import * as t from 'io-ts';
import { FtrProviderContext } from '../../functional/ftr_provider_context';
import { RoleCredentials } from '../../../test_serverless/shared/services';

type DurationUnit = 'm' | 'h' | 'd' | 'w' | 'M';

Expand Down Expand Up @@ -71,71 +72,147 @@ export function SloApiProvider({ getService }: FtrProviderContext) {
const es = getService('es');
const supertest = getService('supertest');
const retry = getService('retry');
const config = getService('config');
const isServerless = config.get('serverless');
let svlUserManager;
if (isServerless) {
svlUserManager = getService('svlUserManager');
}

const requestTimeout = 30 * 1000;
const retryTimeout = 180 * 1000;

return {
async create(slo: SloParams) {
const { body } = await supertest
.post(`/api/observability/slos`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.send(slo);
let roleAuthc: RoleCredentials;
if (isServerless) {
roleAuthc = await svlUserManager.createApiKeyForRole('admin');
const { body } = await supertest
.post(`/api/observability/slos`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.set(roleAuthc.apiKeyHeader)
.send(slo);

return body;
return body;
} else {
const { body } = await supertest
.post(`/api/observability/slos`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.send(slo);

return body;
}
},

async delete(sloId: string) {
const response = await supertest
.delete(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo');
return response;
let roleAuthc: RoleCredentials;
if (isServerless) {
roleAuthc = await svlUserManager.createApiKeyForRole('admin');
const response = await supertest
.delete(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.set(roleAuthc.apiKeyHeader);
return response;
} else {
const response = await supertest
.delete(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo');
return response;
}
},

async fetchHistoricalSummary(
params: FetchHistoricalSummaryParams
): Promise<FetchHistoricalSummaryResponse> {
const { body } = await supertest
.post(`/internal/observability/slos/_historical_summary`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.send(params);
let roleAuthc: RoleCredentials;
if (isServerless) {
roleAuthc = await svlUserManager.createApiKeyForRole('admin');
const { body } = await supertest
.post(`/internal/observability/slos/_historical_summary`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.set(roleAuthc.apiKeyHeader)
.send(params);

return body;
} else {
const { body } = await supertest
.post(`/internal/observability/slos/_historical_summary`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.send(params);

return body;
return body;
}
},

async waitForSloToBeDeleted(sloId: string) {
if (!sloId) {
throw new Error(`sloId is undefined`);
}
let roleAuthc: RoleCredentials;

return await retry.tryForTime(retryTimeout, async () => {
const response = await supertest
.delete(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.timeout(requestTimeout);
if (!response.ok) {
throw new Error(`slodId [${sloId}] was not deleted`);
if (isServerless) {
roleAuthc = await svlUserManager.createApiKeyForRole('admin');
const response = await supertest
.delete(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.set(roleAuthc.apiKeyHeader)
.timeout(requestTimeout);
if (!response.ok) {
throw new Error(`slodId [${sloId}] was not deleted`);
}
return response;
} else {
const response = await supertest
.delete(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.timeout(requestTimeout);
if (!response.ok) {
throw new Error(`slodId [${sloId}] was not deleted`);
}
return response;
}
return response;
});
},

async waitForSloCreated({ sloId }: { sloId: string }) {
if (!sloId) {
throw new Error(`'sloId is undefined`);
}
let roleAuthc: RoleCredentials;

return await retry.tryForTime(retryTimeout, async () => {
const response = await supertest
.get(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.timeout(requestTimeout);
if (response.body.id === undefined) {
throw new Error(`No slo with id ${sloId} found`);
if (isServerless) {
roleAuthc = await svlUserManager.createApiKeyForRole('admin');
const response = await supertest
.get(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.set(roleAuthc.apiKeyHeader)
.timeout(requestTimeout);
if (response.body.id === undefined) {
throw new Error(`No slo with id ${sloId} found`);
}
return response.body;
} else {
const response = await supertest
.get(`/api/observability/slos/${sloId}`)
.set('kbn-xsrf', 'foo')
.set('x-elastic-internal-origin', 'foo')
.timeout(requestTimeout);
if (response.body.id === undefined) {
throw new Error(`No slo with id ${sloId} found`);
}
return response.body;
}
return response.body;
});
},

Expand Down

0 comments on commit 5ab6a6c

Please sign in to comment.