def delete(self, oid, layout_id, pvid_str): """Perform DELETE request and generate response.""" if oid is None: self._logger.error("Object Id is required.") return False, None if layout_id is None: self._logger.error("Layout Id is required.") return False, None if pvid_str is None: self._logger.error("pvid_str is required.") return False, None # The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text. # urllib.parse.urlencode converts a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string. # https://docs.python.org/3/library/urllib.parse.html # For example if oid is 'JwZSAwAAAAA=-AgAAAAAA4Ag=' urllib.parse.quote(oid, safe='') yields 'JwZSAwAAAAA%3D-AgAAAAAA4Ag%3D' and layout_id is 1 # urllib.parse.urlencode({'layout-id': layout_id}) yields layout-id=1 # And request_uri is '/objects/JwZSAwAAAAA%3D-AgAAAAAA4Ag%3D' , # absolute_request_uri is layout-id is '/objects/JwZSAwAAAAA%3D-AgAAAAAA4Ag%3D?layout-id=1' query_params = urllib.parse.urlencode({'layout-id': layout_id}) query_params += "&" + urllib.parse.urlencode({'pvid': pvid_str}, safe='') request_uri = '/objects/' + urllib.parse.quote(oid, safe='') absolute_request_uri = request_uri + '?' + query_params body = '' headers = self.s3_util.prepare_signed_header('DELETE', request_uri, query_params, body) if headers['Authorization'] is None: self._logger.error("Failed to generate v4 signature") return False, None try: response = super(CORTXS3ObjectApi, self).delete(absolute_request_uri, body, headers=headers) except ConnectionRefusedError as ex: #IEMutil("ERROR", IEMutil.S3_CONN_FAILURE, IEMutil.S3_CONN_FAILURE_STR) self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(502, "", "ConnectionRefused") except Exception as ex: self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(500, "", "InternalServerError") if response['status'] == 204: self._logger.info('Object deleted successfully.') return True, CORTXS3SuccessResponse(response['body']) else: self._logger.info('Failed to delete Object.') return False, CORTXS3ErrorResponse(response['status'], response['reason'], response['body'])
def put(self, index_id=None, object_key_name=None, value=""): """Perform PUT request and generate response.""" if index_id is None: self._logger.error("Index Id is required.") return False, None if object_key_name is None: self._logger.error("Key is required") return False, None query_params = "" request_body = value # The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text. # https://docs.python.org/3/library/urllib.parse.html # For example if index_id is 'AAAAAAAAAHg=-AwAQAAAAAAA=' and object_key_name is "testobject+" # urllib.parse.quote(index_id, safe='') and urllib.parse.quote(object_key_name) yields 'testobject%2B' respectively # And request_uri is # '/indexes/AAAAAAAAAHg%3D-AwAQAAAAAAA%3D/testobject%2B' request_uri = '/indexes/' + \ urllib.parse.quote(index_id, safe='') + '/' + \ urllib.parse.quote(object_key_name) headers = self.s3_util.prepare_signed_header('PUT', request_uri, query_params, request_body) if (headers['Authorization'] is None): self._logger.error("Failed to generate v4 signature") return False, None try: response = super(CORTXS3KVApi, self).put(request_uri, request_body, headers=headers) except ConnectionRefusedError as ex: IEMutil("ERROR", IEMutil.S3_CONN_FAILURE, IEMutil.S3_CONN_FAILURE_STR) self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(502, "", "ConnectionRefused") except Exception as ex: self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(500, "", "InternalServerError") if response['status'] == 200: self._logger.info("Key value details added successfully.") return True, CORTXS3SuccessResponse(response['body']) else: self._logger.info('Failed to add key value details.') return False, CORTXS3ErrorResponse(response['status'], response['reason'], response['body'])
def head(self, index_id): """Perform HEAD request and generate response.""" if index_id is None: self._logger.error("Index id is required.") return False, None # The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text. # urllib.parse.urlencode converts a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string. # https://docs.python.org/3/library/urllib.parse.html # For example if oid is 'JwZSAwAAAAA=-AgAAAAAA4Ag=' urllib.parse.quote(oid, safe='') yields 'JwZSAwAAAAA%3D-AgAAAAAA4Ag%3D' # And request_uri is '/indexes/JwZSAwAAAAA%3D-AgAAAAAA4Ag%3D' request_uri = '/indexes/' + urllib.parse.quote(index_id, safe='') query_params = "" body = "" headers = self.s3_util.prepare_signed_header('HEAD', request_uri, query_params, body) if headers['Authorization'] is None: self._logger.error("Failed to generate v4 signature") return False, None try: response = super( CORTXS3IndexApi, self).head( request_uri, body, headers=headers) except ConnectionRefusedError as ex: IEMutil("ERROR", IEMutil.S3_CONN_FAILURE, IEMutil.S3_CONN_FAILURE_STR) self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(502,"","ConnectionRefused") except Exception as ex: self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(500,"","InternalServerError") if response['status'] == 200: self._logger.info("HEAD Index called successfully with status code: "\ + str(response['status']) + " response body: " + str(response['body'])) return True, CORTXS3SuccessResponse(response['body']) else: self._logger.info("Failed to do HEAD Index with status code: "\ + str(response['status']) + " response body: " + str(response['body'])) return False, CORTXS3ErrorResponse( response['status'], response['reason'], response['body'])
def put(self, oid, value): """Perform PUT request and generate response.""" if oid is None: self._logger.error("Object Id is required.") return False, None query_params = "" request_body = value # The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text. # https://docs.python.org/3/library/urllib.parse.html # For example if oid is 'JwZSAwAAAAA=-AgAAAAAA4Ag=' urllib.parse.quote(oid, safe='') yields 'JwZSAwAAAAA%3D-AgAAAAAA4Ag%3D' # And request_uri is '/objects/JwZSAwAAAAA%3D-AgAAAAAA4Ag%3D' request_uri = '/objects/' + urllib.parse.quote(oid, safe='') headers = self.s3_util.prepare_signed_header('PUT', request_uri, query_params, request_body) if headers['Authorization'] is None: self._logger.error("Failed to generate v4 signature") return False, None try: response = super(CORTXS3ObjectApi, self).put(request_uri, request_body, headers=headers) except ConnectionRefusedError as ex: IEMutil("ERROR", IEMutil.S3_CONN_FAILURE, IEMutil.S3_CONN_FAILURE_STR) self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(502, "", "ConnectionRefused") except Exception as ex: self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(500, "", "InternalServerError") if response['status'] == 201: self._logger.info("Object added successfully.") return True, CORTXS3SuccessResponse(response['body']) else: self._logger.info('Failed to add Object.') return False, CORTXS3ErrorResponse(response['status'], response['reason'], response['body'])
def delete(self, index_id): """Perform DELETE request and generate response.""" if index_id is None: self._logger.info("Index Id is required.") return False, None # The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text. # https://docs.python.org/3/library/urllib.parse.html # For example if index_id is 'AAAAAAAAAHg=-AwAQAAAAAAA=' urllib.parse.quote(index_id, safe='') yields 'AAAAAAAAAHg%3D-AwAQAAAAAAA%3D' # And request_uri is '/indexes/AAAAAAAAAHg%3D-AwAQAAAAAAA%3D' request_uri = '/indexes/' + urllib.parse.quote(index_id, safe='') query_params = "" body = "" headers = self.s3_util.prepare_signed_header('DELETE', request_uri, query_params, body) if(headers['Authorization'] is None): self._logger.error("Failed to generate v4 signature") return False, None try: response = super( CORTXS3IndexApi, self).delete( request_uri, headers=headers) except ConnectionRefusedError as ex: IEMutil("ERROR", IEMutil.S3_CONN_FAILURE, IEMutil.S3_CONN_FAILURE_STR) self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(502,"","ConnectionRefused") except Exception as ex: self._logger.error(repr(ex)) return False, CORTXS3ErrorResponse(500,"","InternalServerError") if response['status'] == 204: self._logger.info('Successfully deleted Index.') return True, CORTXS3SuccessResponse(response['body']) else: self._logger.info('Failed to delete Index.') return False, CORTXS3ErrorResponse( response['status'], response['reason'], response['body'])