From a8448cc79df38689355038c517a53f88d649cd29 Mon Sep 17 00:00:00 2001 From: Maxim Babushkin Date: Thu, 21 Mar 2024 13:32:32 +0200 Subject: [PATCH] [catalog] Add catalog object storage functionality Add global catalog object storage functionality to perform the following operations: - Fetch object storage items - Fetch object storage id - Fetch existing object storage plans - Fetch specific object storage plan --- examples/catalog/catalog.py | 10 +++ ibmcloud_python_sdk/catalog/__init__.py | 0 .../catalog/catalog_service.py | 90 +++++++++++++++++++ ibmcloud_python_sdk/config.py | 1 + ibmcloud_python_sdk/utils/common.py | 2 + ibmcloud_python_sdk/utils/constants.py | 1 + 6 files changed, 104 insertions(+) create mode 100644 examples/catalog/catalog.py create mode 100644 ibmcloud_python_sdk/catalog/__init__.py create mode 100644 ibmcloud_python_sdk/catalog/catalog_service.py diff --git a/examples/catalog/catalog.py b/examples/catalog/catalog.py new file mode 100644 index 000000000..122433005 --- /dev/null +++ b/examples/catalog/catalog.py @@ -0,0 +1,10 @@ +from ibmcloud_python_sdk.catalog import catalog_service as cs + +# Intentiate the class +catalog = cs.CatalogService() + +# Retrieve catalog cloud object storage plans list +catalog.get_cloud_object_storage() + +# Retrieve specific catalog cloud object storage plan +catalog.get_requested_object_storage_plan("standard") diff --git a/ibmcloud_python_sdk/catalog/__init__.py b/ibmcloud_python_sdk/catalog/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/ibmcloud_python_sdk/catalog/catalog_service.py b/ibmcloud_python_sdk/catalog/catalog_service.py new file mode 100644 index 000000000..16ebca0b1 --- /dev/null +++ b/ibmcloud_python_sdk/catalog/catalog_service.py @@ -0,0 +1,90 @@ +from ibmcloud_python_sdk.config import params +from ibmcloud_python_sdk.auth import get_headers as headers +from ibmcloud_python_sdk.utils.common import query_wrapper as qw +from ibmcloud_python_sdk.utils.common import resource_not_found + + +class CatalogService(): + + def __init__(self): + self.cfg = params() + + def get_cloud_object_storage(self): + """Retrieve cloud object storage list + + :return: List of cloud object storage + "rtype: list + """ + try: + # Connect to api endpoint for cloud-object-storage + path = "/api/v1?q=cloud-object-storage" + + # Return data + return qw("gc", "GET", path, headers())["data"] + + except Exception as error: + print("Error fetching cloud object storage. {}".format(error)) + raise + + def get_cloud_object_storage_id(self): + """Retrieve cloud object storage id + + :return: String of cloud object storage id + :rtype: str + """ + try: + cloud_obj_storage = self.get_cloud_object_storage() + if "errors" in cloud_obj_storage: + return cloud_obj_storage + + for cloud_object in cloud_obj_storage["resources"]: + if cloud_object['children'][0]['id']: + return cloud_object['children'][0]['id'] + + return resource_not_found() + + except Exception as error: + print("Error fetching object storage id. {}". format( + error)) + raise + + def get_object_storage_plans(self): + """Retrieve object storage plans + + :return: List of cloud storage plans + :rtype: list + """ + storage_id = self.get_cloud_object_storage_id() + + try: + # Connect to api endpoint of defined storage plan + path = "/api/v1/{}/plan".format(storage_id) + + # Return data + return qw("gc", "GET", path, headers())["data"] + + except Exception as error: + print("Error fetching storage plan. {}".format(error)) + raise + + def get_requested_object_storage_plan(self, plan_name): + """Retrieve defined object storage plan + + :return: Dictionary of selected storage plan + :rtype: dict + """ + try: + storage_plans = self.get_object_storage_plans() + if "errors" in storage_plans: + return storage_plans + + for storage_plan in storage_plans["resources"]: + if storage_plan["name"] == plan_name: + return storage_plan + + return resource_not_found() + + except Exception as error: + print("Error fetching requested storage plan {}. {}".format( + plan_name, error)) + raise diff --git a/ibmcloud_python_sdk/config.py b/ibmcloud_python_sdk/config.py index 1e91055ae..35bba3c01 100644 --- a/ibmcloud_python_sdk/config.py +++ b/ibmcloud_python_sdk/config.py @@ -14,6 +14,7 @@ def params(): option["rg_url"] = constants.RG_URL option["em_url"] = constants.EM_URL option["sl_url"] = constants.SL_URL + option["gc_url"] = constants.GC_URL option["http_timeout"] = constants.HTTP_TIMEOUT if path.isfile(creds): diff --git a/ibmcloud_python_sdk/utils/common.py b/ibmcloud_python_sdk/utils/common.py index 122cb81a3..553f58f84 100644 --- a/ibmcloud_python_sdk/utils/common.py +++ b/ibmcloud_python_sdk/utils/common.py @@ -67,6 +67,8 @@ def query_wrapper(conn_type, method, path, headers=None, payload=None): conn = http.client.HTTPSConnection(cfg["sl_url"], timeout=timeout) elif conn_type == "power": conn = http.client.HTTPSConnection(cfg["pi_url"], timeout=timeout) + elif conn_type == "gc": + conn = http.client.HTTPSConnection(cfg["gc_url"], timeout=timeout) if cache.client(): if method == "GET" and conn_type != "auth": diff --git a/ibmcloud_python_sdk/utils/constants.py b/ibmcloud_python_sdk/utils/constants.py index 0b696cf1f..336b1ca2b 100644 --- a/ibmcloud_python_sdk/utils/constants.py +++ b/ibmcloud_python_sdk/utils/constants.py @@ -9,5 +9,6 @@ SL_URL = "api.softlayer.com" PI_URL = "power-iaas.cloud.ibm.com" COS_DOMAIN = "cloud-object-storage.appdomain.cloud" +GC_URL = "globalcatalog.cloud.ibm.com" HTTP_TIMEOUT = 60 USER_AGENT = "IBM Cloud Python SDK"