Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ oks-cli fullhelp
| cluster list | List all clusters |
| cluster create | Create a new cluster |
| cluster get | Get a cluster by name |
| cluster health | Get the health status of a cluster |
| cluster update | Update a cluster by name |
| cluster upgrade | Upgrade a cluster by name |
| cluster delete | Delete a cluster by name |
Expand Down
19 changes: 19 additions & 0 deletions oks_cli/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ def cluster_get_command(ctx, project_name, cluster_name, output, profile):

print_output(data, output)

# GET CLUSTER HEALTH BY NAME
@cluster.command('health', help="Get the health status of a cluster")
@click.option('--project-name', '-p', required=False, help="Project Name", shell_complete=project_completer)
@click.option('--cluster-name', '--name', '-c', required=False, help="Cluster Name", shell_complete=cluster_completer)
@click.option('--output', '-o', type=click.Choice(["json", "yaml"]), help="Specify output format, by default is json")
@click.option('--profile', help="Configuration profile to use", shell_complete=profile_completer)
@click.pass_context
def cluster_health_command(ctx, project_name, cluster_name, output, profile):
"""Retrieve and display the health status of a specific cluster."""
project_name, cluster_name, profile = ctx_update(ctx, project_name, cluster_name, profile)
login_profile(profile)

project_id = find_project_id_by_name(project_name)
cluster_id = find_cluster_id_by_name(project_id, cluster_name)

data = do_request("GET", f'clusters/{cluster_id}/health')

print_output(data, output)


def prepare_cluster_template(cluster_config):
cluster_template = get_template("cluster")
Expand Down
2 changes: 2 additions & 0 deletions oks_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def find_response_object(data):
return response["Cluster"]
elif key == "Clusters":
return response["Clusters"]
elif key == "Health":
return response["Health"]
elif key == "Project":
return response["Project"]
elif key == "Projects":
Expand Down
37 changes: 37 additions & 0 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,43 @@ def test_cluster_get_yaml(mock_request, add_default_profile):
assert data["name"] == "test"


# Test the "cluster health" command: verifies fetching the health status of a specific cluster
@patch("oks_cli.utils.requests.request")
def test_cluster_health_command(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers = {}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers = {}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers = {}, json=lambda: {"ResponseContext": {}, "Health": {"status": "HEALTHY", "status_message": "ok"}})
]

runner = CliRunner()
result = runner.invoke(cli, ["cluster", "health", "-p", "test", "-c", "test"])
assert result.exit_code == 0
assert '"status": "HEALTHY"' in result.output

# Test the "cluster health" command with YAML output
@patch("oks_cli.utils.requests.request")
def test_cluster_health_command_yaml(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Health": {"status": "WARNING", "status_message": "health data not yet available"}}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "health",
"-p", "test",
"-c", "test",
"-o", "yaml",
"--profile", "default"
])

assert result.exit_code == 0

output = yaml.safe_load(result.output)
assert output["status"] == "WARNING"

# Test the "cluster create" command: verifies creating a new cluster in a project
@patch("oks_cli.utils.requests.request")
def test_cluster_create_command(mock_request, add_default_profile):
Expand Down
Loading