Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(configtree): prefix keys with '/' when importing to etcd #336

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions riocli/configtree/etcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional
from base64 import b64encode
from etcd3gw import Etcd3Client
from typing import Optional

from etcd3gw import Etcd3Client

def import_in_etcd(data: dict, endpoint: str, port: Optional[int] = None, prefix: Optional[str] = None) -> None:
if port is None:
port = 2379

def import_in_etcd(
data: dict,
endpoint: str,
port: Optional[int] = 2379,
prefix: Optional[str] = None,
) -> None:
cli = Etcd3Client(host=endpoint, port=port)

if prefix:
Expand All @@ -30,9 +33,10 @@ def import_in_etcd(data: dict, endpoint: str, port: Optional[int] = None, prefix

compares, failures = [], []

prefix = prefix or ''

for key, val in data.items():
if prefix:
key = '{}/{}'.format(prefix, key)
key = '{}/{}'.format(prefix, key)
Comment on lines +36 to +39
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python3
Python 3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> prefix = None
>>> prefix = prefix or ''
>>> print(prefix)

>>> key = 'master/path/to/key'
>>> key = '{}/{}'.format(prefix, key)
>>> print(key)
/master/path/to/key
>>> 


enc_key = b64encode(str(key).encode('utf-8')).decode()
enc_val = b64encode(str(val).encode('utf-8')).decode()
Expand All @@ -56,8 +60,9 @@ def import_in_etcd(data: dict, endpoint: str, port: Optional[int] = None, prefix

cli.transaction(txn)


def _delete_all_keys(client: Etcd3Client) -> None:
null_char = '\x00'
enc_null = b64encode(null_char.encode('utf-8')).decode()
enc_null = b64encode(null_char.encode('utf-8')).decode()

client.delete('\x00', range_end=enc_null)
Loading