Skip to content

Commit

Permalink
Merge pull request #1059 from mkurek/feature/scrooge-api-services-env…
Browse files Browse the repository at this point in the history
…ironments

added environments to service in pricing api; revert venture_id in virtual usages api
  • Loading branch information
xor-xor committed Sep 9, 2014
2 parents 0a8e0ca + ae5d25d commit b2d6314
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/ralph/util/api_pricing.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -401,4 +407,5 @@ def get_services():
'id',
flat=True,
)),
'environments': [e.child.id for e in environments]
}
8 changes: 7 additions & 1 deletion src/ralph/util/tests/tests.py
Expand Up @@ -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,
)
Expand All @@ -229,13 +229,18 @@ 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,
'ci_uid': service.uid,
'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])

Expand All @@ -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])

Expand Down
18 changes: 16 additions & 2 deletions src/ralph/util/tests/utils.py
Expand Up @@ -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'
Expand All @@ -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

0 comments on commit b2d6314

Please sign in to comment.