Skip to content

Commit

Permalink
Deleted objects no longer have an ansible_id
Browse files Browse the repository at this point in the history
So we have to pull that out before we delete.

Signed-off-by: Rick Elrod <[email protected]>
  • Loading branch information
relrod committed Aug 7, 2024
1 parent 2b73d12 commit 4918979
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ansible_base/resource_registry/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ def save(self, *args, _original_save=cls._original_save, **kwargs):

# Avoid late binding issues
def delete(self, *args, _original_delete=cls._original_delete, **kwargs):
object_ansible_id = self.resource.ansible_id
with ensure_transaction():
_original_delete(self, *args, **kwargs)
sync_to_resource_server(self, "delete")
sync_to_resource_server(self, "delete", ansible_id=object_ansible_id)

cls.delete = delete

Expand Down
25 changes: 22 additions & 3 deletions ansible_base/resource_registry/utils/sync_to_resource_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,31 @@
logger = logging.getLogger('ansible_base.resource_registry.utils.sync_to_resource_server')


def sync_to_resource_server(instance, action):
def sync_to_resource_server(instance, action, ansible_id=None):
"""
Use the resource server API to sync the resource across.
When action is "delete", the ansible_id is required, because by the time we
get here, we've already deleted the object and its resource. So we can't
pull the ansible_id from the resource object. It's on the caller to pull
the ansible_id from the object before deleting it.
For all other actions, ansible_id is ignored and retrieved from the resource
object. (For create, the resource is expected to exist before calling this
function.)
"""
try:
if not getattr(instance, 'resource', None) or not instance.resource.ansible_id:
if action != "delete" and ansible_id is not None:
raise Exception("ansible_id should not be provided for create/update actions")
elif action == "delete" and ansible_id is None:
raise Exception("ansible_id should be provided for delete actions")
elif not getattr(instance, 'resource', None) or not instance.resource.ansible_id:
# We can't sync if we don't have a resource and an ansible_id.
logger.error(f"Resource {instance} does not have a resource or ansible_id")
return
except Resource.DoesNotExist:
# The getattr() will raise a Resource.DoesNotExist if the resource doesn't exist.
logger.error(f"Resource {instance} does not have a resource")
return

user_ansible_id = None
Expand All @@ -32,15 +47,19 @@ def sync_to_resource_server(instance, action):
try:
user_ansible_id = user.resource.ansible_id
except AttributeError:
logger.error(f"User {user} does not have a resource")
pass
else:
logger.error("No user found, syncing to resource server with jwt_user_id=None")

client = get_resource_server_client(
settings.RESOURCE_SERVICE_PATH,
jwt_user_id=user_ansible_id,
raise_if_bad_request=True,
)

ansible_id = instance.resource.ansible_id
if action != "delete":
ansible_id = instance.resource.ansible_id

resource_type = instance.resource.content_type.resource_type
data = resource_type.serializer_class(instance).data
Expand Down

0 comments on commit 4918979

Please sign in to comment.