def set_keyvalue(self, keyvalue, modify_options=None): """ Sets a key-value's properties within a configuration store. If the key-value does not exist it will be created. :param KeyValue keyvalue: The key-value to set. :param ModifyKeyValueOptions modify_options: Optional parameter to set keyvalue modification options :return: The key-value that was set in the configuration store. :rtype: KeyValue """ if modify_options is None: modify_options = models.ModifyKeyValueOptions() key, label = utils.encode_key_and_label(keyvalue.key, keyvalue.label) body_content = { "content_type": keyvalue.content_type, "value": keyvalue.value, "tags": keyvalue.tags } return self.__write_key(key, label, body_content, modify_options)
def __query_key(self, key, query_kv_option): key, label = utils.encode_key_and_label(key, query_kv_option.label) fields = self.__construct_query_fields_to_string( query_kv_option.fields) query_url = '/kv/{}?label={}'.format( key, label if label is not None else '') query_url += '&fields={}'.format('*' if fields is None else fields) endpoint = utils.get_endpoint_from_connection_string( self.connection_string) url = 'https://{}{}'.format(endpoint, query_url) custom_headers = self.__configure_request_ids(query_kv_option) custom_headers.update(self._default_headers) headers = utils.generate_request_header( method=constants.HttpMethods.Get, custom_headers=custom_headers, datetime_=None if query_kv_option is None else query_kv_option.query_datetime) response = self._request_handler.execute( request_message.RequestMessage(constants.HttpMethods.Get, headers, url, ''), self._request_session) if response.status_code == constants.StatusCodes.OK: return mapper.map_json_to_keyvalue(response.json()) if response.status_code == constants.StatusCodes.NOT_FOUND: return None raise exceptions.HTTPException(response.status_code, response.reason, response.headers, response.content)
def add_keyvalue(self, keyvalue, modify_options=None): """ Adds a new key-value to a configuration store. :param KeyValue keyvalue: The key-value to add. :param dict custom_headers: Headers that will be added to the request :param ModifyKeyValueOptions modify_options: Optional parameter to set keyvalue modification options :return: The key-value that was added to the configuration store. :rtype: KeyValue :raises ValueError: If the keyvalue entry alreay exists. """ if modify_options is None: modify_options = models.ModifyKeyValueOptions() key, label = utils.encode_key_and_label(keyvalue.key, keyvalue.label) body_content = { "content_type": keyvalue.content_type, "value": keyvalue.value, "tags": keyvalue.tags } return self.__write_key(key, label, body_content, modify_options, if_match_etag=None, if_none_match_etag='*')
def update_keyvalue(self, keyvalue, modify_options=None): """ Updates a key-value that was retrieved from a configuration store. The ETag property is used to ensure that no external changes to the key-value have occurred in the configuration store since the key-value was retrieved. :param KeyValue keyvalue: The key-value to update. :param ModifyKeyValueOptions modify_options: Optional parameter to set keyvalue modification options :return: The updated key-value. :rtype: KeyValue :raises ValueError: If the keyvalue entry has been modified and etag mismatches. """ if modify_options is None: modify_options = models.ModifyKeyValueOptions() if keyvalue.etag is None: raise ValueError("Etag of the keyvalue cannot be null") key, label = utils.encode_key_and_label(keyvalue.key, keyvalue.label) body_content = { "content_type": keyvalue.content_type, "value": keyvalue.value, "tags": keyvalue.tags } return self.__write_key(key, label, body_content, modify_options, keyvalue.etag)
def delete_keyvalue(self, keyvalue, modify_options=None): """ Deletes a key-value from a configuration store. The ETag property is used to ensure that no external changes to the key-value have occurred in the configuration store since the key-value was retrieved. :param str keyvalue: The key-value to delete. :param ModifyKeyValueOptions modify_options: Optional parameter to set keyvalue modification options :return: The deleted key-value. :rtype: KeyValue :raises ValueError: If the key-value entry has been modified and etag mismatches. """ if modify_options is None: modify_options = models.ModifyKeyValueOptions() if keyvalue.etag is None: raise ValueError("Etag of the keyvalue cannot be null") key, label = utils.encode_key_and_label(keyvalue.key, keyvalue.label) query_url = '/kv/{}?label={}'.format(key, '' if label is None else label) endpoint = utils.get_endpoint_from_connection_string( self.connection_string) url = 'https://{}{}'.format(endpoint, query_url) custom_headers = self.__configure_request_ids(modify_options) custom_headers.update(self._default_headers) headers = utils.generate_request_header( method=constants.HttpMethods.Delete, custom_headers=custom_headers, datetime_=None, if_match_etag=keyvalue.etag) response = self._request_handler.execute( request_message.RequestMessage(constants.HttpMethods.Delete, headers, url, ''), self._request_session) if response.status_code == constants.StatusCodes.OK: return mapper.map_json_to_keyvalue(response.json()) if response.status_code == constants.StatusCodes.PRECONDITION_FAILED: raise ValueError('The keyvalue entry has been modified.') raise exceptions.HTTPException(response.status_code, response.reason, response.headers, response.content)
def delete_keyvalue_by_key_label(self, key, label=None, modify_options=None): """ Deletes a key-value from a configuration store. :param str key: The key of the key-value that should be deleted. :param str label: The label of the key-value that should be deleted. :param ModifyKeyValueOptions modify_options: Optional parameter to set keyvalue modification options :return: The deleted key-value if found, otherwise null. :rtype: KeyValue """ if modify_options is None: modify_options = models.ModifyKeyValueOptions() key, label = utils.encode_key_and_label(key, label) query_url = '/kv/{}?label={}'.format(key, '' if label is None else label) endpoint = utils.get_endpoint_from_connection_string( self.connection_string) url = 'https://{}{}'.format(endpoint, query_url) custom_headers = self.__configure_request_ids(modify_options) custom_headers.update(self._default_headers) headers = utils.generate_request_header( method=constants.HttpMethods.Delete, custom_headers=custom_headers, datetime_=None, if_match_etag=None) response = self._request_handler.execute( request_message.RequestMessage(constants.HttpMethods.Delete, headers, url, ''), self._request_session) if response.status_code == constants.StatusCodes.OK: return mapper.map_json_to_keyvalue(response.json()) if response.status_code == constants.StatusCodes.NO_CONTENT: return None raise exceptions.HTTPException(response.status_code, response.reason, response.headers, response.content)
def unlock_keyvalue(self, keyvalue, modify_options=None): """Unlocks a key-value within a configuration store. :param KeyValue keyvalue: The key-value to be unlocked. :param ModifyKeyValueOptions modify_options: Optional parameter to set keyvalue modification options :return: The unlocked key-value if its ETag matches the ETag in the configuration store, otherwise null. :rtype: KeyValue """ if modify_options is None: modify_options = models.ModifyKeyValueOptions() key, label = utils.encode_key_and_label(keyvalue.key, keyvalue.label) query_url = '/locks/{}'.format(key) query_url += '?label={}'.format('' if label is None else label) endpoint = utils.get_endpoint_from_connection_string( self.connection_string) url = 'https://{}{}'.format(endpoint, query_url) custom_headers = self.__configure_request_ids(modify_options) custom_headers.update(self._default_headers) headers = utils.generate_request_header( method=constants.HttpMethods.Delete, custom_headers=custom_headers, datetime_=None, if_match_etag=keyvalue.etag) response = self._request_handler.execute( request_message.RequestMessage(constants.HttpMethods.Delete, headers, url, ''), self._request_session) if response.status_code == constants.StatusCodes.OK: return mapper.map_json_to_keyvalue(response.json()) if response.status_code == constants.StatusCodes.NO_CONTENT: return None raise exceptions.HTTPException(response.status_code, response.reason, response.headers, response.content)
def __query_keys(self, query_kv_collection_option, continuation_link): key, label = utils.encode_key_and_label( query_kv_collection_option.key_filter, query_kv_collection_option.label_filter) query_datetime = query_kv_collection_option.query_datetime query_fields = self.__construct_query_fields_to_string( query_kv_collection_option.fields) if continuation_link is None: query_url = '/kv?key={}'.format('*' if key is None else key) query_url += '&label={}'.format('*' if label is None else label) query_url += '&fields={}'.format( '*' if query_fields is None else query_fields) else: query_url = self.__parse_link_header(continuation_link) if query_url is None: return [], None endpoint = utils.get_endpoint_from_connection_string( self.connection_string) url = 'https://{}{}'.format(endpoint, query_url) custom_headers = self.__configure_request_ids( query_kv_collection_option) custom_headers.update(self._default_headers) headers = utils.generate_request_header( method=constants.HttpMethods.Get, custom_headers=custom_headers, datetime_=query_datetime) response = self._request_handler.execute( request_message.RequestMessage(constants.HttpMethods.Get, headers, url, ''), self._request_session) if response.status_code == constants.StatusCodes.OK: if constants.HttpHeaders.Link in response.headers: return mapper.map_json_to_keyvalues( response.json()['items']), response.headers[ constants.HttpHeaders.Link] return mapper.map_json_to_keyvalues(response.json()['items']), None raise exceptions.HTTPException(response.status_code, response.reason, response.headers, response.content)