From 496c07a277b4daa8cbce549099ccd9356efa3ca4 Mon Sep 17 00:00:00 2001 From: Vladimir Mutafov Date: Mon, 13 Nov 2023 16:43:44 +0200 Subject: [PATCH 1/3] feat: python core api --- .../python-modules/dirigible/core/__init__.py | 0 .../dirigible/core/configurations.py | 48 +++++++ .../python-modules/dirigible/core/context.py | 15 +++ .../dirigible/core/destinations.py | 118 ++++++++++++++++++ .../python-modules/dirigible/core/env.py | 16 +++ .../python-modules/dirigible/core/globals.py | 18 +++ 6 files changed, 215 insertions(+) create mode 100644 components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/__init__.py create mode 100644 components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py create mode 100644 components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py create mode 100644 components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py create mode 100644 components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py create mode 100644 components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/__init__.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py new file mode 100644 index 00000000000..c25fc059ca1 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py @@ -0,0 +1,48 @@ +# Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v20.html +# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# SPDX-License-Identifier: EPL-2.0 + +from org.eclipse.dirigible.commons.config import Configuration + +def get(key, defaultValue=None): + if defaultValue is not None: + return Configuration.get(key, defaultValue) + return Configuration.get(key) + +def set(key, value): + Configuration.set(key, value) + +def remove(key): + Configuration.remove(key) + +def getKeys(): + keys = [] + keysAsArray = Configuration.getKeys() + for i in range(len(keysAsArray)): + keys.append(keysAsArray[i]) + return keys + +def load(path): + Configuration.load(path) + +def update(): + Configuration.update() + +def getOS(): + return Configuration.getOS() + +def isOSWindows(): + return Configuration.isOSWindows() + +def isOSMac(): + return Configuration.isOSMac() + +def isOSUNIX(): + return Configuration.isOSUNIX() + +def isOSSolaris(): + return Configuration.isOSSolaris() diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py new file mode 100644 index 00000000000..1a8a5cf3284 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py @@ -0,0 +1,15 @@ +# Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v20.html +# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# SPDX-License-Identifier: EPL-2.0 + +from org.eclipse.dirigible.components.api.core import ContextFacade + +def get(name): + return ContextFacade.get(name) + +def set(name, value): + ContextFacade.set(name, value) diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py new file mode 100644 index 00000000000..4f655ef2d86 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py @@ -0,0 +1,118 @@ +# Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v20.html +# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# SPDX-License-Identifier: EPL-2.0 + +from org.eclipse.dirigible.components.api.core import DestinationsFacade +from org.eclipse.dirigible.components.api.cf import CloudFoundryModule +from org.eclipse.dirigible.components.api.kyma import KymaModule +from org.eclipse.dirigible.http.client import httpClient +from java.lang import Class +import json + +def get(name): + if is_cloud_environment(): + return json.loads(get_cloud_destination(name)) + return json.loads(DestinationsFacade.get(name)) + +def set(name, destination): + if is_cloud_environment(): + create_or_update_cloud_destination(name, destination) + DestinationsFacade.set(name, json.dumps(destination)) + +def remove(name): + if not is_cloud_environment(): + raise Exception("The delete destination operation is not supported for non-cloud environments") + delete_cloud_destination(name) + +def is_cloud_environment(): + try: + Class.forName("org.eclipse.dirigible.cf.CloudFoundryModule") + return True + except: + pass + + try: + Class.forName("org.eclipse.dirigible.kyma.KymaModule") + return True + except: + pass + + return False + +def get_cloud_destination(name): + token = get_oauth_token() + destination_url = f"{get_destinations_base_path()}/{name}" + headers = [ + {"name": "Authorization", "value": f"{token.token_type} {token.access_token}"} + ] + response = httpClient.get(destination_url, {"headers": headers}) + + if response.statusCode == 404: + raise Exception(f"Destination with name '{name}' not found") + + return response.text + +def get_destinations_base_path(): + return f"{configurations.get('DIRIGIBLE_DESTINATION_URI')}/destination-configuration/v1/destinations" + +def get_instance_destinations_base_path(): + return f"{configurations.get('DIRIGIBLE_DESTINATION_URI')}/destination-configuration/v1/instanceDestinations" + +def create_or_update_cloud_destination(name, destination): + is_existing_destination = True + try: + get_cloud_destination(name) + except: + is_existing_destination = False + + instance_destination_url = get_instance_destinations_base_path() + destination["Name"] = name + token = get_oauth_token() + headers = [ + {"name": "Authorization", "value": f"{token.token_type} {token.access_token}"} + ] + options = { + "headers": headers, + "text": json.dumps(destination) + } + + if is_existing_destination: + response = httpClient.put(instance_destination_url, options) + if response.statusCode != 200: + raise Exception(f"Error occurred while updating destination '{name}': {response.text}") + else: + response = httpClient.post(instance_destination_url, options) + if response.statusCode != 201: + raise Exception(f"Error occurred while creating destination '{name}': {response.text}") + +def delete_cloud_destination(name): + token = get_oauth_token() + headers = [ + {"name": "Authorization", "value": f"{token.token_type} {token.access_token}"} + ] + url = f"{get_instance_destinations_base_path()}/{name}" + response = httpClient.delete(url, {"headers": headers}) + if response.statusCode != 200: + raise Exception(f"Error occurred while deleting destination '{name}': {response.text}") + +def get_oauth_token(): + oauth_client_id = configurations.get("DIRIGIBLE_DESTINATION_CLIENT_ID") + oauth_client_secret = configurations.get("DIRIGIBLE_DESTINATION_CLIENT_SECRET") + oauth_url = configurations.get("DIRIGIBLE_DESTINATION_URL") + uri = configurations.get("DIRIGIBLE_DESTINATION_URI") + + if not oauth_client_id or not oauth_client_secret or not oauth_url or not uri: + raise Exception("Invalid destination configuration") + + oauth_config = { + "url": oauth_url, + "clientId": oauth_client_id, + "clientSecret": oauth_client_secret + } + + client = oauth.getClient(oauth_config) + return client.getToken() diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py new file mode 100644 index 00000000000..2d37e89ba7d --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py @@ -0,0 +1,16 @@ + +# Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v20.html +# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# SPDX-License-Identifier: EPL-2.0 + +from org.eclipse.dirigible.components.api.core import EnvFacade + +def get(name): + return EnvFacade.get(name) + +def list(): + return EnvFacade.list() diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py new file mode 100644 index 00000000000..465924abe88 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py @@ -0,0 +1,18 @@ +# Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v20.html +# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors +# SPDX-License-Identifier: EPL-2.0 + +from org.eclipse.dirigible.components.api.core import GlobalsFacade + +def get(name): + return GlobalsFacade.get(name) + +def set(name, value): + GlobalsFacade.set(name, value) + +def list(): + return GlobalsFacade.list() From 1494e4d515bed2850a6bdb3be2c6adc790f6dc6f Mon Sep 17 00:00:00 2001 From: Vladimir Mutafov Date: Mon, 13 Nov 2023 16:58:43 +0200 Subject: [PATCH 2/3] fix: move functions in static class members and remove destinations api --- .../dirigible/core/configurations.py | 73 ++++++----- .../dirigible/core/destinations.py | 118 ------------------ 2 files changed, 44 insertions(+), 147 deletions(-) delete mode 100644 components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py index c25fc059ca1..1fa5a749387 100644 --- a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/configurations.py @@ -6,43 +6,58 @@ # SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors # SPDX-License-Identifier: EPL-2.0 -from org.eclipse.dirigible.commons.config import Configuration +import java -def get(key, defaultValue=None): - if defaultValue is not None: - return Configuration.get(key, defaultValue) - return Configuration.get(key) +Configuration = java.type('org.eclipse.dirigible.commons.config') -def set(key, value): - Configuration.set(key, value) +class Configurations: -def remove(key): - Configuration.remove(key) + @staticmethod + def get(key, defaultValue=None): + if defaultValue is not None: + return Configuration.get(key, defaultValue) + return Configuration.get(key) -def getKeys(): - keys = [] - keysAsArray = Configuration.getKeys() - for i in range(len(keysAsArray)): - keys.append(keysAsArray[i]) - return keys + @staticmethod + def set(key, value): + Configuration.set(key, value) -def load(path): - Configuration.load(path) + @staticmethod + def remove(key): + Configuration.remove(key) -def update(): - Configuration.update() + @staticmethod + def getKeys(): + keys = [] + keysAsArray = Configuration.getKeys() + for i in range(len(keysAsArray)): + keys.append(keysAsArray[i]) + return keys -def getOS(): - return Configuration.getOS() + @staticmethod + def load(path): + Configuration.load(path) -def isOSWindows(): - return Configuration.isOSWindows() + @staticmethod + def update(): + Configuration.update() -def isOSMac(): - return Configuration.isOSMac() + @staticmethod + def getOS(): + return Configuration.getOS() -def isOSUNIX(): - return Configuration.isOSUNIX() + @staticmethod + def isOSWindows(): + return Configuration.isOSWindows() -def isOSSolaris(): - return Configuration.isOSSolaris() + @staticmethod + def isOSMac(): + return Configuration.isOSMac() + + @staticmethod + def isOSUNIX(): + return Configuration.isOSUNIX() + + @staticmethod + def isOSSolaris(): + return Configuration.isOSSolaris() diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py deleted file mode 100644 index 4f655ef2d86..00000000000 --- a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/destinations.py +++ /dev/null @@ -1,118 +0,0 @@ -# Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Eclipse Public License v2.0 -# which accompanies this distribution, and is available at -# http://www.eclipse.org/legal/epl-v20.html -# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors -# SPDX-License-Identifier: EPL-2.0 - -from org.eclipse.dirigible.components.api.core import DestinationsFacade -from org.eclipse.dirigible.components.api.cf import CloudFoundryModule -from org.eclipse.dirigible.components.api.kyma import KymaModule -from org.eclipse.dirigible.http.client import httpClient -from java.lang import Class -import json - -def get(name): - if is_cloud_environment(): - return json.loads(get_cloud_destination(name)) - return json.loads(DestinationsFacade.get(name)) - -def set(name, destination): - if is_cloud_environment(): - create_or_update_cloud_destination(name, destination) - DestinationsFacade.set(name, json.dumps(destination)) - -def remove(name): - if not is_cloud_environment(): - raise Exception("The delete destination operation is not supported for non-cloud environments") - delete_cloud_destination(name) - -def is_cloud_environment(): - try: - Class.forName("org.eclipse.dirigible.cf.CloudFoundryModule") - return True - except: - pass - - try: - Class.forName("org.eclipse.dirigible.kyma.KymaModule") - return True - except: - pass - - return False - -def get_cloud_destination(name): - token = get_oauth_token() - destination_url = f"{get_destinations_base_path()}/{name}" - headers = [ - {"name": "Authorization", "value": f"{token.token_type} {token.access_token}"} - ] - response = httpClient.get(destination_url, {"headers": headers}) - - if response.statusCode == 404: - raise Exception(f"Destination with name '{name}' not found") - - return response.text - -def get_destinations_base_path(): - return f"{configurations.get('DIRIGIBLE_DESTINATION_URI')}/destination-configuration/v1/destinations" - -def get_instance_destinations_base_path(): - return f"{configurations.get('DIRIGIBLE_DESTINATION_URI')}/destination-configuration/v1/instanceDestinations" - -def create_or_update_cloud_destination(name, destination): - is_existing_destination = True - try: - get_cloud_destination(name) - except: - is_existing_destination = False - - instance_destination_url = get_instance_destinations_base_path() - destination["Name"] = name - token = get_oauth_token() - headers = [ - {"name": "Authorization", "value": f"{token.token_type} {token.access_token}"} - ] - options = { - "headers": headers, - "text": json.dumps(destination) - } - - if is_existing_destination: - response = httpClient.put(instance_destination_url, options) - if response.statusCode != 200: - raise Exception(f"Error occurred while updating destination '{name}': {response.text}") - else: - response = httpClient.post(instance_destination_url, options) - if response.statusCode != 201: - raise Exception(f"Error occurred while creating destination '{name}': {response.text}") - -def delete_cloud_destination(name): - token = get_oauth_token() - headers = [ - {"name": "Authorization", "value": f"{token.token_type} {token.access_token}"} - ] - url = f"{get_instance_destinations_base_path()}/{name}" - response = httpClient.delete(url, {"headers": headers}) - if response.statusCode != 200: - raise Exception(f"Error occurred while deleting destination '{name}': {response.text}") - -def get_oauth_token(): - oauth_client_id = configurations.get("DIRIGIBLE_DESTINATION_CLIENT_ID") - oauth_client_secret = configurations.get("DIRIGIBLE_DESTINATION_CLIENT_SECRET") - oauth_url = configurations.get("DIRIGIBLE_DESTINATION_URL") - uri = configurations.get("DIRIGIBLE_DESTINATION_URI") - - if not oauth_client_id or not oauth_client_secret or not oauth_url or not uri: - raise Exception("Invalid destination configuration") - - oauth_config = { - "url": oauth_url, - "clientId": oauth_client_id, - "clientSecret": oauth_client_secret - } - - client = oauth.getClient(oauth_config) - return client.getToken() From ae39e4b94810b1df883884518b03f094ee8fe1ff Mon Sep 17 00:00:00 2001 From: MrGoblings Date: Tue, 14 Nov 2023 12:48:05 +0200 Subject: [PATCH 3/3] fix: changed the imports from core apis --- .../python-modules/dirigible/core/context.py | 15 ++++++++----- .../python-modules/dirigible/core/env.py | 14 ++++++++----- .../python-modules/dirigible/core/globals.py | 21 ++++++++++++------- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py index 1a8a5cf3284..6df59670582 100644 --- a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/context.py @@ -6,10 +6,15 @@ # SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors # SPDX-License-Identifier: EPL-2.0 -from org.eclipse.dirigible.components.api.core import ContextFacade -def get(name): - return ContextFacade.get(name) +import java +ContextFacade = java.type('org.eclipse.dirigible.components.api.core') -def set(name, value): - ContextFacade.set(name, value) +class Context: + @staticmethod + def get(name): + return ContextFacade.get(name) + + @staticmethod + def set(name, value): + ContextFacade.set(name, value) diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py index 2d37e89ba7d..893e39bca20 100644 --- a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/env.py @@ -7,10 +7,14 @@ # SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors # SPDX-License-Identifier: EPL-2.0 -from org.eclipse.dirigible.components.api.core import EnvFacade +import java +EnvFacade = java.type('org.eclipse.dirigible.components.api.core') -def get(name): - return EnvFacade.get(name) +class Env: + @staticmethod + def get(name): + return EnvFacade.get(name) -def list(): - return EnvFacade.list() + @staticmethod + def list(): + return EnvFacade.list() diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py index 465924abe88..b200c068891 100644 --- a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/core/globals.py @@ -6,13 +6,20 @@ # SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors # SPDX-License-Identifier: EPL-2.0 -from org.eclipse.dirigible.components.api.core import GlobalsFacade -def get(name): - return GlobalsFacade.get(name) +import java +GlobalsFacade = java.type('org.eclipse.dirigible.components.api.core') -def set(name, value): - GlobalsFacade.set(name, value) -def list(): - return GlobalsFacade.list() +class Globals: + @staticmethod + def get(name): + return GlobalsFacade.get(name) + + @staticmethod + def set(name, value): + GlobalsFacade.set(name, value) + + @staticmethod + def list(): + return GlobalsFacade.list()