From e02dc221c30f6278c68e086d5c7ce76da8201f84 Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 14:40:21 +0200 Subject: [PATCH 1/8] chore(organization): add logging and minor code adjustments --- Babylon/commands/api/organizations/create.py | 13 +++--- Babylon/commands/api/organizations/delete.py | 22 +++++----- Babylon/commands/api/organizations/get.py | 14 +++---- Babylon/commands/api/organizations/get_all.py | 12 +++--- .../api/organizations/security/add.py | 29 +++++++------ .../api/organizations/security/delete.py | 15 ++++--- .../api/organizations/security/get.py | 17 ++++---- .../api/organizations/security/get_all.py | 17 ++++---- .../api/organizations/security/set_default.py | 17 ++++---- .../api/organizations/security/update.py | 19 +++++---- .../services/organization_api_svc.py | 42 +++++++------------ Babylon/commands/api/organizations/update.py | 17 ++++---- Babylon/commands/macro/apply.py | 32 +++++++------- Babylon/commands/macro/deploy_organization.py | 5 +-- Babylon/utils/environment.py | 21 +++++----- 15 files changed, 141 insertions(+), 151 deletions(-) diff --git a/Babylon/commands/api/organizations/create.py b/Babylon/commands/api/organizations/create.py index f176bf5bf..279575854 100644 --- a/Babylon/commands/api/organizations/create.py +++ b/Babylon/commands/api/organizations/create.py @@ -1,10 +1,9 @@ import pathlib import json -import click from logging import getLogger from typing import Any -from click import argument, command +from click import argument, command, echo, style from click import Path from Babylon.utils.decorators import injectcontext from Babylon.utils.decorators import retrieve_state @@ -28,10 +27,10 @@ def create(state: Any, keycloak_token: str, payload_file: pathlib.Path) -> Comma """ Register new organization """ - _ret = [""] - _ret.append("Register new organization") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Register new organization") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) spec = dict() with open(payload_file, 'r') as f: spec["payload"] = env.fill_template_jsondump(data=f.read(), state=state) @@ -46,5 +45,5 @@ def create(state: Any, keycloak_token: str, payload_file: pathlib.Path) -> Comma if env.remote: env.store_state_in_cloud(state) logger.info(json.dumps(organization, indent=2)) - logger.info(f"[api] Organization {organization.get('id')} successfully created") + logger.info(f"[api] Organization {[organization.get('id')]} successfully created") return CommandResponse.success(organization) diff --git a/Babylon/commands/api/organizations/delete.py b/Babylon/commands/api/organizations/delete.py index 2c2c4f16f..47f4b8f8e 100644 --- a/Babylon/commands/api/organizations/delete.py +++ b/Babylon/commands/api/organizations/delete.py @@ -1,9 +1,6 @@ -import click - from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.utils.decorators import injectcontext, retrieve_state from Babylon.utils.response import CommandResponse from Babylon.utils.credentials import pass_keycloak_token @@ -22,19 +19,20 @@ @retrieve_state def delete(state: Any, keycloak_token: str, organization_id: str, force_validation: bool = False) -> CommandResponse: """Delete an organization""" - _ret = [""] - _ret.append("Delete organization") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) - state['services']["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) + _org = [""] + _org.append("Delete organization") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) + service_state = state["services"] + service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service = OrganizationService(state=state['services'], keycloak_token=keycloak_token) logger.info("[api] Deleting organization") response = service.delete(force_validation=force_validation) if response is None: return CommandResponse.fail() - logger.info(f"[api] Organization {state['services']['api']['organization_id']} successfully deleted") - state["services"]["api"]["organization_id"] = "" + logger.info(f"[api] Organization {[service_state['api']['organization_id']]} successfully deleted") + service_state["api"]["organization_id"] = '' env.store_state_in_local(state) if env.remote: env.store_state_in_cloud(state) - return CommandResponse.success() + return CommandResponse.success(response) diff --git a/Babylon/commands/api/organizations/get.py b/Babylon/commands/api/organizations/get.py index 5a390ed76..eea060f78 100644 --- a/Babylon/commands/api/organizations/get.py +++ b/Babylon/commands/api/organizations/get.py @@ -1,10 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import option -from click import command +from click import option, command, style, echo from Babylon.utils.decorators import injectcontext, retrieve_state from Babylon.utils.decorators import output_to_file from Babylon.utils.response import CommandResponse @@ -24,14 +22,14 @@ @retrieve_state def get(state: Any, organization_id: str, keycloak_token: str) -> CommandResponse: """Get an organization details""" - _ret = [""] - _ret.append("Get organization details") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Get organization details") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) services_state = state["services"] services_state["api"]["organization_id"] = (organization_id or services_state["api"]["organization_id"]) organizations_service = OrganizationService(state=state['services'], keycloak_token=keycloak_token) - logger.info(f"[api] Retrieving organization {services_state['api']['organization_id']} details") + logger.info(f"[api] Retrieving organization {[services_state['api']['organization_id']]} details") response = organizations_service.get() if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/api/organizations/get_all.py b/Babylon/commands/api/organizations/get_all.py index 8ce94923f..c66ae926d 100644 --- a/Babylon/commands/api/organizations/get_all.py +++ b/Babylon/commands/api/organizations/get_all.py @@ -1,11 +1,9 @@ import jmespath import json -import click from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.utils.decorators import retrieve_state from Babylon.utils.decorators import injectcontext from Babylon.utils.response import CommandResponse @@ -28,10 +26,10 @@ def get_all(state: Any, keycloak_token: str, filter: str) -> CommandResponse: """ Get all organizations details """ - _ret = [""] - _ret.append("Get all organizations details") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Get all organizations details") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) organization_service = OrganizationService(state=state['services'], keycloak_token=keycloak_token) logger.info("[api] Retrieving all organizations details") response = organization_service.get_all() diff --git a/Babylon/commands/api/organizations/security/add.py b/Babylon/commands/api/organizations/security/add.py index d6ce44e37..71515db9a 100644 --- a/Babylon/commands/api/organizations/security/add.py +++ b/Babylon/commands/api/organizations/security/add.py @@ -1,9 +1,8 @@ import json -import click + from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.organizations.services.organization_security_svc import ( OrganizationSecurityService, ) from Babylon.utils.credentials import pass_keycloak_token @@ -25,22 +24,26 @@ @pass_keycloak_token() @option("--role", "role", type=str, required=True, help="Role RBAC") @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) @retrieve_state -def add(state: Any, keycloak_token: str, email: str, role: str = None) -> CommandResponse: +def add(state: Any, keycloak_token: str, organization_id: str, email: str, role: str = None) -> CommandResponse: """ Add organization users RBAC access """ - _ret = [""] - _ret.append("Add organization user RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Add organization user RBAC access") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service = OrganizationSecurityService(keycloak_token=keycloak_token, state=service_state) details = json.dumps(obj={"id": email, "role": role}, indent=2, ensure_ascii=True) - logger.info(f"[api] Adding user {email} RBAC access to the organization {service_state['api']['organization_id']}") + logger.info( + f"[api] Adding user {[email]} RBAC access to the organization {[service_state['api']['organization_id']]}") response = service.add(details) - if response: - rbacs = response.json() - logger.info(json.dumps(rbacs, indent=2)) - logger.info("[api] User RBAC access successfully added") + if response is None: + return CommandResponse.fail() + rbacs = response.json() + logger.info(json.dumps(rbacs, indent=2)) + logger.info("[api] User RBAC access successfully added") return CommandResponse.success(rbacs) diff --git a/Babylon/commands/api/organizations/security/delete.py b/Babylon/commands/api/organizations/security/delete.py index d84aaeed1..19f208c51 100644 --- a/Babylon/commands/api/organizations/security/delete.py +++ b/Babylon/commands/api/organizations/security/delete.py @@ -1,7 +1,6 @@ import logging -import click -from click import command, option +from click import command, option, style, echo from Babylon.utils.decorators import injectcontext from Babylon.utils.environment import Environment from Babylon.utils.response import CommandResponse @@ -25,17 +24,17 @@ def delete(state: dict, keycloak_token: str, email: str, organization_id: str) - """ Delete organization users RBAC access """ - _ret = [""] - _ret.append("Delete organization user RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Delete organization user RBAC access") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] service = OrganizationSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info( - f"[api] Deleting user {email} RBAC access from the organization {service_state['api']['organization_id']}") + f"[api] Deleting user {[email]} RBAC access from the organization {[service_state['api']['organization_id']]}") response = service.delete(id=email) if response is None: return CommandResponse.fail() - logger.info(f"[api] User {email} RBAC access successfully deleted") + logger.info(f"[api] User {[email]} RBAC access successfully deleted") return CommandResponse.success(response) diff --git a/Babylon/commands/api/organizations/security/get.py b/Babylon/commands/api/organizations/security/get.py index d05e13133..e741b06ee 100644 --- a/Babylon/commands/api/organizations/security/get.py +++ b/Babylon/commands/api/organizations/security/get.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import option, command +from click import option, command, echo, style from Babylon.commands.api.organizations.services.organization_security_svc import OrganizationSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -20,18 +19,20 @@ @output_to_file @pass_keycloak_token() @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) @retrieve_state -def get(state: Any, keycloak_token: str, email: str) -> CommandResponse: +def get(state: Any, organization_id: str, keycloak_token: str, email: str) -> CommandResponse: """ Get organization user RBAC access """ - _ret = [""] - _ret.append("Get organization user RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Get organization user RBAC access") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service = OrganizationSecurityService(keycloak_token=keycloak_token, state=service_state) - logger.info(f"[api] Get user {email} RBAC access to the organization {service_state['api']['organization_id']}") + logger.info(f"[api] Get user {[email]} RBAC access to the organization {[service_state['api']['organization_id']]}") response = service.get(id=email) if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/api/organizations/security/get_all.py b/Babylon/commands/api/organizations/security/get_all.py index aebacfa5e..eee2e58ba 100644 --- a/Babylon/commands/api/organizations/security/get_all.py +++ b/Babylon/commands/api/organizations/security/get_all.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import command +from click import command, echo, style, option from Babylon.commands.api.organizations.services.organization_security_svc import OrganizationSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -19,18 +18,20 @@ @injectcontext() @output_to_file @pass_keycloak_token() +@option("--organization-id", "organization_id", type=str) @retrieve_state -def get_all(state: Any, keycloak_token: str) -> CommandResponse: +def get_all(state: Any, organization_id: str, keycloak_token: str) -> CommandResponse: """ Get all RBAC access to the organization """ - _ret = [""] - _ret.append("Get all RBAC access to the organization") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Get all RBAC access to the organization") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service = OrganizationSecurityService(keycloak_token=keycloak_token, state=service_state) - logger.info(f"[api] Retrieving all RBAC access to the organization {service_state['api']['organization_id']}") + logger.info(f"[api] Retrieving all RBAC access to the organization {[service_state['api']['organization_id']]}") response = service.get_all() if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/api/organizations/security/set_default.py b/Babylon/commands/api/organizations/security/set_default.py index efb0fe126..36361d9d6 100644 --- a/Babylon/commands/api/organizations/security/set_default.py +++ b/Babylon/commands/api/organizations/security/set_default.py @@ -1,9 +1,8 @@ import json -import click + from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.organizations.services.organization_security_svc import OrganizationSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -40,19 +39,19 @@ def set_default( """ Set default RBAC access to the organization """ - _ret = [""] - _ret.append("Set default RBAC access to the organization") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Set default RBAC access to the organization") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service = OrganizationSecurityService(keycloak_token=keycloak_token, state=service_state) details = json.dumps(obj={"role": role}, indent=2, ensure_ascii=True) - logger.info(f"[api] Setting default RBAC access to the organization {service_state['api']['organization_id']}") + logger.info(f"[api] Setting default RBAC access to the organization {[service_state['api']['organization_id']]}") response = service.set_default(details) if response is None: return CommandResponse.fail() default_security = response.json() logger.info(json.dumps(default_security, indent=2)) - logger.info("[api] default RBAC access successfully setted") + logger.info(f"[api] default RBAC access successfully setted with role {[role]}") return CommandResponse.success(default_security) diff --git a/Babylon/commands/api/organizations/security/update.py b/Babylon/commands/api/organizations/security/update.py index 41bc8b901..e75fdc75e 100644 --- a/Babylon/commands/api/organizations/security/update.py +++ b/Babylon/commands/api/organizations/security/update.py @@ -1,8 +1,7 @@ import json import logging -import click -from click import command, option +from click import command, option, echo, style from Babylon.utils.decorators import injectcontext from Babylon.utils.environment import Environment from Babylon.utils.response import CommandResponse @@ -26,25 +25,27 @@ required=True, help="Role RBAC", ) +@option("--organization-id", "organization_id", type=str) @option("--email", "email", type=str, required=True, help="Email valid") @retrieve_state -def update(state: dict, keycloak_token: str, email: str, role: str) -> CommandResponse: +def update(state: dict, keycloak_token: str, organization_id: str, email: str, role: str) -> CommandResponse: """ Update organization users RBAC access """ - _ret = [""] - _ret.append("Update organization user RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Update organization user RBAC access") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] details = json.dumps({"id": email, "role": role}) service = OrganizationSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info( - f"[api] Updating user {email} RBAC access in the organization {service_state['api']['organization_id']}") + f"[api] Updating user {[email]} RBAC access in the organization {[service_state['api']['organization_id']]}") response = service.update(id=email, details=details) if response is None: return CommandResponse.fail() rbacs = response.json() logger.info(json.dumps(rbacs, indent=2)) - logger.info(f"[api] User {email} RBAC access successfully Updated") + logger.info(f"[api] User {[email]} RBAC access successfully updated") return CommandResponse.success(rbacs) diff --git a/Babylon/commands/api/organizations/services/organization_api_svc.py b/Babylon/commands/api/organizations/services/organization_api_svc.py index d31209bfd..ab2adcdb4 100644 --- a/Babylon/commands/api/organizations/services/organization_api_svc.py +++ b/Babylon/commands/api/organizations/services/organization_api_svc.py @@ -7,6 +7,7 @@ from Babylon.utils.interactive import confirm_deletion from Babylon.utils.request import oauth_request from Babylon.utils.environment import Environment +from Babylon.utils.response import CommandResponse logger = getLogger("Babylon") env = Environment() @@ -26,15 +27,11 @@ def __init__(self, state: dict, keycloak_token: str, spec: dict = None): def create(self): details = self.spec["payload"] response = oauth_request(f"{self.url}/organizations", self.keycloak_token, type="POST", data=details) - if response is None: - logger.error('An error occurred while creating the organisation') return response def delete(self, force_validation: bool): organization_id = self.state["api"]["organization_id"] - if not organization_id: - logger.error("organization id not found") - sys.exit(1) + check_if_organization_exists(organization_id) if not force_validation and not confirm_deletion("organization", organization_id): return None response = oauth_request( @@ -42,37 +39,28 @@ def delete(self, force_validation: bool): self.keycloak_token, type="DELETE", ) - if response is None: - logger.error(f'An error occurred while deleting the organisation with id: {organization_id}') return response def get(self): organization_id = self.state["api"]["organization_id"] - if not organization_id: - logger.error("Organization id is missing") - return None + check_if_organization_exists(organization_id) response = oauth_request(f"{self.url}/organizations/{organization_id}", self.keycloak_token) - if response is None: - logger.error(f"An error occurred while getting the organisation with id: {organization_id}") return response def get_all(self): response = oauth_request(f"{self.url}/organizations", self.keycloak_token) - if response is None: - logger.error('An error occurred while getting of all organisations') return response def update(self): - details = self.spec["payload"] organization_id = self.state["api"]["organization_id"] + check_if_organization_exists(organization_id) + details = self.spec["payload"] response = oauth_request( f"{self.url}/organizations/{organization_id}", self.keycloak_token, type="PATCH", data=details, ) - if response is None: - logger.error(f"An error occurred while updating the organisation with id: {organization_id}") return response def update_security(self, old_security: dict): @@ -80,7 +68,7 @@ def update_security(self, old_security: dict): payload = json.loads(self.spec["payload"]) security_spec = payload.get("security") if not security_spec: - logger.error("security is missing") + logger.error("[babylon] Security is missing") sys.exit(1) ids_spec = [i.get("id") for i in security_spec["accessControlList"]] ids_existing = [i.get("id") for i in old_security["accessControlList"]] @@ -88,25 +76,27 @@ def update_security(self, old_security: dict): data = json.dumps(obj={"role": security_spec["default"]}, indent=2, ensure_ascii=True) response = self.security_svc.set_default(data) if response is None: - logger.error('An error occurred while updating a default security role in organization') - return None + return CommandResponse.fail() for g in security_spec["accessControlList"]: if g.get("id") in ids_existing: details = json.dumps(obj=g, indent=2, ensure_ascii=True) response = self.security_svc.update(id=g.get("id"), details=details) if response is None: - logger.error('An error occurred while updating a security role in organization') - return None + return CommandResponse.fail() if g.get("id") not in ids_existing: details = json.dumps(obj=g, indent=2, ensure_ascii=True) response = self.security_svc.add(details) if response is None: - logger.error('An error occurred while adding a security role in organization') - return None + return CommandResponse.fail() for s in ids_existing: if s not in ids_spec: response = self.security_svc.delete(id=s) if response is None: - logger.error('An error occurred while deleting a security role in organization') - return None + return CommandResponse.fail() return security_spec + + +def check_if_organization_exists(dataset_id: str): + if not dataset_id: + logger.error("[babylon] organization_id is missing check the state or use --organization-id") + sys.exit(1) diff --git a/Babylon/commands/api/organizations/update.py b/Babylon/commands/api/organizations/update.py index e4e8bbd36..d1a3fc8ba 100644 --- a/Babylon/commands/api/organizations/update.py +++ b/Babylon/commands/api/organizations/update.py @@ -1,10 +1,9 @@ import pathlib import json -import click from logging import getLogger from typing import Any -from click import Path, argument, command, option +from click import Path, argument, command, option, echo, style from Babylon.utils.decorators import retrieve_state from Babylon.utils.decorators import injectcontext from Babylon.utils.response import CommandResponse @@ -28,19 +27,21 @@ def update(state: Any, keycloak_token: str, organization_id: str, payload_file: """ Update an organization """ - _ret = [""] - _ret.append("Update an organization") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _org = [""] + _org.append("Update an organization") + _org.append("") + echo(style("\n".join(_org), bold=True, fg="green")) spec = dict() with open(payload_file, 'r') as f: spec["payload"] = env.fill_template_jsondump(data=f.read(), state=state) + services_state = state["services"] + services_state["api"]["organization_id"] = (organization_id or services_state["api"]["organization_id"]) organizations_service = OrganizationService(state=state['services'], keycloak_token=keycloak_token, spec=spec) - logger.info(f"[api] Updating organization {state['services']['api']['organization_id']}") + logger.info(f"[api] Updating organization {[services_state['api']['organization_id']]}") response = organizations_service.update() if response is None: return CommandResponse.fail() organization = response.json() logger.info(json.dumps(organization, indent=2)) - logger.info(f"[api] Organization {organization.get('id')} successfully updated") + logger.info(f"[api] Organization {[organization.get('id')]} successfully updated") return CommandResponse.success(organization) diff --git a/Babylon/commands/macro/apply.py b/Babylon/commands/macro/apply.py index 73f8053d1..712e13624 100644 --- a/Babylon/commands/macro/apply.py +++ b/Babylon/commands/macro/apply.py @@ -1,11 +1,10 @@ from typing import Iterable import yaml -import click import pathlib import os from logging import getLogger -from click import Path, argument, command, option +from click import Path, argument, command, option, style, echo from Babylon.utils.environment import Environment from Babylon.utils.decorators import injectcontext from Babylon.commands.macro.deploy_webapp import deploy_swa @@ -79,10 +78,13 @@ def apply( namespace = s.get("namespace") deploy_solution(namespace=namespace, file_content=content, deploy_dir=deploy_dir, payload_only=payload_only) - for swa in webapps: - content = swa.get("content") - namespace = swa.get("namespace") - deploy_swa(namespace=namespace, file_content=content) + # For the web app, we need to wait to know whether it will be deployed as a Helm chart + # or continue as a static Azure Web App. + + # for swa in webapps: + # content = swa.get("content") + # namespace = swa.get("namespace") + # deploy_swa(namespace=namespace, file_content=content) for w in workspaces: content = w.get("content") @@ -92,12 +94,14 @@ def apply( deploy_dir=deploy_dir, payload_only=payload_only) - for d in datasets: - content = d.get("content") - namespace = d.get("namespace") - deployed_dataset_id = deploy_dataset(namespace=namespace, file_content=content, deploy_dir=deploy_dir) - if deployed_dataset_id: - final_datasets.append(deployed_dataset_id) + # To do: This is not implemented yet. We are waiting for the API implementation before working on it again. + + # for d in datasets: + # content = d.get("content") + # namespace = d.get("namespace") + # deployed_dataset_id = deploy_dataset(namespace=namespace, file_content=content, deploy_dir=deploy_dir) + # if deployed_dataset_id: + # final_datasets.append(deployed_dataset_id) else: if organization: for o in organizations: @@ -156,5 +160,5 @@ def apply( _logs.append(f" * The Babylon log and error files are generated at: {vars.get('path_logs')}") else: _logs.append(f" * The Babylon log and error files are generated at: {logfile_directory}") - click.echo(click.style("\n".join(_ret), fg="green")) - click.echo(click.style("\n".join(_logs), fg="green")) + echo(style("\n".join(_ret), fg="green")) + echo(style("\n".join(_logs), fg="green")) diff --git a/Babylon/commands/macro/deploy_organization.py b/Babylon/commands/macro/deploy_organization.py index 15844188e..d79afbe8b 100644 --- a/Babylon/commands/macro/deploy_organization.py +++ b/Babylon/commands/macro/deploy_organization.py @@ -3,8 +3,7 @@ import json from logging import getLogger - -import click +from click import echo, style from Babylon.utils.environment import Environment from Babylon.utils.credentials import get_keycloak_token from Babylon.commands.api.organizations.services.organization_api_svc import OrganizationService @@ -17,7 +16,7 @@ def deploy_organization(namespace: str, file_content: str): _ret = [""] _ret.append("Organization deployment") _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + echo(style("\n".join(_ret), bold=True, fg="green")) platform_url = env.get_ns_from_text(content=namespace) state = env.retrieve_state_func(state_id=env.state_id) state["services"]["api"]["url"] = platform_url diff --git a/Babylon/utils/environment.py b/Babylon/utils/environment.py index 4947685d9..53200c432 100644 --- a/Babylon/utils/environment.py +++ b/Babylon/utils/environment.py @@ -15,7 +15,6 @@ from flatten_json import flatten from Babylon.config import config_files from azure.storage.blob import BlobServiceClient - from Babylon.utils import ORIGINAL_TEMPLATE_FOLDER_PATH from Babylon.utils.working_dir import WorkingDir from Babylon.utils.yaml_utils import yaml_to_json @@ -86,7 +85,7 @@ def get_variables(self): merged_data, duplicate_keys = self.merge_yaml_files(self.variable_files) if len(duplicate_keys) > 0: for key, files in duplicate_keys.items(): - logger.error(f"The key '{key}' is duplicated in variable files {' and '.join(files)}") + logger.error(f"[babylon] The key '{key}' is duplicated in variable files {' and '.join(files)}") sys.exit(1) else: merged_data["secret_powerbi"] = "" @@ -104,16 +103,16 @@ def get_ns_from_text(self, content: str): remote: bool = payload_dict.get("remote", self.remote) self.remote = remote if not state_id: - logger.error("state id is mandatory") + logger.error("[babylon] state_id is mandatory") sys.exit(1) plt_obj = payload_dict.get("platform", {}) platform_id = plt_obj.get("id", "") if not platform_id: - logger.error("platform id is mandatory") + logger.error("[babylon] platform_id is mandatory") sys.exit(1) platform_url = plt_obj.get("url", "") if not platform_url: - logger.error("url is mandatory") + logger.error("[babylon] url is mandatory") sys.exit(1) self.set_state_id(state_id=state_id) self.set_context(context_id=context_id) @@ -309,7 +308,7 @@ def get_state_from_vault_by_platform(self, platform: str): for r in resources: response = self.hvac_client.read(path=f"{organization_name}/{tenant_id}/babylon/config/{platform}/{r}") if not response: - logger.error(f"platform id '{platform}' not found in vault service") + logger.error(f"[babylon] platform id '{platform}' not found in vault service") sys.exit(1) response_parsed.setdefault(r, dict(response["data"].items())) return response_parsed @@ -447,7 +446,7 @@ def set_ns_from_yaml(self, content: str, state: dict = None, ext_args: dict = No plt_obj = ns.get("platform", {}) platform_id = plt_obj.get("id", "") if not platform_id: - logger.error("platform id is mandatory") + logger.error("[babylon] platform_id is mandatory") sys.exit(1) platform_url = plt_obj.get("url", "") if not platform_url: @@ -456,7 +455,7 @@ def set_ns_from_yaml(self, content: str, state: dict = None, ext_args: dict = No url_ = re.compile(f"https:\\/\\/{platform_id}\\.") match_content = url_.match(platform_url) if not match_content: - logger.error("url not match") + logger.error("[babylon] url not match") sys.exit(1) self.state_id = state_id self.set_context(context_id=context_id) @@ -484,7 +483,7 @@ def load_yaml_file(self, file_path: Path): try: return yaml.safe_load(file) or {} except yaml.YAMLError as e: - logger.error(f"File '{file_path}' is not a valid YAML file. Details: {str(e)}") + logger.error(f"[babylon] File '{file_path}' is not a valid YAML file. Details: {str(e)}") sys.exit(1) def merge_yaml_files(self, file_paths: [Path]): @@ -493,10 +492,10 @@ def merge_yaml_files(self, file_paths: [Path]): for file_path in file_paths: if not file_path.endswith('.yaml'): - logger.error(f"File '{file_path}' is not a valid YAML file.") + logger.error(f"[babylon] File '{file_path}' is not a valid YAML file.") sys.exit(1) if os.path.getsize(file_path) == 0: - logger.error(f"File '{file_path}' is empty.") + logger.error(f"[babylon] File '{file_path}' is empty.") sys.exit(1) data = self.load_yaml_file(file_path) From 51b2ca3e1836432fe8bae0d648ecea2ca3e77acb Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 15:29:18 +0200 Subject: [PATCH 2/8] chore(solutions): add logging and minor code adjustments --- Babylon/commands/api/solutions/create.py | 14 +++++------- Babylon/commands/api/solutions/delete.py | 17 ++++++-------- Babylon/commands/api/solutions/get.py | 13 +++++------ Babylon/commands/api/solutions/get_all.py | 12 +++++----- .../api/solutions/security/__init__.py | 4 ++-- .../commands/api/solutions/security/add.py | 18 +++++++++------ .../security/{remove.py => delete.py} | 20 +++++++++-------- .../commands/api/solutions/security/get.py | 18 ++++++++------- .../api/solutions/security/get_all.py | 17 +++++++++----- .../api/solutions/security/get_users.py | 17 ++++++++------ .../api/solutions/security/set_default.py | 14 +++++------- .../commands/api/solutions/security/update.py | 22 +++++++++++-------- .../solutions/services/solutions_api_svc.py | 2 +- .../services/solutions_security_svc.py | 2 +- Babylon/commands/api/solutions/update.py | 15 +++++-------- Babylon/commands/macro/deploy_solution.py | 8 +++---- 16 files changed, 109 insertions(+), 104 deletions(-) rename Babylon/commands/api/solutions/security/{remove.py => delete.py} (62%) diff --git a/Babylon/commands/api/solutions/create.py b/Babylon/commands/api/solutions/create.py index 344f40a84..81d4e1b94 100644 --- a/Babylon/commands/api/solutions/create.py +++ b/Babylon/commands/api/solutions/create.py @@ -1,13 +1,9 @@ import pathlib import json -import click from logging import getLogger from typing import Any -from click import Path -from click import argument -from click import command -from click import option +from click import Path, argument, command, option, echo, style from Babylon.commands.api.solutions.services.solutions_api_svc import SolutionService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -30,10 +26,10 @@ def create(state: Any, keycloak_token: str, organization_id: str, payload_file: """ Register a new solution """ - _ret = [""] - _ret.append("Register new organization") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Register new organization") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) spec = dict() diff --git a/Babylon/commands/api/solutions/delete.py b/Babylon/commands/api/solutions/delete.py index bdf9254b4..1be8c65f1 100644 --- a/Babylon/commands/api/solutions/delete.py +++ b/Babylon/commands/api/solutions/delete.py @@ -1,10 +1,7 @@ -import click - from logging import getLogger from typing import Any from Babylon.utils.environment import Environment -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.solutions.services.solutions_api_svc import SolutionService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import injectcontext, retrieve_state @@ -31,10 +28,10 @@ def delete( """ Delete a solution """ - _ret = [""] - _ret.append("Delete solution") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Delete solution") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) @@ -43,9 +40,9 @@ def delete( response = solutions_service.delete(force_validation=force_validation) if response is None: return CommandResponse.fail() - logger.info(f" [api] Solution {[service_state['api']['solution_id']]} successfully deleted") + logger.info(f"[api] Solution {[service_state['api']['solution_id']]} successfully deleted") state["services"]["api"]["solution_id"] = "" env.store_state_in_local(state) if env.remote: env.store_state_in_cloud(state) - return CommandResponse.success() + return CommandResponse.success(response) diff --git a/Babylon/commands/api/solutions/get.py b/Babylon/commands/api/solutions/get.py index d908767e1..52af63596 100644 --- a/Babylon/commands/api/solutions/get.py +++ b/Babylon/commands/api/solutions/get.py @@ -1,9 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.solutions.services.solutions_api_svc import SolutionService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -26,14 +25,14 @@ def get(state: Any, keycloak_token: str, organization_id: str, solution_id: str) """ Get a solution details """ - _ret = [""] - _ret.append("Get solution details") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Get solution details") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) - logger.info(f"[api] Retrieving solution {[service_state['api']['solution_id']]}") + logger.info(f"[api] Retrieving solution {[service_state['api']['solution_id']]} details") organizations_service = SolutionService(state=service_state, keycloak_token=keycloak_token) response = organizations_service.get() if response is None: diff --git a/Babylon/commands/api/solutions/get_all.py b/Babylon/commands/api/solutions/get_all.py index dbb754ea9..034b05d06 100644 --- a/Babylon/commands/api/solutions/get_all.py +++ b/Babylon/commands/api/solutions/get_all.py @@ -1,11 +1,9 @@ import jmespath import json -import click from logging import getLogger from typing import Any, Optional -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.solutions.services.solutions_api_svc import SolutionService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -26,10 +24,10 @@ def get_all(state: Any, keycloak_token: str, organization_id: str, filter: Optio """ Get all solutions details """ - _ret = [""] - _ret.append("Get all solutions details") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Get all solutions details") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) solutions_service = SolutionService(keycloak_token=keycloak_token, state=service_state) diff --git a/Babylon/commands/api/solutions/security/__init__.py b/Babylon/commands/api/solutions/security/__init__.py index b14bc0eae..cb173a3c7 100644 --- a/Babylon/commands/api/solutions/security/__init__.py +++ b/Babylon/commands/api/solutions/security/__init__.py @@ -2,7 +2,7 @@ from .add import add from click import group from .update import update -from .remove import remove +from .delete import delete from .get_all import get_all from .get_users import get_users from .set_default import set_default @@ -14,7 +14,7 @@ def security(): pass -list_commands = [add, get, get_all, get_users, remove, set_default, update] +list_commands = [add, get, get_all, get_users, delete, set_default, update] for _command in list_commands: security.add_command(_command) diff --git a/Babylon/commands/api/solutions/security/add.py b/Babylon/commands/api/solutions/security/add.py index 5c5271337..fa48f2147 100644 --- a/Babylon/commands/api/solutions/security/add.py +++ b/Babylon/commands/api/solutions/security/add.py @@ -1,9 +1,7 @@ import json -import click from typing import Any -from click import option -from click import command +from click import option, command, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -22,21 +20,27 @@ @pass_keycloak_token() @option("--role", "role", type=str, required=True, default="viewer", help="Role RBAC") @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--solution-id", "solution_id", type=str) @retrieve_state def add( state: Any, keycloak_token: str, + organization_id: str, + solution_id: str, role: str, email: str, ) -> CommandResponse: """ Add solution users RBAC access """ - _ret = [""] - _ret.append("Add solution users RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Add solution users RBAC access") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) solution_service = SolutionSecurityService(keycloak_token=keycloak_token, state=service_state) details = json.dumps(obj={"id": email, "role": role}, indent=2, ensure_ascii=True) logger.info(f"[api] Granting user {[email]} RBAC permissions on solution {[service_state['api']['solution_id']]}") diff --git a/Babylon/commands/api/solutions/security/remove.py b/Babylon/commands/api/solutions/security/delete.py similarity index 62% rename from Babylon/commands/api/solutions/security/remove.py rename to Babylon/commands/api/solutions/security/delete.py index dacee182c..5aee8e75a 100644 --- a/Babylon/commands/api/solutions/security/remove.py +++ b/Babylon/commands/api/solutions/security/delete.py @@ -1,6 +1,4 @@ -import click - -from click import option, command +from click import option, command, echo, style from logging import getLogger from Babylon.utils.decorators import injectcontext from Babylon.utils.response import CommandResponse @@ -18,19 +16,23 @@ @pass_keycloak_token() @output_to_file @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--solution-id", "solution_id", type=str) @retrieve_state -def remove(state: dict, keycloak_token: str, email: str) -> CommandResponse: +def delete(state: dict, organization_id: str, solution_id: str, keycloak_token: str, email: str) -> CommandResponse: """ Remove the specified access from the Solution """ - _ret = [""] - _ret.append("Delete solution users RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Delete solution users RBAC access") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) solution_service = SolutionSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Deleting user {[email]} RBAC permissions on solution {[service_state['api']['solution_id']]}") - response = solution_service.remove(email) + response = solution_service.delete(email) if response is None: return CommandResponse.fail() logger.info("[api] User RBAC permissions successfully deleted") diff --git a/Babylon/commands/api/solutions/security/get.py b/Babylon/commands/api/solutions/security/get.py index a66592c2d..6bab005e8 100644 --- a/Babylon/commands/api/solutions/security/get.py +++ b/Babylon/commands/api/solutions/security/get.py @@ -1,9 +1,7 @@ import json -import click from typing import Any -from click import option -from click import command +from click import option, command, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -21,16 +19,20 @@ @output_to_file @pass_keycloak_token() @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--solution-id", "solution_id", type=str) @retrieve_state -def get(state: Any, keycloak_token: str, email: str) -> CommandResponse: +def get(state: Any, organization_id: str, solution_id: str, keycloak_token: str, email: str) -> CommandResponse: """ Get solution users RBAC access """ - _ret = [""] - _ret.append("Get solution user RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Get solution user RBAC access") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) solution_service = SolutionSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Get user {[email]} RBAC access to the solution {[service_state['api']['solution_id']]}") response = solution_service.get(email) diff --git a/Babylon/commands/api/solutions/security/get_all.py b/Babylon/commands/api/solutions/security/get_all.py index 163d8a281..8ea6e1125 100644 --- a/Babylon/commands/api/solutions/security/get_all.py +++ b/Babylon/commands/api/solutions/security/get_all.py @@ -1,8 +1,7 @@ import json -import click from typing import Any -from click import command +from click import command, option, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -19,19 +18,25 @@ @injectcontext() @output_to_file @pass_keycloak_token() +@option("--organization-id", "organization_id", type=str) +@option("--solution-id", "solution_id", type=str) @retrieve_state def get_all( state: Any, + organization_id: str, + solution_id: str, keycloak_token: str, ) -> CommandResponse: """ Get all RBAC access to the Solution """ - _ret = [""] - _ret.append("Get all RBAC access to the solution") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Get all RBAC access to the solution") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) solution_service = SolutionSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Retrieving all RBAC access to the solution {[service_state['api']['solution_id']]}") response = solution_service.get_all() diff --git a/Babylon/commands/api/solutions/security/get_users.py b/Babylon/commands/api/solutions/security/get_users.py index f3c3055a4..53f621383 100644 --- a/Babylon/commands/api/solutions/security/get_users.py +++ b/Babylon/commands/api/solutions/security/get_users.py @@ -1,7 +1,6 @@ import json -import click -from click import command +from click import command, option, echo, style from logging import getLogger from Babylon.utils.decorators import injectcontext from Babylon.utils.response import CommandResponse @@ -18,16 +17,20 @@ @injectcontext() @pass_keycloak_token() @output_to_file +@option("--organization-id", "organization_id", type=str) +@option("--solution-id", "solution_id", type=str) @retrieve_state -def get_users(state: dict, keycloak_token: str) -> CommandResponse: +def get_users(state: dict, organization_id: str, solution_id: str, keycloak_token: str) -> CommandResponse: """ Get the Solution security users list """ - _ret = [""] - _ret.append("Get solution security users list") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Get solution security users list") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) solution_service = SolutionSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Fetching solution {[service_state['api']['solution_id']]} RBAC users") response = solution_service.get_users() diff --git a/Babylon/commands/api/solutions/security/set_default.py b/Babylon/commands/api/solutions/security/set_default.py index fe72c323c..ea7982932 100644 --- a/Babylon/commands/api/solutions/security/set_default.py +++ b/Babylon/commands/api/solutions/security/set_default.py @@ -1,9 +1,7 @@ import json -import click from typing import Any -from click import option -from click import command +from click import option, command, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -40,10 +38,10 @@ def set_default( """ Set the Solution default security """ - _ret = [""] - _ret.append("Set default RBAC access to the solution") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Set default RBAC access to the solution") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) @@ -55,5 +53,5 @@ def set_default( return CommandResponse.fail() default_security = response.json() logger.info(json.dumps(default_security, indent=2)) - logger.info("[api] default RBAC access successfully setted") + logger.info(f"[api] default RBAC access successfully setted with role {[role]}") return CommandResponse.success(response) diff --git a/Babylon/commands/api/solutions/security/update.py b/Babylon/commands/api/solutions/security/update.py index 2c300e688..8aaaf6b31 100644 --- a/Babylon/commands/api/solutions/security/update.py +++ b/Babylon/commands/api/solutions/security/update.py @@ -1,8 +1,7 @@ import json -import logging -import click -from click import option, command +from click import option, command, echo, style +from logging import getLogger from Babylon.utils.decorators import injectcontext from Babylon.utils.response import CommandResponse from Babylon.utils.environment import Environment @@ -10,7 +9,7 @@ from Babylon.utils.decorators import output_to_file, retrieve_state from Babylon.commands.api.solutions.services.solutions_security_svc import SolutionSecurityService -logger = logging.getLogger("Babylon") +logger = getLogger("Babylon") env = Environment() @@ -18,6 +17,8 @@ @injectcontext() @pass_keycloak_token() @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--solution-id", "solution_id", type=str) @option( "--role", "role", @@ -27,15 +28,18 @@ ) @output_to_file @retrieve_state -def update(state: dict, keycloak_token: str, email: str, role: str) -> CommandResponse: +def update(state: dict, organization_id: str, solution_id: str, keycloak_token: str, email: str, + role: str) -> CommandResponse: """ Update solution users RBAC access """ - _ret = [""] - _ret.append("Update solution user RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Update solution user RBAC access") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) + service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) details = json.dumps({"id": email, "role": role}) solution_service = SolutionSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Updating user {[email]} RBAC access in the solution {[service_state['api']['solution_id']]}") diff --git a/Babylon/commands/api/solutions/services/solutions_api_svc.py b/Babylon/commands/api/solutions/services/solutions_api_svc.py index c66aba384..257e68d52 100644 --- a/Babylon/commands/api/solutions/services/solutions_api_svc.py +++ b/Babylon/commands/api/solutions/services/solutions_api_svc.py @@ -111,5 +111,5 @@ def update_security(self, old_security: dict): def check_if_solution_exists(solution_id: str): if solution_id is None: - logger.error(f"solution {solution_id} is missing") + logger.error("[babylon] solution_id is missing check the state or use --solution-id") sys.exit(1) diff --git a/Babylon/commands/api/solutions/services/solutions_security_svc.py b/Babylon/commands/api/solutions/services/solutions_security_svc.py index e730c4535..6603682f0 100644 --- a/Babylon/commands/api/solutions/services/solutions_security_svc.py +++ b/Babylon/commands/api/solutions/services/solutions_security_svc.py @@ -60,7 +60,7 @@ def set_default(self, details: str): ) return response - def remove(self, identity_id: str): + def delete(self, identity_id: str): response = oauth_request( f"{self.url}/organizations/{self.organization_id}/solutions/" f"{self.solution_id}/security/access/{identity_id}", diff --git a/Babylon/commands/api/solutions/update.py b/Babylon/commands/api/solutions/update.py index 0482a7e0e..ac4d831ef 100644 --- a/Babylon/commands/api/solutions/update.py +++ b/Babylon/commands/api/solutions/update.py @@ -1,12 +1,9 @@ import pathlib import json -import click from logging import getLogger from typing import Any -from click import Path, argument -from click import command -from click import option +from click import Path, argument, option, echo, style, command from Babylon.commands.api.solutions.services.solutions_api_svc import SolutionService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -36,10 +33,10 @@ def update( """ Update a solution """ - _ret = [""] - _ret.append("Update an solution") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Update an solution") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) @@ -47,7 +44,7 @@ def update( with open(payload_file, 'r') as f: spec["payload"] = env.fill_template_jsondump(data=f.read(), state=state) solutions_service = SolutionService(state=service_state, keycloak_token=keycloak_token, spec=spec) - logger.info(f"[api] Updating solution {[state['services']['api']['solution_id']]}") + logger.info(f"[api] Updating solution {[service_state['api']['solution_id']]}") response = solutions_service.update() if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/macro/deploy_solution.py b/Babylon/commands/macro/deploy_solution.py index 8d6a49229..107e04329 100644 --- a/Babylon/commands/macro/deploy_solution.py +++ b/Babylon/commands/macro/deploy_solution.py @@ -1,10 +1,9 @@ import os import sys import json -import click from logging import getLogger - +from click import echo, style from Babylon.utils.environment import Environment from Babylon.utils.credentials import get_keycloak_token from Babylon.commands.api.solutions.services.solutions_api_svc import SolutionService @@ -18,7 +17,7 @@ def deploy_solution(namespace: str, file_content: str) -> bool: _ret = [""] _ret.append("Solution deployment") _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + echo(style("\n".join(_ret), bold=True, fg="green")) platform_url = env.get_ns_from_text(content=namespace) state = env.retrieve_state_func(state_id=env.state_id) state["services"]["api"]["url"] = platform_url @@ -38,7 +37,8 @@ def deploy_solution(namespace: str, file_content: str) -> bool: state["services"]["api"]["organization_id"] = metadata["selector"].get("organization_id", "") else: if not state["services"]["api"]["organization_id"]: - logger.error(f"Missing 'organization_id' in metadata -> selector field : {metadata.get('selector')}") + logger.error( + f"[babylon] Missing 'organization_id' in metadata -> selector field : {metadata.get('selector')}") sys.exit(1) spec = dict() spec["payload"] = json.dumps(payload, indent=2, ensure_ascii=True) From 5e8c837c8599b5a9452e8125305bfecdb8490bd8 Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 16:17:58 +0200 Subject: [PATCH 3/8] chore(workspaces): add logging and minor code adjustments --- Babylon/commands/api/workspaces/create.py | 27 +++++++++---------- Babylon/commands/api/workspaces/delete.py | 23 +++++++--------- Babylon/commands/api/workspaces/get.py | 19 +++++++------ Babylon/commands/api/workspaces/get_all.py | 26 ++++++++++-------- .../commands/api/workspaces/security/add.py | 19 ++++++++----- .../api/workspaces/security/delete.py | 17 +++++++----- .../commands/api/workspaces/security/get.py | 14 ++++++---- .../api/workspaces/security/get_all.py | 17 ++++++------ .../api/workspaces/security/set_default.py | 19 +++++++------ .../api/workspaces/security/update.py | 19 ++++++++----- .../workspaces/services/workspaces_api_svc.py | 18 ++++++++----- Babylon/commands/api/workspaces/update.py | 19 ++++++------- Babylon/commands/macro/deploy_workspace.py | 7 ++--- 13 files changed, 130 insertions(+), 114 deletions(-) diff --git a/Babylon/commands/api/workspaces/create.py b/Babylon/commands/api/workspaces/create.py index 30527fafb..fc1c45d01 100644 --- a/Babylon/commands/api/workspaces/create.py +++ b/Babylon/commands/api/workspaces/create.py @@ -1,11 +1,9 @@ import pathlib -import click import json + from logging import getLogger from typing import Any -from click import argument, command -from click import option -from click import Path +from click import argument, command, option, echo, style, Path from Babylon.commands.api.workspaces.services.workspaces_api_svc import WorkspaceService from Babylon.utils.decorators import ( injectcontext, @@ -24,29 +22,28 @@ @injectcontext() @output_to_file @pass_keycloak_token() -@option("--organization-id", type=str) -@argument("payload_file", type=Path(path_type=pathlib.Path)) +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) +@argument("payload_file", type=Path(path_type=pathlib.Path, exists=True)) @retrieve_state def create( state: Any, keycloak_token: str, organization_id: str, + workspace_id: str, payload_file: pathlib.Path = None, ) -> CommandResponse: """ Register a workspace. """ - _ret = [""] - _ret.append("Register a workspace") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Register a workspace") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] - service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) - + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["workspace_id"] = organization_id or service_state["api"]["workspace_id"] spec = dict() - if not payload_file.exists(): - print(f"file {payload_file} not found in directory") - return CommandResponse.fail() with open(payload_file, 'r') as f: spec["payload"] = env.fill_template_jsondump(data=f.read(), state=state) workspace_service = WorkspaceService(state=service_state, keycloak_token=keycloak_token, spec=spec) diff --git a/Babylon/commands/api/workspaces/delete.py b/Babylon/commands/api/workspaces/delete.py index d73a9d53e..0b123792b 100644 --- a/Babylon/commands/api/workspaces/delete.py +++ b/Babylon/commands/api/workspaces/delete.py @@ -1,9 +1,6 @@ -import click from logging import getLogger from typing import Any -from click import command -from click import option - +from click import command, option, echo, style from Babylon.commands.api.workspaces.services.workspaces_api_svc import WorkspaceService from Babylon.utils.decorators import ( injectcontext, @@ -22,8 +19,8 @@ @retrieve_state @pass_keycloak_token() @option("-D", "force_validation", is_flag=True, help="Force Delete") -@option("--organization-id", type=str) -@option("--workspace-id", type=str) +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) def delete( state: Any, keycloak_token: str, @@ -34,13 +31,13 @@ def delete( """ Delete a workspace """ - _ret = [""] - _ret.append("Delete a workspace") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Delete a workspace") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] - service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) - service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) + service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) + service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) workspace_service = WorkspaceService(state=service_state, keycloak_token=keycloak_token) logger.info("[api] Deleting workspace") response = workspace_service.delete(force_validation=force_validation) @@ -51,4 +48,4 @@ def delete( env.store_state_in_local(state) if env.remote: env.store_state_in_cloud(state) - return CommandResponse.success() + return CommandResponse.success(response) diff --git a/Babylon/commands/api/workspaces/get.py b/Babylon/commands/api/workspaces/get.py index d0dd310e3..5748e3a31 100644 --- a/Babylon/commands/api/workspaces/get.py +++ b/Babylon/commands/api/workspaces/get.py @@ -1,9 +1,8 @@ -import click import json + from logging import getLogger from typing import Any -from click import command, option - +from click import command, option, echo, style from Babylon.commands.api.workspaces.services.workspaces_api_svc import WorkspaceService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -22,22 +21,22 @@ @injectcontext() @output_to_file @pass_keycloak_token() -@option("--organization-id", type=str) -@option("--workspace-id", type=str) +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) @retrieve_state def get(state: Any, organization_id: str, keycloak_token: str, workspace_id: str) -> CommandResponse: """ Get a workspace details """ - _ret = [""] - _ret.append("Get a workspace details") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Get a workspace details") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) workspace_service = WorkspaceService(state=service_state, keycloak_token=keycloak_token) - logger.info(f"[api] Retrieving workspace {[service_state['api']['workspace_id']]}") + logger.info(f"[api] Retrieving workspace {[service_state['api']['workspace_id']]} details") response = workspace_service.get() if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/api/workspaces/get_all.py b/Babylon/commands/api/workspaces/get_all.py index 25482457c..843248d48 100644 --- a/Babylon/commands/api/workspaces/get_all.py +++ b/Babylon/commands/api/workspaces/get_all.py @@ -1,11 +1,9 @@ -import click import jmespath import json + from logging import getLogger from typing import Any, Optional -from click import command -from click import option - +from click import command, option, echo, style from Babylon.commands.api.workspaces.services.workspaces_api_svc import WorkspaceService from Babylon.utils.decorators import ( injectcontext, @@ -24,19 +22,25 @@ @injectcontext() @output_to_file @pass_keycloak_token() -@option("--organization-id", type=str) +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) @option("--filter", "filter", help="Filter response with a jmespath query") @retrieve_state -def get_all(state: Any, organization_id: str, keycloak_token: str, filter: Optional[str] = None) -> CommandResponse: +def get_all(state: Any, + organization_id: str, + workspace_id: str, + keycloak_token: str, + filter: Optional[str] = None) -> CommandResponse: """ Get all workspaces details """ - _ret = [""] - _ret.append("Get all workspaces details") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Get all workspaces details") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] - service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) + service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) + service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) workspace_service = WorkspaceService(state=service_state, keycloak_token=keycloak_token) logger.info(f"[api] Getting all workspaces from organization {[service_state['api']['organization_id']]}") response = workspace_service.get_all() diff --git a/Babylon/commands/api/workspaces/security/add.py b/Babylon/commands/api/workspaces/security/add.py index 7ecb32175..a94632e37 100644 --- a/Babylon/commands/api/workspaces/security/add.py +++ b/Babylon/commands/api/workspaces/security/add.py @@ -1,9 +1,8 @@ import json -import click + from logging import getLogger from typing import Any -from click import option -from click import command +from click import option, command, echo, style from Babylon.commands.api.workspaces.services.workspaces_security_svc import ApiWorkspaceSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -21,21 +20,27 @@ @pass_keycloak_token() @option("--role", "role", type=str, required=True, default="viewer", help="Role RBAC") @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) @retrieve_state def add( state: Any, keycloak_token: str, + organization_id: str, + workspace_id: str, role: str, email: str, ) -> CommandResponse: """ Add workspace users RBAC access """ - _ret = [""] - _ret.append("Add workspace users RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Add workspace users RBAC access") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) + service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) service = ApiWorkspaceSecurityService(keycloak_token=keycloak_token, state=service_state) details = json.dumps(obj={"id": email, "role": role}, indent=2, ensure_ascii=True) logger.info(f"[api] Granting user {[email]} RBAC permissions on workspace {[service_state['api']['workspace_id']]}") diff --git a/Babylon/commands/api/workspaces/security/delete.py b/Babylon/commands/api/workspaces/security/delete.py index 5f3cdf4eb..a0e185b9d 100644 --- a/Babylon/commands/api/workspaces/security/delete.py +++ b/Babylon/commands/api/workspaces/security/delete.py @@ -1,7 +1,6 @@ import logging -import click -from click import option, command +from click import option, command, echo, style from Babylon.commands.api.workspaces.services.workspaces_security_svc import ApiWorkspaceSecurityService from Babylon.utils.decorators import injectcontext from Babylon.utils.environment import Environment @@ -18,16 +17,20 @@ @pass_keycloak_token() @output_to_file @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) @retrieve_state -def delete(state: dict, keycloak_token: str, email: str) -> CommandResponse: +def delete(state: dict, organization_id: str, workspace_id: str, keycloak_token: str, email: str) -> CommandResponse: """ Delete workspace users RBAC access """ - _ret = [""] - _ret.append("Delete workspace users RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Delete workspace users RBAC access") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) + service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) service = ApiWorkspaceSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Deleting user {[email]} RBAC permissions on workspace {[service_state['api']['workspace_id']]}") response = service.delete(id=email) diff --git a/Babylon/commands/api/workspaces/security/get.py b/Babylon/commands/api/workspaces/security/get.py index a10dfe014..bb6fa4b63 100644 --- a/Babylon/commands/api/workspaces/security/get.py +++ b/Babylon/commands/api/workspaces/security/get.py @@ -20,16 +20,20 @@ @output_to_file @pass_keycloak_token() @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) @retrieve_state -def get(state: Any, keycloak_token: str, email: str) -> CommandResponse: +def get(state: Any, organization_id: str, workspace_id: str, keycloak_token: str, email: str) -> CommandResponse: """ Get workspace users RBAC access """ - _ret = [""] - _ret.append("Get workspace users RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Get workspace users RBAC access") + _work.append("") + click.echo(click.style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] service = ApiWorkspaceSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Get user {[email]} RBAC access to the workspace {[service_state['api']['workspace_id']]}") response = service.get(id=email) diff --git a/Babylon/commands/api/workspaces/security/get_all.py b/Babylon/commands/api/workspaces/security/get_all.py index 3c886d917..8398c25af 100644 --- a/Babylon/commands/api/workspaces/security/get_all.py +++ b/Babylon/commands/api/workspaces/security/get_all.py @@ -1,9 +1,8 @@ -import click import json + from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.workspaces.services.workspaces_security_svc import ApiWorkspaceSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -34,13 +33,13 @@ def get_all( """ Get all RBAC access for the workspace """ - _ret = [""] - _ret.append("Get all RBAC access for the workspace") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Get all RBAC access for the workspace") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] - service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] - service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] service = ApiWorkspaceSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Retrieving all RBAC access to the workspace {[service_state['api']['workspace_id']]}") response = service.get_all() diff --git a/Babylon/commands/api/workspaces/security/set_default.py b/Babylon/commands/api/workspaces/security/set_default.py index 767af8be7..8f17bb694 100644 --- a/Babylon/commands/api/workspaces/security/set_default.py +++ b/Babylon/commands/api/workspaces/security/set_default.py @@ -1,9 +1,8 @@ import json -import click + from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, style, echo from Babylon.commands.api.workspaces.services.workspaces_security_svc import ApiWorkspaceSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -42,13 +41,13 @@ def set_default( """ Set default RBAC access to workspace """ - _ret = [""] - _ret.append("Set default RBAC access to workspace") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Set default RBAC access to workspace") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] - service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] - service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] service = ApiWorkspaceSecurityService(keycloak_token=keycloak_token, state=service_state) details = json.dumps(obj={"role": role}, indent=2, ensure_ascii=True) logger.info(f"[api] Setting default RBAC access to the solution {[service_state['api']['workspace_id']]}") @@ -57,5 +56,5 @@ def set_default( return CommandResponse.fail() default_security = response.json() logger.info(json.dumps(default_security, indent=2)) - logger.info("[api] default RBAC access successfully setted") + logger.info(f"[api] default RBAC access successfully setted with role {[role]}") return CommandResponse.success(default_security) diff --git a/Babylon/commands/api/workspaces/security/update.py b/Babylon/commands/api/workspaces/security/update.py index 73e66f14a..f7e730c40 100644 --- a/Babylon/commands/api/workspaces/security/update.py +++ b/Babylon/commands/api/workspaces/security/update.py @@ -1,7 +1,7 @@ import json import logging -import click -from click import command, option + +from click import command, option, echo, style from Babylon.commands.api.workspaces.services.workspaces_security_svc import ( ApiWorkspaceSecurityService, ) from Babylon.utils.decorators import injectcontext @@ -27,16 +27,21 @@ help="Role RBAC", ) @option("--email", "email", type=str, required=True, help="Email valid") +@option("--organization-id", "organization_id", type=str) +@option("--workspace-id", "workspace_id", type=str) @retrieve_state -def update(state: dict, keycloak_token: str, email: str, role: str) -> CommandResponse: +def update(state: dict, organization_id: str, workspace_id: str, keycloak_token: str, email: str, + role: str) -> CommandResponse: """ Update workspace users RBAC access """ - _ret = [""] - _ret.append("Update workspace users RBAC access") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Update workspace users RBAC access") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] + service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] + service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] details = json.dumps({"id": email, "role": role}) service = ApiWorkspaceSecurityService(keycloak_token=keycloak_token, state=service_state) logger.info(f"[api] Updating user {[email]} RBAC access in the workspace {[service_state['api']['workspace_id']]}") diff --git a/Babylon/commands/api/workspaces/services/workspaces_api_svc.py b/Babylon/commands/api/workspaces/services/workspaces_api_svc.py index 62b9baea4..fa6d77f55 100644 --- a/Babylon/commands/api/workspaces/services/workspaces_api_svc.py +++ b/Babylon/commands/api/workspaces/services/workspaces_api_svc.py @@ -3,7 +3,6 @@ from logging import getLogger from typing import Optional - from Babylon.commands.api.workspaces.services.workspaces_security_svc import ( ApiWorkspaceSecurityService, ) from Babylon.utils.environment import Environment @@ -22,12 +21,11 @@ def __init__(self, state: dict, keycloak_token: str, spec: Optional[dict] = None self.state = state self.keycloak_token = keycloak_token self.url = self.state["api"]["url"] - if not self.url: - logger.error("API url not found verify the state") - sys.exit(1) self.organization_id = self.state["api"]["organization_id"] - self.solution_id = self.state["api"]["solution_id"] self.workspace_id = self.state["api"]["workspace_id"] + if not self.url: + logger.error("[babylon] api url not found verify the state") + sys.exit(1) if not self.organization_id: logger.error("[babylon] Organization id is missing verify the state") sys.exit(1) @@ -40,6 +38,7 @@ def get_all(self): return response def get(self): + check_if_workspace_exists(self.workspace_id) response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/{self.workspace_id}", self.keycloak_token, @@ -57,6 +56,7 @@ def create(self): return response def update(self): + check_if_workspace_exists(self.workspace_id) details = self.update_payload_with_state() details_json = json.dumps(details, indent=4, default=str) response = oauth_request( @@ -78,9 +78,9 @@ def update_with_payload(self, payload: dict): return response def delete(self, force_validation: bool): + check_if_workspace_exists(self.workspace_id) if not force_validation and not confirm_deletion("workspace", self.workspace_id): return None - response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/{self.workspace_id}", self.keycloak_token, @@ -142,3 +142,9 @@ def update_payload_with_state(self): if (scenarioData is not None and scenario_view_tag == scenarioData.get("reportTag")): scenarioData["reportId"] = self.state["powerbi"]['scenario_view'][scenario_view_tag] return jsonPayload + + +def check_if_workspace_exists(dataset_id: str): + if not dataset_id: + logger.error("[babylon] workspace_id is missing check the state or use --workspace-id") + sys.exit(1) diff --git a/Babylon/commands/api/workspaces/update.py b/Babylon/commands/api/workspaces/update.py index abd698a5f..b8f5e3eae 100644 --- a/Babylon/commands/api/workspaces/update.py +++ b/Babylon/commands/api/workspaces/update.py @@ -1,11 +1,9 @@ import pathlib -import click import json + from logging import getLogger from typing import Any -from click import Path, argument -from click import command -from click import option +from click import command, argument, option, echo, style, Path from Babylon.commands.api.workspaces.services.workspaces_api_svc import WorkspaceService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -19,8 +17,6 @@ logger = getLogger("Babylon") env = Environment() -payload = {"name": "Updated workspace to be deleted"} - @command() @injectcontext() @@ -34,6 +30,7 @@ help="Organization Id Cosmotech Platform", ) @argument("payload_file", type=Path(path_type=pathlib.Path, exists=True)) +@option("--organization-id", "organization_id", type=str) @option("--workspace-id", "workspace_id", type=str) def update( state: Any, @@ -45,10 +42,10 @@ def update( """ Update a workspace """ - _ret = [""] - _ret.append("Update a workspace") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _work = [""] + _work.append("Update a workspace") + _work.append("") + echo(style("\n".join(_work), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) @@ -56,7 +53,7 @@ def update( with open(payload_file, 'r') as f: spec["payload"] = env.fill_template_jsondump(data=f.read(), state=state) workspace_service = WorkspaceService(state=service_state, keycloak_token=keycloak_token, spec=spec) - logger.info(f"[api] Updating workspace {[state['services']['api']['workspace_id']]}") + logger.info(f"[api] Updating workspace {[service_state['api']['workspace_id']]}") response = workspace_service.update() if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/macro/deploy_workspace.py b/Babylon/commands/macro/deploy_workspace.py index a2bea5230..2db2d3db4 100644 --- a/Babylon/commands/macro/deploy_workspace.py +++ b/Babylon/commands/macro/deploy_workspace.py @@ -1,9 +1,9 @@ import os import sys import json -import click import pathlib +from click import echo, style from logging import getLogger from Babylon.utils.environment import Environment from Babylon.commands.api.workspaces.services.workspaces_api_svc import WorkspaceService @@ -30,7 +30,7 @@ def deploy_workspace(namespace: str, file_content: str, deploy_dir: pathlib.Path _ret = [""] _ret.append("Workspace deployment") _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + echo(style("\n".join(_ret), bold=True, fg="green")) platform_url = env.get_ns_from_text(content=namespace) state = env.retrieve_state_func(state_id=env.state_id) state["services"]["api"]["url"] = platform_url @@ -47,7 +47,8 @@ def deploy_workspace(namespace: str, file_content: str, deploy_dir: pathlib.Path state["services"]["api"]["solution_id"] = metadata["selector"].get("solution_id", "") else: if (not state["services"]["api"]["organization_id"] and not state["services"]["api"]["solution_id"]): - logger.error(f"Missing 'organization_id' in metadata -> selector field : {metadata.get('selector')}") + logger.error(f"[babylon] Missing 'organization_id' and 'solution_id'" + f"in metadata -> selector field : {metadata.get('selector')}") sys.exit(1) content = env.fill_template(data=file_content, state=state) payload: dict = content.get("spec").get("payload") From 46c95a2c07b33963ab2fe8342360b1555d42d132 Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 16:25:54 +0200 Subject: [PATCH 4/8] chore(runTemplates): add logging and minor code adjustments --- .../commands/api/solutions/runTemplates/add.py | 14 ++++++-------- .../commands/api/solutions/runTemplates/delete.py | 12 +++++------- .../commands/api/solutions/runTemplates/get.py | 11 +++++------ .../api/solutions/runTemplates/get_all.py | 11 +++++------ .../commands/api/solutions/runTemplates/update.py | 15 +++++++-------- .../api/solutions/services/solutions_api_svc.py | 2 +- 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/Babylon/commands/api/solutions/runTemplates/add.py b/Babylon/commands/api/solutions/runTemplates/add.py index 7983d88d5..b08452991 100644 --- a/Babylon/commands/api/solutions/runTemplates/add.py +++ b/Babylon/commands/api/solutions/runTemplates/add.py @@ -1,10 +1,8 @@ import json -import click import pathlib from typing import Any -from click import argument, option, command -from click import Path +from click import argument, option, command, Path, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -35,10 +33,10 @@ def add( """ Add runTemplates to solution """ - _ret = [""] - _ret.append("Add runtemplates to solution") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Add runtemplates to solution") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) spec = dict() service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) @@ -53,5 +51,5 @@ def add( run_template = response.json() logger.info(json.dumps(run_template, indent=2)) sol_id = service_state['api']['solution_id'] - logger.info(f"[api] Run Templates id:{[run_template.get('id')]} successfully added to the solution {[sol_id]}") + logger.info(f"[api] Run Templates id {[run_template.get('id')]} successfully added to the solution {[sol_id]}") return CommandResponse.success(run_template) diff --git a/Babylon/commands/api/solutions/runTemplates/delete.py b/Babylon/commands/api/solutions/runTemplates/delete.py index ff045aa6f..18637d1e0 100644 --- a/Babylon/commands/api/solutions/runTemplates/delete.py +++ b/Babylon/commands/api/solutions/runTemplates/delete.py @@ -1,7 +1,5 @@ -import click - from typing import Any -from click import command, option +from click import command, option, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -32,10 +30,10 @@ def delete( """ Delete solution runtemplate by id """ - _ret = [""] - _ret.append("Delete runtemplate in solution") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Delete runtemplate in solution") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) diff --git a/Babylon/commands/api/solutions/runTemplates/get.py b/Babylon/commands/api/solutions/runTemplates/get.py index bbf7b1c68..52a245192 100644 --- a/Babylon/commands/api/solutions/runTemplates/get.py +++ b/Babylon/commands/api/solutions/runTemplates/get.py @@ -1,8 +1,7 @@ import json -import click from typing import Any -from click import command, option +from click import command, option, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -33,10 +32,10 @@ def get( """ Get RunTemplate in the solution by ID """ - _ret = [""] - _ret.append("Get runtemplates in solution by id") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Get runtemplate in solution by id") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) diff --git a/Babylon/commands/api/solutions/runTemplates/get_all.py b/Babylon/commands/api/solutions/runTemplates/get_all.py index 390cdb9f3..8b779c0af 100644 --- a/Babylon/commands/api/solutions/runTemplates/get_all.py +++ b/Babylon/commands/api/solutions/runTemplates/get_all.py @@ -1,8 +1,7 @@ import json -import click from typing import Any -from click import command, option +from click import command, option, echo, style from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -31,10 +30,10 @@ def get_all( """ Get all RunTemplates in solution """ - _ret = [""] - _ret.append("Get all runtemplates in solution") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Get all runtemplates in solution") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["solution_id"] = (solution_id or service_state["api"]["solution_id"]) diff --git a/Babylon/commands/api/solutions/runTemplates/update.py b/Babylon/commands/api/solutions/runTemplates/update.py index 335e30fbf..084e65d9b 100644 --- a/Babylon/commands/api/solutions/runTemplates/update.py +++ b/Babylon/commands/api/solutions/runTemplates/update.py @@ -1,9 +1,8 @@ import json -import click import pathlib from typing import Any -from click import command, option, argument +from click import command, option, argument, echo, style from click import Path from logging import getLogger from Babylon.utils.credentials import pass_keycloak_token @@ -35,12 +34,12 @@ def update( payload_file: pathlib.Path, ) -> CommandResponse: """ - Update Run Templates in the Solution by id + Update Run Template in the Solution by id """ - _ret = [""] - _ret.append("Update solution RunTemplate") - _ret.append("") - click.echo(click.style("\n".join(_ret), bold=True, fg="green")) + _sol = [""] + _sol.append("Update solution RunTemplate") + _sol.append("") + echo(style("\n".join(_sol), bold=True, fg="green")) spec = dict() service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) @@ -55,5 +54,5 @@ def update( run_template = response.json() logger.info(json.dumps(run_template, indent=2)) sol_id = service_state['api']['solution_id'] - logger.info(f"[api] Runtemplate id {[run_template.get('id')]} successfully Updated in the solution {[sol_id]}") + logger.info(f"[api] Runtemplate id {[run_template.get('id')]} successfully updated in the solution {[sol_id]}") return CommandResponse.success(run_template) diff --git a/Babylon/commands/api/solutions/services/solutions_api_svc.py b/Babylon/commands/api/solutions/services/solutions_api_svc.py index 257e68d52..225aee34b 100644 --- a/Babylon/commands/api/solutions/services/solutions_api_svc.py +++ b/Babylon/commands/api/solutions/services/solutions_api_svc.py @@ -103,7 +103,7 @@ def update_security(self, old_security: dict): return CommandResponse.fail() for s in ids_existing: if s not in ids_spec: - response = self.security_svc.remove(id=s) + response = self.security_svc.delete(id=s) if response is None: return CommandResponse.fail() return security_spec From e1a4946d5daf1c95821e2ae3f2c0affe387c1edc Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 17:40:16 +0200 Subject: [PATCH 5/8] chore(runners): add logging and minor code adjustments --- Babylon/commands/api/runners/create.py | 12 +++++------- Babylon/commands/api/runners/delete.py | 7 ++----- Babylon/commands/api/runners/get.py | 5 ++--- Babylon/commands/api/runners/get_all.py | 5 ++--- Babylon/commands/api/runners/security/add.py | 6 ++---- Babylon/commands/api/runners/security/delete.py | 6 ++---- Babylon/commands/api/runners/security/get.py | 5 ++--- Babylon/commands/api/runners/security/get_all.py | 6 ++---- .../commands/api/runners/security/set_default.py | 8 +++----- Babylon/commands/api/runners/security/update.py | 5 ++--- .../api/runners/services/runner_api_svc.py | 14 +++++++++++--- Babylon/commands/api/runners/start.py | 5 ++--- Babylon/commands/api/runners/stop.py | 5 ++--- Babylon/commands/api/runners/update.py | 14 +++++++------- Babylon/commands/api/workspaces/update.py | 6 ------ 15 files changed, 46 insertions(+), 63 deletions(-) diff --git a/Babylon/commands/api/runners/create.py b/Babylon/commands/api/runners/create.py index 03c50c187..1f9fd5f69 100644 --- a/Babylon/commands/api/runners/create.py +++ b/Babylon/commands/api/runners/create.py @@ -1,11 +1,9 @@ import pathlib -import click import json + from logging import getLogger from typing import Any - -from click import command, option, Path, argument - +from click import command, option, Path, argument, echo, style from Babylon.commands.api.runners.services.runner_api_svc import RunnerService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -44,10 +42,10 @@ def create( _run = [""] _run.append("Register new runner") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] - service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) - service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) + service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) + service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) spec = dict() with open(payload_file, 'r') as f: spec["payload"] = env.fill_template_jsondump(data=f.read(), state=state) diff --git a/Babylon/commands/api/runners/delete.py b/Babylon/commands/api/runners/delete.py index 2c3360a77..64efec205 100644 --- a/Babylon/commands/api/runners/delete.py +++ b/Babylon/commands/api/runners/delete.py @@ -1,9 +1,6 @@ -import click from logging import getLogger from typing import Any - -from click import command, option - +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_api_svc import RunnerService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import injectcontext @@ -37,7 +34,7 @@ def delete( _run = [""] _run.append("Delete a runner") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runners/get.py b/Babylon/commands/api/runners/get.py index 910a58903..de29e8ca8 100644 --- a/Babylon/commands/api/runners/get.py +++ b/Babylon/commands/api/runners/get.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_api_svc import RunnerService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -37,7 +36,7 @@ def get( _run = [""] _run.append("Get runner details") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runners/get_all.py b/Babylon/commands/api/runners/get_all.py index d6ccdd440..be0de13c0 100644 --- a/Babylon/commands/api/runners/get_all.py +++ b/Babylon/commands/api/runners/get_all.py @@ -1,9 +1,8 @@ -import click import jmespath import json from logging import getLogger -from click import command, option +from click import command, option, echo, style from typing import Any, Optional from Babylon.commands.api.runners.services.runner_api_svc import RunnerService from Babylon.utils.credentials import pass_keycloak_token @@ -40,7 +39,7 @@ def get_all( _run = [""] _run.append("Get all runners details") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runners/security/add.py b/Babylon/commands/api/runners/security/add.py index c1d0b2e24..a0922aba2 100644 --- a/Babylon/commands/api/runners/security/add.py +++ b/Babylon/commands/api/runners/security/add.py @@ -1,10 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_security_svc import ( RunnerSecurityService, ) from Babylon.utils.credentials import pass_keycloak_token @@ -45,7 +43,7 @@ def add( _run = [""] _run.append("Add a runner users RBAC access") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] diff --git a/Babylon/commands/api/runners/security/delete.py b/Babylon/commands/api/runners/security/delete.py index 64743338f..789c93186 100644 --- a/Babylon/commands/api/runners/security/delete.py +++ b/Babylon/commands/api/runners/security/delete.py @@ -1,8 +1,6 @@ -import click - from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_security_svc import ( RunnerSecurityService, ) from Babylon.utils.credentials import pass_keycloak_token @@ -41,7 +39,7 @@ def delete( _run = [""] _run.append("Delete a runner sers RBAC access") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] diff --git a/Babylon/commands/api/runners/security/get.py b/Babylon/commands/api/runners/security/get.py index f67c5f7d8..499813f24 100644 --- a/Babylon/commands/api/runners/security/get.py +++ b/Babylon/commands/api/runners/security/get.py @@ -1,9 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_security_svc import ( RunnerSecurityService, ) from Babylon.utils.credentials import pass_keycloak_token @@ -42,7 +41,7 @@ def get( _run = [""] _run.append("Get runner RBAC access for user") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] diff --git a/Babylon/commands/api/runners/security/get_all.py b/Babylon/commands/api/runners/security/get_all.py index 22eb6108c..09be5ba35 100644 --- a/Babylon/commands/api/runners/security/get_all.py +++ b/Babylon/commands/api/runners/security/get_all.py @@ -1,10 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_security_svc import ( RunnerSecurityService, ) from Babylon.utils.credentials import pass_keycloak_token @@ -41,7 +39,7 @@ def get_all( _run = [""] _run.append("Get all RBAC access to the Runner") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] diff --git a/Babylon/commands/api/runners/security/set_default.py b/Babylon/commands/api/runners/security/set_default.py index d07fadee9..9e88a595f 100644 --- a/Babylon/commands/api/runners/security/set_default.py +++ b/Babylon/commands/api/runners/security/set_default.py @@ -1,10 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_security_svc import ( RunnerSecurityService, ) from Babylon.utils.credentials import pass_keycloak_token @@ -49,7 +47,7 @@ def set_default( _run = [""] _run.append("Set default RBAC access to the runner") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] @@ -62,5 +60,5 @@ def set_default( return CommandResponse.fail() default_security = response.json() logger.info(json.dumps(default_security, indent=2)) - logger.info("[api] default RBAC access successfully setted") + logger.info(f"[api] default RBAC access successfully setted with role {[role]}") return CommandResponse.success(default_security) diff --git a/Babylon/commands/api/runners/security/update.py b/Babylon/commands/api/runners/security/update.py index 2bdf55066..e51dd87c1 100644 --- a/Babylon/commands/api/runners/security/update.py +++ b/Babylon/commands/api/runners/security/update.py @@ -1,9 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_security_svc import ( RunnerSecurityService, ) from Babylon.utils.credentials import pass_keycloak_token @@ -50,7 +49,7 @@ def update( _run = [""] _run.append("Update runner uner RBAC access") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or state["services"]["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or state["services"]["api"]["workspace_id"] diff --git a/Babylon/commands/api/runners/services/runner_api_svc.py b/Babylon/commands/api/runners/services/runner_api_svc.py index f35d989fb..b91b28b58 100644 --- a/Babylon/commands/api/runners/services/runner_api_svc.py +++ b/Babylon/commands/api/runners/services/runner_api_svc.py @@ -2,9 +2,7 @@ import json from logging import getLogger - from typing import Optional - from Babylon.commands.api.runners.services.runner_security_svc import RunnerSecurityService from Babylon.utils.interactive import confirm_deletion from Babylon.utils.request import oauth_request @@ -22,7 +20,6 @@ def __init__(self, state: dict, keycloak_token: str, spec: Optional[dict] = None self.workspace_id = state["api"]["workspace_id"] self.runner_id = self.state["api"]["runner_id"] self.keycloak_token = keycloak_token - if not self.url: logger.error("[babylon] api url not found verify the state") sys.exit(1) @@ -42,6 +39,7 @@ def get_all(self): return response def get(self): + check_if_runner_exists(self.runner_id) response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/" f"{self.workspace_id}/runners/{self.runner_id}", @@ -50,6 +48,7 @@ def get(self): return response def update(self): + check_if_runner_exists(self.runner_id) details = self.spec["payload"] response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/" @@ -72,6 +71,7 @@ def create(self): return response def delete(self, force_validation: bool): + check_if_runner_exists(self.runner_id) if not force_validation and not confirm_deletion("runner", self.runner_id): return None @@ -84,6 +84,7 @@ def delete(self, force_validation: bool): return response def start(self): + check_if_runner_exists(self.runner_id) response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/" f"{self.workspace_id}/runners/{self.runner_id}/start", @@ -93,6 +94,7 @@ def start(self): return response def stop(self): + check_if_runner_exists(self.runner_id) response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/" f"{self.workspace_id}/runners/{self.runner_id}/stop", @@ -132,3 +134,9 @@ def update_security(self, old_security: dict): if response is None: return None return security_spec + + +def check_if_runner_exists(runner_id: str): + if not runner_id: + logger.error("[babylon] runner_id is missing check the state or use --runner-id") + sys.exit(1) diff --git a/Babylon/commands/api/runners/start.py b/Babylon/commands/api/runners/start.py index 36bc5874a..a0564b4d7 100644 --- a/Babylon/commands/api/runners/start.py +++ b/Babylon/commands/api/runners/start.py @@ -1,7 +1,6 @@ -import click from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_api_svc import RunnerService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -37,7 +36,7 @@ def start( _run = [""] _run.append("Start a runner run") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runners/stop.py b/Babylon/commands/api/runners/stop.py index 2a3486074..eb9ca6b92 100644 --- a/Babylon/commands/api/runners/stop.py +++ b/Babylon/commands/api/runners/stop.py @@ -1,7 +1,6 @@ -import click from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runners.services.runner_api_svc import RunnerService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -37,7 +36,7 @@ def stop( _run = [""] _run.append("Stop a runner run") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runners/update.py b/Babylon/commands/api/runners/update.py index 4089be4d0..db499fa77 100644 --- a/Babylon/commands/api/runners/update.py +++ b/Babylon/commands/api/runners/update.py @@ -1,9 +1,9 @@ import pathlib -import click import json + from logging import getLogger from typing import Any -from click import command, option, Path, argument +from click import command, option, Path, argument, echo, style from Babylon.commands.api.runners.services.runner_api_svc import RunnerService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -41,16 +41,16 @@ def update( _run = [""] _run.append("Update a runner") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] - service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) - service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) - service_state["api"]["runner_id"] = (runner_id or state["services"]["api"]["runner_id"]) + service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) + service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) + service_state["api"]["runner_id"] = (runner_id or service_state["api"]["runner_id"]) spec = dict() with open(payload_file, 'r') as f: spec["payload"] = env.fill_template_jsondump(data=f.read(), state=state) runner_service = RunnerService(state=service_state, spec=spec, keycloak_token=keycloak_token) - logger.info(f"[api] Updating runner {[state['services']['api']['runner_id']]}") + logger.info(f"[api] Updating runner {[service_state['api']['runner_id']]}") response = runner_service.update() if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/api/workspaces/update.py b/Babylon/commands/api/workspaces/update.py index b8f5e3eae..3ee53ebb3 100644 --- a/Babylon/commands/api/workspaces/update.py +++ b/Babylon/commands/api/workspaces/update.py @@ -23,12 +23,6 @@ @output_to_file @pass_keycloak_token() @retrieve_state -@option( - "--organization-id", - "organization_id", - type=str, - help="Organization Id Cosmotech Platform", -) @argument("payload_file", type=Path(path_type=pathlib.Path, exists=True)) @option("--organization-id", "organization_id", type=str) @option("--workspace-id", "workspace_id", type=str) From 11555a8e6d726b05e2563dc726362e1d0735165f Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 17:47:48 +0200 Subject: [PATCH 6/8] chore(runs): add logging and minor code adjustments --- Babylon/commands/api/runs/delete.py | 6 ++---- Babylon/commands/api/runs/get.py | 5 ++--- Babylon/commands/api/runs/get_all.py | 5 ++--- Babylon/commands/api/runs/logs.py | 6 ++---- Babylon/commands/api/runs/services/run_api_svc.py | 13 +++++++++++++ Babylon/commands/api/runs/status.py | 6 +++--- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Babylon/commands/api/runs/delete.py b/Babylon/commands/api/runs/delete.py index 17bd1719e..301a1afa8 100644 --- a/Babylon/commands/api/runs/delete.py +++ b/Babylon/commands/api/runs/delete.py @@ -1,8 +1,6 @@ -import click - from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.utils.environment import Environment from Babylon.commands.api.runs.services.run_api_svc import RunService from Babylon.utils.credentials import pass_keycloak_token @@ -38,7 +36,7 @@ def delete( _run = [""] _run.append("Delete the Run") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state['services'] service_state['api']['organization_id'] = organization_id or service_state['api']['organization_id'] service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runs/get.py b/Babylon/commands/api/runs/get.py index 80aeb5d28..1f6a1cac1 100644 --- a/Babylon/commands/api/runs/get.py +++ b/Babylon/commands/api/runs/get.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runs.services.run_api_svc import RunService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -39,7 +38,7 @@ def get( _run = [""] _run.append("Get run details") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runs/get_all.py b/Babylon/commands/api/runs/get_all.py index 75f080230..6320e4170 100644 --- a/Babylon/commands/api/runs/get_all.py +++ b/Babylon/commands/api/runs/get_all.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runs.services.run_api_svc import RunService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import ( @@ -39,7 +38,7 @@ def get_all( _run = [""] _run.append("Get run details") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or state["services"]["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runs/logs.py b/Babylon/commands/api/runs/logs.py index 1a7ac2668..54a603196 100644 --- a/Babylon/commands/api/runs/logs.py +++ b/Babylon/commands/api/runs/logs.py @@ -1,8 +1,6 @@ -import click - from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runs.services.run_api_svc import RunService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import injectcontext, retrieve_state, output_to_file @@ -28,7 +26,7 @@ def logs(state: Any, keycloak_token: str, organization_id: str, workspace_id: st _run = [""] _run.append("Get the logs for the Run") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state['services'] service_state['api']['organization_id'] = organization_id or service_state['api']['organization_id'] service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) diff --git a/Babylon/commands/api/runs/services/run_api_svc.py b/Babylon/commands/api/runs/services/run_api_svc.py index bb76727f5..156513f30 100644 --- a/Babylon/commands/api/runs/services/run_api_svc.py +++ b/Babylon/commands/api/runs/services/run_api_svc.py @@ -26,20 +26,26 @@ def __init__(self, keycloak_token: str, state: dict): if not self.workspace_id: logger.error("[babylon] Workspace id is missing verify the state") sys.exit(1) + if not self.runner_id: + logger.error("[babylon] Runner id is missing verify the state") + sys.exit(1) def logs(self): + check_if_run_exists(self.run_id) response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/" f"{self.workspace_id}/runners/{self.runner_id}/runs/{self.run_id}/logs", self.keycloak_token) return response def status(self): + check_if_run_exists(self.run_id) response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/" f"{self.workspace_id}/runners/{self.runner_id}/runs/{self.run_id}/status", self.keycloak_token) return response def delete(self, force_validation: bool): + check_if_run_exists(self.run_id) if not force_validation and not confirm_deletion("workspace", self.run_id): return None response = oauth_request( @@ -51,6 +57,7 @@ def delete(self, force_validation: bool): return response def get(self): + check_if_run_exists(self.run_id) response = oauth_request( f"{self.url}/organizations/{self.organization_id}/workspaces/" f"{self.workspace_id}/runners/{self.runner_id}/runs/{self.run_id}", self.keycloak_token) @@ -61,3 +68,9 @@ def get_all(self): f"{self.url}/organizations/{self.organization_id}/workspaces/" f"{self.workspace_id}/runners/{self.runner_id}/runs", self.keycloak_token) return response + + +def check_if_run_exists(run_id: str): + if not run_id: + logger.error("[babylon] run_id is missing check the state or use --run-id") + sys.exit(1) diff --git a/Babylon/commands/api/runs/status.py b/Babylon/commands/api/runs/status.py index 685ba339b..cb1183029 100644 --- a/Babylon/commands/api/runs/status.py +++ b/Babylon/commands/api/runs/status.py @@ -1,8 +1,8 @@ import json -import click + from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.runs.services.run_api_svc import RunService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import injectcontext, retrieve_state, output_to_file @@ -28,7 +28,7 @@ def status(state: Any, keycloak_token: str, organization_id: str, workspace_id: _run = [""] _run.append("Get the status of the Run") _run.append("") - click.echo(click.style("\n".join(_run), bold=True, fg="green")) + echo(style("\n".join(_run), bold=True, fg="green")) service_state = state['services'] service_state['api']['organization_id'] = organization_id or service_state['api']['organization_id'] service_state["api"]["workspace_id"] = (workspace_id or state["services"]["api"]["workspace_id"]) From 3fb3cb6864887123db9ca38000f0a397ee115be6 Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 17:56:21 +0200 Subject: [PATCH 7/8] chore(datasets): add logging and minor code adjustments --- Babylon/commands/api/datasets/delete.py | 8 ++------ Babylon/commands/api/datasets/get.py | 7 +++---- Babylon/commands/api/datasets/get_all.py | 7 +++---- Babylon/commands/api/datasets/search.py | 6 ++---- Babylon/commands/api/datasets/security/add.py | 6 ++---- Babylon/commands/api/datasets/security/delete.py | 10 ++++------ Babylon/commands/api/datasets/security/get.py | 5 ++--- Babylon/commands/api/datasets/security/get_all.py | 5 ++--- Babylon/commands/api/datasets/security/set_default.py | 11 +++++------ Babylon/commands/api/datasets/security/update.py | 9 ++++----- .../api/datasets/services/datasets_api_svc.py | 2 +- 11 files changed, 30 insertions(+), 46 deletions(-) diff --git a/Babylon/commands/api/datasets/delete.py b/Babylon/commands/api/datasets/delete.py index 16be56146..61ea832cb 100644 --- a/Babylon/commands/api/datasets/delete.py +++ b/Babylon/commands/api/datasets/delete.py @@ -1,10 +1,6 @@ -import click - from logging import getLogger from typing import Any -from click import command -from click import option - +from click import command, option, echo, style from Babylon.commands.api.datasets.services.datasets_api_svc import DatasetService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import retrieve_state, injectcontext @@ -33,7 +29,7 @@ def delete(state: Any, _data = [""] _data.append("Delete a dataset") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) diff --git a/Babylon/commands/api/datasets/get.py b/Babylon/commands/api/datasets/get.py index cba19832f..f2628a170 100644 --- a/Babylon/commands/api/datasets/get.py +++ b/Babylon/commands/api/datasets/get.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.datasets.services.datasets_api_svc import DatasetService from Babylon.utils.decorators import retrieve_state from Babylon.utils.decorators import output_to_file @@ -29,13 +28,13 @@ def get(state: Any, keycloak_token: str, organization_id: str, workspace_id: str _data = [""] _data.append("Get dataset details") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) service_state["api"]["dataset_id"] = (dataset_id or service_state["api"]["dataset_id"]) service = DatasetService(keycloak_token=keycloak_token, state=service_state) - logger.info(f"[api] Searching dataset {[service_state['api']['dataset_id']]}") + logger.info(f"[api] Retrieving dataset {[service_state['api']['dataset_id']]}") response = service.get() if response is None: return CommandResponse.fail() diff --git a/Babylon/commands/api/datasets/get_all.py b/Babylon/commands/api/datasets/get_all.py index 0e41ee7b7..7016ee89a 100644 --- a/Babylon/commands/api/datasets/get_all.py +++ b/Babylon/commands/api/datasets/get_all.py @@ -1,10 +1,9 @@ import jmespath -import click import json + from logging import getLogger from typing import Any, Optional -from click import command -from click import option +from click import command, option, echo, style from Babylon.commands.api.datasets.services.datasets_api_svc import DatasetService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -35,7 +34,7 @@ def get_all(state: Any, _data = [""] _data.append("Get all datasets details") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) diff --git a/Babylon/commands/api/datasets/search.py b/Babylon/commands/api/datasets/search.py index c4f29af89..ade37ecd6 100644 --- a/Babylon/commands/api/datasets/search.py +++ b/Babylon/commands/api/datasets/search.py @@ -1,10 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import argument -from click import command, option +from click import command, option, argument, echo, style from Babylon.commands.api.datasets.services.datasets_api_svc import DatasetService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -29,7 +27,7 @@ def search(state: Any, keycloak_token: str, organization_id: str, workspace_id: _data = [""] _data.append("Get dataset with the given tag") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = (organization_id or service_state["api"]["organization_id"]) service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) diff --git a/Babylon/commands/api/datasets/security/add.py b/Babylon/commands/api/datasets/security/add.py index 3d17e4667..486a143f3 100644 --- a/Babylon/commands/api/datasets/security/add.py +++ b/Babylon/commands/api/datasets/security/add.py @@ -1,10 +1,8 @@ import json -import click from logging import getLogger from typing import Any -from click import command -from click import option +from click import command, option, style, echo from Babylon.commands.api.datasets.services.datasets_security_svc import DatasetSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -46,7 +44,7 @@ def add(state: Any, _data = [""] _data.append(" Add dataset users RBAC access") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) diff --git a/Babylon/commands/api/datasets/security/delete.py b/Babylon/commands/api/datasets/security/delete.py index 98bccafa3..d87ab42d7 100644 --- a/Babylon/commands/api/datasets/security/delete.py +++ b/Babylon/commands/api/datasets/security/delete.py @@ -1,7 +1,5 @@ -import logging -import click - -from click import command, option +from click import command, option, echo, style +from logging import getLogger from Babylon.commands.api.datasets.services.datasets_security_svc import DatasetSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -10,7 +8,7 @@ from Babylon.utils.environment import Environment from Babylon.utils.response import CommandResponse -logger = logging.getLogger("Babylon") +logger = getLogger("Babylon") env = Environment() @@ -31,7 +29,7 @@ def delete(state: dict, keycloak_token: str, email: str, organization_id: str, w _data = [""] _data.append(" Delete dataset users RBAC access") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] service_state["api"]["workspace_id"] = (workspace_id or service_state["api"]["workspace_id"]) diff --git a/Babylon/commands/api/datasets/security/get.py b/Babylon/commands/api/datasets/security/get.py index 17b716e15..f8b971dea 100644 --- a/Babylon/commands/api/datasets/security/get.py +++ b/Babylon/commands/api/datasets/security/get.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.datasets.services.datasets_security_svc import DatasetSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -32,7 +31,7 @@ def get(state: Any, keycloak_token: str, email: str, organization_id: str, works _data = [""] _data.append(" Get dataset user RBAC access") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] diff --git a/Babylon/commands/api/datasets/security/get_all.py b/Babylon/commands/api/datasets/security/get_all.py index fe1ab7132..899e4abe2 100644 --- a/Babylon/commands/api/datasets/security/get_all.py +++ b/Babylon/commands/api/datasets/security/get_all.py @@ -1,9 +1,8 @@ -import click import json from logging import getLogger from typing import Any -from click import command, option +from click import command, option, echo, style from Babylon.commands.api.datasets.services.datasets_security_svc import DatasetSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file @@ -30,7 +29,7 @@ def get_all(state: Any, keycloak_token: str, organization_id: str, workspace_id: _data = [""] _data.append(" Get dataset users access") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] diff --git a/Babylon/commands/api/datasets/security/set_default.py b/Babylon/commands/api/datasets/security/set_default.py index 57ce7c9a7..15f9be33e 100644 --- a/Babylon/commands/api/datasets/security/set_default.py +++ b/Babylon/commands/api/datasets/security/set_default.py @@ -1,8 +1,7 @@ import json -import logging -import click -from click import command, option +from logging import getLogger +from click import command, option, echo, style from Babylon.commands.api.datasets.services.datasets_security_svc import DatasetSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file, retrieve_state @@ -10,7 +9,7 @@ from Babylon.utils.environment import Environment from Babylon.utils.response import CommandResponse -logger = logging.getLogger("Babylon") +logger = getLogger("Babylon") env = Environment() @@ -37,7 +36,7 @@ def set_default(state: dict, keycloak_token: str, role: str, organization_id: st _data = [""] _data.append(" Set dataset default security RBAC") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] @@ -50,5 +49,5 @@ def set_default(state: dict, keycloak_token: str, role: str, organization_id: st return CommandResponse.fail() default_security = response.json() logger.info(json.dumps(default_security, indent=2)) - logger.info("[api] default RBAC access successfully setted") + logger.info(f"[api] default RBAC access successfully setted with role {[role]}") return CommandResponse.success(default_security) diff --git a/Babylon/commands/api/datasets/security/update.py b/Babylon/commands/api/datasets/security/update.py index 98d331a9b..9dbfcadef 100644 --- a/Babylon/commands/api/datasets/security/update.py +++ b/Babylon/commands/api/datasets/security/update.py @@ -1,8 +1,7 @@ import json -import logging -import click -from click import command, option +from logging import getLogger +from click import command, option, echo, style from Babylon.commands.api.datasets.services.datasets_security_svc import DatasetSecurityService from Babylon.utils.credentials import pass_keycloak_token from Babylon.utils.decorators import output_to_file, retrieve_state @@ -10,7 +9,7 @@ from Babylon.utils.environment import Environment from Babylon.utils.response import CommandResponse -logger = logging.getLogger("Babylon") +logger = getLogger("Babylon") env = Environment() @@ -39,7 +38,7 @@ def update(state: dict, keycloak_token: str, identity_id: str, email: str, role: _data = [""] _data.append(" Update dataset users RBAC access") _data.append("") - click.echo(click.style("\n".join(_data), bold=True, fg="green")) + echo(style("\n".join(_data), bold=True, fg="green")) service_state = state["services"] service_state["api"]["organization_id"] = organization_id or service_state["api"]["organization_id"] service_state["api"]["workspace_id"] = workspace_id or service_state["api"]["workspace_id"] diff --git a/Babylon/commands/api/datasets/services/datasets_api_svc.py b/Babylon/commands/api/datasets/services/datasets_api_svc.py index 74d0ef1ce..f03671380 100644 --- a/Babylon/commands/api/datasets/services/datasets_api_svc.py +++ b/Babylon/commands/api/datasets/services/datasets_api_svc.py @@ -3,7 +3,6 @@ import sys from typing import Optional - from Babylon.commands.api.datasets.services.datasets_security_svc import DatasetSecurityService from Babylon.utils.environment import Environment from Babylon.utils.interactive import confirm_deletion @@ -35,6 +34,7 @@ def __init__(self, keycloak_token: str, state: dict, spec: Optional[dict] = None sys.exit(1) def delete(self, force_validation: bool): + check_if_dataset_exists(self.dataset_id) if not force_validation and not confirm_deletion("dataset", self.dataset_id): return None response = oauth_request( From 23db290358e5ccbba2730a0896426e6b3fd77cec Mon Sep 17 00:00:00 2001 From: Mohcine Tor Date: Thu, 16 Oct 2025 18:01:29 +0200 Subject: [PATCH 8/8] chore: remove unused connector commands --- Babylon/commands/api/connectors/__init__.py | 17 ------ Babylon/commands/api/connectors/create.py | 47 --------------- Babylon/commands/api/connectors/delete.py | 46 -------------- Babylon/commands/api/connectors/get.py | 33 ---------- Babylon/commands/api/connectors/get_all.py | 37 ------------ .../api/connectors/services/__init__.py | 0 .../api/connectors/services/connectors_svc.py | 60 ------------------- 7 files changed, 240 deletions(-) delete mode 100644 Babylon/commands/api/connectors/__init__.py delete mode 100644 Babylon/commands/api/connectors/create.py delete mode 100644 Babylon/commands/api/connectors/delete.py delete mode 100644 Babylon/commands/api/connectors/get.py delete mode 100644 Babylon/commands/api/connectors/get_all.py delete mode 100644 Babylon/commands/api/connectors/services/__init__.py delete mode 100644 Babylon/commands/api/connectors/services/connectors_svc.py diff --git a/Babylon/commands/api/connectors/__init__.py b/Babylon/commands/api/connectors/__init__.py deleted file mode 100644 index 584210ec2..000000000 --- a/Babylon/commands/api/connectors/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -from click import group -from .create import create -from .delete import delete -from .get import get -from .get_all import get_all - -list_commands = [delete, get_all, get, create] - - -@group() -def connectors(): - """Connectors - Cosmotech API""" - pass - - -for _command in list_commands: - connectors.add_command(_command) diff --git a/Babylon/commands/api/connectors/create.py b/Babylon/commands/api/connectors/create.py deleted file mode 100644 index 18617043b..000000000 --- a/Babylon/commands/api/connectors/create.py +++ /dev/null @@ -1,47 +0,0 @@ -import logging -import pathlib - -from typing import Any -from click import Path, argument -from click import command -from Babylon.commands.api.connectors.services.connectors_svc import ( - ConnectorService, ) -from Babylon.utils.credentials import pass_azure_token -from Babylon.utils.decorators import injectcontext -from Babylon.utils.decorators import retrieve_state -from Babylon.utils.decorators import output_to_file -from Babylon.utils.environment import Environment -from Babylon.utils.response import CommandResponse - -logger = logging.getLogger("Babylon") -env = Environment() - - -@command() -@injectcontext() -@pass_azure_token("csm_api") -@argument("payload_file", type=Path(path_type=pathlib.Path)) -@output_to_file -@retrieve_state -def create(state: Any, azure_token: str, payload_file: pathlib.Path) -> CommandResponse: - """ - Register new Connector - """ - service_state = state["services"] - if not payload_file.exists(): - print(f"file {payload_file} not found in directory") - return CommandResponse.fail() - spec = dict() - with open(payload_file, 'r') as f: - spec["payload"] = env.fill_template(data=f.read(), state=state) - service = ConnectorService(azure_token=azure_token, state=service_state, spec=spec) - response = service.create() - if response is None: - return CommandResponse.fail() - connector = response.json() - state["services"]["api"]["connector_id"] = connector.get("id") - env.store_state_in_local(state) - if env.remote: - env.store_state_in_cloud(state) - logger.info(f"Connector '{connector.get('id')}' successfully saved in state {state.get('id')}") - return CommandResponse.success(connector, verbose=True) diff --git a/Babylon/commands/api/connectors/delete.py b/Babylon/commands/api/connectors/delete.py deleted file mode 100644 index 744d38974..000000000 --- a/Babylon/commands/api/connectors/delete.py +++ /dev/null @@ -1,46 +0,0 @@ -import logging - -from typing import Any -from click import command -from click import option -from Babylon.commands.api.connectors.services.connectors_svc import ConnectorService -from Babylon.utils.decorators import injectcontext -from Babylon.utils.decorators import retrieve_state -from Babylon.utils.response import CommandResponse -from Babylon.utils.credentials import pass_azure_token -from Babylon.utils.environment import Environment - -logger = logging.getLogger("Babylon") -env = Environment() - - -@command() -@injectcontext() -@pass_azure_token("csm_api") -@option("-D", "force_validation", is_flag=True, help="Force Delete") -@option("--connector-id", "connector_id", type=str) -@retrieve_state -def delete( - state: Any, - azure_token: str, - connector_id: str, - force_validation: bool = False, -) -> CommandResponse: - """Delete a registered connector""" - service_state = state["services"] - service_state["api"]["connector_id"] = (connector_id or service_state["api"]["connector_id"]) - - service = ConnectorService(azure_token=azure_token, state=service_state) - logger.info(f"Deleting connector: {service_state['api']['solution_id']}") - response = service.delete(force_validation=force_validation) - - if response: - logger.info(f'Connector {service_state["api"]["connector_id"]} successfully deleted') - if (service_state["api"]["connector_id"] == state["services"]["api"]["connector_id"]): - state["services"]["api"]["connector_id"] = "" - env.store_state_in_local(state) - if env.remote: - env.store_state_in_cloud(state) - logger.info(f'Connector {state["services"]["api"]["connector_id"]} ' - f'successfully removed from state {state.get("id")}') - return CommandResponse.success() diff --git a/Babylon/commands/api/connectors/get.py b/Babylon/commands/api/connectors/get.py deleted file mode 100644 index 224aed5f4..000000000 --- a/Babylon/commands/api/connectors/get.py +++ /dev/null @@ -1,33 +0,0 @@ -from logging import getLogger -from typing import Any -from click import option -from click import command -from Babylon.commands.api.connectors.services.connectors_svc import ConnectorService -from Babylon.utils.decorators import injectcontext -from Babylon.utils.decorators import retrieve_state -from Babylon.utils.response import CommandResponse -from Babylon.utils.credentials import pass_azure_token -from Babylon.utils.environment import Environment -from Babylon.utils.decorators import output_to_file - -logger = getLogger("Babylon") -env = Environment() - - -@command() -@injectcontext() -@output_to_file -@pass_azure_token("csm_api") -@option("--connector-id", "connector_id", type=str) -@retrieve_state -def get(state: Any, azure_token: str, connector_id: str) -> CommandResponse: - """Get a registered connector details""" - service_state = state["services"] - service_state["api"]["connector_id"] = (connector_id or service_state["api"]["connector_id"]) - logger.info(f"Searching connector: {service_state['api']['connector_id']}") - service = ConnectorService(azure_token=azure_token, state=service_state) - response = service.get() - if response is None: - return CommandResponse.fail() - connector = response.json() - return CommandResponse.success(connector, verbose=True) diff --git a/Babylon/commands/api/connectors/get_all.py b/Babylon/commands/api/connectors/get_all.py deleted file mode 100644 index 134425b9f..000000000 --- a/Babylon/commands/api/connectors/get_all.py +++ /dev/null @@ -1,37 +0,0 @@ -from logging import getLogger -from typing import Any, Optional - -import jmespath -from click import command, option -from Babylon.commands.api.connectors.services.connectors_svc import ConnectorService -from Babylon.utils.decorators import output_to_file -from Babylon.utils.decorators import injectcontext -from Babylon.utils.decorators import retrieve_state -from Babylon.utils.response import CommandResponse -from Babylon.utils.environment import Environment -from Babylon.utils.credentials import pass_azure_token - -logger = getLogger("Babylon") -env = Environment() - - -@command() -@injectcontext() -@output_to_file -@pass_azure_token("csm_api") -@option("--filter", "filter", type=str, help="Filter response with a jmespath query") -@retrieve_state -def get_all(state: Any, azure_token: str, filter: Optional[str] = None) -> CommandResponse: - """ - Get all connectors details. - Can be filtered with jmespath queries: https://jmespath.org/specification.html#grammar - """ - service_state = state["services"] - service = ConnectorService(azure_token=azure_token, state=service_state) - response = service.get_all() - if response is None: - return CommandResponse.fail() - connectors = response.json() - if len(connectors) and filter: - connectors = jmespath.search(filter, connectors) - return CommandResponse.success(connectors, verbose=True) diff --git a/Babylon/commands/api/connectors/services/__init__.py b/Babylon/commands/api/connectors/services/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/Babylon/commands/api/connectors/services/connectors_svc.py b/Babylon/commands/api/connectors/services/connectors_svc.py deleted file mode 100644 index 57c1ccb39..000000000 --- a/Babylon/commands/api/connectors/services/connectors_svc.py +++ /dev/null @@ -1,60 +0,0 @@ -import logging -import sys - -from git import Optional -from Babylon.utils.environment import Environment -from Babylon.utils.interactive import confirm_deletion -from Babylon.utils.request import oauth_request - -logger = logging.getLogger("Babylon") -env = Environment() - - -class ConnectorService: - - def __init__(self, azure_token: str, state: dict, spec: Optional[dict] = None): - self.state = state - self.spec = spec - self.azure_token = azure_token - self.url = state["api"]["url"] - - if not self.url: - logger.error("API url not found") - sys.exit(1) - - def create(self): - details = self.spec["payload"] - response = oauth_request( - f'{self.url}/connectors', - self.azure_token, - type="POST", - data=details, - ) - return response - - def delete(self, force_validation: bool): - connector_id = self.state["api"]["connector_id"] - if not connector_id: - logger.error('Connector_id is missing') - sys.exit(1) - if not force_validation and not confirm_deletion("connector", connector_id): - return None - response = oauth_request( - f'{self.url}/connectors/{connector_id}', - self.azure_token, - type="DELETE", - ) - return response - - def get_all(self): - response = oauth_request(f'{self.url}/connectors', self.azure_token) - return response - - def get(self): - connector_id = self.state["api"]["connector_id"] - if not connector_id: - logger.error('Connector_id is missing') - sys.exit(1) - response = oauth_request(f'{self.url}/connectors/{connector_id}', self.azure_token) - - return response