From cd413ee4b9fb76cc962eb8f243bdf95fa4b22144 Mon Sep 17 00:00:00 2001
From: Karel van Klink <karel.vanklink@geant.org>
Date: Tue, 23 Apr 2024 12:16:54 +0200
Subject: [PATCH] add import site workflow

---
 ...e_add_site_modification_import_workflow.py | 39 +++++++++++++++++++
 gso/translations/en-GB.json                   |  3 +-
 gso/workflows/__init__.py                     |  1 +
 gso/workflows/site/import_site.py             | 27 +++++++++++++
 4 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 gso/migrations/versions/2024-04-23_8ebfc8a34c2e_add_site_modification_import_workflow.py
 create mode 100644 gso/workflows/site/import_site.py

diff --git a/gso/migrations/versions/2024-04-23_8ebfc8a34c2e_add_site_modification_import_workflow.py b/gso/migrations/versions/2024-04-23_8ebfc8a34c2e_add_site_modification_import_workflow.py
new file mode 100644
index 00000000..f1802a3f
--- /dev/null
+++ b/gso/migrations/versions/2024-04-23_8ebfc8a34c2e_add_site_modification_import_workflow.py
@@ -0,0 +1,39 @@
+"""Add site modification import workflow.
+
+Revision ID: 8ebfc8a34c2e
+Revises: ab8d805d27b3
+Create Date: 2024-04-23 11:32:39.502729
+
+"""
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision = '8ebfc8a34c2e'
+down_revision = 'ab8d805d27b3'
+branch_labels = None
+depends_on = None
+
+
+from orchestrator.migrations.helpers import create_workflow, delete_workflow
+
+new_workflows = [
+    {
+        "name": "import_site",
+        "target": "MODIFY",
+        "description": "Import Site",
+        "product_type": "ImportedSite"
+    }
+]
+
+
+def upgrade() -> None:
+    conn = op.get_bind()
+    for workflow in new_workflows:
+        create_workflow(conn, workflow)
+
+
+def downgrade() -> None:
+    conn = op.get_bind()
+    for workflow in new_workflows:
+        delete_workflow(conn, workflow["name"])
diff --git a/gso/translations/en-GB.json b/gso/translations/en-GB.json
index 2ef5f1c0..e2b50e04 100644
--- a/gso/translations/en-GB.json
+++ b/gso/translations/en-GB.json
@@ -50,6 +50,7 @@
         "create_imported_router": "NOT FOR HUMANS -- Import existing router",
         "create_imported_iptrunk": "NOT FOR HUMANS -- Import existing IP trunk",
         "create_imported_super_pop_switch": "NOT FOR HUMANS -- Import existing super PoP switch",
-        "create_imported_office_router": "NOT FOR HUMANS -- Import existing office router"
+        "create_imported_office_router": "NOT FOR HUMANS -- Import existing office router",
+        "import_site": "Finalize import into a Site product"
     }
 }
diff --git a/gso/workflows/__init__.py b/gso/workflows/__init__.py
index 5e76a43d..535f6e1f 100644
--- a/gso/workflows/__init__.py
+++ b/gso/workflows/__init__.py
@@ -41,6 +41,7 @@ LazyWorkflowInstance("gso.workflows.site.create_site", "create_site")
 LazyWorkflowInstance("gso.workflows.site.modify_site", "modify_site")
 LazyWorkflowInstance("gso.workflows.site.terminate_site", "terminate_site")
 LazyWorkflowInstance("gso.workflows.site.create_imported_site", "create_imported_site")
+LazyWorkflowInstance("gso.workflows.site.import_site", "import_site")
 LazyWorkflowInstance("gso.workflows.router.create_imported_router", "create_imported_router")
 LazyWorkflowInstance("gso.workflows.iptrunk.create_imported_iptrunk", "create_imported_iptrunk")
 LazyWorkflowInstance(
diff --git a/gso/workflows/site/import_site.py b/gso/workflows/site/import_site.py
new file mode 100644
index 00000000..b49e8a06
--- /dev/null
+++ b/gso/workflows/site/import_site.py
@@ -0,0 +1,27 @@
+"""A modification workflow for setting a new :term:`ISIS` metric for an IP trunk."""
+
+from orchestrator.targets import Target
+from orchestrator.types import State, UUIDstr
+from orchestrator.workflow import StepList, done, init, step, workflow
+from orchestrator.workflows.steps import resync, store_process_subscription, unsync
+from orchestrator.workflows.utils import wrap_modify_initial_input_form
+
+from gso.products import ProductName
+from gso.products.product_types.site import ImportedSite, Site
+from gso.services.subscriptions import get_product_id_by_name
+
+
+@step("Create new site subscription")
+def import_site_subscription(subscription_id: UUIDstr) -> State:
+    """Take an ImportedSite subscription, and turn it into a Site subscription."""
+    old_site = ImportedSite.from_subscription(subscription_id)
+    new_subscription_id = get_product_id_by_name(ProductName.SITE)
+    new_subscription = Site.from_other_product(old_site, new_subscription_id)
+
+    return {"subscription": new_subscription}
+
+
+@workflow("Import Site", target=Target.MODIFY, initial_input_form=wrap_modify_initial_input_form(None))
+def import_site() -> StepList:
+    """Modify an ImportedSite subscription into a Site subscription to complete the import."""
+    return init >> store_process_subscription(Target.MODIFY) >> unsync >> import_site_subscription >> resync >> done
-- 
GitLab