|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from spark_history_mcp.api.emr_persistent_ui_client import EMRPersistentUIClient |
| 4 | +from spark_history_mcp.api.spark_client import SparkRestClient |
| 5 | +from spark_history_mcp.config.config import ServerConfig |
| 6 | + |
| 7 | + |
| 8 | +def create_spark_client_from_config(server_config: ServerConfig) -> SparkRestClient: |
| 9 | + """ |
| 10 | + Create a SparkRestClient from a ServerConfig. |
| 11 | +
|
| 12 | + This function handles both regular Spark History Servers and EMR Persistent UI configurations. |
| 13 | +
|
| 14 | + Args: |
| 15 | + server_config: The server configuration |
| 16 | +
|
| 17 | + Returns: |
| 18 | + SparkRestClient instance properly configured |
| 19 | + """ |
| 20 | + # Check if this is an EMR server configuration |
| 21 | + if server_config.emr_cluster_arn: |
| 22 | + return create_spark_emr_client(server_config.emr_cluster_arn, server_config) |
| 23 | + else: |
| 24 | + # Regular Spark REST client |
| 25 | + return SparkRestClient(server_config) |
| 26 | + |
| 27 | + |
| 28 | +def create_spark_emr_client( |
| 29 | + emr_cluster_arn: str, server_config: Optional[ServerConfig] = None |
| 30 | +) -> SparkRestClient: |
| 31 | + """ |
| 32 | + Create a SparkRestClient from EMR cluster arn and optional ServerConfig. |
| 33 | +
|
| 34 | + This function handles EMR Persistent UI applications. |
| 35 | +
|
| 36 | + Args: |
| 37 | + emr_cluster_arn: The EMR cluster ARN |
| 38 | + server_config: The server configuration |
| 39 | +
|
| 40 | + Returns: |
| 41 | + SparkRestClient instance properly configured |
| 42 | + """ |
| 43 | + if server_config is None: |
| 44 | + server_config = ServerConfig() |
| 45 | + server_config.emr_cluster_arn = emr_cluster_arn |
| 46 | + emr_client = EMRPersistentUIClient(server_config) |
| 47 | + |
| 48 | + # Initialize EMR client (create persistent UI, get presigned URL, setup session) |
| 49 | + base_url, session = emr_client.initialize() |
| 50 | + |
| 51 | + # Create a modified server config with the base URL |
| 52 | + if server_config is None: |
| 53 | + server_config = ServerConfig() |
| 54 | + else: |
| 55 | + server_config = server_config.model_copy() |
| 56 | + server_config.url = base_url |
| 57 | + |
| 58 | + # Create SparkRestClient with the session |
| 59 | + spark_client = SparkRestClient(server_config) |
| 60 | + spark_client.session = session # Use the authenticated session |
| 61 | + |
| 62 | + return spark_client |
0 commit comments