Esempio n. 1
0
    def test_delete(self, mock_events: mock.Mock):

        EventRepository.delete(1)

        mock_events.return_value.delete_event.assert_called_once_with("NARUKO-1")

        with self.assertRaises(ObjectDoesNotExist):
            EventModel.get(pk=1)
Esempio n. 2
0
    def delete_schedule(self, request_user: UserModel, tenant: TenantModel, aws_environment: AwsEnvironmentModel,
                        event_id: int):
        self.logger.info("START: delete")
        if not request_user.is_belong_to_tenant(tenant):
            raise PermissionDenied("request user is not belong to tenant. user_id:{} tenant_id:{}"
                                   .format(request_user.id, tenant.id))

        if not request_user.has_aws_env(aws_environment):
            raise PermissionDenied("request user doesn't have aws environments. id:{}".format(request_user.id))

        EventRepository.delete(event_id)

        self.logger.info("END: delete")
Esempio n. 3
0
    def test_get(self, mock_events: mock.Mock):

        res = EventRepository.get(pk=1)
        mock_events.return_value.describe_event.assert_called_once_with(
            "NARUKO-1")

        self.assertTrue(isinstance(res, Schedule))
Esempio n. 4
0
    def test_fetch_schedules_by_resource(self, mock_events: mock.Mock):
        mock_resource = mock.Mock()
        mock_resource.resource_id = "i-01234567890"
        mock_resource.get_service_name.return_value = "ec2"
        mock_resource.region = "ap-northeast-1"
        aws = AwsEnvironmentModel.objects.get(name="test_name1")

        list_rules = mock_events.return_value.list_rules
        expected = CloudWatchEvent(name="NARUKO-1",
                                   schedule_expression="TEST",
                                   is_active=True)
        list_rules.return_value = [
            expected,
            CloudWatchEvent(name="NARUKO-2",
                            schedule_expression="TEST",
                            is_active=True),
            CloudWatchEvent(name="NARUKO-3",
                            schedule_expression="TEST",
                            is_active=True)
        ]

        schedules_by_resource = EventRepository.fetch_schedules_by_resource(
            mock_resource, aws)

        list_rules.assert_called_once()
        self.assertEqual(len(schedules_by_resource), 1)
        self.assertEqual(schedules_by_resource[0].cloudwatchevent, expected)
Esempio n. 5
0
def event_execute(request: Request):
    log = NarukoLogging(request)
    logger = log.get_logger(__name__)
    logger.info("START: event_execute")

    # リクエストがSNSからであるかを検証する
    use_case = ControlEventUseCase(log)
    body_data = json.loads(request.body)
    logger.info(body_data)
    use_case.verify_sns_notification(body_data)

    # SNSの種別ごとに分岐
    sns_type = body_data.get("Type")
    logger.info("Message Type is {}.".format(sns_type))
    if sns_type == "Notification":
        # 通知:アラームの内容に従って通知処理を実施する
        message = json.loads(body_data["Message"])
        event = EventRepository.get(message["id"])
        use_case.execute(event)
    elif sns_type == "SubscriptionConfirmation":

        # 購読開始:SNSトピック登録時に初期検証を実施する
        use_case.confirm_subscription(body_data)
    elif sns_type == "UnsubscribeConfirmation":
        # 購読解除:SNSトピック購読解除
        logger.info("UnsubscribeConfirmation. {}".format(body_data["Message"]))
    else:
        # 存在しない種別
        logger.warning("UnknownSnsMessageType.")
        logger.warning(body_data)

    logger.info("END: event_execute")
    return Response(status=status.HTTP_200_OK)
Esempio n. 6
0
    def test_save(self, mock_events: mock.Mock):
        test_event = mock.Mock()

        repository_save = EventRepository.save(test_event)

        test_event.save.asssert_called_once()
        mock_events.return_value.save_event.assert_called_once_with(test_event)
        self.assertEqual(repository_save, mock_events.return_value.save_event.return_value)
Esempio n. 7
0
    def fetch_schedules(self, request_user: UserModel, tenant: TenantModel, aws_environment: AwsEnvironmentModel,
                        resource: Resource):
        self.logger.info("START: fetch_schedules")
        if not request_user.is_belong_to_tenant(tenant):
            raise PermissionDenied("request user is not belong to tenant. user_id:{} tenant_id:{}"
                                   .format(request_user.id, tenant.id))

        if not request_user.has_aws_env(aws_environment):
            raise PermissionDenied("request user doesn't have aws environments. id:{}".format(request_user.id))

        schedules = EventRepository.fetch_schedules_by_resource(resource, aws_environment)

        self.logger.info("END: fetch_schedules")
        return schedules