コード例 #1
0
 def destroy(self, request, tenant_pk=None, pk=None):
     log = NarukoLogging(request)
     logger = log.get_logger(__name__)
     logger.info("START: destroy")
     model = AwsEnvironmentModel.objects.get(id=pk, tenant_id=tenant_pk)
     ControlAwsEnvironment(log).delete_aws_environment(request.user, model)
     logger.info("END: destroy")
     return Response(status=status.HTTP_204_NO_CONTENT)
コード例 #2
0
    def test_save_aws_environment(self, iam_mock, user_model_mock):
        mock_user = Mock()
        aws_environment_mock = Mock()

        mock_scheduler = Mock()
        user_model_mock.get_scheduler.return_value = mock_scheduler

        target = ControlAwsEnvironment(Mock())
        result = target.save_aws_environment(mock_user, aws_environment_mock)

        mock_user.is_belong_to_tenant.assert_called_once()
        mock_user.can_control_aws.assert_called_once()

        iam_mock.assert_called_once_with(aws_environment_mock, None)
        iam_mock.return_value.validate_role.assert_called_once_with(
            aws_environment_mock.aws_account_id, aws_environment_mock.aws_role)
        aws_environment_mock.save.assert_called_once()
        mock_scheduler.save.assert_called_once()
        self.assertEqual(result, aws_environment_mock)
コード例 #3
0
    def test_delete_aws_environment(self):
        mock_user = Mock()
        mock_aws = Mock()

        ControlAwsEnvironment(Mock()).delete_aws_environment(
            mock_user, mock_aws)

        mock_user.is_belong_to_tenant.assert_called_once()
        mock_user.can_control_aws.assert_called_once()
        mock_aws.delete.assert_called_once()
コード例 #4
0
 def billing(self, request, tenant_pk=None, pk=None):
     log = NarukoLogging(request)
     logger = log.get_logger(__name__)
     logger.info("START: billing")
     aws_environment = AwsEnvironmentModel.objects.get(id=pk,
                                                       tenant_id=tenant_pk)
     billing_graph = ControlAwsEnvironment(log).billing_graph(
         request.user, aws_environment, **request.data)
     logger.info("END: billing")
     return Response(data=billing_graph, status=status.HTTP_200_OK)
コード例 #5
0
    def test_fetch_aws_environments_cant_control_aws(self, mock_aws_model):
        mock_user = Mock()
        mock_user.can_control_aws.return_value = False
        mock_tenant = Mock()

        with self.assertRaises(PermissionDenied):
            ControlAwsEnvironment(Mock()).fetch_aws_environments(
                mock_user, mock_tenant)

        mock_user.is_belong_to_tenant.assert_called_once()
        mock_user.can_control_aws.assert_called_once()
        mock_aws_model.objects.filter.assert_not_called()
コード例 #6
0
    def test_delete_aws_environment_cant_control_aws(self):
        mock_user = Mock()
        mock_user.can_control_aws.return_value = False
        mock_aws = Mock()

        with self.assertRaises(PermissionDenied):
            ControlAwsEnvironment(Mock()).delete_aws_environment(
                mock_user, mock_aws)

        mock_user.is_belong_to_tenant.assert_called_once()
        mock_user.can_control_aws.assert_called_once()
        mock_aws.delete.assert_not_called()
コード例 #7
0
    def test_fetch_aws_environments(self, mock_aws_model):
        mock_user = Mock()
        mock_tenant = Mock()
        objects_filter = mock_aws_model.objects.filter

        res = ControlAwsEnvironment(Mock()).fetch_aws_environments(
            mock_user, mock_tenant)

        mock_user.is_belong_to_tenant.assert_called_once()
        mock_user.can_control_aws.assert_called_once()
        objects_filter.assert_called_once_with(tenant_id=mock_tenant.id)
        self.assertEqual(res, objects_filter.return_value)
