Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Limmen committed Jul 28, 2023
1 parent 2863472 commit 2f68b9d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from csle_collector.client_manager.dao.client_arrival_type import ClientArrivalType
from typing import Dict, Any
from csle_collector.client_manager.dao.client_arrival_type import ClientArrivalType
from csle_base.json_serializable import JSONSerializable


class ArrivalConfig():
class ArrivalConfig(JSONSerializable):
"""
Abstract arrival configuration class
"""
Expand All @@ -24,3 +25,28 @@ def to_dict(self) -> Dict[str, Any]:
d = {}
d["client_arrival_type"] = self.client_arrival_type
return d

@staticmethod
def from_dict(d: Dict[str, Any]) -> "ArrivalConfig":
"""
Converts a dict representation to an instance
:param d: the dict to convert
:return: the created instance
"""
obj = ArrivalConfig(client_arrival_type=d["client_arrival_type"])
return obj

@staticmethod
def from_json_file(json_file_path: str) -> "ArrivalConfig":
"""
Reads a json file and converts it to a DTO
:param json_file_path: the json file path
:return: the converted DTO
"""
import io
import json
with io.open(json_file_path, 'r') as f:
json_str = f.read()
return ArrivalConfig.from_dict(json.loads(json_str))
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,14 @@ def from_dict(d: Dict[str, Any]) -> "ConstantArrivalConfig":
:param d: the dict to convert
:return: the created instance
"""
if "lamb" not in d:
obj = ConstantArrivalConfig(lamb=None)
return obj
# obj = ConstantArrivalConfig(lamb=1.0)
else:
obj = ConstantArrivalConfig(lamb=d["lamb"])
return obj
obj = ConstantArrivalConfig(lamb=d["lamb"])
return obj

def to_grpc_object(self) -> csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO:
"""
:return: a GRPC serializable version of the object
"""
return csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO(
lamb=self.lamb
)
return csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO(lamb=self.lamb)

@staticmethod
def from_grpc_object(obj: csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def __str__(self) -> str:
cr = []
if self.credentials is not None:
cr = list(map(lambda x: str(x), self.credentials))
return "protocol:{}, port:{}, name:{}, credentials: {}".format(self.protocol, self.port, self.name,
cr)
return f"protocol: {self.protocol}, port: {self.port}, name: {self.name}, cr: {cr}"

def to_dict(self) -> Dict[str, Any]:
"""
Expand All @@ -57,21 +56,17 @@ def from_dict(d: Dict[str, Any]) -> "NetworkService":
:return: a dto representation of the object
"""
credentials = None
if d["credentials"] is not None:
dto = NetworkService(name=d["name"], port=d["port"], protocol=d["protocol"],
credentials=list(map(lambda x: Credential.from_dict(x), d["credentials"])))
else:
dto = NetworkService(name=d["name"], port=d["port"], protocol=d["protocol"],
credentials=None)
credentials = list(map(lambda x: Credential.from_dict(x), d["credentials"]))
dto = NetworkService(name=d["name"], port=d["port"], protocol=d["protocol"], credentials=credentials)
return dto

def copy(self) -> "NetworkService":
"""
:return: a copy of the DTO
"""
return NetworkService(
protocol=self.protocol, port=self.port, name=self.name, credentials=self.credentials
)
return NetworkService(protocol=self.protocol, port=self.port, name=self.name, credentials=self.credentials)

@staticmethod
def pw_vuln_services():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def emulation_by_id(emulation_id: int) -> Tuple[Response, int]:
"""
requires_admin = False
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_DELETE or \
request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST:
request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST:
requires_admin = True
authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=requires_admin)

Expand All @@ -129,8 +129,7 @@ def emulation_by_id(emulation_id: int) -> Tuple[Response, int]:
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
running_emulation_names = running_emulation_names + list(ClusterController.list_all_running_emulations(
ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT
).runningEmulations)
ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT).runningEmulations)
em_dict = {}
if em is not None:
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET or \
Expand All @@ -140,17 +139,16 @@ def emulation_by_id(emulation_id: int) -> Tuple[Response, int]:
em.running = True
for exec in executions:
exec.emulation_env_config.running = True
if MetastoreFacade.get_emulation_image(emulation_name=em.name) is not None:
em_name, img = MetastoreFacade.get_emulation_image(emulation_name=em.name)
em_img = MetastoreFacade.get_emulation_image(emulation_name=em.name)
if em_img is not None:
em_name, img = em_img
em.image = base64.b64encode(img).decode()
for exec in executions:
exec.emulation_env_config.image = base64.b64encode(img).decode()
else:
img = img = MetastoreFacade.get_emulation_image(emulation_name=em.name)
em.image = img
em.image = ""
for exec in executions:
exec.emulation_env_config.image = img

