async def link_object(ctx, entity_bucket, input_object): # assuming input_object to be the object name and the uuid # check if the name already exist in the objects list # if exists, return None/failure # else add object name and uuid in bucket' objects list # and return the bucket reference try: objects_list = entity_bucket.spec.objects if not any(obj.name == input_object.object_name for obj in objects_list): papiea_test.logger.debug( "Object does not exist. Linking the object...") async with ctx.entity_client_for_user( papiea_test.bucket_kind_dict) as bucket_entity_client: entity_bucket.spec.objects.append( AttributeDict(name=input_object.object_name, reference=EntityReference( uuid=input_object.object_uuid, kind=papiea_test.OBJECT_KIND))) await bucket_entity_client.update( metadata=entity_bucket.metadata, spec=entity_bucket.spec) async with ctx.entity_client_for_user( papiea_test.object_kind_dict) as object_entity_client: ret_entity = await object_entity_client.get( AttributeDict(uuid=input_object.object_uuid)) return EntityReference(uuid=ret_entity.metadata.uuid, kind=ret_entity.metadata.kind) else: raise Exception("Object already exists in the bucket") except: raise raise Exception("Unable to link object entity")
async def ensure_bucket_exists(ctx, input_bucket_name): # Run get query to obtain the list of buckets # Check if bucket_name exists in the bucket list # If true, simply return the bucket # Else, create a new bucket with input_bucket_name and return try: async with ctx.entity_client_for_user( papiea_test.bucket_kind_dict) as bucket_entity_client: desired_bucket = await bucket_entity_client.filter( AttributeDict(spec=AttributeDict(name=input_bucket_name))) if len(desired_bucket.results) != 0: papiea_test.logger.debug( "Bucket already exists. Returning it...") return EntityReference( uuid=desired_bucket.results[0].metadata.uuid, kind=desired_bucket.results[0].metadata.kind) papiea_test.logger.debug( "Bucket not found. Creating new bucket...") bucket_ref = await bucket_entity_client.create({ "name": input_bucket_name, "objects": list(), "owner": "nutanix" }) ret_entity = await bucket_entity_client.get(bucket_ref.metadata) return EntityReference(uuid=ret_entity.metadata.uuid, kind=ret_entity.metadata.kind) except: raise raise Exception("Unable to create bucket entity")
async def unlink_object(ctx, entity_bucket, input_object_name): # assuming input_object to be the object name # check if the name exists in the object list # if does not exists, return None/failure # else remove the object name and reference from the bucket' objects list and # and return the bucket reference try: objects_list = entity_bucket.spec.objects if any(obj.name == input_object_name for obj in objects_list): papiea_test.logger.debug("Object found. Unlinking the object...") async with ctx.entity_client_for_user(papiea_test.bucket_kind_dict) as bucket_entity_client: entity_bucket.spec.objects = [d for d in entity_bucket.spec.objects if d.get("name") != input_object_name] await bucket_entity_client.update( metadata=entity_bucket.metadata, spec=entity_bucket.spec ) ret_entity = await bucket_entity_client.get(entity_bucket.metadata) return EntityReference( uuid=ret_entity.metadata.uuid, kind=ret_entity.metadata.kind ) else: raise Exception("Object not found in the bucket") except: raise return EntityReference(uuid="", kind="", message="Unable to unlink object entity")
async def change_bucket_name(ctx, entity_bucket, new_bucket_name): # check if there's any bucket with the new name # if found, return None/failure # else update name and bucket entity try: async with ctx.entity_client_for_user(papiea_test.bucket_kind_dict) as bucket_entity_client: matched_bucket = await bucket_entity_client.filter(AttributeDict(spec=AttributeDict(name=new_bucket_name))) if len(matched_bucket.results) == 0: papiea_test.logger.debug("Bucket found. Changing the bucket name...") entity_bucket.spec.name = new_bucket_name await bucket_entity_client.update( metadata=entity_bucket.metadata, spec=entity_bucket.spec ) ret_entity = await bucket_entity_client.get(entity_bucket.metadata) return EntityReference( uuid=ret_entity.metadata.uuid, kind=ret_entity.metadata.kind ) else: raise Exception("Bucket with new name already exists") except: raise return EntityReference(uuid="", kind="", message="Unable to change name for the bucket entity")
async def create_object(ctx, entity_bucket, input_object_name): # check if object name already exists in entity.objects # if found, return None/failure # else create a new object entity and add the object name # reference in the objects list and return the bucket reference try: objects_list = entity_bucket.spec.objects if not any(obj.name == input_object_name for obj in objects_list): papiea_test.logger.debug("Object does not exist. Creating new object...") async with ctx.entity_client_for_user(papiea_test.object_kind_dict) as object_entity_client: entity_object = await object_entity_client.create( Spec(content=""), metadata_extension={ "owner": "nutanix" } ) async with ctx.entity_client_for_user(papiea_test.bucket_kind_dict) as bucket_entity_client: entity_bucket.spec.objects.append( AttributeDict(name=input_object_name, reference=EntityReference( uuid=entity_object.metadata.uuid, kind=papiea_test.OBJECT_KIND ) ) ) await bucket_entity_client.update( metadata=entity_bucket.metadata, spec=entity_bucket.spec ) ret_entity = await object_entity_client.get(entity_object.metadata) return EntityReference( uuid=ret_entity.metadata.uuid, kind=ret_entity.metadata.kind ) else: raise Exception("Object already exists in the bucket") except: raise return EntityReference(uuid="", kind="", message="Unable to create object entity")