From a122821948a542df63fe1f1f4e82813f1d67f181 Mon Sep 17 00:00:00 2001 From: Neda Moeini <neda.moeini@ga0479-nmoeini.home> Date: Tue, 22 Aug 2023 23:34:00 +0200 Subject: [PATCH] Improved codebase. --- .gitlab-ci.yml | 6 +++--- gso/services/subscriptions.py | 30 ++++++++++++++++++++++++++++ gso/workflows/tasks/import_router.py | 25 ++--------------------- test/conftest.py | 2 +- test/test_imports.py | 15 +++++++------- 5 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 gso/services/subscriptions.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3ed25c8b..4aa7c821 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,9 +20,9 @@ run-tox-pipeline: # only cache local items. variables: POSTGRES_DB: gso-test-db - POSTGRES_USER: gso - POSTGRES_PASSWORD: gso - DATABASE_URI_TEST: 'postgresql://gso:gso@postgres:5432/gso-test-db' + POSTGRES_USER: nwa + POSTGRES_PASSWORD: nwa + DATABASE_URI_TEST: 'postgresql://nwa:nwa@postgres:5432/gso-test-db' PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip" # Pip's cache doesn't store the python packages diff --git a/gso/services/subscriptions.py b/gso/services/subscriptions.py new file mode 100644 index 00000000..3cfbd16c --- /dev/null +++ b/gso/services/subscriptions.py @@ -0,0 +1,30 @@ +from orchestrator.db import ( + ProductTable, + ResourceTypeTable, + SubscriptionInstanceTable, + SubscriptionInstanceValueTable, + SubscriptionTable, +) + +from gso.products.product_types.site import Site + + +def get_site_by_name(site_name: str) -> Site: + """Get a site by its name. + + Args: + ---- + site_name (str): The name of the site. + """ + subscription = ( + SubscriptionTable.query.join( + ProductTable, SubscriptionInstanceTable, SubscriptionInstanceValueTable, ResourceTypeTable + ) + .filter(SubscriptionInstanceValueTable.value == site_name) + .filter(ResourceTypeTable.resource_type == "site_name") + .filter(SubscriptionTable.status == "active") + .first() + ) + if not subscription: + raise ValueError(f"Site with name {site_name} not found.") + return Site.from_subscription(subscription.subscription_id) diff --git a/gso/workflows/tasks/import_router.py b/gso/workflows/tasks/import_router.py index 547f8c8a..30f56a53 100644 --- a/gso/workflows/tasks/import_router.py +++ b/gso/workflows/tasks/import_router.py @@ -3,13 +3,7 @@ from typing import Optional from uuid import UUID from orchestrator import workflow -from orchestrator.db import ( - ProductTable, - ResourceTypeTable, - SubscriptionInstanceTable, - SubscriptionInstanceValueTable, - SubscriptionTable, -) +from orchestrator.db import ProductTable from orchestrator.forms import FormPage from orchestrator.targets import Target from orchestrator.types import FormGenerator, State, SubscriptionLifecycle @@ -20,8 +14,8 @@ from gso.products.product_blocks import router as router_pb from gso.products.product_blocks.router import RouterRole, RouterVendor from gso.products.product_types import router from gso.products.product_types.router import RouterInactive -from gso.products.product_types.site import Site from gso.services.crm import get_customer_by_name +from gso.services.subscriptions import get_site_by_name @step("Create subscription") @@ -60,21 +54,6 @@ def initial_input_form_generator() -> FormGenerator: return user_input.dict() -def get_site_by_name(site_name: str) -> Site: - subscription = ( - SubscriptionTable.query.join( - ProductTable, SubscriptionInstanceTable, SubscriptionInstanceValueTable, ResourceTypeTable - ) - .filter(SubscriptionInstanceValueTable.value == site_name) - .filter(ResourceTypeTable.resource_type == "site_name") - .filter(SubscriptionTable.status == "active") - .first() - ) - if not subscription: - raise ValueError(f"Site with name {site_name} not found.") - return Site.from_subscription(subscription.subscription_id) - - @step("Initialize subscription") def initialize_subscription( subscription: RouterInactive, diff --git a/test/conftest.py b/test/conftest.py index 626ea3fe..7fdf3345 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -93,7 +93,7 @@ def data_config_filename(configuration_data) -> str: def db_uri(): """Provide the database uri configuration to run the migration on.""" - return os.environ.get("DATABASE_URI_TEST", "postgresql://gso:gos@localhost/gso-test-db") + return os.environ.get("DATABASE_URI_TEST", "postgresql://nwa:nwa@localhost/gso-test-db") def run_migrations(db_uri: str) -> None: diff --git a/test/test_imports.py b/test/test_imports.py index 27ea48e6..e89341aa 100644 --- a/test/test_imports.py +++ b/test/test_imports.py @@ -51,6 +51,7 @@ class TestImportEndpoints: resource_type="site_name", value=self.site_data["site_name"] ) assert subscription is not None + self.site_data.pop("customer") def test_import_site_endpoint_with_existing_site(self): response = self.client.post(self.site_import_endpoint, json=self.site_data) @@ -63,11 +64,10 @@ class TestImportEndpoints: def test_import_site_endpoint_with_invalid_data(self): # invalid data, missing site_latitude and invalid site_longitude - site_data = self.site_data.copy() - site_data.pop("site_latitude") - site_data["site_longitude"] = "invalid" + self.site_data.pop("site_latitude") + self.site_data["site_longitude"] = "invalid" assert SubscriptionTable.query.count() == 0 - response = self.client.post(self.site_import_endpoint, json=site_data) + response = self.client.post(self.site_import_endpoint, json=self.site_data) assert response.status_code == 422 assert SubscriptionTable.query.count() == 0 response = response.json() @@ -92,10 +92,9 @@ class TestImportEndpoints: assert SubscriptionTable.query.count() == 1 # invalid data, missing hostname and invalid router_lo_ipv6_address - router_data = self.router_data.copy() - router_data.pop("hostname") - router_data["router_lo_ipv6_address"] = "invalid" - response = self.client.post(self.router_import_endpoint, json=router_data) + self.router_data.pop("hostname") + self.router_data["router_lo_ipv6_address"] = "invalid" + response = self.client.post(self.router_import_endpoint, json=self.router_data) assert response.status_code == 422 assert SubscriptionTable.query.count() == 1 response = response.json() -- GitLab