forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split alerts file into two - for actions and alerts
- Loading branch information
Showing
3 changed files
with
204 additions
and
187 deletions.
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/action_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { CoreSetup } from 'kibana/server'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { FixtureStartDeps, FixtureSetupDeps } from './plugin'; | ||
import { ActionType, ActionTypeExecutorOptions } from '../../../../../../../plugins/actions/server'; | ||
|
||
export function defineActionTypes( | ||
core: CoreSetup<FixtureStartDeps>, | ||
{ actions }: Pick<FixtureSetupDeps, 'actions'> | ||
) { | ||
const clusterClient = core.elasticsearch.adminClient; | ||
|
||
// Action types | ||
const noopActionType: ActionType = { | ||
id: 'test.noop', | ||
name: 'Test: Noop', | ||
minimumLicenseRequired: 'gold', | ||
async executor() { | ||
return { status: 'ok', actionId: '' }; | ||
}, | ||
}; | ||
const indexRecordActionType: ActionType = { | ||
id: 'test.index-record', | ||
name: 'Test: Index Record', | ||
minimumLicenseRequired: 'gold', | ||
validate: { | ||
params: schema.object({ | ||
index: schema.string(), | ||
reference: schema.string(), | ||
message: schema.string(), | ||
}), | ||
config: schema.object({ | ||
unencrypted: schema.string(), | ||
}), | ||
secrets: schema.object({ | ||
encrypted: schema.string(), | ||
}), | ||
}, | ||
async executor({ config, secrets, params, services, actionId }: ActionTypeExecutorOptions) { | ||
await services.callCluster('index', { | ||
index: params.index, | ||
refresh: 'wait_for', | ||
body: { | ||
params, | ||
config, | ||
secrets, | ||
reference: params.reference, | ||
source: 'action:test.index-record', | ||
}, | ||
}); | ||
return { status: 'ok', actionId }; | ||
}, | ||
}; | ||
const failingActionType: ActionType = { | ||
id: 'test.failing', | ||
name: 'Test: Failing', | ||
minimumLicenseRequired: 'gold', | ||
validate: { | ||
params: schema.object({ | ||
index: schema.string(), | ||
reference: schema.string(), | ||
}), | ||
}, | ||
async executor({ config, secrets, params, services }: ActionTypeExecutorOptions) { | ||
await services.callCluster('index', { | ||
index: params.index, | ||
refresh: 'wait_for', | ||
body: { | ||
params, | ||
config, | ||
secrets, | ||
reference: params.reference, | ||
source: 'action:test.failing', | ||
}, | ||
}); | ||
throw new Error(`expected failure for ${params.index} ${params.reference}`); | ||
}, | ||
}; | ||
const rateLimitedActionType: ActionType = { | ||
id: 'test.rate-limit', | ||
name: 'Test: Rate Limit', | ||
minimumLicenseRequired: 'gold', | ||
maxAttempts: 2, | ||
validate: { | ||
params: schema.object({ | ||
index: schema.string(), | ||
reference: schema.string(), | ||
retryAt: schema.number(), | ||
}), | ||
}, | ||
async executor({ config, params, services }: ActionTypeExecutorOptions) { | ||
await services.callCluster('index', { | ||
index: params.index, | ||
refresh: 'wait_for', | ||
body: { | ||
params, | ||
config, | ||
reference: params.reference, | ||
source: 'action:test.rate-limit', | ||
}, | ||
}); | ||
return { | ||
status: 'error', | ||
retry: new Date(params.retryAt), | ||
actionId: '', | ||
}; | ||
}, | ||
}; | ||
const authorizationActionType: ActionType = { | ||
id: 'test.authorization', | ||
name: 'Test: Authorization', | ||
minimumLicenseRequired: 'gold', | ||
validate: { | ||
params: schema.object({ | ||
callClusterAuthorizationIndex: schema.string(), | ||
savedObjectsClientType: schema.string(), | ||
savedObjectsClientId: schema.string(), | ||
index: schema.string(), | ||
reference: schema.string(), | ||
}), | ||
}, | ||
async executor({ params, services, actionId }: ActionTypeExecutorOptions) { | ||
// Call cluster | ||
let callClusterSuccess = false; | ||
let callClusterError; | ||
try { | ||
await services.callCluster('index', { | ||
index: params.callClusterAuthorizationIndex, | ||
refresh: 'wait_for', | ||
body: { | ||
param1: 'test', | ||
}, | ||
}); | ||
callClusterSuccess = true; | ||
} catch (e) { | ||
callClusterError = e; | ||
} | ||
// Call scoped cluster | ||
const callScopedCluster = services.getScopedCallCluster(clusterClient); | ||
let callScopedClusterSuccess = false; | ||
let callScopedClusterError; | ||
try { | ||
await callScopedCluster('index', { | ||
index: params.callClusterAuthorizationIndex, | ||
refresh: 'wait_for', | ||
body: { | ||
param1: 'test', | ||
}, | ||
}); | ||
callScopedClusterSuccess = true; | ||
} catch (e) { | ||
callScopedClusterError = e; | ||
} | ||
// Saved objects client | ||
let savedObjectsClientSuccess = false; | ||
let savedObjectsClientError; | ||
try { | ||
await services.savedObjectsClient.get( | ||
params.savedObjectsClientType, | ||
params.savedObjectsClientId | ||
); | ||
savedObjectsClientSuccess = true; | ||
} catch (e) { | ||
savedObjectsClientError = e; | ||
} | ||
// Save the result | ||
await services.callCluster('index', { | ||
index: params.index, | ||
refresh: 'wait_for', | ||
body: { | ||
state: { | ||
callClusterSuccess, | ||
callClusterError, | ||
callScopedClusterSuccess, | ||
callScopedClusterError, | ||
savedObjectsClientSuccess, | ||
savedObjectsClientError, | ||
}, | ||
params, | ||
reference: params.reference, | ||
source: 'action:test.authorization', | ||
}, | ||
}); | ||
return { | ||
actionId, | ||
status: 'ok', | ||
}; | ||
}, | ||
}; | ||
actions.registerType(noopActionType); | ||
actions.registerType(indexRecordActionType); | ||
actions.registerType(failingActionType); | ||
actions.registerType(rateLimitedActionType); | ||
actions.registerType(authorizationActionType); | ||
} |
Oops, something went wrong.