diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3ed25c8b63e8c457bbdc4ae29a1b55c0c3e3416a..4aa7c82146a7b3e627d678bc7d8034e89c9267eb 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 0000000000000000000000000000000000000000..3cfbd16c2ca8d4a879449266ba427607ec61a0e4 --- /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 547f8c8ae29f79d58b80a3ec14e94b8890a8bb91..30f56a53b4cfaf785b22add2541fde110a2c4573 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 626ea3fe1f6805782c6aac55b7ee92a573c9450e..7fdf33454808a5bd33cbaa1e3e4d6b6ff823bff3 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 27ea48e6dd1336e02c8143bfc6ce5916399356bf..e89341aa92038f334888e2534742d4c1e4965976 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()