def graph(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None, pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: graph") try: aws_environment = AwsEnvironmentModel.objects.get( id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) monitor_graph = MonitorGraph(metric_name=pk, **request.data) monitor_graph = ControlMonitorUseCase(log).graph( request.user, resource, aws_environment, monitor_graph) except (TypeError, ValueError, KeyError, NarukoException) as e: # リクエストデータが不正 logger.exception(e) return Response(status=status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: # AWS環境が存在しない logger.exception(e) return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: logger.info("END: graph") return Response(data=monitor_graph.serialize(), status=status.HTTP_200_OK)
def list(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") try: aws = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) monitors = ControlMonitorUseCase(log).fetch_monitors( request.user, aws, resource) except (TypeError, ValueError, KeyError, NarukoException) as e: # リクエストデータが不正 logger.exception(e) return Response(status=status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: # AWS環境が存在しない logger.exception(e) return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: logger.info("END: list") return Response(data=[monitor.serialize() for monitor in monitors], status=status.HTTP_200_OK)
def test_fetch_monitors(self, mock_cloudwatch: mock.Mock): # mock準備 mock_user = mock.Mock() mock_aws = mock.Mock() mock_resource = mock.Mock() res = ControlMonitorUseCase(mock.Mock()).fetch_monitors(mock_user, mock_aws, mock_resource) self.assertEqual(res, mock_cloudwatch.return_value.describe_resource_monitors.return_value) mock_user.has_aws_env.assert_called() mock_cloudwatch.return_value.describe_resource_monitors.assert_called_with(mock_resource)
def test_fetch_monitors_no_aws(self, mock_cloudwatch: mock.Mock): # mock準備 mock_user = mock.Mock() mock_user.has_aws_env.return_value = False mock_aws = mock.Mock() mock_resource = mock.Mock() with self.assertRaises(PermissionDenied): ControlMonitorUseCase(mock.Mock()).fetch_monitors(mock_user, mock_aws, mock_resource) mock_user.has_aws_env.assert_called() mock_cloudwatch.return_value.describe_resource_monitors.assert_not_called()
def test_save_monitor(self, mock_sns: mock.Mock, mock_cloudwatch: mock.Mock): # mock準備 mock_user = mock.Mock() mock_aws = mock.Mock() mock_resource = mock.Mock() res = ControlMonitorUseCase(mock.Mock()).save_monitor(mock_user, mock_resource, mock_aws) self.assertEqual(res, mock_resource) mock_user.has_aws_env.assert_called() mock_sns.return_value.add_permission.assert_called_with(mock_aws) mock_cloudwatch.return_value.put_metric_alarms.assert_called_with(mock_resource, mock_sns.return_value.arn)
def test_save_monitor_no_aws(self, mock_sns: mock.Mock, mock_cloudwatch: mock.Mock): # mock準備 mock_user = mock.Mock() mock_user.has_aws_env.return_value = False mock_aws = mock.Mock() mock_resource = mock.Mock() with self.assertRaises(PermissionDenied): ControlMonitorUseCase(mock.Mock()).save_monitor(mock_user, mock_resource, mock_aws) mock_user.has_aws_env.assert_called() mock_sns.return_value.add_permission.assert_not_called() mock_cloudwatch.return_value.put_metric_alarms.assert_not_called()
def test_graph(self, mock_cloudwatch: mock.Mock): # mock準備 mock_user = mock.Mock() mock_aws = mock.Mock() mock_resource = mock.Mock() mock_monitor_graph = mock.Mock() get_chart = mock_cloudwatch.return_value.get_chart mock_monitor_graph.metric_name = "Valid" mock_resource.get_metrics.return_value = ["Valid"] res = ControlMonitorUseCase(mock.Mock()).graph(mock_user, mock_resource, mock_aws, mock_monitor_graph) mock_user.has_aws_env.assert_called() mock_resource.get_metrics.assert_called_once() get_chart.assert_called_once_with(mock_monitor_graph, mock_resource) self.assertEqual(res, get_chart.return_value)
def test_graph_invalid_metrics(self, mock_cloudwatch: mock.Mock): # mock準備 mock_user = mock.Mock() mock_aws = mock.Mock() mock_resource = mock.Mock() mock_monitor_graph = mock.Mock() get_chart = mock_cloudwatch.return_value.get_chart mock_monitor_graph.metric_name = "invalid" mock_resource.get_metrics.return_value = ["Valid"] with self.assertRaises(ObjectDoesNotExist): ControlMonitorUseCase(mock.Mock()).graph(mock_user, mock_resource, mock_aws, mock_monitor_graph) mock_user.has_aws_env.assert_called() mock_resource.get_metrics.assert_called() get_chart.assert_not_called()
def test_graph_no_aws(self, mock_cloudwatch: mock.Mock): # mock準備 mock_user = mock.Mock() mock_user.has_aws_env.return_value = False mock_aws = mock.Mock() mock_resource = mock.Mock() mock_monitor_graph = mock.Mock() get_chart = mock_cloudwatch.return_value.get_chart mock_monitor_graph.metric_name = "Valid" mock_resource.get_metrics.return_value = ["Valid"] with self.assertRaises(PermissionDenied): ControlMonitorUseCase(mock.Mock()).graph(mock_user, mock_resource, mock_aws, mock_monitor_graph) mock_user.has_aws_env.assert_called() mock_resource.get_metrics.assert_not_called() get_chart.assert_not_called()
def list(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") aws = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) monitors = ControlMonitorUseCase(log).fetch_monitors( request.user, aws, resource) logger.info("END: list") return Response(data=[monitor.serialize() for monitor in monitors], status=status.HTTP_200_OK)
def create(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: create") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) resource.monitors.append(Monitor(**request.data)) resource = ControlMonitorUseCase(log).save_monitor( request.user, resource, aws_environment) logger.info("END: create") return Response(data=resource.monitors[0].serialize(), status=status.HTTP_200_OK)
def graph(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None, pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: graph") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) monitor_graph = MonitorGraph(metric_name=pk, **request.data) monitor_graph = ControlMonitorUseCase(log).graph( request.user, resource, aws_environment, monitor_graph) logger.info("END: graph") return Response(data=monitor_graph.serialize(), status=status.HTTP_200_OK)