exec.emulation_env_config.image = ""
em_dict = em.to_dict()
em_dict[api_constants.MGMT_WEBAPP.EXECUTIONS_SUBRESOURCE] = list(map(lambda x: x.to_dict(), executions))
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST:
Expand Down Expand Up @@ -222,6 +220,8 @@ def get_execution_of_emulation(emulation_id: int, execution_id: int) -> Tuple[Re
requires_admin = True
elif request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET:
requires_admin = False
else:
raise ValueError(f"HTTP method: {request.method} not supported")
authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=requires_admin)
if authorized is not None:
return authorized
Expand Down Expand Up @@ -258,8 +258,7 @@ def monitor_emulation(emulation_id: int, execution_id: int, minutes: int) -> Tup
:param minutes: the number of minutes past to collect data from
:return: the collected data
"""
authorized = rest_api_util.check_if_user_is_authorized(request=request,
requires_admin=False)
authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=False)
if authorized is not None:
return authorized

Expand All @@ -274,8 +273,7 @@ def monitor_emulation(emulation_id: int, execution_id: int, minutes: int) -> Tup
time_series = ClusterController.get_execution_time_series_data(
ip=execution.emulation_env_config.kafka_config.container.physical_host_ip,
port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, minutes=minutes,
ip_first_octet=execution.ip_first_octet, emulation=execution.emulation_env_config.name
)
ip_first_octet=execution.ip_first_octet, emulation=execution.emulation_env_config.name)
time_series = time_series.to_dict()
response = jsonify(time_series)
response.headers.add(api_constants.MGMT_WEBAPP.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import json

import csle_common.constants.constants as constants
import pytest
import pytest_mock
from csle_cluster.cluster_manager.cluster_manager_pb2 import (
NodeStatusDTO,
ServiceStatusDTO,
)
from csle_cluster.cluster_manager.cluster_manager_pb2 import NodeStatusDTO, ServiceStatusDTO
from csle_common.dao.emulation_config.config import Config

import csle_rest_api.constants.constants as api_constants
from csle_rest_api.rest_api import create_app


class TestResourcesDockerStatusSuite:
"""
Test suite for /cluster_status url
Test suite for /docker url
"""

@pytest.fixture
Expand All @@ -28,7 +23,7 @@ def flask_app(self):
return create_app(static_folder="../../../../../management-system/csle-mgmt-webapp/build")

@pytest.fixture
def config(self, mocker, example_config: Config):
def config(self, mocker: pytest_mock.MockFixture, example_config: Config):
"""
Pytest fixture for mocking the get_config function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ def emulations_ids_in_names(self, mocker: pytest_mock.MockFixture):
:param mocker: the pytest mocker object
:return: The mocked function
"""
def list_emulations_ids() -> List[Tuple]:
list_dict = [(10, "a")]
return list_dict
def list_emulations_ids() -> List[Tuple[int, str]]:
list_tuple = [(10, "a")]
return list_tuple
list_emulations_ids_mocker = mocker.MagicMock(side_effect=list_emulations_ids
)
return list_emulations_ids_mocker
Expand Down Expand Up @@ -333,9 +333,9 @@ def emulations_ids_not_in_names(self, mocker: pytest_mock.MockFixture):
:param mocker: the pytest mocker object
:return: The mocked function
"""
def list_emulations_ids() -> List[Tuple]:
list_dict = [(10, "q")]
return list_dict
def list_emulations_ids() -> List[Tuple[int, str]]:
list_tuple = [(10, "q")]
return list_tuple
list_emulations_ids_mocker = mocker.MagicMock(side_effect=list_emulations_ids
)
return list_emulations_ids_mocker
Expand Down

0 comments on commit 2f68b9d

Please sign in to comment.