This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Get All and Explain All #149
Merged
bowenlan-amzn
merged 13 commits into
opendistro-for-elasticsearch:main
from
bowenlan-amzn:getall
Feb 11, 2021
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
30fea71
save works
bowenlan-amzn 3a3cdb0
add explain change
bowenlan-amzn 426182b
finish work
bowenlan-amzn 27f4cdf
can use now
bowenlan-amzn 44d53e6
clean up
bowenlan-amzn e8e88a5
get all policies and explain all
bowenlan-amzn 5e256d6
Merge branch 'getall' of github.com:bowenlan-amzn/index-management-ki…
bowenlan-amzn f295a9d
bug fix and accommodate backend total_policis total_managed_indices
bowenlan-amzn dc6f16d
managed index policy not exist, wrap in a do catch
bowenlan-amzn cfbc0cf
small adds
bowenlan-amzn ac3ae25
Merge branch 'main' into getall
bowenlan-amzn 5b60230
Merge branch 'main' into getall
bowenlan-amzn 3b1a4d4
show initializing for policy
bowenlan-amzn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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 |
---|---|---|
|
@@ -15,7 +15,16 @@ | |
|
||
import { RequestParams } from "@elastic/elasticsearch"; | ||
import { INDEX, Setting } from "../utils/constants"; | ||
import { AcknowledgedResponse, ApplyPolicyResponse, AddResponse, CatIndex, GetIndicesResponse, SearchResponse } from "../models/interfaces"; | ||
import { | ||
AcknowledgedResponse, | ||
ApplyPolicyResponse, | ||
AddResponse, | ||
CatIndex, | ||
GetIndicesResponse, | ||
SearchResponse, | ||
ExplainResponse, | ||
ExplainAPIManagedIndexMetaData, | ||
} from "../models/interfaces"; | ||
import { ServerResponse } from "../models/types"; | ||
import { KibanaRequest, KibanaResponseFactory, IClusterClient, IKibanaResponse, RequestHandlerContext } from "../../../../src/core/server"; | ||
|
||
|
@@ -26,35 +35,6 @@ export default class IndexService { | |
this.esDriver = esDriver; | ||
} | ||
|
||
search = async ( | ||
context: RequestHandlerContext, | ||
request: KibanaRequest, | ||
response: KibanaResponseFactory | ||
): Promise<IKibanaResponse<ServerResponse<any>>> => { | ||
try { | ||
const { query, index, size = 0 } = request.body as { query: object; index: string; size?: number }; | ||
const params: RequestParams.Search = { index, size, body: query }; | ||
const { callAsCurrentUser: callWithRequest } = this.esDriver.asScoped(request); | ||
const results: SearchResponse<any> = await callWithRequest("search", params); | ||
return response.custom({ | ||
statusCode: 200, | ||
body: { | ||
ok: true, | ||
response: results, | ||
}, | ||
}); | ||
} catch (err) { | ||
console.error("Index Management - IndexService - search", err); | ||
return response.custom({ | ||
statusCode: 200, | ||
body: { | ||
ok: false, | ||
error: err.message, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
getIndices = async ( | ||
context: RequestHandlerContext, | ||
request: KibanaRequest, | ||
|
@@ -81,17 +61,17 @@ export default class IndexService { | |
const fromNumber = parseInt(from, 10); | ||
const sizeNumber = parseInt(size, 10); | ||
const paginatedIndices = indicesResponse.slice(fromNumber, fromNumber + sizeNumber); | ||
const indexUuids = paginatedIndices.map((value: CatIndex) => value.uuid); | ||
const indexNames = paginatedIndices.map((value: CatIndex) => value.index); | ||
|
||
const managedStatus = await this._getManagedStatus(request, indexUuids); | ||
const managedStatus = await this._getManagedStatus(request, indexNames); | ||
|
||
// NOTE: Cannot use response.ok due to typescript type checking | ||
return response.custom({ | ||
statusCode: 200, | ||
body: { | ||
ok: true, | ||
response: { | ||
indices: paginatedIndices.map((catIndex: CatIndex) => ({ ...catIndex, managed: managedStatus[catIndex.uuid] || "N/A" })), | ||
indices: paginatedIndices.map((catIndex: CatIndex) => ({ ...catIndex, managed: managedStatus[catIndex.index] || "N/A" })), | ||
totalIndices: indicesResponse.length, | ||
}, | ||
}, | ||
|
@@ -121,30 +101,25 @@ export default class IndexService { | |
} | ||
}; | ||
|
||
// given a list of indexUuids return the managed status of each (true, false, N/A) | ||
_getManagedStatus = async (request: KibanaRequest, indexUuids: string[]): Promise<{ [indexUuid: string]: string }> => { | ||
_getManagedStatus = async (request: KibanaRequest, indexNames: string[]): Promise<{ [indexName: string]: string }> => { | ||
try { | ||
const searchParams: RequestParams.Search = { | ||
index: INDEX.OPENDISTRO_ISM_CONFIG, | ||
size: indexUuids.length, | ||
body: { _source: "_id", query: { ids: { values: indexUuids } } }, | ||
}; | ||
const { callAsCurrentUser: searchCallWithRequest } = this.esDriver.asScoped(request); | ||
const results: SearchResponse<any> = await searchCallWithRequest("search", searchParams); | ||
const managed: { [indexUuid: string]: string } = results.hits.hits.reduce( | ||
(accu: object, hit: { _id: string }) => ({ ...accu, [hit._id]: "Yes" }), | ||
{} | ||
); | ||
return indexUuids.reduce((accu: object, value: string) => ({ ...accu, [value]: managed[value] || "No" }), {}); | ||
} catch (err) { | ||
// If the config index does not exist then nothing is being managed | ||
if (err.statusCode === 404 && err.body.error.type === "index_not_found_exception") { | ||
return indexUuids.reduce((accu, value) => ({ ...accu, [value]: "No" }), {}); | ||
const explainParamas = { index: indexNames.toString() }; | ||
const { callAsCurrentUser: callWithRequest } = this.esDriver.asScoped(request); | ||
const explainResponse: ExplainResponse = await callWithRequest("ism.explain", explainParamas); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here |
||
|
||
const managed: { [indexName: string]: string } = {}; | ||
for (const indexName in explainResponse) { | ||
if (indexName === "total_managed_indices") continue; | ||
const explain = explainResponse[indexName] as ExplainAPIManagedIndexMetaData; | ||
managed[indexName] = explain["index.opendistro.index_state_management.policy_id"] === null ? "No" : "Yes"; | ||
bowenlan-amzn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return managed; | ||
} catch (err) { | ||
// otherwise it could be an unauthorized access error to config index or some other error | ||
// in which case we will return managed status N/A | ||
console.error("Index Management - IndexService - _getManagedStatus:", err); | ||
return indexUuids.reduce((accu, value) => ({ ...accu, [value]: "N/A" }), {}); | ||
return indexNames.reduce((accu, value) => ({ ...accu, [value]: "N/A" }), {}); | ||
} | ||
}; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a typo? I am assuming that this should be
explainParams
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, will fix it in today's bump version small PR