Commit 5c996046 authored by Lukas Burgey's avatar Lukas Burgey

Change the site field of services to not nullable

parent 71faf4ec
# Generated by Django 2.1.3 on 2018-11-21 10:49
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0024_auto_20181120_1858'),
]
operations = [
migrations.RemoveField(
model_name='service',
name='site',
),
migrations.AddField(
model_name='service',
name='site',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='services', to='backend.Site'),
preserve_default=False,
),
]
......@@ -88,16 +88,17 @@ class Service(models.Model):
unique=True,
)
site = models.ForeignKey(
Site,
related_name='services',
on_delete=models.CASCADE,
)
description = models.TextField(
max_length=300,
blank=True,
)
site = models.ManyToManyField(
Site,
related_name='services',
)
vos = models.ManyToManyField(
VO,
related_name='services',
......@@ -105,22 +106,28 @@ class Service(models.Model):
)
@classmethod
def get_service(cls, name, description='', sites=None, vos=None):
def get_service(cls, name, site, description='', vos=[]):
try:
return cls.objects.get(name=name)
service = cls.objects.get(
name=name,
)
if service.description != description:
service.description = description
service.save()
return service
except cls.DoesNotExist:
service = cls(
name=name,
site=site,
description=description,
)
service.save()
if sites is not None:
for site in sites:
service.site.add(site)
if vos is not None:
for vo in vos:
service.vos.add(vos)
for vo in vos:
service.vos.add(vo)
return service
......@@ -196,36 +203,26 @@ class Deployment(models.Model):
def services(self):
return self.vo.services.all()
@property
def sites(self):
return [
service.site
for service in self.services
]
def create_state_items(self):
for service in self.services:
LOGGER.debug('create_state_items: creating DeploymentState for service %s at sites %s', service, service.site.all())
# if not service.site.exists():
# raise ValueError('Cannot create state item for service without site')
for service_site in service.site.all():
# LOGGER.debug('create_state_items: creating DeploymentStates for service %s at site %s', service, service_site)
DeploymentState.get_state_item(
parent=self,
user=self.user,
site=service_site,
service=service,
).save()
LOGGER.debug('create_state_items: creating DeploymentState for service %s at site %s', service, service.site)
DeploymentState.get_state_item(
parent=self,
user=self.user,
site=service.site,
service=service,
)
@classmethod
def get_deployment(cls, user, vo):
try:
return cls.objects.get(
deployment = cls.objects.get(
user=user,
vo=vo,
)
deployment.create_state_items()
return deployment
except cls.DoesNotExist:
deployment = cls(
......@@ -268,16 +265,13 @@ class Deployment(models.Model):
def service_added(self, service):
LOGGER.debug(self.msg('Adding service {}'.format(service)))
for site in service.site.all():
# create new DeploymentStates
item = DeploymentState.get_state_item(
parent=self,
site=site,
service=service,
)
item.save()
if self.state_target == 'deployed':
item.user_deploy()
item = DeploymentState.get_state_item(
parent=self,
site=service.site,
service=service,
)
if self.state_target == 'deployed':
item.user_deploy()
def publish_to_client(self):
# avoiding circular dependencies here
......@@ -402,7 +396,6 @@ class DeploymentState(models.Model):
site=site,
service=service,
)
# LOGGER.debug('get_state_item: item already exists')
return item
except cls.DoesNotExist:
......@@ -422,7 +415,7 @@ class DeploymentState(models.Model):
if settings.DEBUG_CREDENTIALS:
LOGGER.debug('user_credential_added: %s %s', self, credential)
credential_state = CredentialState.get_credential_state(
CredentialState.get_credential_state(
credential,
self,
)
......
......@@ -16,7 +16,7 @@ class SiteSerializer(serializers.ModelSerializer):
class ServiceSerializer(serializers.ModelSerializer):
site = SiteSerializer(many=True)
site = SiteSerializer()
vos = VOSerializer(many=True)
class Meta:
......@@ -54,7 +54,6 @@ class DeploymentStateSerializer(serializers.ModelSerializer):
class DeploymentSerializer(serializers.ModelSerializer):
sites = SiteSerializer(many=True)
services = ServiceSerializer(many=True)
state_items = DeploymentStateSerializer(many=True)
......@@ -66,7 +65,6 @@ class DeploymentSerializer(serializers.ModelSerializer):
'vo',
'id',
'state_items',
'sites',
]
......
......@@ -80,35 +80,28 @@ def setup_fixture():
site2.save()
service = models.Service.get_service(
name=TEST_SERVICE,
sites=[site],
TEST_SERVICE,
site,
)
service.save()
service2 = models.Service.get_service(
name=TEST_SERVICE_2,
TEST_SERVICE_2,
site,
vos=[
group_one,
group_mult,
],
sites=[site],
)
service2.save()
service3 = models.Service.get_service(
name=TEST_SERVICE_3,
TEST_SERVICE_3,
site,
vos=[group_mult],
sites=[site],
)
service3.save()
# broken, because it has no sites
broken_service = models.Service.get_service(
name=BROKEN_SERVICE,
vos=[group_for_broken],
)
broken_service.save()
models.SSHPublicKey(
name=TEST_NAME,
key=TEST_KEY,
......@@ -136,15 +129,15 @@ class DeploymentTest(TestCase):
delayed_service.save()
delayed_service.site.add(models.Site.objects.get(name=TEST_SITE))
#def setUp(self):
# setup_fixture()
# delayed_service = models.Service(name='DELAYED_SERVICE')
# delayed_service.save()
# delayed_service.site.add(models.Site.objects.get(name=TEST_SITE))
# def setUp(self):
# setup_fixture()
# delayed_service = models.Service(name='DELAYED_SERVICE')
# delayed_service.save()
# delayed_service.site.add(models.Site.objects.get(name=TEST_SITE))
#def tearDown(self):
# for svc in models.Service.objects.(name='DELAYED_SERVICE').delete()
# #teardown_fixture()
# def tearDown(self):
# for svc in models.Service.objects.(name='DELAYED_SERVICE').delete()
# #teardown_fixture()
def deployment_run(self, deployment, service_count):
self.assertIsNotNone(deployment)
......@@ -283,12 +276,3 @@ class DeploymentTest(TestCase):
group=group,
)
self.deployment_run(deployment, 2)
def test_broken_service(self):
user = models.User.objects.get(username=TEST_NAME)
service = models.Service.objects.get(name=BROKEN_SERVICE)
with self.assertRaises(ValueError):
models.Deployment.get_deployment(
user,
service=service,
)
......@@ -87,34 +87,23 @@ class ConfigurationView(views.APIView):
for group_service in group_service_list:
name = group_service.get('name', None)
if name is None:
LOGGER.error('Client pushed invalid service: %s', group_service)
continue
description = group_service.get('description', None)
service = None
service = models.Service.get_service(
name,
site,
description=description,
)
# add group to the services VOs
try:
service = models.Service.objects.get(name=name)
# update description if needed
if service.description != description:
LOGGER.info("Updating service description")
service.description = description
service.save()
# test if service has the group
try:
service.vos.get(name=group_name)
except VO.DoesNotExist:
service.vos.add(group)
except models.Service.DoesNotExist:
LOGGER.info("Site %s pushed new service %s", site, name)
service = models.Service(
name=name,
description=description,
)
service.save()
service.vos.get(name=group_name)
except VO.DoesNotExist:
service.vos.add(group)
service.site.add(site)
service.handle_vo_deployments()
@staticmethod
def handle_entitlement_to_services(site, entitlement_to_services):
......@@ -125,34 +114,23 @@ class ConfigurationView(views.APIView):
for entitlement_service in entitlement_service_list:
name = entitlement_service.get('name', None)
description = entitlement_service.get('description', None)
if name is None:
LOGGER.error('Client pushed invalid service: %s', entitlement_service)
continue
description = entitlement_service.get('description', '')
service = models.Service.get_service(
name,
site,
description=description,
)
service = None
# add entitlement to the services VOs
try:
service = models.Service.objects.get(name=name)
# update description if needed
if service.description != description:
LOGGER.info("Updating service description")
service.description = description
service.save()
# test if service has the entitlement
try:
service.vos.get(name=entitlement_name)
except VO.DoesNotExist:
service.vos.add(entitlement)
except models.Service.DoesNotExist:
LOGGER.info("Site %s pushed new service %s", site, name)
service = models.Service(
name=name,
description=description,
)
service.save()
service.vos.get(name=entitlement_name)
except VO.DoesNotExist:
service.vos.add(entitlement)
service.site.add(site)
service.handle_vo_deployments()
def put(self, request):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment