Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate alert to rule... doesnt work :( #91144

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 53 additions & 53 deletions x-pack/plugins/alerts/server/alerts_client/alerts_client.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion x-pack/plugins/alerts/server/alerts_client_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class AlertsClientFactory {
alertTypeRegistry: this.alertTypeRegistry,
unsecuredSavedObjectsClient: savedObjects.getScopedClient(request, {
excludedWrappers: ['security'],
includedHiddenTypes: ['alert', 'api_key_pending_invalidation'],
includedHiddenTypes: ['rule', 'api_key_pending_invalidation'],
}),
authorization,
actionsAuthorization: actions.getActionsAuthorizationWithRequest(request),
Expand Down
16 changes: 8 additions & 8 deletions x-pack/plugins/alerts/server/health/get_health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const getHealth = async (
};

const { saved_objects: decryptErrorData } = await internalSavedObjectsRepository.find<RawAlert>({
filter: `alert.attributes.executionStatus.status:error and alert.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Decrypt}`,
filter: `rule.attributes.executionStatus.status:error and rule.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Decrypt}`,
fields: ['executionStatus'],
type: 'alert',
type: 'rule',
sortField: 'executionStatus.lastExecutionDate',
sortOrder: 'desc',
page: 1,
Expand All @@ -44,9 +44,9 @@ export const getHealth = async (
}

const { saved_objects: executeErrorData } = await internalSavedObjectsRepository.find<RawAlert>({
filter: `alert.attributes.executionStatus.status:error and alert.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Execute}`,
filter: `rule.attributes.executionStatus.status:error and rule.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Execute}`,
fields: ['executionStatus'],
type: 'alert',
type: 'rule',
sortField: 'executionStatus.lastExecutionDate',
sortOrder: 'desc',
page: 1,
Expand All @@ -61,9 +61,9 @@ export const getHealth = async (
}

const { saved_objects: readErrorData } = await internalSavedObjectsRepository.find<RawAlert>({
filter: `alert.attributes.executionStatus.status:error and alert.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Read}`,
filter: `rule.attributes.executionStatus.status:error and rule.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Read}`,
fields: ['executionStatus'],
type: 'alert',
type: 'rule',
sortField: 'executionStatus.lastExecutionDate',
sortOrder: 'desc',
page: 1,
Expand All @@ -78,9 +78,9 @@ export const getHealth = async (
}

const { saved_objects: noErrorData } = await internalSavedObjectsRepository.find<RawAlert>({
filter: 'not alert.attributes.executionStatus.status:error',
filter: 'not rule.attributes.executionStatus.status:error',
fields: ['executionStatus'],
type: 'alert',
type: 'rule',
sortField: 'executionStatus.lastExecutionDate',
sortOrder: 'desc',
});
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerts/server/health/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function healthCheckTaskRunner(
async run() {
try {
const alertingHealthStatus = await getHealth(
(await coreStartServices)[0].savedObjects.createInternalRepository(['alert'])
(await coreStartServices)[0].savedObjects.createInternalRepository(['rule'])
);
return {
state: {
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/alerts/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class AlertingPlugin {
licenseState?.setNotifyUsage(plugins.licensing.featureUsage.notifyUsage);

const encryptedSavedObjectsClient = plugins.encryptedSavedObjects.getClient({
includedHiddenTypes: ['alert'],
includedHiddenTypes: ['rule'],
});

const spaceIdToNamespace = (spaceId?: string) => {
Expand Down Expand Up @@ -369,11 +369,11 @@ export class AlertingPlugin {
encryptedSavedObjectsClient,
basePathService: core.http.basePath,
eventLogger: this.eventLogger!,
internalSavedObjectsRepository: core.savedObjects.createInternalRepository(['alert']),
internalSavedObjectsRepository: core.savedObjects.createInternalRepository(['rule']),
alertTypeRegistry: this.alertTypeRegistry!,
});

this.eventLogService!.registerSavedObjectProvider('alert', (request) => {
this.eventLogService!.registerSavedObjectProvider('rule', (request) => {
const client = getAlertsClientWithRequest(request);
return (objects?: SavedObjectsBulkGetObject[]) =>
objects
Expand All @@ -390,7 +390,7 @@ export class AlertingPlugin {
listTypes: alertTypeRegistry!.list.bind(this.alertTypeRegistry!),
getAlertsClientWithRequest,
getFrameworkHealth: async () =>
await getHealth(core.savedObjects.createInternalRepository(['alert'])),
await getHealth(core.savedObjects.createInternalRepository(['rule'])),
};
}

Expand All @@ -406,7 +406,7 @@ export class AlertingPlugin {
},
listTypes: alertTypeRegistry!.list.bind(alertTypeRegistry!),
getFrameworkHealth: async () =>
await getHealth(savedObjects.createInternalRepository(['alert'])),
await getHealth(savedObjects.createInternalRepository(['rule'])),
};
};
};
Expand All @@ -429,7 +429,7 @@ export class AlertingPlugin {
savedObjects: SavedObjectsServiceStart,
request: KibanaRequest
) {
return savedObjects.getScopedClient(request, { includedHiddenTypes: ['alert', 'action'] });
return savedObjects.getScopedClient(request, { includedHiddenTypes: ['rule', 'action'] });
}

public stop() {
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/alerts/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export function setupSavedObjects(
migrations: getMigrations(encryptedSavedObjects),
mappings: mappings.alert,
});
savedObjects.registerType({
name: 'rule',
hidden: true,
namespaceType: 'single',
mappings: mappings.alert,
});

savedObjects.registerType({
name: 'api_key_pending_invalidation',
Expand Down
26 changes: 25 additions & 1 deletion x-pack/plugins/alerts/server/saved_objects/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ export function getMigrations(
RawAlert,
RawAlert
>(
// migrate all documents in 7.11 in order to add the "updatedAt" and "notifyWhen" fields
// migrate all documents in 7.12 in order to add the "updatedAt" and "notifyWhen" fields
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
pipeMigrations(setAlertUpdatedAtDate, setNotifyWhen)
);

const migrationAlertToRule = encryptedSavedObjects.createMigration<
RawAlert,
RawAlert
>(
// migrate all documents in 7.12 in order to turn them into rules
(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
convertAlertIntoRule
);

return {
'7.10.0': executeMigrationWithErrorHandling(migrationWhenRBACWasIntroduced, '7.10.0'),
'7.11.0': executeMigrationWithErrorHandling(migrationAlertUpdatedAtAndNotifyWhen, '7.11.0'),
'7.12.0': executeMigrationWithErrorHandling(migrationAlertToRule, '7.12.0'),
};
}

Expand Down Expand Up @@ -95,6 +105,20 @@ const setNotifyWhen = (
};
};

const convertAlertIntoRule = (
{ migrationVersion, ...doc}: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> => {
console.log(`=-=-=-=-=-=-=-=-=-=-=-`)
console.log(`=-=-=-=-=-=-=-=-=-=-=-`)
console.log(JSON.stringify(doc))
console.log(`=-=-=-=-=-=-=-=-=-=-=-`)
console.log(`=-=-=-=-=-=-=-=-=-=-=-`)
return {
...doc,
type: 'rule'
};
};

const consumersToChange: Map<string, string> = new Map(
Object.entries({
alerting: 'alerts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function partiallyUpdateAlert(
const updateOptions: SavedObjectsUpdateOptions = pick(options, 'namespace', 'version', 'refresh');

try {
await savedObjectsClient.update<RawAlert>('alert', id, attributeUpdates, updateOptions);
await savedObjectsClient.update<RawAlert>('rule', id, attributeUpdates, updateOptions);
} catch (err) {
if (options?.ignore404 && SavedObjectsErrorHelpers.isNotFoundError(err)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function createExecutionHandler<
apiKey: apiKey ?? null,
source: asSavedObjectExecutionSource({
id: alertId,
type: 'alert',
type: 'rule',
}),
});

Expand All @@ -176,7 +176,7 @@ export function createExecutionHandler<
action_subgroup: actionSubgroup,
},
saved_objects: [
{ rel: SAVED_OBJECT_REL_PRIMARY, type: 'alert', id: alertId, ...namespace },
{ rel: SAVED_OBJECT_REL_PRIMARY, type: 'rule', id: alertId, ...namespace },
{ type: 'action', id: action.id, ...namespace },
],
},
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/alerts/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class TaskRunner<
const {
attributes: { apiKey },
} = await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser<RawAlert>(
'alert',
'rule',
alertId,
{ namespace }
);
Expand Down Expand Up @@ -460,7 +460,7 @@ export class TaskRunner<
saved_objects: [
{
rel: SAVED_OBJECT_REL_PRIMARY,
type: 'alert',
type: 'rule',
id: alertId,
namespace,
},
Expand Down Expand Up @@ -629,7 +629,7 @@ function generateNewAndRecoveredInstanceEvents<
saved_objects: [
{
rel: SAVED_OBJECT_REL_PRIMARY,
type: 'alert',
type: 'rule',
id: alertId,
namespace,
},
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/alerts/server/usage/alerts_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export async function getTotalCountAggregations(callCluster: LegacyAPICaller, ki
body: {
query: {
bool: {
filter: [{ term: { type: 'alert' } }],
filter: [{ term: { type: 'rule' } }],
},
},
aggs: {
Expand All @@ -228,7 +228,7 @@ export async function getTotalCountAggregations(callCluster: LegacyAPICaller, ki
intervalTime: intervalTimeMetric,
connectorsAgg: {
nested: {
path: 'alert.actions',
path: 'rule.actions',
},
aggs: {
connectors: connectorsMetric,
Expand Down Expand Up @@ -292,7 +292,7 @@ export async function getTotalCountInUse(callCluster: LegacyAPICaller, kibanaIne
body: {
query: {
bool: {
filter: [{ term: { type: 'alert' } }, { term: { 'alert.enabled': true } }],
filter: [{ term: { type: 'rule' } }, { term: { 'rule.enabled': true } }],
},
},
aggs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { buildUp, tearDown } from '..';

// eslint-disable-next-line import/no-default-export
export default function alertingTests({ loadTestFile, getService }: FtrProviderContext) {
describe('Alerting', () => {
describe.only('Alerting', () => {
before(async () => buildUp(getService));
after(async () => tearDown(getService));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function createGetTests({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

describe('migrations', () => {
describe.only('migrations', () => {
before(async () => {
await esArchiver.load('alerts');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Spaces } from '../scenarios';

// eslint-disable-next-line import/no-default-export
export default function alertingApiIntegrationTests({ loadTestFile }: FtrProviderContext) {
describe('alerting api integration spaces only', function () {
describe.only('alerting api integration spaces only', function () {
this.tags('ciGroup12');

loadTestFile(require.resolve('./actions'));
Expand Down