-
Notifications
You must be signed in to change notification settings - Fork 113
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
Making snapshot name to scripted input in template #77
Changes from all commits
87df7ea
6636c68
c7af351
5b7b8c3
096aff4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,8 +39,13 @@ import org.opensearch.indexmanagement.indexstatemanagement.model.managedindexmet | |
import org.opensearch.indexmanagement.indexstatemanagement.model.managedindexmetadata.StepMetaData | ||
import org.opensearch.indexmanagement.indexstatemanagement.settings.ManagedIndexSettings.Companion.SNAPSHOT_DENY_LIST | ||
import org.opensearch.indexmanagement.indexstatemanagement.step.Step | ||
import org.opensearch.indexmanagement.opensearchapi.convertToMap | ||
import org.opensearch.indexmanagement.opensearchapi.suspendUntil | ||
import org.opensearch.rest.RestStatus | ||
import org.opensearch.script.Script | ||
import org.opensearch.script.ScriptService | ||
import org.opensearch.script.ScriptType | ||
import org.opensearch.script.TemplateScript | ||
import org.opensearch.snapshots.ConcurrentSnapshotExecutionException | ||
import org.opensearch.transport.RemoteTransportException | ||
import java.time.LocalDateTime | ||
|
@@ -50,6 +55,7 @@ import java.util.Locale | |
|
||
class AttemptSnapshotStep( | ||
val clusterService: ClusterService, | ||
val scriptService: ScriptService, | ||
val client: Client, | ||
val config: SnapshotActionConfig, | ||
managedIndexMetaData: ManagedIndexMetaData | ||
|
@@ -74,15 +80,15 @@ class AttemptSnapshotStep( | |
info = mutableInfo.toMap() | ||
return this | ||
} | ||
val snapshotNameSuffix = "-".plus( | ||
LocalDateTime.now(ZoneId.of("UTC")) | ||
.format(DateTimeFormatter.ofPattern("uuuu.MM.dd-HH:mm:ss.SSS", Locale.ROOT)) | ||
) | ||
|
||
snapshotName = config | ||
.snapshot | ||
.plus("-") | ||
.plus( | ||
LocalDateTime | ||
.now(ZoneId.of("UTC")) | ||
.format(DateTimeFormatter.ofPattern("uuuu.MM.dd-HH:mm:ss.SSS", Locale.ROOT)) | ||
) | ||
val snapshotScript = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, config.snapshot, mapOf()) | ||
// If user intentionally set the snapshot name empty then we are going to honor it | ||
val defaultSnapshotName = if (config.snapshot.isBlank()) config.snapshot else indexName | ||
snapshotName = compileTemplate(snapshotScript, managedIndexMetaData, defaultSnapshotName).plus(snapshotNameSuffix) | ||
|
||
val createSnapshotRequest = CreateSnapshotRequest() | ||
.userMetadata(mapOf("snapshot_created" to "Open Distro for Elasticsearch Index Management")) | ||
|
@@ -148,6 +154,16 @@ class AttemptSnapshotStep( | |
info = mutableInfo.toMap() | ||
} | ||
|
||
private fun compileTemplate(template: Script, managedIndexMetaData: ManagedIndexMetaData, defaultValue: String): String { | ||
val contextMap = managedIndexMetaData.convertToMap().filterKeys { key -> | ||
key in validTopContextFields | ||
} | ||
val compiledValue = scriptService.compile(template, TemplateScript.CONTEXT) | ||
.newInstance(template.params + mapOf("ctx" to contextMap)) | ||
.execute() | ||
return if (compiledValue.isBlank()) defaultValue else compiledValue | ||
} | ||
|
||
override fun getUpdatedManagedIndexMetaData(currentMetaData: ManagedIndexMetaData): ManagedIndexMetaData { | ||
val currentActionMetaData = currentMetaData.actionMetaData | ||
return currentMetaData.copy( | ||
|
@@ -159,6 +175,7 @@ class AttemptSnapshotStep( | |
} | ||
|
||
companion object { | ||
val validTopContextFields = setOf("index", "indexUuid") | ||
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. Just to confirm is this all users have asked for? (Access to index/indexUuid in the snapshot name) 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. the users only asked for indexName in the issue, I have added indexUuid too since its readily available |
||
const val name = "attempt_snapshot" | ||
fun getBlockedMessage(denyList: List<String>, repoName: String, index: String) = | ||
"Snapshot repository [$repoName] is blocked in $denyList [index=$index]" | ||
|
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.
When does compiledValue.isBlank() occur?
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.
if someone puts in a template with just a replaceable field and that is not valid e.g.
{{ctx.name}}
or{{ctx.anything_not_allowed}}
this will be empty stringIf someone specifies the template as
{{ctx.anything_not_allowed}}_snapshot
then the compiled value is just_snapshot
If someone specifies the template as
{ctx.anything_not_allowed}
since this is not a valid template placeholder it will be compiled as{ctx.anything_not_allowed}
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.
Hmm can someone specify an empty string as the snapshot name currently or do we block that?
i.e. if currently sameone has "" and it's allowed then instead of "" + the postfix stuff it'll change to $indexName + postfix stuff (since it looks like we add the index name as the default to use if this returns blank)
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.
good point, there is no check for it being non empty at the moment the field needs to be present in the policy but it can be empty string.
I can update the default value to empty string in that case - i.e if the original name is empty string