Ejemplo n.º 1
0
    def test_remove_aws(self):
        """Remove aws provider."""
        # Create Provider
        provider_authentication = ProviderAuthentication.objects.create(provider_resource_name='arn:aws:iam::2:role/mg')
        provider_billing = ProviderBillingSource.objects.create(bucket='my_s3_bucket')
        provider = Provider.objects.create(name='awsprovidername',
                                           created_by=self.user,
                                           customer=self.customer,
                                           authentication=provider_authentication,
                                           billing_source=provider_billing)
        provider_uuid = provider.uuid

        new_user_dict = self._create_user_data()
        request_context = self._create_request_context(self.customer_data,
                                                       new_user_dict, False)
        user_serializer = UserSerializer(data=new_user_dict, context=request_context)
        other_user = None
        if user_serializer.is_valid(raise_exception=True):
            other_user = user_serializer.save()

        with tenant_context(self.tenant):
            manager = ProviderManager(provider_uuid)
            manager.remove(self._create_delete_request(other_user))

        provider_query = Provider.objects.all().filter(uuid=provider_uuid)
        auth_count = ProviderAuthentication.objects.count()
        billing_count = ProviderBillingSource.objects.count()
        self.assertFalse(provider_query)
        self.assertEqual(auth_count, 0)
        self.assertEqual(billing_count, 0)
Ejemplo n.º 2
0
    def test_remove_not_removeable(self, _):
        """Test error raised if user without capability tries to remove a provider."""
        # Create Provider
        credentials = {"role_arn": "arn:aws:iam::2:role/mg"}
        provider_authentication = ProviderAuthentication.objects.create(credentials=credentials)
        data_source = {"bucket": "my_s3_bucket"}
        provider_billing = ProviderBillingSource.objects.create(data_source=data_source)
        with patch("masu.celery.tasks.check_report_updates"):
            provider = Provider.objects.create(
                name="awsprovidername",
                created_by=self.user,
                customer=self.customer,
                authentication=provider_authentication,
                billing_source=provider_billing,
            )
        provider_uuid = provider.uuid

        new_user_dict = self._create_user_data()
        request_context = self._create_request_context(self.customer_data, new_user_dict, False, create_user=False)
        user_serializer = UserSerializer(data=new_user_dict, context=request_context)
        other_user = None
        if user_serializer.is_valid(raise_exception=True):
            other_user = user_serializer.save()

        with tenant_context(self.tenant):
            manager = ProviderManager(provider_uuid)
            with self.assertRaises(ProviderManagerError):
                manager.remove(self._create_delete_request(other_user))
Ejemplo n.º 3
0
    def test_remove_ocp_added_via_sources(self):
        """Remove ocp provider added via sources."""
        # Create Provider
        credentials = {"cluster_id": "cluster_id_1001"}
        provider_authentication = ProviderAuthentication.objects.create(
            credentials=credentials)
        with patch("masu.celery.tasks.check_report_updates"):
            provider = Provider.objects.create(
                name="ocpprovidername",
                created_by=self.user,
                customer=self.customer,
                authentication=provider_authentication,
            )
        provider_uuid = provider.uuid

        sources = Sources.objects.create(source_id=1,
                                         auth_header="testheader",
                                         offset=1,
                                         koku_uuid=provider_uuid)
        sources.save()
        delete_request = self._create_delete_request(
            self.user, {"Sources-Client": "True"})
        with tenant_context(self.tenant):
            manager = ProviderManager(provider_uuid)
            manager.remove(delete_request, from_sources=True)
        provider_query = Provider.objects.all().filter(uuid=provider_uuid)
        self.assertFalse(provider_query)
Ejemplo n.º 4
0
    def test_direct_remove_ocp_added_via_sources(self):
        """Remove ocp provider added via sources directly."""
        # Create Provider
        credentials = {"cluster_id": "cluster_id_1001"}
        provider_authentication = ProviderAuthentication.objects.create(
            credentials=credentials)
        with patch("masu.celery.tasks.check_report_updates"):
            provider = Provider.objects.create(
                name="ocpprovidername",
                created_by=self.user,
                customer=self.customer,
                authentication=provider_authentication,
            )
        provider_uuid = provider.uuid

        sources = Sources.objects.create(source_id=1,
                                         auth_header="testheader",
                                         offset=1,
                                         koku_uuid=provider_uuid)
        sources.save()
        delete_request = self._create_delete_request(self.user)
        with tenant_context(self.tenant):
            manager = ProviderManager(provider_uuid)
            with self.assertRaises(ProviderManagerError):
                manager.remove(delete_request)
