def test_schema_name_present_on_customer(self): """Test that schema_name is returned on customer.""" iam_arn = 'arn:aws:s3:::my_s3_bucket' bucket_name = 'my_s3_bucket' provider = { 'name': 'test_provider', 'type': Provider.PROVIDER_AWS, 'authentication': { 'provider_resource_name': iam_arn }, 'billing_source': { 'bucket': bucket_name } } with patch.object(ProviderAccessor, 'cost_usage_source_ready', returns=True): serializer = AdminProviderSerializer(data=provider, context=self.request_context) if serializer.is_valid(raise_exception=True): serializer.save() account = self.customer.account_id expected_schema_name = create_schema_name(account) schema_name = serializer.data['customer'].get('schema_name') self.assertIsNotNone(schema_name) self.assertEqual(schema_name, expected_schema_name)
def _create_customer(account): """Create a customer. Args: account (str): The account identifier Returns: (Customer) The created customer """ try: with transaction.atomic(): schema_name = create_schema_name(account) customer = Customer(account_id=account, schema_name=schema_name) customer.save() tenant = Tenant(schema_name=schema_name) tenant.save() unique_account_counter.inc() logger.info('Created new customer from account_id %s.', account) except IntegrityError: customer = Customer.objects.filter(account_id=account).get() return customer
def test_schema_name_present_on_customer(self): """Test that schema_name is returned on customer.""" iam_arn = "arn:aws:s3:::my_s3_bucket" bucket_name = "my_s3_bucket" provider = { "name": "test_provider", "type": Provider.PROVIDER_AWS.lower(), "authentication": { "credentials": { "role_arn": iam_arn } }, "billing_source": { "data_source": { "bucket": bucket_name } }, } with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): serializer = AdminProviderSerializer(data=provider, context=self.request_context) if serializer.is_valid(raise_exception=True): serializer.save() account = self.customer.account_id expected_schema_name = create_schema_name(account) schema_name = serializer.data["customer"].get("schema_name") self.assertIsNotNone(schema_name) self.assertEqual(schema_name, expected_schema_name)
def test_schema_name_present(self): """Test that the serializer contains schema_name.""" serializer = AdminCustomerSerializer(data=self._create_customer_data()) serializer.is_valid() serializer.save() expected_schema_name = create_schema_name(serializer.data.get('account_id')) schema_name = serializer.data.get('schema_name') self.assertIsNotNone(schema_name) self.assertEqual(schema_name, expected_schema_name)
def setUp(self): """Set up middleware tests.""" super().setUp() self.user_data = self._create_user_data() self.customer = self._create_customer_data() self.schema_name = create_schema_name(self.customer['account_id']) self.request_context = self._create_request_context( self.customer, self.user_data, create_customer=False) self.request = self.request_context['request'] self.request.path = '/api/v1/providers/' self.request.META['QUERY_STRING'] = ''
def stats(self, request, pk=None): """Get source stats.""" account_id = request.user.customer.account_id schema_name = create_schema_name(account_id) source = self.get_object() stats = {} try: manager = ProviderManager(source.source_uuid) except ProviderManagerError: stats["provider_linked"] = False else: stats["provider_linked"] = True tenant = Tenant.objects.get(schema_name=schema_name) stats.update(manager.provider_statistics(tenant)) return Response(stats)
def setUp(self): """Set up middleware tests.""" super().setUp() self.user_data = self._create_user_data() self.customer = self._create_customer_data() self.schema_name = create_schema_name(self.customer['account_id']) self.request_context = self._create_request_context( self.customer, self.user_data) request = self.request_context['request'] request.path = '/api/v1/providers/' serializer = UserSerializer(data=self.user_data, context=self.request_context) if serializer.is_valid(raise_exception=True): user = serializer.save() request.user = user
def _create_customer(cls, account, create_tenant=False): """Create a customer. Args: account (str): The account identifier Returns: (Customer) The created customer """ connection.set_schema_to_public() schema_name = create_schema_name(account) customer = Customer.objects.get_or_create(account_id=account, schema_name=schema_name)[0] customer.save() if create_tenant: tenant = Tenant.objects.get_or_create(schema_name=schema_name)[0] tenant.save() return customer
def _create_customer(account): """Create a customer. Args: account (str): The account identifier Returns: (Customer) The created customer """ schema_name = create_schema_name(account) customer = Customer(account_id=account, schema_name=schema_name) customer.save() tenant = Tenant(schema_name=schema_name) tenant.save() unique_account_counter.inc() logger.info('Created new customer from account_id %s.', account) return customer
def create_customer(account): """Create a customer. Args: account (str): The account identifier Returns: (Customer) The created customer """ try: with transaction.atomic(): schema_name = create_schema_name(account) customer = Customer(account_id=account, schema_name=schema_name) customer.save() UNIQUE_ACCOUNT_COUNTER.inc() LOG.info("Created new customer from account_id %s.", account) except IntegrityError: customer = Customer.objects.filter(account_id=account).get() return customer
def _get_account_and_tenant(self, request): """Get account_id and tenant from request.""" account_id = request.user.customer.account_id schema_name = create_schema_name(account_id) tenant = tenant = Tenant.objects.get(schema_name=schema_name) return (account_id, tenant)