Fix error renaming keys

This function was not implemented and thus failed; implements it.
This commit is contained in:
Joshua Boniface 2020-03-30 21:16:39 -04:00
parent 09aeb33d13
commit 44efd66f2c
2 changed files with 40 additions and 1 deletions

View File

@ -965,7 +965,7 @@ def rename_volume(zk_conn, pool, name, new_name):
# 2. Rename the volume in Zookeeper
zkhandler.renamekey(zk_conn, {
'/ceph/volumes/{}/{}'.format(pool, name): '/ceph/volumes/{}/{}'.format(pool, new_name),
'/ceph/snapshots/{}/{}'.format(pool, name): '/ceph/snapshots/{}/{}'.format(pool, new_name),
'/ceph/snapshots/{}/{}'.format(pool, name): '/ceph/snapshots/{}/{}'.format(pool, new_name)
})
# 3. Get volume stats

View File

@ -42,6 +42,45 @@ def listchildren(zk_conn, key):
def deletekey(zk_conn, key, recursive=True):
zk_conn.delete(key, recursive=recursive)
# Rename key recursive function
def rename_key_element(zk_conn, zk_transaction, source_key, destination_key):
data_raw = zk_conn.get(source_key)
data = data_raw[0]
zk_transaction.create(destination_key, data)
if zk_conn.get_children(source_key):
for child_key in zk_conn.get_children(source_key):
child_source_key = "{}/{}".format(source_key, child_key)
child_destination_key = "{}/{}".format(destination_key, child_key)
rename_key_element(zk_conn, zk_transaction, child_source_key, child_destination_key)
zk_transaction.delete(source_key)
# Rename key function
def renamekey(zk_conn, kv):
# Start up a transaction
zk_transaction = zk_conn.transaction()
# Proceed one KV pair at a time
for source_key in sorted(kv):
destination_key = kv[source_key]
# Check if the source key exists or fail out
if not zk_conn.exists(source_key):
raise
# Check if the destination key exists and fail out
if zk_conn.exists(destination_key):
raise
rename_key_element(zk_conn, zk_transaction, source_key, destination_key)
# Commit the transaction
try:
zk_transaction.commit()
return True
except Exception:
return False
# Data read function
def readdata(zk_conn, key):
data_raw = zk_conn.get(key)