Ejemplo n.º 5
0
    def test_remove_ocp_added_via_sources(self, mock_delete_report):
        """Remove ocp provider added via sources."""
        # Create Provider
        provider_authentication = ProviderAuthentication.objects.create(
            provider_resource_name='cluster_id_1001')
        provider = Provider.objects.create(
            name='ocpprovidername',
            created_by=self.user,
            customer=self.customer,
            authentication=provider_authentication,
        )
        provider_uuid = provider.uuid

        sources = Sources.objects.create(source_id=1,
                                         auth_header='testheader',
                                         offset=1,
                                         koku_uuid=provider_uuid)
        sources.save()
        delete_request = self._create_delete_request(
            self.user, {'Sources-Client': 'True'})
        with tenant_context(self.tenant):
            manager = ProviderManager(provider_uuid)
            manager.remove(delete_request)
        provider_query = Provider.objects.all().filter(uuid=provider_uuid)
        self.assertFalse(provider_query)
Ejemplo n.º 6
0
    def test_remove(self):
        """Remove provider."""
        # Create Customer
        customer = None
        serializer = CustomerSerializer(data=self.customer_data[0])
        if serializer.is_valid(raise_exception=True):
            customer = serializer.save()

        # Create Provider
        provider = Provider.objects.create(name='providername', created_by=customer.owner, customer=customer)
        provider_uuid = provider.uuid

        # Create another user for negative tests
        # Add another user
        group = Group.objects.get(name=customer.name)

        new_user_dict = self.gen_user_data()
        user_serializer = UserSerializer(data=new_user_dict)
        other_user = None
        if user_serializer.is_valid(raise_exception=True):
            other_user = user_serializer.save()
        group.user_set.add(other_user)

        manager = ProviderManager(provider_uuid)

        with self.assertRaises(ProviderManagerError):
            self.assertFalse(manager.remove(other_user))

        manager.remove(customer.owner)
        provider_query = Provider.objects.all().filter(uuid=provider_uuid)
        self.assertFalse(provider_query)
Ejemplo n.º 7
0
    def destroy(self, request, *args, **kwargs):
        """Delete a customer.

            @api {delete} /api/v1/customers/:uuid/ Delete a customer
            @apiName DeleteCustomers
            @apiGroup Customer
            @apiVersion 1.0.0
            @apiDescription Delete a customer.

            @apiHeader {String} token Service Admin authorizaton token.
            @apiHeaderExample {json} Header-Example:
                {
                    "Authorizaton": "Token 45138a913da44ab89532bab0352ef84b"
                }

            @apiParam {String} uuid Customer unique ID.

            @apiSuccessExample {json} Success-Response:
                HTTP/1.1 204 NO CONTENT
            """
        user_savepoint = transaction.savepoint()
        customer_manager = None
        try:
            customer_manager = CustomerManager(kwargs['uuid'])
        except CustomerManagerDoesNotExist:
            LOG.error('Unable to find provider for uuid {}.'.format(
                kwargs['uuid']))
            raise NotFound

        providers = ProviderManager.get_providers_queryset_for_customer(
            customer_manager.get_model())

        try:
            customer_manager.remove_users(request.user)
            customer_manager.remove_tenant(request.user)
        except DatabaseError:
            transaction.savepoint_rollback(user_savepoint)
            LOG.error('Failed to remove assets for customer {}.'.format(
                customer_manager.get_name()))
            raise UserDeleteException

        try:
            for provider in providers:
                provider_manager = ProviderManager(provider.uuid)
                provider_manager.remove(request.user,
                                        customer_remove_context=True)
        except (DatabaseError, ProviderManagerError):
            transaction.savepoint_rollback(user_savepoint)
            LOG.error('{} failed to remove provider {}.'.format(
                request.user.username, provider_manager.get_name()))
            raise ProviderDeleteException

        http_response = super().destroy(request=request,
                                        args=args,
                                        kwargs=kwargs)
        if http_response.status_code is not 204:
            transaction.savepoint_rollback(user_savepoint)
        return http_response
