From c17dc8c6ade6c9e0b102c831529b0ffc2641b6cd Mon Sep 17 00:00:00 2001 From: Doug Goldstein Date: Wed, 13 Nov 2024 13:11:09 -0600 Subject: [PATCH] fix(workflows): create tenant in nautobot if it doesn't exist When updating a tenant that doesn't exist in Nautobot, just create it instead to get it in sync. --- .../understack_workflows/main/sync_keystone.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/python/understack-workflows/understack_workflows/main/sync_keystone.py b/python/understack-workflows/understack_workflows/main/sync_keystone.py index a18865a2..b38c5810 100644 --- a/python/understack-workflows/understack_workflows/main/sync_keystone.py +++ b/python/understack-workflows/understack_workflows/main/sync_keystone.py @@ -77,10 +77,20 @@ def handle_project_create(conn: Connection, nautobot: Nautobot, project_id: uuid def handle_project_update(conn: Connection, nautobot: Nautobot, project_id: uuid.UUID): logger.info(f"got request to update tenant {project_id!s}") project = conn.identity.get_project(project_id.hex) # type: ignore - ten = nautobot.session.tenancy.tenants.get(project_id) - ten.description = project.description # type: ignore - ten.save() # type: ignore - logger.info(f"tenant '{project_id!s}' last updated {ten.last_updated}") # type: ignore + tenant_api = nautobot.session.tenancy.tenants + + existing_tenant = tenant_api.get(project_id) + if existing_tenant is None: + new_tenant = tenant_api.create( + id=str(project_id), name=project.name, description=project.description + ) + logger.info(f"tenant '{project_id!s}' created {new_tenant.created}") # type: ignore + else: + existing_tenant.description = project.description # type: ignore + existing_tenant.save() # type: ignore + logger.info( + f"tenant '{project_id!s}' last updated {existing_tenant.last_updated}" # type: ignore + ) def handle_project_delete(conn: Connection, nautobot: Nautobot, project_id: uuid.UUID):