Skip to content

Commit

Permalink
Merge pull request #21 from MaxBab/add_catalog_object_storage_functio…
Browse files Browse the repository at this point in the history
…nality

[catalog] Add catalog object storage functionality
  • Loading branch information
goldyfruit authored Mar 21, 2024
2 parents 8c9ef8f + a8448cc commit 3a3b1dc
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/catalog/catalog.py
Original file line number Diff line number Diff line change
@@ -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")
Empty file.
90 changes: 90 additions & 0 deletions ibmcloud_python_sdk/catalog/catalog_service.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions ibmcloud_python_sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions ibmcloud_python_sdk/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
1 change: 1 addition & 0 deletions ibmcloud_python_sdk/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 3a3b1dc

Please sign in to comment.