Skip to content
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

MCOL-5133: Stage1 to stand alone cli tool. #3378

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
110 changes: 110 additions & 0 deletions cmapi/cmapi_server/controllers/api_clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import requests
from typing import Any, Dict, Optional, Union
from cmapi_server.controllers.dispatcher import _version


class ClusterControllerClient:
def __init__(self, base_url: str):
"""Initialize the ClusterControllerClient with the base URL.

:param base_url: The base URL for the API endpoints.
"""
self.base_url = base_url

def start_cluster(self) -> Union[Dict[str, Any], Dict[str, str]]:
"""Start the cluster.

:return: The response from the API.
"""
return self._request('put', 'start')

def shutdown_cluster(self) -> Union[Dict[str, Any], Dict[str, str]]:
"""Shutdown the cluster.

:return: The response from the API.
"""
return self._request('put', 'shutdown')

def set_mode(self, mode: str) -> Union[Dict[str, Any], Dict[str, str]]:
"""Set the cluster mode.

:param mode: The mode to set.
:return: The response from the API.
"""
return self._request('put', 'mode-set', {'mode': mode})

def add_node(
self, node_info: Dict[str, Any]
) -> Union[Dict[str, Any], Dict[str, str]]:
"""Add a node to the cluster.

:param node_info: Information about the node to add.
:return: The response from the API.
"""
return self._request('put', 'node', node_info)

def remove_node(
self, node_id: str
) -> Union[Dict[str, Any], Dict[str, str]]:
"""Remove a node from the cluster.

:param node_id: The ID of the node to remove.
:return: The response from the API.
"""
return self._request('delete', 'node', {'node_id': node_id})

def get_status(self) -> Union[Dict[str, Any], Dict[str, str]]:
"""Get the status of the cluster.

:return: The response from the API.
"""
return self._request('get', 'status')

def set_api_key(
self, api_key: str
) -> Union[Dict[str, Any], Dict[str, str]]:
"""Set the API key for the cluster.

:param api_key: The API key to set.
:return: The response from the API.
"""
return self._request('put', 'apikey-set', {'api_key': api_key})

def set_log_level(
self, log_level: str
) -> Union[Dict[str, Any], Dict[str, str]]:
"""Set the log level for the cluster.

:param log_level: The log level to set.
:return: The response from the API.
"""
return self._request('put', 'log-level', {'log_level': log_level})

def load_s3data(
self, s3data_info: Dict[str, Any]
) -> Union[Dict[str, Any], Dict[str, str]]:
"""Load S3 data into the cluster.

:param s3data_info: Information about the S3 data to load.
:return: The response from the API.
"""
return self._request('put', 'load_s3data', s3data_info)

def _request(
self, method: str, endpoint: str,
data: Optional[Dict[str, Any]] = None
) -> Union[Dict[str, Any], Dict[str, str]]:
"""Make a request to the API.

:param method: The HTTP method to use.
:param endpoint: The API endpoint to call.
:param data: The data to send with the request.
:return: The response from the API.
"""
url = f'{self.base_url}/cmapi/{_version}/cluster/{endpoint}'
try:
response = requests.request(method, url, json=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
return {'error': str(e)}
29 changes: 11 additions & 18 deletions cmapi/mcs_cluster_tool/cluster_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from cmapi_server.managers.transaction import TransactionManager
from mcs_cluster_tool.decorators import handle_output
from mcs_node_control.models.node_config import NodeConfig
from cmapi.cmapi_server.controllers.api_clients import ClusterControllerClient


logger = logging.getLogger('mcs_cli')
Expand All @@ -34,13 +35,13 @@
set_app = typer.Typer(help='Set cluster parameters.')
app.add_typer(set_app, name='set')

client = ClusterControllerClient(base_url="http://localhost:8640")

@app.command()
@handle_output
def status():
"""Get status information."""
return ClusterHandler.status(logger=logger)

return client.get_status()

@app.command()
@handle_output
Expand Down Expand Up @@ -165,21 +166,19 @@ def stop(
@handle_output
def start():
"""Start the Columnstore cluster."""
return ClusterHandler.start(logger=logger)

return client.start_cluster()

@app.command()
@handle_output
def restart():
"""Restart the Columnstore cluster."""
stop_result = ClusterHandler.shutdown(logger=logger)
stop_result = client.shutdown_cluster()
if 'error' in stop_result:
return stop_result
result = ClusterHandler.start(logger=logger)
result = client.start_cluster()
result['stop_timestamp'] = stop_result['timestamp']
return result


@node_app.command()
@handle_output
def add(
Expand All @@ -195,10 +194,9 @@ def add(
"""Add nodes to the Columnstore cluster."""
result = []
for node in nodes:
result.append(ClusterHandler.add_node(node, logger=logger))
result.append(client.add_node({'node': node}))
return result


@node_app.command()
@handle_output
def remove(nodes: Optional[List[str]] = typer.Option(
Expand All @@ -213,10 +211,9 @@ def remove(nodes: Optional[List[str]] = typer.Option(
"""Remove nodes from the Columnstore cluster."""
result = []
for node in nodes:
result.append(ClusterHandler.remove_node(node, logger=logger))
result.append(client.remove_node(node))
return result


@set_app.command()
@handle_output
def mode(cluster_mode: str = typer.Option(
Expand All @@ -233,8 +230,7 @@ def mode(cluster_mode: str = typer.Option(
raise typer.BadParameter(
'"readonly" or "readwrite" are the only acceptable modes now.'
)
return ClusterHandler.set_mode(cluster_mode, logger=logger)

return client.set_mode(cluster_mode)

@set_app.command()
@handle_output
Expand All @@ -246,10 +242,7 @@ def api_key(key: str = typer.Option(..., help='API key to set.')):
if not key:
raise typer.BadParameter('Empty API key not allowed.')

totp = pyotp.TOTP(SECRET_KEY)

return ClusterHandler.set_api_key(key, totp.now(), logger=logger)

return client.set_api_key(key)

@set_app.command()
@handle_output
Expand All @@ -261,4 +254,4 @@ def log_level(level: str = typer.Option(..., help='Logging level to set.')):
if not level:
raise typer.BadParameter('Empty log level not allowed.')

return ClusterHandler.set_log_level(level, logger=logger)
return client.set_log_level(level)