def update(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: update") try: with transaction.atomic(): tenant = TenantModel.objects.get(id=tenant_pk) aws_env = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant=tenant) schedule = ScheduleFactory.create( resource_id=resource_pk, service=service_pk, region=region_pk, aws_id=aws_env_pk, event_id=pk, **request.data) create_schedule = ControlScheduleUseCase(log).save_schedule(request.user, tenant, aws_env, schedule) except (TypeError, ValueError, KeyError, ClientError, 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: update") return Response(data=create_schedule.serialize(), status=status.HTTP_201_CREATED)
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: with transaction.atomic(): tenant = TenantModel.objects.get(id=tenant_pk) aws_env = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant=tenant) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) schedules = ControlScheduleUseCase(log).fetch_schedules(request.user, tenant, aws_env, resource) except (TypeError, ValueError, KeyError, ClientError, 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=[schedule.serialize() for schedule in schedules], status=status.HTTP_200_OK)
def test_delete_schedule(self, mock_repo: mock.Mock): # mock準備 mock_user = mock.Mock() mock_tenant = mock.Mock() mock_aws = mock.Mock() ControlScheduleUseCase(mock.Mock()).delete_schedule( mock_user, mock_tenant, mock_aws, 1) mock_user.is_belong_to_tenant.assert_called_once_with(mock_tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) mock_repo.delete.assert_called_once_with(1)
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") tenant = TenantModel.objects.get(id=tenant_pk) aws_env = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant=tenant) schedule = ScheduleFactory.create(resource_id=resource_pk, service=service_pk, region=region_pk, aws_id=aws_env_pk, **request.data) create_schedule = ControlScheduleUseCase(log).save_schedule( request.user, tenant, aws_env, schedule) logger.info("END: create") return Response(data=create_schedule.serialize(), status=status.HTTP_201_CREATED)
def test_delete_schedule_no_aws(self, mock_repo: mock.Mock): # mock準備 mock_user = mock.Mock() mock_user.has_aws_env.return_value = False mock_tenant = mock.Mock() mock_aws = mock.Mock() with self.assertRaises(PermissionDenied): ControlScheduleUseCase(mock.Mock()).delete_schedule( mock_user, mock_tenant, mock_aws, 1) mock_user.is_belong_to_tenant.assert_called_once_with(mock_tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) mock_repo.delete.assert_not_called()
def test_save_schedule(self, mock_repo: mock.Mock): # mock準備 mock_user = mock.Mock() mock_tenant = mock.Mock() mock_aws = mock.Mock() mock_schedule = mock.Mock() res = ControlScheduleUseCase(mock.Mock()).save_schedule( mock_user, mock_tenant, mock_aws, mock_schedule) mock_user.is_belong_to_tenant.assert_called_once_with(mock_tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) mock_repo.save.assert_called_once_with(mock_schedule) self.assertEqual(res, mock_repo.save.return_value)
def test_fetch_schedules_not_belong_to_tenant(self, mock_repo: mock.Mock): # mock準備 mock_user = mock.Mock() mock_user.is_belong_to_tenant.return_value = False mock_tenant = mock.Mock() mock_aws = mock.Mock() mock_resource = mock.Mock() with self.assertRaises(PermissionDenied): ControlScheduleUseCase(mock.Mock()).fetch_schedules( mock_user, mock_tenant, mock_aws, mock_resource) mock_user.is_belong_to_tenant.assert_called_once_with(mock_tenant) mock_user.has_aws_env.assert_not_called() mock_repo.fetch_schedules_by_resource.assert_not_called()
def delete(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: delete") tenant = TenantModel.objects.get(id=tenant_pk) aws_env = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant=tenant) ControlScheduleUseCase(log).delete_schedule(request.user, tenant, aws_env, pk) logger.info("END: delete") return Response(status=status.HTTP_204_NO_CONTENT)
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") tenant = TenantModel.objects.get(id=tenant_pk) aws_env = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant=tenant) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) schedules = ControlScheduleUseCase(log).fetch_schedules( request.user, tenant, aws_env, resource) logger.info("END: list") return Response(data=[schedule.serialize() for schedule in schedules], status=status.HTTP_200_OK)
def delete(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: delete") try: with transaction.atomic(): tenant = TenantModel.objects.get(id=tenant_pk) aws_env = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant=tenant) ControlScheduleUseCase(log).delete_schedule(request.user, tenant, aws_env, pk) except (TypeError, ValueError, KeyError, ClientError, 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: delete") return Response(status=status.HTTP_204_NO_CONTENT)