This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit 14830a71a41f19056b9118dfdb81067cea0f4b34
Author: Jan Høydahl <[email protected]>
AuthorDate: Thu Jul 30 17:02:40 2026 +0200

    Extract CLIUtils.solrUrlFromConnection() from normalizeSolrUrl(CommandLine) 
(#4683)
    
    (cherry picked from commit 9f5fac2a4e98f56fe07a9da1d7eab6f3c2a6a9eb)
---
 .../src/java/org/apache/solr/cli/CLIUtils.java     | 50 +++++++++++++---------
 1 file changed, 29 insertions(+), 21 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/cli/CLIUtils.java 
b/solr/core/src/java/org/apache/solr/cli/CLIUtils.java
index b6eda1f40d8..e0b3db25d29 100644
--- a/solr/core/src/java/org/apache/solr/cli/CLIUtils.java
+++ b/solr/core/src/java/org/apache/solr/cli/CLIUtils.java
@@ -235,34 +235,42 @@ public final class CLIUtils {
             "Neither --solr-connection, --zk-host or --solr-url parameters, 
nor SOLR_CONNECTION, ZK_HOST env var provided, so assuming solr url is "
                 + solrUrl
                 + ".");
-      } else if (!solrConnection.isZookeeper()) {
-        // HTTP form (e.g. `-s http://host:port`): the connection string 
already names a Solr URL,
-        // so use it directly without spinning up a CloudSolrClient.
-        solrUrl = normalizeSolrUrl(solrConnection.quorumItems().get(0), false);
       } else {
-        var builder =
-            new HttpJettySolrClient.Builder()
-                .withOptionalBasicAuthCredentials(
-                    cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
-        try (CloudSolrClient cloudSolrClient = 
getCloudSolrClient(solrConnection, builder)) {
-          Set<String> liveNodes = 
cloudSolrClient.getClusterState().getLiveNodes();
-          if (liveNodes.isEmpty())
-            throw new IllegalStateException(
-                "No live nodes found! Cannot determine 'solrUrl' from 
SolrCloud: "
-                    + solrConnection);
-
-          String firstLiveNode = liveNodes.iterator().next();
-          String urlScheme =
-              
cloudSolrClient.getClusterStateProvider().getClusterProperty("urlScheme", 
"http");
-          solrUrl = URLUtil.getBaseUrlForNodeName(firstLiveNode, urlScheme, 
false);
-          solrUrl = normalizeSolrUrl(solrUrl, false);
-        }
+        solrUrl =
+            solrUrlFromConnection(
+                solrConnection, 
cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
       }
     }
     solrUrl = normalizeSolrUrl(solrUrl);
     return solrUrl;
   }
 
+  /**
+   * Resolves a base Solr URL from a parsed connection. The HTTP form (e.g. 
{@code -s
+   * http://host:port}) already names a Solr URL, so it is used directly 
without spinning up a
+   * CloudSolrClient; the ZooKeeper form queries the cluster for a live node's 
base URL.
+   */
+  public static String solrUrlFromConnection(
+      CloudSolrClient.CloudSolrClientConnection solrConnection, String 
credentials)
+      throws Exception {
+    if (!solrConnection.isZookeeper()) {
+      return normalizeSolrUrl(solrConnection.quorumItems().get(0), false);
+    }
+    var builder = new 
HttpJettySolrClient.Builder().withOptionalBasicAuthCredentials(credentials);
+    try (CloudSolrClient cloudSolrClient = getCloudSolrClient(solrConnection, 
builder)) {
+      Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();
+      if (liveNodes.isEmpty())
+        throw new IllegalStateException(
+            "No live nodes found! Cannot determine 'solrUrl' from SolrCloud: " 
+ solrConnection);
+
+      String firstLiveNode = liveNodes.iterator().next();
+      String urlScheme =
+          
cloudSolrClient.getClusterStateProvider().getClusterProperty("urlScheme", 
"http");
+      return normalizeSolrUrl(
+          URLUtil.getBaseUrlForNodeName(firstLiveNode, urlScheme, false), 
false);
+    }
+  }
+
   /**
    * Returns true if the user supplied any of the connection-related CLI 
options ({@code
    * --solr-url}, {@code --solr-connection}, or {@code --zk-host}). Use this 
to gate logic that

Reply via email to