Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ data class ClusterMetricsInput(
var path = ""
var pathParams = ""
var url = ""
var apiType = ""
val clusters = mutableListOf<String>()

XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
Expand All @@ -131,6 +132,7 @@ data class ClusterMetricsInput(
val fieldName = xcp.currentName()
xcp.nextToken()
when (fieldName) {
API_TYPE_FIELD -> apiType = xcp.text()
PATH_FIELD -> path = xcp.text()
PATH_PARAMS_FIELD -> pathParams = xcp.text()
URL_FIELD -> url = xcp.text()
Expand All @@ -144,6 +146,17 @@ data class ClusterMetricsInput(
}
}
}

if (apiType.isNotEmpty() && path.isNotEmpty()) {
val derivedType = ClusterMetricType.values()
.filter { it != ClusterMetricType.BLANK }
.find { path.startsWith(it.prependPath) || path.startsWith(it.defaultPath) }

require(derivedType != null && derivedType.name == apiType) {
"The provided api_type [$apiType] does not match the path [$path]."
}
}

return ClusterMetricsInput(path, pathParams, url, clusters)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,43 @@ class ClusterMetricsInputTests {
)
}
}

@Test
fun `test parseInner rejects mismatched api_type and path`() {
val inputJson = """
{"uri":{"api_type":"CLUSTER_STATS","path":"/_cat/indices","path_params":"","url":"","clusters":[]}}
""".trimIndent()
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
.createParser(
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
inputJson
)
xcp.nextToken() // START_OBJECT (root)
xcp.nextToken() // FIELD_NAME "uri"
xcp.nextToken() // START_OBJECT (uri)

assertFailsWith<IllegalArgumentException>("The provided api_type") {
ClusterMetricsInput.parseInner(xcp)
}
}

@Test
fun `test parseInner accepts matching api_type and path`() {
val inputJson = """
{"uri":{"api_type":"CAT_INDICES","path":"/_cat/indices","path_params":"","url":"","clusters":[]}}
""".trimIndent()
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
.createParser(
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
inputJson
)
xcp.nextToken()
xcp.nextToken()
xcp.nextToken()

val input = ClusterMetricsInput.parseInner(xcp)
assertEquals(ClusterMetricsInput.ClusterMetricType.CAT_INDICES, input.clusterMetricType)
}
}
Loading