コード例 #8
0
    def test_billing_graph(self, mock_cloudwatch: Mock):
        # テスト用の引数準備
        external_result = [{
            'timestamps': [],
            'values': [],
            'config': {
                'metric_name':
                'EstimatedCharges',
                'dimensions': [{
                    'Name': 'ServiceName',
                    'Value': 'AmazonCloudWatch'
                }, {
                    'Name': 'Currency',
                    'Value': 'USD'
                }]
            }
        }]
        result = [{
            'service': 'AmazonCloudWatch',
            'timestamps': [],
            'values': []
        }]
        # mock準備
        mock_user = Mock()
        mock_aws = Mock()
        mock_start_time = Mock()
        mock_end_time = Mock()
        mock_period = Mock()
        mock_stat = Mock()

        mock_user.can_fetch_billing.return_value = True
        mock_user.has_aws_env.return_value = True

        get_multi_charts = mock_cloudwatch.return_value.get_multi_charts
        get_multi_charts.return_value = external_result
        list_metrics = mock_cloudwatch.return_value.list_metrics
        res = ControlAwsEnvironment(Mock()).billing_graph(
            mock_user, mock_aws, mock_start_time, mock_end_time, mock_period,
            mock_stat)
        params = dict(name_space='AWS/Billing',
                      period=mock_period,
                      stat=mock_stat,
                      start_time=mock_start_time,
                      end_time=mock_end_time,
                      metric_data_queries=[])

        mock_user.has_aws_env.assert_called()
        mock_user.can_fetch_billing.assert_called()
        mock_cloudwatch.assert_called_with(mock_aws, 'us-east-1')
        list_metrics.assert_called_once_with('AWS/Billing', 'EstimatedCharges',
                                             [])
        get_multi_charts.assert_called_once_with(**params)
        self.assertEqual(res, result)
コード例 #9
0
    def test_save_aws_environment_not_belong_to_tenant(self, iam_mock,
                                                       user_model_mock):
        mock_user = Mock()
        mock_user.is_belong_to_tenant.return_value = False
        aws_environment_mock = Mock()

        mock_scheduler = Mock()
        user_model_mock.get_scheduler.return_value = mock_scheduler

        target = ControlAwsEnvironment(Mock())

        with self.assertRaises(PermissionDenied):
            target.save_aws_environment(mock_user, aws_environment_mock)

        mock_user.is_belong_to_tenant.assert_called_once()
        mock_user.can_control_aws.assert_not_called()

        iam_mock.assert_not_called()
        iam_mock.return_value.validate_role.assert_not_called()

        aws_environment_mock.save.assert_not_called()
        mock_scheduler.save.assert_not_called()
コード例 #10
0
 def create(self, request, tenant_pk=None):
     log = NarukoLogging(request)
     logger = log.get_logger(__name__)
     logger.info("START: create")
     request.data['tenant'] = tenant_pk
     create_serializer = AwsEnvironmentModelCreateSerializer(
         data=request.data)
     create_serializer.is_valid(raise_exception=True)
     model = create_serializer.save()
     ControlAwsEnvironment(log).save_aws_environment(request.user, model)
     data = AwsEnvironmentModelGetDetailSerializer(model).data
     logger.info("END: create")
     return Response(data=data, status=status.HTTP_201_CREATED)
コード例 #11
0
 def list(self, request, tenant_pk=None, detail=True):
     log = NarukoLogging(request)
     logger = log.get_logger(__name__)
     logger.info("START: list")
     tenant = TenantModel.objects.get(id=tenant_pk)
     aws_environments = ControlAwsEnvironment(log).fetch_aws_environments(
         request.user, tenant)
     return Response(
         data={
             "aws_environments": [
                 AwsEnvironmentModelGetDetailSerializer(
                     aws_environment).data
                 for aws_environment in aws_environments
             ]
         })
コード例 #12
0
    def update(self, request, tenant_pk=None, pk=None):
        log = NarukoLogging(request)
        logger = log.get_logger(__name__)
        logger.info("START: update")
        model = AwsEnvironmentModel.objects.get(id=pk, tenant_id=tenant_pk)
        serializer = AwsEnvironmentModelUpdateSerializer(instance=model,
                                                         data=request.data,
                                                         partial=True)
        serializer.is_valid(raise_exception=True)
        updated_model = serializer.save()

        ControlAwsEnvironment(log).save_aws_environment(
            request.user, updated_model)
        data = AwsEnvironmentModelGetDetailSerializer(updated_model).data
        logger.info("END: update")
        return Response(data=data, status=status.HTTP_200_OK)
コード例 #13
0
    def test_billing_graph_dont_have_permission(self):
        mock_user = Mock()
        mock_aws = Mock()
        mock_start_time = Mock()
        mock_end_time = Mock()
        mock_period = Mock()
        mock_stat = Mock()

        mock_user.can_fetch_billing.return_value = False
        mock_user.has_aws_env.return_value = True

        with self.assertRaises(PermissionDenied):
            ControlAwsEnvironment(Mock()).billing_graph(
                mock_user, mock_aws, mock_start_time, mock_end_time,
                mock_period, mock_stat)

        mock_user.has_aws_env.assert_called()
        mock_user.can_fetch_billing.assert_called()