-
Notifications
You must be signed in to change notification settings - Fork 132
Add new functions and template example for ILM and tranforms #3221
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
Open
jrmolin
wants to merge
16
commits into
elastic:main
Choose a base branch
from
jrmolin:add_new_readme_functions-ilm_and_transform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+579
−1
Open
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e473435
Add the functions and template
jrmolin 497bc5d
fix the case where the transform directory doesn't exist
jrmolin 6c578d1
run the formatter
jrmolin 5336d0e
add code comments to explain what this does
jrmolin 8c3efae
break down the ILM policies into a markdown table
jrmolin f829cb2
need to sort the ILM keys, so tests pass
jrmolin 57895e2
added a transform function that reads through the transform information
jrmolin 49fd377
update the tests so the expected is in line with actual expectations
jrmolin 405be83
code review updates
jrmolin 25d5e4d
code review updates
jrmolin 0171df6
more code review updates
jrmolin 4d99c36
move the Input Docs text into the rendering function
jrmolin 3c2ad84
Merge branch 'main' of github.com:jrmolin/elastic-package into add_ne…
jrmolin 528d9ee
also render fields associated with transforms
jrmolin 144d1b7
consolidate an unnecessary variable
jrmolin 9986a78
make the check-static tests pass
jrmolin 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,122 @@ | ||
| // 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. | ||
|
|
||
| package docs | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| // flattenNestedMap flattens a nested JSON-like structure (maps and slices) into | ||
| // a flat map with dot-separated keys. | ||
| func flattenNestedMap(prefix string, nested map[string]interface{}, flatMap map[string]string) { | ||
| for k, v := range nested { | ||
| key := k | ||
| if prefix != "" { | ||
| key = fmt.Sprintf("%s.%s", prefix, k) | ||
| } | ||
|
|
||
| switch child := v.(type) { | ||
| case map[string]interface{}: | ||
| flattenNestedMap(key, child, flatMap) | ||
| case []interface{}: | ||
| for i, val := range child { | ||
| // handle slices with index | ||
| newKey := fmt.Sprintf("%s.%d", key, i) | ||
| if nextMap, ok := val.(map[string]interface{}); ok { | ||
| flattenNestedMap(newKey, nextMap, flatMap) | ||
| } else { | ||
| flatMap[newKey] = fmt.Sprintf("%v", val) | ||
| } | ||
| } | ||
| default: | ||
| flatMap[key] = fmt.Sprintf("%v", v) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func getILMPolicyMap(path string) (map[string]string, error) { | ||
| content, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading ILM policy file failed: %w", err) | ||
| } | ||
| var policy map[string]interface{} | ||
| err = json.Unmarshal(content, &policy) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unmarshalling ILM policy failed: %w", err) | ||
| } | ||
|
|
||
| flatMap := make(map[string]string) | ||
| flattenNestedMap("", policy, flatMap) | ||
| return flatMap, nil | ||
| } | ||
|
|
||
| func renderILMPolicyMap(output *strings.Builder, policyMap map[string]string) { | ||
| keys := make([]string, 0, len(policyMap)) | ||
| for key := range policyMap { | ||
| keys = append(keys, key) | ||
| } | ||
| sort.Strings(keys) | ||
| for _, key := range keys { | ||
| output.WriteString(fmt.Sprintf("| %s | %s |\n", key, policyMap[key])) | ||
| } | ||
| } | ||
|
|
||
| func renderILMPaths(packageRoot string) (string, error) { | ||
| // gather the list of data streams that have ILM policies defined | ||
| // if the list is empty, return "" | ||
| // if the list is not empty, format the list as a markdown list | ||
| ilmPaths, err := findILMPaths(packageRoot) | ||
| if err != nil { | ||
| return "", fmt.Errorf("finding ILM paths failed: %w", err) | ||
| } | ||
| if len(ilmPaths) == 0 { | ||
| return "", nil | ||
| } | ||
|
|
||
| sort.Strings(ilmPaths) | ||
| var renderedDocs strings.Builder | ||
| renderedDocs.WriteString("\n### Data streams using ILM policies:\n") | ||
jrmolin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for _, ilmPath := range ilmPaths { | ||
| ilmPolicyPath := filepath.Join(packageRoot, "data_stream", ilmPath, "elasticsearch", "ilm", "default_policy.json") | ||
| // get the policy map | ||
| policyMap, err := getILMPolicyMap(ilmPolicyPath) | ||
jrmolin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return "", fmt.Errorf("getting ILM policy map for path %s failed: %w", ilmPolicyPath, err) | ||
| } | ||
| renderedDocs.WriteString(fmt.Sprintf("#### %s\n", ilmPath)) | ||
|
|
||
| // render the policy map as a markdown table | ||
| renderedDocs.WriteString("| Key | Value |\n") | ||
| renderedDocs.WriteString("|---|---|\n") | ||
| renderILMPolicyMap(&renderedDocs, policyMap) | ||
| } | ||
| return renderedDocs.String(), nil | ||
| } | ||
|
|
||
| // findILMPaths scans a given package path for data streams that have ILM policies | ||
| // and returns a list of all data stream names that have ILM policies defined. | ||
| func findILMPaths(packageRoot string) ([]string, error) { | ||
| // look for ilm/ from the packageRoot/data_stream/<data_stream_name>/elasticsearch/ilm/ | ||
| // add the data_stream_name to the list | ||
| ilmPaths, err := filepath.Glob(filepath.Join(packageRoot, "data_stream", "*", "elasticsearch", "ilm")) | ||
kcreddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("finding ILM paths failed: %w", err) | ||
| } | ||
|
|
||
| result := make([]string, 0, len(ilmPaths)) | ||
|
|
||
| // return the list of globbed paths | ||
| for _, ilmPath := range ilmPaths { | ||
| // get the data_stream_name from the ilmPath | ||
| dataStreamName := filepath.Base(filepath.Dir(filepath.Dir(ilmPath))) | ||
| result = append(result, dataStreamName) | ||
| } | ||
| return result, nil | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.