diff --git a/README.md b/README.md index 9ac2074..5511eb2 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/oks_cli/cluster.py b/oks_cli/cluster.py index 75b15ff..bac986c 100644 --- a/oks_cli/cluster.py +++ b/oks_cli/cluster.py @@ -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") diff --git a/oks_cli/utils.py b/oks_cli/utils.py index 432b8db..7f08b4e 100644 --- a/oks_cli/utils.py +++ b/oks_cli/utils.py @@ -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": diff --git a/tests/test_cluster.py b/tests/test_cluster.py index 249c00d..a4ffd81 100644 --- a/tests/test_cluster.py +++ b/tests/test_cluster.py @@ -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):