Skip to content

Commit

Permalink
Merge pull request #138 from Limmen/dev
Browse files Browse the repository at this point in the history
Testing done
  • Loading branch information
Limmen authored Jul 11, 2023
2 parents 92872df + 53afa32 commit abe6f80
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
Routes and sub-resources for the /docker resource
"""
import json
from flask import Blueprint, jsonify, request

import csle_common.constants.constants as constants
from csle_cluster.cluster_manager.cluster_controller import ClusterController
from csle_common.metastore.metastore_facade import MetastoreFacade
from flask import Blueprint, jsonify, request

import csle_rest_api.constants.constants as api_constants
import csle_rest_api.util.rest_api_util as rest_api_util
from csle_common.metastore.metastore_facade import MetastoreFacade
from csle_cluster.cluster_manager.cluster_controller import ClusterController

# Creates a blueprint "sub application" of the main REST app
docker_bp = Blueprint(api_constants.MGMT_WEBAPP.DOCKER_RESOURCE, __name__,
Expand All @@ -25,24 +27,26 @@ def docker():
authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=requires_admin)
if authorized is not None:
return authorized

json_data = json.loads(request.data)
if api_constants.MGMT_WEBAPP.IP_PROPERTY not in json_data:
return jsonify({}), constants.HTTPS.BAD_REQUEST_STATUS_CODE
ip = json_data[api_constants.MGMT_WEBAPP.IP_PROPERTY]
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST:
json_data = json.loads(request.data)
if api_constants.MGMT_WEBAPP.IP_PROPERTY not in json_data:
return jsonify({}), constants.HTTPS.BAD_REQUEST_STATUS_CODE
ip = json_data[api_constants.MGMT_WEBAPP.IP_PROPERTY]

config = MetastoreFacade.get_config(id=1)
cluster_statuses = []
ip_found = False
for node in config.cluster_config.cluster_nodes:
node_status = ClusterController.get_node_status(ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT)
if node.ip == ip:
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST:
if node_status.dockerRunning:
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST:
if node.ip == ip:
ip_found = True
if node_status.dockerEngineRunning:
ClusterController.stop_docker_engine(ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT)
node_status.dockerRunning = False
node_status.dockerEngineRunning = False
else:
ClusterController.start_docker_engine(ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT)
node_status.dockerRunning = True
node_status.dockerEngineRunning = True
cluster_status_dict = {
api_constants.MGMT_WEBAPP.CADVISOR_RUNNING_PROPERTY: node_status.cAdvisorRunning,
api_constants.MGMT_WEBAPP.GRAFANA_RUNNING_PROPERTY: node_status.grafanaRunning,
Expand Down Expand Up @@ -74,6 +78,10 @@ def docker():
api_constants.MGMT_WEBAPP.LEADER_PROPERTY: node.leader
}
cluster_statuses.append(cluster_status_dict)
response = jsonify(cluster_statuses)
response.headers.add(api_constants.MGMT_WEBAPP.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*")
return response, constants.HTTPS.OK_STATUS_CODE
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST and ip_found is False:
response = jsonify({"reason": f"node with ip {ip} does not exist"})
return response, constants.HTTPS.BAD_REQUEST_STATUS_CODE
else:
response = jsonify(cluster_statuses)
response.headers.add(api_constants.MGMT_WEBAPP.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*")
return response, constants.HTTPS.OK_STATUS_CODE
51 changes: 51 additions & 0 deletions simulation-system/libs/csle-rest-api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from typing import List

import csle_common.constants.constants as constants
import pytest
from csle_cluster.cluster_manager.cluster_manager_pb2 import (
Expand Down Expand Up @@ -254,3 +256,52 @@ def check_if_user_is_authorized(request, requires_admin):
side_effect=check_if_user_is_authorized
)
return check_if_user_is_authorized_mock


@pytest.fixture
def cluster_node_status(example_config: Config,
example_node_status: NodeStatusDTO) -> List[dict]:
"""
Staticmethod that reurns an example cluster node status, that is
to be tested against the mocking ofthe API
:param example_config: an example Config class
:param exmaple_node_status: an example node status class
:return: a list of a dict containing an example cluster node status
"""
config = example_config
cluster_statuses = []
for node in config.cluster_config.cluster_nodes:
node_status = example_node_status
cluster_status_dict = {
api_constants.MGMT_WEBAPP.CADVISOR_RUNNING_PROPERTY: node_status.cAdvisorRunning,
api_constants.MGMT_WEBAPP.GRAFANA_RUNNING_PROPERTY: node_status.grafanaRunning,
api_constants.MGMT_WEBAPP.POSTGRESQL_RUNNING_PROPERTY: node_status.postgreSQLRunning,
api_constants.MGMT_WEBAPP.NODE_EXPORTER_RUNNING_PROPERTY: node_status.nodeExporterRunning,
api_constants.MGMT_WEBAPP.DOCKER_ENGINE_RUNNING_PROPERTY: node_status.dockerEngineRunning,
api_constants.MGMT_WEBAPP.NGINX_RUNNING_PROPERTY: node_status.nginxRunning,
api_constants.MGMT_WEBAPP.FLASK_RUNNING_PROPERTY: node_status.flaskRunning,
api_constants.MGMT_WEBAPP.PROMETHEUS_RUNNING_PROPERTY: node_status.prometheusRunning,
api_constants.MGMT_WEBAPP.PGADMIN_RUNNING_PROPERTY: node_status.pgAdminRunning,
api_constants.MGMT_WEBAPP.CADVISOR_URL_PROPERTY:
f"http://{node.ip}:{constants.COMMANDS.CADVISOR_PORT}/",
api_constants.MGMT_WEBAPP.GRAFANA_URL_PROPERTY: f"http://{node.ip}:{constants.COMMANDS.GRAFANA_PORT}/",
api_constants.MGMT_WEBAPP.NODE_EXPORTER_URL_PROPERTY: f"http://{node.ip}:"
f"{constants.COMMANDS.NODE_EXPORTER_PORT}/",
api_constants.MGMT_WEBAPP.FLASK_URL_PROPERTY: f"http://{node.ip}:{constants.COMMANDS.FLASK_PORT}/",
api_constants.MGMT_WEBAPP.PROMETHEUS_URL_PROPERTY: f"http://{node.ip}:"
f"{constants.COMMANDS.PROMETHEUS_PORT}/",
api_constants.MGMT_WEBAPP.PGADMIN_URL_PROPERTY: f"http://{node.ip}:{constants.COMMANDS.PGADMIN_PORT}/",
api_constants.MGMT_WEBAPP.CADVISOR_PORT_PROPERTY: constants.COMMANDS.CADVISOR_PORT,
api_constants.MGMT_WEBAPP.GRAFANA_PORT_PROPERTY: constants.COMMANDS.GRAFANA_PORT,
api_constants.MGMT_WEBAPP.NODE_EXPORTER_PORT_PROPERTY: constants.COMMANDS.NODE_EXPORTER_PORT,
api_constants.MGMT_WEBAPP.FLASK_PORT_PROPERTY: constants.COMMANDS.FLASK_PORT,
api_constants.MGMT_WEBAPP.PROMETHEUS_PORT_PROPERTY: constants.COMMANDS.PROMETHEUS_PORT,
api_constants.MGMT_WEBAPP.PGADMIN_PORT_PROPERTY: constants.COMMANDS.PGADMIN_PORT,
api_constants.MGMT_WEBAPP.IP_PROPERTY: node.ip,
api_constants.MGMT_WEBAPP.CPUS_PROPERTY: node.cpus,
api_constants.MGMT_WEBAPP.GPUS_PROPERTY: node.gpus,
api_constants.MGMT_WEBAPP.RAM_PROPERTY: node.RAM,
api_constants.MGMT_WEBAPP.LEADER_PROPERTY: node.leader
}
cluster_statuses.append(cluster_status_dict)
return cluster_statuses
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
from typing import List

import csle_common.constants.constants as constants
import pytest
Expand Down Expand Up @@ -63,52 +62,11 @@ def get_node_status(ip: str, port: int) -> NodeStatusDTO:
get_node_status_mock = mocker.MagicMock(side_effect=get_node_status)
return get_node_status_mock

@staticmethod
def cluster_node_status(example_config: Config,
example_node_status: NodeStatusDTO) -> List[dict]:
config = example_config
cluster_statuses = []
for node in config.cluster_config.cluster_nodes:
node_status = example_node_status
cluster_status_dict = {
api_constants.MGMT_WEBAPP.CADVISOR_RUNNING_PROPERTY: node_status.cAdvisorRunning,
api_constants.MGMT_WEBAPP.GRAFANA_RUNNING_PROPERTY: node_status.grafanaRunning,
api_constants.MGMT_WEBAPP.POSTGRESQL_RUNNING_PROPERTY: node_status.postgreSQLRunning,
api_constants.MGMT_WEBAPP.NODE_EXPORTER_RUNNING_PROPERTY: node_status.nodeExporterRunning,
api_constants.MGMT_WEBAPP.DOCKER_ENGINE_RUNNING_PROPERTY: node_status.dockerEngineRunning,
api_constants.MGMT_WEBAPP.NGINX_RUNNING_PROPERTY: node_status.nginxRunning,
api_constants.MGMT_WEBAPP.FLASK_RUNNING_PROPERTY: node_status.flaskRunning,
api_constants.MGMT_WEBAPP.PROMETHEUS_RUNNING_PROPERTY: node_status.prometheusRunning,
api_constants.MGMT_WEBAPP.PGADMIN_RUNNING_PROPERTY: node_status.pgAdminRunning,
api_constants.MGMT_WEBAPP.CADVISOR_URL_PROPERTY:
f"http://{node.ip}:{constants.COMMANDS.CADVISOR_PORT}/",
api_constants.MGMT_WEBAPP.GRAFANA_URL_PROPERTY: f"http://{node.ip}:{constants.COMMANDS.GRAFANA_PORT}/",
api_constants.MGMT_WEBAPP.NODE_EXPORTER_URL_PROPERTY: f"http://{node.ip}:"
f"{constants.COMMANDS.NODE_EXPORTER_PORT}/",
api_constants.MGMT_WEBAPP.FLASK_URL_PROPERTY: f"http://{node.ip}:{constants.COMMANDS.FLASK_PORT}/",
api_constants.MGMT_WEBAPP.PROMETHEUS_URL_PROPERTY: f"http://{node.ip}:"
f"{constants.COMMANDS.PROMETHEUS_PORT}/",
api_constants.MGMT_WEBAPP.PGADMIN_URL_PROPERTY: f"http://{node.ip}:{constants.COMMANDS.PGADMIN_PORT}/",
api_constants.MGMT_WEBAPP.CADVISOR_PORT_PROPERTY: constants.COMMANDS.CADVISOR_PORT,
api_constants.MGMT_WEBAPP.GRAFANA_PORT_PROPERTY: constants.COMMANDS.GRAFANA_PORT,
api_constants.MGMT_WEBAPP.NODE_EXPORTER_PORT_PROPERTY: constants.COMMANDS.NODE_EXPORTER_PORT,
api_constants.MGMT_WEBAPP.FLASK_PORT_PROPERTY: constants.COMMANDS.FLASK_PORT,
api_constants.MGMT_WEBAPP.PROMETHEUS_PORT_PROPERTY: constants.COMMANDS.PROMETHEUS_PORT,
api_constants.MGMT_WEBAPP.PGADMIN_PORT_PROPERTY: constants.COMMANDS.PGADMIN_PORT,
api_constants.MGMT_WEBAPP.IP_PROPERTY: node.ip,
api_constants.MGMT_WEBAPP.CPUS_PROPERTY: node.cpus,
api_constants.MGMT_WEBAPP.GPUS_PROPERTY: node.gpus,
api_constants.MGMT_WEBAPP.RAM_PROPERTY: node.RAM,
api_constants.MGMT_WEBAPP.LEADER_PROPERTY: node.leader
}
cluster_statuses.append(cluster_status_dict)
return cluster_statuses

def test_cluster_status_get(self, flask_app, mocker,
node_status, config,
not_logged_in, logged_in,
logged_in_as_admin, example_config,
example_node_status):
logged_in_as_admin,
cluster_node_status):
"""
Tests the GET HTTPS method for the /cluster_status url
Expand All @@ -121,8 +79,8 @@ def test_cluster_status_get(self, flask_app, mocker,
:param node_status: the node_status fixture
:return: None
"""
test_ns = TestResourcesClusterStatusSuite.cluster_node_status(example_config,
example_node_status)
test_ns = cluster_node_status

test_ns_dict = test_ns[0]
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized",
side_effect=not_logged_in)
Expand Down
Loading

0 comments on commit abe6f80

Please sign in to comment.