Skip to content

Commit

Permalink
fix code
Browse files Browse the repository at this point in the history
Signed-off-by: Oded Viner <[email protected]>
  • Loading branch information
OdedViner committed Mar 5, 2025
1 parent eb8b95b commit 990434c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
21 changes: 12 additions & 9 deletions ocs_ci/cleanup/ibm/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def delete_buckets(hours):
api_key = config.AUTH["ibmcloud"]["api_key"]
service_instance_id = config.AUTH["ibmcloud"]["cos_instance_crn"]
endpoint_url = constants.IBM_COS_GEO_ENDPOINT_TEMPLATE.format("us")
cos = IBMCloudObjectStorage(
ibmcloud_storage_obj = IBMCloudObjectStorage(
api_key=api_key,
service_instance_id=service_instance_id,
endpoint_url=endpoint_url,
)
buckets_time = cos.get_buckets_data()
buckets_time = ibmcloud_storage_obj.get_buckets_data()
buckets_region = get_bucket_regions_map()
buckets_combine = {}
for bucket_name, bucket_region in buckets_region.items():
Expand All @@ -84,11 +84,11 @@ def delete_buckets(hours):
bucket_region,
buckets_time[0]["CreationDate"],
]
bucket_delete_names = buckets_to_delete(buckets_combine, hours)
for bucket_delete_name in bucket_delete_names:
res = cos.delete_bucket(bucket_delete_name)
buckets_delete = buckets_to_delete(buckets_combine, hours)
for bucket_name, bucket_region in buckets_delete.items():
res = ibmcloud_storage_obj.delete_bucket(bucket_name, bucket_region)
if res is False:
status.append(bucket_delete_name)
status.append(bucket_name)
if len(status) > 0:
raise Exception(f"Failed to delelte buckets {status}")

Expand All @@ -100,7 +100,7 @@ def buckets_to_delete(buckets, hours):
Args:
"""
buckets_delete = []
buckets_delete = {}
current_date = datetime.now(timezone.utc)
for bucket_name, bucket_data in buckets.items():
age_in_hours = (current_date - bucket_data[1]).total_seconds() / 3600
Expand All @@ -111,5 +111,8 @@ def buckets_to_delete(buckets, hours):
if hours_bucket == "never":
continue
if hours_bucket < age_in_hours:
buckets_delete.append(bucket_name)
return buckets_delete[:10]
buckets_delete[bucket_name] = bucket_data[0]
import random

return dict(random.sample(buckets_delete.items(), 10))
# return buckets_delete
44 changes: 34 additions & 10 deletions ocs_ci/utility/ibmcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ def __init__(self, api_key, service_instance_id, endpoint_url):
endpoint_url=self.cos_endpoint,
)

def get_bucket_objects(self, bucket_name):
def get_bucket_objects(self, bucket_name, bucket_region):
"""
Fetches the objects in a bucket
Expand All @@ -1326,8 +1326,15 @@ def get_bucket_objects(self, bucket_name):
"""
bucket_objects = []
logger.info(f"Retrieving bucket contents from {bucket_name}")
cos_client = ibm_boto3.client(
"s3",
ibm_api_key_id=self.cos_api_key_id,
ibm_service_instance_id=self.cos_instance_crn,
config=IBMBotocoreConfig(signature_version="oauth"),
endpoint_url=constants.IBM_COS_GEO_ENDPOINT_TEMPLATE.format(bucket_region),
)
try:
objects = self.cos_client.list_objects(Bucket=bucket_name)
objects = cos_client.list_objects(Bucket=bucket_name)
for obj in objects.get("Contents", []):
bucket_objects.append(obj["Key"])
except ClientError as ce:
Expand All @@ -1337,21 +1344,29 @@ def get_bucket_objects(self, bucket_name):
logger.info(f"bucket objects: {bucket_objects}")
return bucket_objects

def delete_objects(self, bucket_name):
def delete_objects(self, bucket_name, bucket_region):
"""
Delete objects in a bucket
Args:
bucket_name (str): Name of the bucket
"""
objects = self.get_bucket_objects(bucket_name)
objects = self.get_bucket_objects(bucket_name, bucket_region)
if objects:
try:
# Form the delete request
cos_client = ibm_boto3.client(
"s3",
ibm_api_key_id=self.cos_api_key_id,
ibm_service_instance_id=self.cos_instance_crn,
config=IBMBotocoreConfig(signature_version="oauth"),
endpoint_url=constants.IBM_COS_GEO_ENDPOINT_TEMPLATE.format(
bucket_region
),
)
for obj in objects:
delete_request = {"Objects": [{"Key": obj}]}
response = self.cos_client.delete_objects(
response = cos_client.delete_objects(
Bucket=bucket_name, Delete=delete_request
)
logger.info(f"Deleted items for {bucket_name}")
Expand All @@ -1361,18 +1376,27 @@ def delete_objects(self, bucket_name):
except Exception as e:
logger.error(f"Unable to delete objects: {e}")

def delete_bucket(self, bucket_name):
def delete_bucket(self, bucket_name, bucket_region):
"""
Delete the bucket
Args:
bucket_name (str): Name of the bucket
"""
logger.info(f"Deleting bucket: {bucket_name}")
logger.info(f"Deleting bucket: {bucket_name} in region{bucket_region}")
try:
self.delete_objects(bucket_name=bucket_name)
self.cos_client.delete_bucket(Bucket=bucket_name)
cos_client = ibm_boto3.client(
"s3",
ibm_api_key_id=self.cos_api_key_id,
ibm_service_instance_id=self.cos_instance_crn,
config=IBMBotocoreConfig(signature_version="oauth"),
endpoint_url=constants.IBM_COS_GEO_ENDPOINT_TEMPLATE.format(
bucket_region
),
)
self.delete_objects(bucket_name=bucket_name, bucket_region=bucket_region)
cos_client.delete_bucket(Bucket=bucket_name)
logger.info(f"Bucket: {bucket_name} deleted!")
return True
except ClientError as ce:
Expand Down

0 comments on commit 990434c

Please sign in to comment.