mirror of
https://github.com/ansible/awx.git
synced 2026-05-24 00:57:48 -02:30
Look for and report on transaction errors within our implicit RBAC fields
When a transaction is in a failed state these fields will not be able to create new role/resource entries. This check just makes it easier to see what's going on and aids in debugging.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
# All Rights Reserved.
|
# All Rights Reserved.
|
||||||
|
|
||||||
# Django
|
# Django
|
||||||
|
from django.db import connection
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.db.models.signals import m2m_changed
|
from django.db.models.signals import m2m_changed
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@@ -14,6 +15,7 @@ from django.db.models.fields.related import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from django.core.exceptions import FieldError
|
from django.core.exceptions import FieldError
|
||||||
|
from django.db.transaction import TransactionManagementError
|
||||||
|
|
||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
@@ -63,6 +65,8 @@ class ResourceFieldDescriptor(ReverseSingleRelatedObjectDescriptor):
|
|||||||
resource = super(ResourceFieldDescriptor, self).__get__(instance, instance_type)
|
resource = super(ResourceFieldDescriptor, self).__get__(instance, instance_type)
|
||||||
if resource:
|
if resource:
|
||||||
return resource
|
return resource
|
||||||
|
if connection.needs_rollback:
|
||||||
|
raise TransactionManagementError('Current transaction has failed, cannot create implicit resource')
|
||||||
resource = Resource.objects.create(content_object=instance)
|
resource = Resource.objects.create(content_object=instance)
|
||||||
setattr(instance, self.field.name, resource)
|
setattr(instance, self.field.name, resource)
|
||||||
instance.save(update_fields=[self.field.name,])
|
instance.save(update_fields=[self.field.name,])
|
||||||
@@ -107,6 +111,9 @@ class ImplicitRoleDescriptor(ReverseSingleRelatedObjectDescriptor):
|
|||||||
if not self.role_name:
|
if not self.role_name:
|
||||||
raise FieldError('Implicit role missing `role_name`')
|
raise FieldError('Implicit role missing `role_name`')
|
||||||
|
|
||||||
|
if connection.needs_rollback:
|
||||||
|
raise TransactionManagementError('Current transaction has failed, cannot create implicit role')
|
||||||
|
|
||||||
role = Role.objects.create(name=self.role_name, content_object=instance)
|
role = Role.objects.create(name=self.role_name, content_object=instance)
|
||||||
if self.parent_role:
|
if self.parent_role:
|
||||||
def resolve_field(obj, field):
|
def resolve_field(obj, field):
|
||||||
|
|||||||
Reference in New Issue
Block a user