diff --git a/src/ralph/util/api_pricing.py b/src/ralph/util/api_pricing.py index 5eb7561c7d..4523d7ffa6 100644 --- a/src/ralph/util/api_pricing.py +++ b/src/ralph/util/api_pricing.py @@ -125,6 +125,7 @@ def get_virtual_usages(parent_venture_name=None): yield { 'name': device.name, 'device_id': device.id, + 'venture_id': device.venture_id, 'service_ci_uid': device.service.uid if device.service else None, 'environment': device.device_environment, 'hypervisor_id': device.parent.id, @@ -383,12 +384,17 @@ def get_services(): """ service_type = CIType.objects.get(name='Service') profit_center_type = CIType.objects.get(name='ProfitCenter') + environment_type = CIType.objects.get(name='Environment') for service in CI.objects.filter( type=service_type ).select_related('relations'): profit_center = service.child.filter( parent__type=profit_center_type ).values_list('parent__uid', flat=True) + # TODO: verify relation + environments = service.parent.filter( + child__type=environment_type, + ) yield { 'ci_uid': service.uid, 'name': service.name, @@ -401,4 +407,5 @@ def get_services(): 'id', flat=True, )), + 'environments': [e.child.id for e in environments] } diff --git a/src/ralph/util/tests/tests.py b/src/ralph/util/tests/tests.py index a5af9daa44..7e750b39f6 100644 --- a/src/ralph/util/tests/tests.py +++ b/src/ralph/util/tests/tests.py @@ -215,7 +215,7 @@ def test_get_owners(self): def test_get_services(self): service = utils.ServiceFactory() profit_center = utils.ProfitCenterFactory() - utils.ServiceBusinessLineRelationFactory( + utils.ServiceProfitCenterRelationFactory( parent=profit_center, child=service, ) @@ -229,6 +229,10 @@ def test_get_services(self): ci=service, type=models_ci.CIOwnershipType.technical, ) + environments = utils.ServiceEnvironmentRelationFactory.create_batch( + 2, + parent=service, + ) result = [a for a in api_pricing.get_services()] service_dict = { 'name': service.name, @@ -236,6 +240,7 @@ def test_get_services(self): 'profit_center': profit_center.uid, 'business_owners': [bo.owner.id for bo in business_ownership], 'technical_owners': [to.owner.id for to in technical_ownership], + 'environments': [e.child.id for e in environments], } self.assertEquals(result, [service_dict]) @@ -248,6 +253,7 @@ def test_get_services_without_profit_center(self): 'profit_center': None, 'technical_owners': [], 'business_owners': [], + 'environments': [], } self.assertEquals(result, [service_dict]) diff --git a/src/ralph/util/tests/utils.py b/src/ralph/util/tests/utils.py index 789186ca97..de892580c7 100644 --- a/src/ralph/util/tests/utils.py +++ b/src/ralph/util/tests/utils.py @@ -31,6 +31,12 @@ def type(self): return models_ci.CIType.objects.get(name='Service') +class EnvironmentFactory(CIFactory): + @factory.lazy_attribute + def type(self): + return models_ci.CIType.objects.get(name='Environment') + + class CIOwnerFactory(DjangoModelFactory): FACTORY_FOR = models_ci.CIOwner first_name = 'Some' @@ -45,9 +51,17 @@ class ServiceOwnershipFactory(DjangoModelFactory): ci = factory.SubFactory(ServiceFactory) -class ServiceBusinessLineRelationFactory(DjangoModelFactory): +class ServiceEnvironmentRelationFactory(DjangoModelFactory): + FACTORY_FOR = models_ci.CIRelation + parent = factory.SubFactory(ServiceFactory) + child = factory.SubFactory(EnvironmentFactory) + type = models_ci.CI_RELATION_TYPES.HASROLE + readonly = False + + +class ServiceProfitCenterRelationFactory(DjangoModelFactory): FACTORY_FOR = models_ci.CIRelation - parent = factory.SubFactory(BusinessLineFactory) + parent = factory.SubFactory(ProfitCenterFactory) child = factory.SubFactory(ServiceFactory) type = models_ci.CI_RELATION_TYPES.CONTAINS readonly = False