Ejemplo n.º 8
0
    def test_remove_aws_auth_billing_remain(self):
        """Remove aws provider."""
        # Create Provider
        credentials = {"role_arn": "arn:aws:iam::2:role/mg"}
        provider_authentication = ProviderAuthentication.objects.create(
            credentials=credentials)

        expected_auth_count = ProviderAuthentication.objects.count()
        credentials = {"role_arn": "arn:aws:iam::3:role/mg"}
        provider_authentication2 = ProviderAuthentication.objects.create(
            credentials=credentials)

        data_source = {"bucket": "my_s3_bucket"}
        provider_billing = ProviderBillingSource.objects.create(
            data_source=data_source)
        expected_billing_count = ProviderBillingSource.objects.count()
        with patch("masu.celery.tasks.check_report_updates"):
            provider = Provider.objects.create(
                name="awsprovidername",
                created_by=self.user,
                customer=self.customer,
                authentication=provider_authentication,
                billing_source=provider_billing,
            )
            provider2 = Provider.objects.create(
                name="awsprovidername2",
                created_by=self.user,
                customer=self.customer,
                authentication=provider_authentication2,
                billing_source=provider_billing,
            )
        provider_uuid = provider2.uuid

        self.assertNotEqual(provider.uuid, provider2.uuid)
        new_user_dict = self._create_user_data()
        request_context = self._create_request_context(self.customer_data,
                                                       new_user_dict,
                                                       False,
                                                       create_user=False)
        user_serializer = UserSerializer(data=new_user_dict,
                                         context=request_context)
        other_user = None
        if user_serializer.is_valid(raise_exception=True):
            other_user = user_serializer.save()

        with tenant_context(self.tenant):
            manager = ProviderManager(provider_uuid)
            manager.remove(self._create_delete_request(other_user))
        auth_count = ProviderAuthentication.objects.count()
        billing_count = ProviderBillingSource.objects.count()
        provider_query = Provider.objects.all().filter(uuid=provider_uuid)

        self.assertFalse(provider_query)
        self.assertEqual(auth_count, expected_auth_count)
        self.assertEqual(billing_count, expected_billing_count)
Ejemplo n.º 9
0
 def destroy_provider(self, provider_uuid):
     """Call to destroy provider."""
     connection.set_schema_to_public()
     _, customer, user = self._create_context()
     tenant = Tenant.objects.get(schema_name=customer.schema_name)
     connection.set_tenant(tenant)
     try:
         manager = ProviderManager(provider_uuid)
     except ProviderManagerError:
         LOG.info("Provider does not exist, skipping Provider delete.")
     else:
         manager.remove(user=user, from_sources=True)
     connection.set_schema_to_public()
Ejemplo n.º 10
0
    def test_remove_all_aws_providers(self):
        """Remove all AWS providers."""
        provider_query = Provider.objects.all().filter(type="AWS-local")

        customer = None
        for provider in provider_query:
            customer = provider.customer
            with tenant_context(provider.customer):
                manager = ProviderManager(provider.uuid)
                manager.remove(self._create_delete_request(self.user, {"Sources-Client": "False"}))
        for view in AWS_MATERIALIZED_VIEWS:
            with tenant_context(customer):
                self.assertFalse(view.objects.count())
Ejemplo n.º 11
0
 def destroy_provider(self, provider_uuid, retry_count=None):
     """Call to destroy provider."""
     connection.set_schema_to_public()
     _, customer, user = self._create_context()
     tenant = self._tenant_for_schema(customer.schema_name)
     connection.set_tenant(tenant)
     try:
         manager = ProviderManager(provider_uuid)
     except ProviderManagerError:
         LOG.info("Provider does not exist, skipping Provider delete.")
     else:
         manager.remove(user=user, from_sources=True, retry_count=retry_count)
         invalidate_view_cache_for_tenant_and_cache_key(customer.schema_name, SOURCES_CACHE_PREFIX)
     connection.set_schema_to_public()
Ejemplo n.º 12
0
    def test_remove_all_ocp_providers(self):
        """Remove all OCP providers."""
        provider_query = Provider.objects.all().filter(type="OCP")

        customer = None
        for provider in provider_query:
            customer = provider.customer
            with tenant_context(provider.customer):
                manager = ProviderManager(provider.uuid)
                manager.remove(
                    self._create_delete_request(self.user,
                                                {"Sources-Client": "False"}))
        for view in OCP_UI_SUMMARY_TABLES:
            with tenant_context(customer):
                model = get_model(view)
                self.assertFalse(model.objects.count())
