Ejemplo n.º 1
0
    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.unescape_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)
Ejemplo n.º 2
0
    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.unescape_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='*')
Ejemplo n.º 3
0
    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.unescape_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)
Ejemplo n.º 4
0
    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.unescape_encode_key_and_label(
            keyvalue.key, keyvalue.label)
        query_url = '/kv/{}?label={}'.format(key,
                                             '' if label is None else label)
        query_url = self.__append_api_version(query_url)

        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)
Ejemplo n.º 5
0
    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.unescape_encode_key_and_label(key, label)
        query_url = '/kv/{}?label={}'.format(key,
                                             '' if label is None else label)
        query_url = self.__append_api_version(query_url)

        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)
Ejemplo n.º 6
0
    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.unescape_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)