Skip to content

Commit c182f19

Browse files
committed
Fix detekt
Signed-off-by: Craig Perkins <[email protected]>
1 parent 5a1a284 commit c182f19

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/IndexStateManagementRestTestCase.kt

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ abstract class IndexStateManagementRestTestCase : IndexManagementRestTestCase()
9090

9191
@Before
9292
protected fun attemptAddAttributesToAdmin() {
93-
// Attributes your tests may read
9493
val attrs = mapOf(
9594
"department" to "engineering",
9695
"env" to "itest",
@@ -102,8 +101,8 @@ abstract class IndexStateManagementRestTestCase : IndexManagementRestTestCase()
102101
}
103102
}
104103

105-
protected fun addAttributesToAdmin(attributes: Map<String, String>) {
106-
fun jsonPatch(op: String) = buildString {
104+
private fun buildAttributesPatch(op: String, attributes: Map<String, String>): StringEntity {
105+
val payload = buildString {
107106
append("[{\"op\":\"").append(op).append("\",\"path\":\"/attributes\",\"value\":{")
108107
append(
109108
attributes.entries.joinToString(",") { (k, v) ->
@@ -112,49 +111,38 @@ abstract class IndexStateManagementRestTestCase : IndexManagementRestTestCase()
112111
)
113112
append("}}]")
114113
}
114+
return StringEntity(payload, ContentType.APPLICATION_JSON)
115+
}
115116

116-
val endpoints = listOf("/_plugins/_security/api/internalusers/admin", "/_opendistro/_security/api/internalusers/admin")
117+
/** Returns true if the PATCH succeeded (2xx), false if the route isn’t usable, and throws on fatal errors. */
118+
private fun tryPatchAttributes(endpoint: String, entity: StringEntity): Boolean = try {
119+
val resp = adminClient().makeRequest("PATCH", endpoint, emptyMap(), entity)
120+
resp.restStatus().status in 200..299
121+
} catch (e: ResponseException) {
122+
// Treat common “route not usable / blocked” statuses as a soft failure (false), rethrow other errors.
123+
val softStatuses = setOf(
124+
RestStatus.NOT_FOUND, RestStatus.METHOD_NOT_ALLOWED, RestStatus.FORBIDDEN, RestStatus.UNAUTHORIZED, RestStatus.BAD_REQUEST,
125+
)
126+
if (e.response.restStatus() in softStatuses) false else throw e
127+
} catch (_: Exception) {
128+
// admin client couldn't reach security, treat as soft failure to try the next route
129+
false
130+
}
117131

118-
// Try add first (creates /attributes if missing), then replace (updates when it exists)
119-
for (endpoint in endpoints) {
120-
// 1) Try ADD
121-
try {
122-
val resp = adminClient().makeRequest(
123-
"PATCH",
124-
endpoint,
125-
emptyMap(),
126-
StringEntity(jsonPatch("add"), ContentType.APPLICATION_JSON),
127-
)
128-
if (resp.restStatus().status in 200..299) return
129-
} catch (e: ResponseException) {
130-
// If add fails because it already exists, we’ll try replace below.
131-
when (e.response.restStatus()) {
132-
RestStatus.NOT_FOUND, RestStatus.METHOD_NOT_ALLOWED, RestStatus.FORBIDDEN, RestStatus.UNAUTHORIZED -> continue
133-
else -> {
134-
// fall through to try replace if it’s a 400 due to existing field or similar
135-
}
136-
}
137-
} catch (_: Exception) {
138-
// If admin client can’t reach security, try the next route
139-
continue
140-
}
132+
protected fun addAttributesToAdmin(attributes: Map<String, String>) {
133+
val endpoints = listOf(
134+
"/_plugins/_security/api/internalusers/admin",
135+
"/_opendistro/_security/api/internalusers/admin",
136+
)
141137

142-
// 2) Try REPLACE (for clusters where /attributes already exists)
143-
try {
144-
val resp = adminClient().makeRequest(
145-
"PATCH",
146-
endpoint,
147-
emptyMap(),
148-
StringEntity(jsonPatch("replace"), ContentType.APPLICATION_JSON),
149-
)
150-
if (resp.restStatus().status in 200..299) return
151-
} catch (e: ResponseException) {
152-
// If this route doesn't exist or we’re blocked, keep trying the next route.
153-
when (e.response.restStatus()) {
154-
RestStatus.NOT_FOUND, RestStatus.METHOD_NOT_ALLOWED, RestStatus.FORBIDDEN, RestStatus.UNAUTHORIZED -> continue
155-
else -> throw e
156-
}
157-
}
138+
val addBody = buildAttributesPatch("add", attributes)
139+
val replaceBody = buildAttributesPatch("replace", attributes)
140+
141+
for (endpoint in endpoints) {
142+
// First try ADD (creates /attributes if missing)
143+
if (tryPatchAttributes(endpoint, addBody)) return
144+
// Then try REPLACE (updates if it already exists)
145+
if (tryPatchAttributes(endpoint, replaceBody)) return
158146
}
159147

160148
logger.info("Could not set admin attributes via any Security route; continuing without attributes.")

0 commit comments

Comments
 (0)