Ejemplo n.º 13
0
    def test_direct_remove_ocp_added_via_sources(self):
        """Remove ocp provider added via sources directly."""
        # Create Provider
        provider_authentication = ProviderAuthentication.objects.create(provider_resource_name='cluster_id_1001')
        provider = Provider.objects.create(name='ocpprovidername',
                                           created_by=self.user,
                                           customer=self.customer,
                                           authentication=provider_authentication,)
        provider_uuid = provider.uuid

        sources = Sources.objects.create(source_id=1,
                                         auth_header='testheader',
                                         offset=1,
                                         koku_uuid=provider_uuid)
        sources.save()
        delete_request = self._create_delete_request(self.user)
        with tenant_context(self.tenant):
            manager = ProviderManager(provider_uuid)
            with self.assertRaises(ProviderManagerError):
                manager.remove(delete_request)
Ejemplo n.º 14
0
 def test_remove_still_processing(self, mock_is_processing):
     """Test a provider remove while still processing data"""
     mock_is_processing.return_value = True
     provider = Provider.objects.first()
     with tenant_context(self.tenant):
         with self.assertRaises(ProviderProcessingError):
             # Test that we throw an execption instead of deleting
             manager = ProviderManager(str(provider.uuid))
             manager.remove(self._create_delete_request(self.user),
                            from_sources=True,
                            retry_count=0)
             self.assertTrue(
                 Provider.objects.filter(uuid=str(provider.uuid)).exists())
         # Now test that we DO delete after the given number of retries
         manager = ProviderManager(str(provider.uuid))
         manager.remove(self._create_delete_request(self.user),
                        from_sources=True,
                        retry_count=25)
         self.assertFalse(
             Provider.objects.filter(uuid=str(provider.uuid)).exists())
Ejemplo n.º 15
0
    def test_remove(self):
        """Remove provider."""
        # Create Customer
        customer = None
        serializer = CustomerSerializer(data=self.customer_data[0])
        if serializer.is_valid(raise_exception=True):
            customer = serializer.save()

        # Create Provider
        provider_authentication = ProviderAuthentication.objects.create(provider_resource_name='arn:aws:iam::2:role/mg')
        provider_billing = ProviderBillingSource.objects.create(bucket='my_s3_bucket')
        provider = Provider.objects.create(name='providername',
                                           created_by=customer.owner,
                                           customer=customer,
                                           authentication=provider_authentication,
                                           billing_source=provider_billing)
        provider_uuid = provider.uuid

        # Create another user for negative tests
        # Add another user
        group = Group.objects.get(name=customer.name)

        new_user_dict = self.gen_user_data()
        user_serializer = UserSerializer(data=new_user_dict)
        other_user = None
        if user_serializer.is_valid(raise_exception=True):
            other_user = user_serializer.save()
        group.user_set.add(other_user)

        manager = ProviderManager(provider_uuid)

        with self.assertRaises(ProviderManagerError):
            self.assertFalse(manager.remove(other_user))

        manager.remove(customer.owner)
        provider_query = Provider.objects.all().filter(uuid=provider_uuid)
        self.assertFalse(provider_query)
Ejemplo n.º 16
0
    def test_remove_ocp(self, mock_delete_report):
        """Remove ocp provider."""
        # Create Provider
        provider_authentication = ProviderAuthentication.objects.create(provider_resource_name='cluster_id_1001')
        provider = Provider.objects.create(name='ocpprovidername',
                                           created_by=self.user,
                                           customer=self.customer,
                                           authentication=provider_authentication,)
        provider_uuid = provider.uuid

        new_user_dict = self._create_user_data()
        request_context = self._create_request_context(self.customer_data,
                                                       new_user_dict, False)
        user_serializer = UserSerializer(data=new_user_dict, context=request_context)
        other_user = None
        if user_serializer.is_valid(raise_exception=True):
            other_user = user_serializer.save()

        with tenant_context(self.tenant):
            rate = {'provider_uuid': provider.uuid,
                    'metric': Rate.METRIC_CPU_CORE_USAGE_HOUR,
                    'rates': {'tiered_rate': [{
                        'unit': 'USD',
                        'value': 1.0,
                        'usage_start': None,
                        'usage_end': None
                    }]}
                    }

            Rate.objects.create(**rate)
            manager = ProviderManager(provider_uuid)
            manager.remove(other_user)
            rates_query = Rate.objects.all().filter(provider_uuid=provider_uuid)
            self.assertFalse(rates_query)
        provider_query = Provider.objects.all().filter(uuid=provider_uuid)
        self.assertFalse(provider_query)