Example #1
0
    def test_add_subscription_id_to_credentials_non_existent(self):
        """Test to add subscription_id to a non-existent Source."""
        test_source_id = 4
        subscription_id = 'test_sub_id'

        with self.assertRaises(SourcesStorageError):
            storage.add_subscription_id_to_credentials({'source_id': test_source_id}, subscription_id)
Example #2
0
    def test_add_subscription_id_to_credentials(self):
        """Test to add subscription_id to AZURE credentials."""
        test_source_id = 2
        subscription_id = 'test_sub_id'
        azure_obj = Sources(source_id=test_source_id,
                            auth_header=self.test_header,
                            offset=2,
                            source_type='AZURE',
                            name='Test Azure Source',
                            authentication={
                                'credentials': {
                                    'client_id': 'test_client',
                                    'tenant_id': 'test_tenant',
                                    'client_secret': 'test_secret'
                                }
                            },
                            billing_source={
                                'data_source': {
                                    'resource_group': 'RG1',
                                    'storage_account': 'test_storage'
                                }
                            })
        azure_obj.save()
        storage.add_subscription_id_to_credentials(test_source_id,
                                                   subscription_id)

        response_obj = Sources.objects.get(source_id=test_source_id)
        self.assertEqual(
            response_obj.authentication.get('credentials').get(
                'subscription_id'), subscription_id)
Example #3
0
    def test_add_subscription_id_to_credentials_non_azure(self):
        """Test to add subscription_id to a non-AZURE credentials."""
        test_source_id = 3
        subscription_id = 'test_sub_id'
        ocp_obj = Sources(source_id=test_source_id,
                          auth_header=self.test_header,
                          offset=3,
                          source_type='AWS',
                          name='Test AWS Source',
                          authentication={'resource_name': 'arn:test'},
                          billing_source={'bucket': 'test-bucket'})
        ocp_obj.save()

        with self.assertRaises(SourcesStorageError):
            storage.add_subscription_id_to_credentials({'source_id': test_source_id}, subscription_id)
Example #4
0
    def test_add_subscription_id_to_credentials_malformed_cred(self):
        """Test to add subscription_id to with a malformed authentication structure."""
        test_source_id = 3
        subscription_id = 'test_sub_id'
        azure_obj = Sources(source_id=test_source_id,
                            auth_header=self.test_header,
                            offset=3,
                            source_type='AZURE',
                            name='Test AZURE Source',
                            authentication={},
                            billing_source={'billing_source': {'data_source': {'resource_group': 'foo',
                                                                               'storage_account': 'bar'}}})
        azure_obj.save()

        with self.assertRaises(SourcesStorageError):
            storage.add_subscription_id_to_credentials({'source_id': test_source_id}, subscription_id)
Example #5
0
def authentication(request):
    """Create Subscription-ID for Azure authentication."""
    request_data = request.data

    try:
        if request_data.get('credentials'):
            subscription_id = request_data.get('credentials').get('subscription_id')
            if subscription_id:
                add_subscription_id_to_credentials(request_data, subscription_id)
            else:
                raise SourcesStorageError('Subscription ID not found')
        else:
            raise SourcesStorageError('Malformed JSON data.')
        response = request_data
        status_code = status.HTTP_201_CREATED
    except SourcesStorageError as error:
        response = str(error)
        status_code = status.HTTP_400_BAD_REQUEST
    return Response(response, status=status_code)